Whitespace corrections.
[philipp/winterrodeln/mediawiki_extensions/wrreport.git] / wrreport_body.php
index 95e4407767894f6fd38ca74d32dbc292a4403a59..0e44cc041c2cb7b8827a6b5f20d5d48ed5af4f33 100644 (file)
@@ -171,7 +171,7 @@ class WrServicesLibravatar extends Services_Libravatar {
 
 /// Forces a regeneration of region overview pages ('Tirol', 'Vorarlberg', ...)
 function wrRecacheRegions() {
-       $dbr = wfGetDB(DB_SLAVE);
+       $dbr = wfGetDB(DB_REPLICA);
        // SELECT cl_from FROM categorylinks where cl_to = 'Region'
        $res = $dbr->select('categorylinks', 'cl_from', array('cl_to' => 'Region'));
        $page_ids = array();
@@ -185,7 +185,7 @@ function wrRecacheRegions() {
 
 /// Returns the tuple ($report_id, $sledrun_condition, $date_report)
 /// $date_report is NULL or a time as returned by strtotime
-/// Expects a database connection ($dbr = wfGetDB(DB_SLAVE);)
+/// Expects a database connection ($dbr = wfGetDB(DB_REPLICA);)
 /// and a page_id describung the page where the condition should be returned.
 /// If no condition is found, (NULL, NULL, NULL) is returned.
 function wrGetSledrunCondition($dbr, $page_id) {
@@ -215,13 +215,12 @@ function wrGetSledrunCondition($dbr, $page_id) {
 /// Updates the line of the wrreportcache table that corresponds to the $page_id parameter
 function wrUpdateWrReportCacheTable($page_id) {
        // Determine the new content for the row that should be updated
-       $dbr = wfGetDB(DB_SLAVE);
+       $dbr = wfGetDB(DB_REPLICA);
        list($report_id, $condition, $date_report) = wrGetSledrunCondition($dbr, $page_id);
        $rows = wrReportGetReports(array('id' => $report_id), 1);
 
        // Delete the old content (if any)
        $dbw = wfGetDB(DB_MASTER);
-       $dbw->begin();
        $dbw->delete('wrreportcache', array('page_id' => $page_id));
 
        // Insert the updated row
@@ -237,7 +236,6 @@ function wrUpdateWrReportCacheTable($page_id) {
                        'author_name' => $row['author_name'],
                        'author_username' => (is_null($row['author_userid']) ? NULL : $row['author_username'])));
        }
-       $dbw->commit();
 }
 
 
@@ -318,10 +316,10 @@ function wrReportFormRender($hide_save_button = TRUE, $page_title = NULL, $date_
        $select = $td->appendElement('select', array('name' => 'condition', 'required' => 'required'));
        $option = $select->appendElement('option', array('value' => '0'));
        $option->appendText(wfMessage('wrreport-condition-select')->text());
-       foreach (WrReport::$wrConditions as $condition_num => $condition_text) {
+       foreach (WrReport::$wrConditions as $condition_num) {
                $option = $select->appendElement('option', array('value' => (string) $condition_num));
                if ($condition == $condition_num) $option->setAttribute('selected', 'selected');
-               $option->appendText($condition_text);
+               $option->appendText(WrReport::getWrConditionText($condition_num));
        }
        $option = $select->appendElement('option', array('value' => '-1'));
        if ($condition == -1) $option->setAttribute('selected', 'selected');
@@ -455,7 +453,7 @@ function wrReportTableRowRender($table, $row, $format, $showActions, $parser) {
 
        // $row['condition']
        $condition_text = '---';
-       if (isset(WrReport::$wrConditions[$row['condition']])) $condition_text = WrReport::$wrConditions[$row['condition']];
+       if (in_array($row['condition'], WrReport::$wrConditions)) $condition_text = WrReport::getWrConditionText($row['condition']);
        $td = $tr->appendElement('td');
        if ($row['delete_date']) $td->appendElement('em')->appendText(wfMessage('wrreport-deletereport-deleted')->text());
        else $td->appendText($condition_text);
@@ -554,7 +552,7 @@ function wrReportGetColumnNames() {
 ///   $conditions = array('page_title' => 'Birgitzer Alm', 'date_invalid > now()');
 ///   $limit = 1; // or NULL for no limit
 function wrReportGetReports($conditions, $limit=NULL) {
-       $dbr = wfGetDB(DB_SLAVE);
+       $dbr = wfGetDB(DB_REPLICA);
        $columns = wrReportGetColumnNames();
        global $wgDBtype;
        if ($wgDBtype == "mysql") // "condition" is a reserved word in mysql
@@ -573,7 +571,7 @@ function wrReportGetReports($conditions, $limit=NULL) {
 ///
 /// If no condition is present, array(NULL, NULL) is returned
 function wrReportConditionRender($page_title) {
-       $dbr = wfGetDB(DB_SLAVE);
+       $dbr = wfGetDB(DB_REPLICA);
        $cond = 'condition';
        global $wgDBtype;
        // select wrreport.id as report_id, `condition`, date_report from wrreport where page_title='Axamer Lizum' and `condition` is not null and date_invalid > now() and delete_date is null order by date_report desc, date_entry desc limit 1;
@@ -610,19 +608,19 @@ function wrReportUserMayDelete() {
 // -------------------
 
 class WrReport {
-       // Conditions: array(1 => 'Sehr gut', 2 => 'Gut', 3 => 'Mittelmäßig', 4 => 'Schlecht', 5 => 'Geht nicht');
-       public static $wrConditions;
+       // Conditions: 1 => 'Sehr gut', 2 => 'Gut', 3 => 'Mittelmäßig', 4 => 'Schlecht', 5 => 'Geht nicht'
+       public static $wrConditions = array(1, 2, 3, 4, 5);
 
 
-       public static function initWrConditions() {
-               WrReport::$wrConditions = array();
-               for ($i = 1; $i != 6; ++$i) WrReport::$wrConditions[$i] = wfMessage('wrreport-condition-' . $i)->text();
+       /// Returns the translated text of a specified numeric condition
+       public static function getWrConditionText(int $condition): string {
+               return wfMessage('wrreport-condition-' . $condition)->text();
        }
 
 
        /// Returns the number of sledrun reports issued by a user with the given id.
        public static function getUserSeldrunReportCount($user_id) {
-               $dbr = wfGetDB(DB_SLAVE);
+               $dbr = wfGetDB(DB_REPLICA);
                // select count(*) from wrreport where author_userid = 1 and delete_date is null and `condition` is not null;
                $res = $dbr->select('wrreport', 'count(*)', array('author_userid' => $user_id, 'delete_date' => null, '`condition` is not null'));
                $row = $res->fetchRow();
@@ -712,7 +710,7 @@ class WrReport {
        /// conditions is an array that's given to the where clause
        private static function getRegionDetails($conditions) {
                // Example: SELECT name FROM wrregion WHERE page_id = 882;
-               $dbr = wfGetDB(DB_SLAVE);
+               $dbr = wfGetDB(DB_REPLICA);
                $res = $dbr->select('wrregion', array('id', 'name', 'aswkb(border)'), $conditions);
                if ($dbr->numRows($res) == 1) {
                        $row = $dbr->fetchRow($res);
@@ -748,22 +746,33 @@ class WrReport {
 
        /// Creates the HTML of the <bahnentabelle>, <bahnenregiontabelle> and <rodelbahntabelle> tags.
        private static function createBahnentabelle($page_titles) {
-               $dbr = wfGetDB(DB_SLAVE);
+               $dbr = wfGetDB(DB_REPLICA);
 
-               // SELECT p.page_id,p.page_title, c.length, c.walkup_time, c.top_elevation, c.bottom_elevation, c.walkup_separate, c.lift, c.night_light, c.public_transport, c.sled_rental, c.information_phone FROM `page` p, wrsledruncache c WHERE (p.page_title in ('Birgitzer_Alm_(vom_Adelshof)', 'Kemater_Alm', 'Axamer_Lizum') and p.page_id=c.page_id) ORDER BY page_title
-               $where_array = array('page.page_id = wrsledruncache.page_id');
+               // SELECT p.page_id, p.page_title, c.length, c.walkup_time, c.top_elevation, c.bottom_elevation, c.walkup_separate, c.lift, c.night_light, c.public_transport,
+               // c.sled_rental, c.information_phone, s.show_status, r.status, r.last_check, r.last_update
+               // FROM (`page` p JOIN  wrsledruncache c ON p.page_id=c.page_id) LEFT OUTER JOIN (wrintermapssledrun s JOIN wrintermapsreport r ON s.intermaps_sledrun_id=r.intermaps_sledrun_id) ON p.page_id=s.wr_page_id
+               // WHERE p.page_title in ('Birgitzer_Alm_(vom_Adelshof)', 'Mutterer_Alm_-_Sportrodelbahn', 'Axamer_Lizum') ORDER BY page_title;
+               $where_array = array();
                if (count($page_titles) > 0) {
                        $mysql_page_ids = array();
                        foreach ($page_titles as $page_title) $mysql_page_ids[] = $page_title->getArticleID();
-                       $where_array[] = 'page.page_id in (' . implode(', ', $mysql_page_ids) . ')';
+                       $where_array[] = 'p.page_id in (' . implode(', ', $mysql_page_ids) . ')';
                } else $where_array[] = 'false';
-               $res = $dbr->select(array('page', 'wrsledruncache'), array('page.page_id', 'page.page_title', 'page_namespace', 'length', 'walkup_time', 'top_elevation', 'bottom_elevation', 'walkup_possible', 'walkup_separate', 'lift', 'night_light', 'public_transport', 'sled_rental', 'information_phone', 'information_web'), $where_array, 'bahnentabelleParserHook', array('ORDER BY' => 'page.page_title'));
+               $res = $dbr->select(['AA' => ['p' => 'page', 'c' => 'wrsledruncache'], 'BB' => ['r' => 'wrintermapsreport', 's' => 'wrintermapssledrun']],
+                                                       ['p.page_id', 'p.page_title', 'p.page_namespace',
+                                                        'c.length', 'c.walkup_time', 'c.top_elevation', 'c.bottom_elevation', 'c.walkup_possible', 'c.walkup_separate',
+                                                        'c.lift', 'c.night_light', 'c.public_transport', 'c.sled_rental', 'c.information_phone', 'c.information_web',
+                                                        's.show_status', 'r.status', 'r.last_check', 'r.last_update'],
+                                                       $where_array,
+                                                       'bahnentabelleParserHook',
+                                                       ['ORDER BY' => 'p.page_title'],
+                                                       ['p' => ['JOIN', 'p.page_id=c.page_id'], 'r' => ['JOIN', 's.intermaps_sledrun_id=r.intermaps_sledrun_id'], 'AA' => ['LEFT OUTER JOIN', 'p.page_id=s.wr_page_id']]
+                                               );
 
                global $wgUser;
                global $wgWrReportMode; // e.g. 'summer'
                global $wgWrReportBlackListAll;
                global $wgWrReportBlackListStrangers;
-               global $wgDBtype;
 
                // Determine, whether the user is allowed to make a new report
                $userMayReport = ($wgWrReportMode == 'allow' || ($wgWrReportMode == 'loggedin' && $wgUser->isLoggedIn()));
@@ -779,6 +788,7 @@ class WrReport {
                $tr->appendElement('th')->appendElement('img', array('src' => '/vorlagen/s_lift.png', 'alt' => wfMessage('wrreport-icon-lift')->text(), 'title' => wfMessage('wrreport-icon-lift')->text()));
                $tr->appendElement('th')->appendElement('img', array('src' => '/vorlagen/s_walk.png', 'alt' => wfMessage('wrreport-icon-walkupseparate')->text(), 'title' => wfMessage('wrreport-icon-walkupseparate')->text()));
                $tr->appendElement('th')->appendElement('img', array('src' => '/vorlagen/s_bus.png', 'alt' => wfMessage('wrreport-icon-publictransport')->text(), 'title' => wfMessage('wrreport-icon-publictransport')->text()));
+               if ($wgWrReportMode != 'summer') $tr->appendElement('th')->appendElement('img', array('src' => '/vorlagen/s_opened.png', 'alt' => wfMessage('wrreport-icon-intermaps-status')->text(), 'title' => wfMessage('wrreport-icon-intermaps-status')->text()));
                $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun')->text());
                if ($wgWrReportMode != 'summer') $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-condition')->text());
                $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun-information')->text());
@@ -810,6 +820,22 @@ class WrReport {
                        $td = $tr->appendElement('td');
                        if ($row->public_transport and $row->public_transport != 5) $td->appendElement('img', array('src' => '/vorlagen/s_bus.png', 'alt' => wfMessage('wrreport-icon-publictransport')->text(), 'title' => wfMessage('wrreport-icon-publictransport')->text()));
 
+                       if ($wgWrReportMode != 'summer') {
+                               $td = $tr->appendElement('td');
+                               if ($row->show_status) {
+                                       if ($row->status == 'open') {
+                                               $td->appendElement('img', array('src' => '/vorlagen/s_opened.png', 'alt' => wfMessage('wrreport-icon-intermaps-opened')->text(), 'title' => wfMessage('wrreport-icon-intermaps-opened')->text()));
+                                       } elseif ($row->status == 'closed') {
+                                               $td->appendElement('img', array('src' => '/vorlagen/s_closed.png', 'alt' => wfMessage('wrreport-icon-intermaps-closed')->text(), 'title' => wfMessage('wrreport-icon-intermaps-closed')->text()));
+                                       } else {
+                                               $td->appendText($row->status);
+                                       }
+                               }
+                               // s_closed
+                               // no info
+                               // check whether info should be shown at all
+                       }
+
                        $tr->appendElement('td')->appendElement('a', array('href' => $title->getLocalURL()))->appendText($title->getPrefixedText());
 
                        if ($wgWrReportMode != 'summer') {
@@ -826,8 +852,8 @@ class WrReport {
                                else $date = strftime('%d.%m.', $date_report);
 
                                $td = $tr->appendElement('td');
-                               if (isset(WrReport::$wrConditions[$condition])) {
-                                       $td->appendElement('a', array('href' => $title->getLocalURL() . '#' . Sanitizer::escapeIdForLink(wfMessage('wrreport-reports-sectionname'))))->appendText(WrReport::$wrConditions[$condition]);
+                               if (in_array($condition, WrReport::$wrConditions)) {
+                                       $td->appendElement('a', array('href' => $title->getLocalURL() . '#' . Sanitizer::escapeIdForLink(wfMessage('wrreport-reports-sectionname'))))->appendText(WrReport::getWrConditionText($condition));
                                        $td->appendText(' ');
                                        $small = $td->appendElement('small');
                                        $small->appendText($date);
@@ -943,7 +969,7 @@ class WrReport {
 
                        // get titles that are in the region
                        $page_titles = array();
-                       $dbr = wfGetDB(DB_SLAVE);
+                       $dbr = wfGetDB(DB_REPLICA);
                        // the following line would work if MySQL 5.5 would implement a real geospatial version of CONTAINS.
                        // $res = $dbr->select('wrsledruncache', 'page_id', array('CONTAINS(GEOMFROMWKB(' . $dbr->addQuotes($region_border_wkb) . '), POINT(position_longitude, position_latitude))', 'NOT under_construction'), __METHOD__, 'page_title');
                        $res = $dbr->select(array('wrsledruncache', 'wrregioncache'), array('page_id' => 'wrregioncache.page_id'), array('wrregioncache.region_id' => $region_id, 'wrregioncache.page_id=wrsledruncache.page_id', 'NOT under_construction'), __METHOD__, 'page_title');
@@ -973,7 +999,7 @@ class WrReport {
                // Note: As (of MediaWiki 1.19), only one feed can be added and each feed added replaces the previous one, the following is possible without risk of having duplicated feed entries.
                WrReport::addRegionFeedLink($parser->getTitle());
 
-               $dbr = wfGetDB(DB_SLAVE);
+               $dbr = wfGetDB(DB_REPLICA);
                try {
                        libxml_use_internal_errors(true); // without that, we get PHP Warnings if the $input is not well-formed
                        try {
@@ -1109,11 +1135,11 @@ class WrReport {
        }
 
 
-       public static function MobileMenuHook($name, \MobileFrontend\MenuBuilder &$menu) {
+       public static function MobileMenuHook($name, \MediaWiki\Minerva\MenuBuilder &$menu) {
                if ($name === 'discovery') {
                        // delete "Random page". As a removeEntry function is missing,
                        // a new menu is built here without the random page.
-                       $new_menu = new MobileFrontend\MenuBuilder();
+                       $new_menu = new \MediaWiki\Minerva\MenuBuilder();
                        foreach ($menu->getEntries() as $menuEntryRepresentation) {
                                $new_entry_name = $menuEntryRepresentation['name'];
                                if ($new_entry_name == 'random') continue;
@@ -1137,7 +1163,6 @@ class WrReport {
        }
 
 }
-WrReport::initWrConditions();
 
 
 
@@ -1151,15 +1176,6 @@ class SpecialWrReport extends SpecialPage {
        }
 
 
-       static function LanguageGetSpecialPageAliasesHook(&$specialPageArray, $languageCode) {
-               $text = wfMessage('wrreport')->text(); // 'Bahnberichte'
-               $title = Title::newFromText($text); // 'Bahnberichte'
-               $specialPageArray['wrreport'][] = $title->getDBKey(); // 'Bahnberichte'
-               return true;
-       }
-
-
-
        /// \param $par Possibilities:
        /// - action == 'view' (default)
        /// - action == 'preview': Preview new report
@@ -1367,7 +1383,7 @@ class SpecialWrReport extends SpecialPage {
 
                                // Chech whether identical reports are present
                                if (!$errorMsg) {
-                                       $dbr = wfGetDB(DB_SLAVE);
+                                       $dbr = wfGetDB(DB_REPLICA);
                                        $cond = 'condition';
                                        global $wgDBtype;
                                        if ($wgDBtype == "mysql") $cond = "`$cond`"; // "condition" is a reserved word in mysql