Implemented basic functionality. Not working smoothly yet.
[philipp/winterrodeln/mediawiki_extensions/wrreport.git] / wrreport.body.php
index c09d795944bb61c69a5d86ae06fa6a85e659b613..5d407a7bd85ce30a0304abdf1963bcc7d6e4aa89 100644 (file)
@@ -896,13 +896,10 @@ class WrReport {
                        // page_ids of sledrun titles that that are going to be returned
                        $page_ids = array();
                        foreach ($xml as $entry) { // entry is <rodelbahn>, <region> or <rodelbahnen>
                        // page_ids of sledrun titles that that are going to be returned
                        $page_ids = array();
                        foreach ($xml as $entry) { // entry is <rodelbahn>, <region> or <rodelbahnen>
+                               $entry_page_ids = array(); // page_ids selected (or un-selected) for this entry.
                                $tagname = $entry->getName();
                                $attributes = array();
                                $tagname = $entry->getName();
                                $attributes = array();
-                               foreach ($entry->attributes() as $key => $value) $attributes[] = $key;
-
-                               // is the element known?
-                               if (!in_array($tagname, array('rodelbahn', 'region', 'rodelbahnen')))
-                                       throw new WrReportException(wfMessage('wrreport-rodelbahntabelle-invalid-element', $tagname)->text());
+                               foreach ($entry->attributes() as $key => $value) $attributes[(string) $key] = (string) $value;
 
                                // parse operation attribute
                                $operation = '+'; // '+' (append) or '-' (subtract)
 
                                // parse operation attribute
                                $operation = '+'; // '+' (append) or '-' (subtract)
@@ -914,12 +911,13 @@ class WrReport {
                                }
 
                                // parse in_arbeit attribute
                                }
 
                                // parse in_arbeit attribute
-                               $unter_construction = false;
+                               $under_construction = false; // false (only sledruns that are not under construction), true (only sledruns under construction) or null (doen't matter)
+                               if ($tagname === 'rodelbahn') $under_construction = null; // different default value for tag <rodelbahn>.
                                if (array_key_exists('in_arbeit', $attributes)) {
                                if (array_key_exists('in_arbeit', $attributes)) {
-                                       if (!in_array($attributes['in_arbeit'], array('ja', 'nein', '*')))
-                                               throw new WrReportException(wfMessage('wrreport-rodelbahntabelle-invalid-attribute-value', $tagname, 'operation', $operation)->text());
-                                       if (in_array($attributes['in_arbeit'] === 'nein')) $under_construction = false;
-                                       elseif (in_array($attributes['*'] === '*')) $under_construction = null;
+                                       if ($attributes['in_arbeit'] === 'nein') $under_construction = false;
+                                       elseif ($attributes['in_arbeit'] === 'ja') $under_construction = true;
+                                       elseif ($attributes['in_arbeit'] === '*') $under_construction = null;
+                                       else throw new WrReportException(wfMessage('wrreport-rodelbahntabelle-invalid-attribute-value', $tagname, 'in_arbeit', $under_construction)->text());
                                        unset($attributes['in_arbeit']);
                                }
 
                                        unset($attributes['in_arbeit']);
                                }
 
@@ -930,34 +928,47 @@ class WrReport {
                                // <rodelbahn>
                                if ($tagname == 'rodelbahn') {
                                        $page_title = Title::newFromText(trim($entry));
                                // <rodelbahn>
                                if ($tagname == 'rodelbahn') {
                                        $page_title = Title::newFromText(trim($entry));
-                                       if ($page_title && $page_title->exists()) $page_ids[] = $page_title->getArticleID();
+                                       if ($page_title && $page_title->exists()) $entry_page_ids[] = $page_title->getArticleID();
                                }
 
                                }
 
-                               // <region>
-                               elseif ($tagname == 'region') {
-                                       // get titles that are in the region
-                                       // 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');
+                               // <region> and <rodelbahn>
+                               elseif ($tagname === 'region' || $tagname === 'rodelbahnen') {
+                                       $tables = array('wrsledruncache');
+                                       $where = array();
+
+                                       // region
+                                       if ($tagname === 'region') {
+                                               // the following line would work if MySQL 5.5 would implement a real geospatial version of CONTAINS.
+                                               // $where[] = 'CONTAINS(GEOMFROMWKB(' . $dbr->addQuotes($region_border_wkb) . '), POINT(position_longitude, position_latitude))'
+                                               $tables[] = 'wrregion';
+                                               $tables[] = 'wrregioncache';
+                                               $where[] = 'wrregioncache.region_id=wrregion.id';
+                                               $where[] = 'wrregioncache.page_id=wrsledruncache.page_id';
+                                               $where['wrregion.name'] = $entry;
+                                       }
+
+                                       // under contruction
+                                       if ($under_construction === true) $where[] = 'wrsledruncache.under_construction';
+                                       if ($under_construction === false) $where[] = 'not wrsledruncache.under_construction';
+
+                                       // query
+                                       $res = $dbr->select($tables, array('page_id' => 'wrsledruncache.page_id'), $where, __METHOD__, 'page_id');
                                        foreach ($res as $row) {
                                        foreach ($res as $row) {
-                                               $page_titles[] = Title::newFromId($row->page_id);
+                                               $entry_page_ids[] = $row->page_id;
                                        }
                                        $dbr->freeResult($res);
                                }
 
                                        }
                                        $dbr->freeResult($res);
                                }
 
-                               // <rodelbahnen>
-                               elseif ($tagname == 'rodelbahnen') {
-                               }
-
                                else throw new WrReportException(wfMessage('wrreport-rodelbahntabelle-invalid-element', $tagname)->text());
 
                                else throw new WrReportException(wfMessage('wrreport-rodelbahntabelle-invalid-element', $tagname)->text());
 
+                               // merge the entry page_ids with the page_ids
+                               if ($operation == '+') $page_ids = array_merge($page_ids, $entry_page_ids);
+                               elseif ($operation == '-') $page_ids = array_diff($page_ids, $entry_page_ids);
                        }
 
                        // page_titles that are going to be returned
                        }
 
                        // page_titles that are going to be returned
-                       $page_titles = array();
-                       foreach ($page_ids as $page_id) {
-                               $page_titles[] = Title::newFromId($page_id);
-                       }
+                       $page_titles = Title::newFromIDs($page_ids);
+
                        $html = WrReport::createBahnentabelle($page_titles);
 
                } catch (WrReportException $e) {
                        $html = WrReport::createBahnentabelle($page_titles);
 
                } catch (WrReportException $e) {