Increase the version number to 3.0.2.
[philipp/winterrodeln/mediawiki_extensions/wrreport.git] / wrreport_body.php
1 <?php
2 // File encoding: utf-8
3 // This extension does not depend on other extensions.
4 //
5 // Variables that can be changed in LocalSettings.php:
6 //     $wgWrReportMode = 'allow'; // 'summer', 'allow', 'loggedin', 'deny'
7 //     $wgWrReportBlackListAll = array(); // array of page names where reports disallowed for all users. Example: array('Birgitzer Alm (vom Adelshof)');
8 //     $wgWrReportBlackListStrangers = array(); // array of page names where reports are disallowed for not logged in users
9 //     $wgWrReportDeleteMode = 'loggedin'; // 'allow', 'loggedin', 'deny'
10 //     $wgWrReportFeedRoot = 'http://www.winterrodeln.org/feed'; // root URL of the Winterrodeln feed without trailing slash
11 //
12 //
13 // The following tags are supported:
14 // <bahnentabelle/>
15 //     Creates an overview table of all sledruns specified (one per line) in the tag.
16 //
17 // <bahnenregiontabelle/>
18 //     Like <bahnentabelle> but includes all sledruns that are in the region of the current page
19 //     or in the region specified by one of the following parameters:
20 //         <bahnenregiontabelle wiki="Innsbruck" /> (refers to region represented by the MediaWiki Title name)
21 //         <bahnenregiontabelle region_id="3" /> (refers to id in the wrregion table)
22 //         <bahnenregiontabelle region_name="Innsbruck" /> (refers to name in the wrregion table)
23 //     This tag does not accept any contents.
24 //
25 // <bahnberichte/>
26 //     Shows an overview of the sledrun reports of the current page.
27 //
28 // <bahnberichtformular/>
29 //     Creates the form that is used to enter sledrun reports.
30 //
31 // <rodelbahntabelle/>
32 //     Generates a list of sledrun entries in a flexible way.
33 //     Each line (entry) either add sledruns or removes sledruns.
34 //     Without entries, the table contains no sledruns.
35 //
36 //     Examples:
37 //     Empty list:
38 //         <rodelbahntabelle/>
39 //
40 //     Sledrun "Rumer Alm" and sledrun "Juifenalm"
41 //         <rodelbahntabelle>
42 //             <rodelbahn>Juifenalm</rodelbahn>
43 //             <rodelbahn>Rumer Alm</rodelbahn>
44 //         </rodelbahntabelle>
45 //
46 //     All sledruns in region Innsbruck thats entries are not "under construction".
47 //     The name of the region has to correspond to a name (column name)
48 //     in the table wrregion.
49 //         <rodelbahntabelle>
50 //             <region>Innsbruck</region>
51 //         </rodelbahntabelle>
52 //
53 //     Same as above but excluding the sledrun "Rumer Alm":
54 //         <rodelbahntabelle>
55 //             <region>Innsbruck</region>
56 //             <rodelbahn operation="-">Rumer Alm</rodelbahn>
57 //         </rodelbahntabelle>
58 //
59 //     All sledruns thats entries are "under construction"
60 //         <rodelbahntabelle>
61 //             <rodelbahnen in_arbeit="ja"/>
62 //         </rodelbahntabelle>
63 //
64 //     Attributes:
65 //         * in_arbeit: values "ja", "nein" (default for <region> and <rodelbahnen>), "*" (default for <rodelbahn>)
66 //             Just include the sledrun(s) if the condition is fulfilled.
67 //         * operation: values "+" (add the sledrun(s) to the set, default), "-" (subtract the sledrun(s) from the set)
68 //     Attributes that may be implemented later
69 //         * beleuchtungstage: values "0", "unknown" (is null), ">0" (excludes null), "7", "*" (includes null)
70 //             Just include the sledrun(s) if the condition is fulfilled.
71 //
72
73
74 // Constants
75 // ---------
76
77 // Constants for wrReportTableRender
78 define('WRREPORT_COMPACT_PAGE', 1); ///< includes the page name
79 define('WRREPORT_COMPACT', 2);      ///< shown on a single page
80 define('WRREPORT_DETAIL', 3);       ///< more columns
81
82
83
84 // DOM helper classes
85 // ------------------
86
87 class WrDOMDocument extends DOMDocument {
88         function __construct() {
89                 parent::__construct('1.0', 'utf-8');
90                 $this->registerNodeClass('DOMElement', 'WrDOMElement');
91         }
92
93         /// Creates and adds the element with the given tag name and returns it.
94         /// Additionally, it calls setAttribute($key, $value) for every entry
95         /// in $attributes.
96         function appendElement($tagName, $attributes=array()) {
97                 $child = $this->appendChild($this->createElement($tagName));
98                 foreach ($attributes as $key => $value) $child->setAttribute($key, $value);
99                 return $child;
100         }
101
102 }
103
104
105 class WrDOMElement extends DOMElement {
106
107         /// Creates and adds the element with the given tag name and returns it
108         /// Additionally, it calls setAttribute($key, $value) for every entry
109         /// in $attributes.
110         function appendElement($tagName, $attributes=array()) {
111                 $child = $this->appendChild($this->ownerDocument->createElement($tagName));
112                 foreach ($attributes as $key => $value) $child->setAttribute($key, $value);
113                 return $child;
114         }
115
116         /// Adds any UTF-8 string as content of the element - it will be escaped.
117         function appendText($text) {
118                 return $this->appendChild($this->ownerDocument->createTextNode($text));
119         }
120
121         // Appends a CDATASections to the element. This can be used to include
122         // raw (unparsed) HTML to the DOM tree as it is necessary because
123         // $parser->recursiveTagParse does not always escape & characters.
124         // (see https://bugzilla.wikimedia.org/show_bug.cgi?id=55526 )
125         // Workaround: Use a CDATA section. When serializing with $doc->saveHTML,
126         // the <![CDATA[...]]> is returned as ... .
127         // However, we end up having unescaped & in the output due to this bug in recursiveTagParse.
128         function appendCDATA($data) {
129                 return $this->appendChild($this->ownerDocument->createCDATASection($data));
130         }
131 }
132
133
134
135 // WrReportException
136 // -----------------
137
138 /// Exception type that is used internally by WrReport
139 class WrReportException extends Exception {}
140
141
142
143 // Fast version of Services_Libravatar
144 // -----------------------------------
145
146 /// This is a fast version of Services_Libravatar by omitting the DNS
147 /// lookup and always falling back to libravatar.org
148 class WrServicesLibravatar extends Services_Libravatar {
149         function __construct() {
150                 $this->setSize(32);
151                 $this->setDefault('monsterid');
152                 $this->setHttps(true);
153         }
154
155
156         protected function srvGet($domain, $https = false) {
157                 if ($https === true) return 'seccdn.libravatar.org';
158                 return 'cdn.libravatar.org';
159         }
160
161
162         public function getSize() {
163                 return $this->size;
164         }
165 }
166
167
168
169 // Tool functions
170 // --------------
171
172 /// Forces a regeneration of region overview pages ('Tirol', 'Vorarlberg', ...)
173 function wrRecacheRegions() {
174         $dbr = wfGetDB(DB_SLAVE);
175         // SELECT cl_from FROM categorylinks where cl_to = 'Region'
176         $res = $dbr->select('categorylinks', 'cl_from', array('cl_to' => 'Region'));
177         $page_ids = array();
178         while ($row = $dbr->fetchObject($res)) $page_ids[] = $row->cl_from;
179         $dbr->freeResult($res);
180
181         $titles = Title::newFromIDs($page_ids);
182         foreach ($titles as $title) $title->invalidateCache();
183 }
184
185
186 /// Returns the tuple ($report_id, $sledrun_condition, $date_report)
187 /// $date_report is NULL or a time as returned by strtotime
188 /// Expects a database connection ($dbr = wfGetDB(DB_SLAVE);)
189 /// and a page_id describung the page where the condition should be returned.
190 /// If no condition is found, (NULL, NULL, NULL) is returned.
191 function wrGetSledrunCondition($dbr, $page_id) {
192         // 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;
193         $cres = $dbr->select(
194                 'wrreport',
195                 array('wrreport.id as report_id', '`condition`', 'date_report'),
196                 array('page_id' => $page_id, '`condition` is not null', 'date_invalid > now()', 'delete_date is null'),
197                 'wrReportConditionRender',
198                 array('ORDER BY' => 'date_report desc, date_entry desc', 'LIMIT' => '1')
199         );
200         if ($cres->numRows() <= 0) {
201                 $report_id = NULL;
202                 $condition = NULL;
203                 $date_report = NULL;
204         } else {
205                 $crow = $dbr->fetchObject($cres);
206                 $report_id = $crow->report_id;
207                 $condition = $crow->condition;
208                 $date_report = strtotime($crow->date_report);
209         }
210         $dbr->freeResult($cres);
211         return array($report_id, $condition, $date_report);
212 }
213
214
215 /// Updates the line of the wrreportcache table that corresponds to the $page_id parameter
216 function wrUpdateWrReportCacheTable($page_id) {
217         // Determine the new content for the row that should be updated
218         $dbr = wfGetDB(DB_SLAVE);
219         list($report_id, $condition, $date_report) = wrGetSledrunCondition($dbr, $page_id);
220         $rows = wrReportGetReports(array('id' => $report_id), 1);
221
222         // Delete the old content (if any)
223         $dbw = wfGetDB(DB_MASTER);
224         $dbw->begin();
225         $dbw->delete('wrreportcache', array('page_id' => $page_id));
226
227         // Insert the updated row
228         if (count($rows) == 1) {
229                 $row = $rows[0];
230                 $dbw->insert('wrreportcache', array(
231                         'page_id' => $row['page_id'],
232                         'page_title' => $row['page_title'],
233                         'report_id' => $row['id'],
234                         'date_report' => $row['date_report'],
235                         '`condition`' => $row['condition'],
236                         'description' => $row['description'],
237                         'author_name' => $row['author_name'],
238                         'author_username' => (is_null($row['author_userid']) ? NULL : $row['author_username'])));
239         }
240         $dbw->commit();
241 }
242
243
244
245 // Render Functions
246 // ----------------
247
248 /// \brief Returns a form to enter a report (string containing HTML).
249 ///
250 /// All parameters have to be UTF-8 encoded.
251 /// \param $page_title Name of the sledrun.
252 /// \param $condition 1 to 5 for normal condition, 0 or NULL for missing condition and -1 for intentionally no condition.
253 /// \return UTF-8 encoded HTML form
254 function wrReportFormRender($hide_save_button = TRUE, $page_title = NULL, $date_report = NULL, $time_report = NULL, $condition = NULL, $description = NULL, $author_name = NULL, $page_title_list = NULL) {
255         $doc = new WrDOMDocument();
256         $rownum = 0;
257
258         // Info about special page
259         $specialPageName = wfMessage('wrreport')->text(); // 'Bahnberichte'
260         $title = Title::newFromText($specialPageName, NS_SPECIAL);
261         $specialPageUrl = $title->getLocalURL(); // e.g. '/wiki/Spezial:Bahnberichte'
262
263         // form
264         $form = $doc->appendElement('form', array('action' => $specialPageUrl, 'method' => 'post'));
265
266         // table (for layout of form elements)
267         $table = $form->appendElement('table', array('class' => 'wrreportform'));
268
269         // sledrun name
270         $tr = $table->appendElement('tr');
271         $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun')->text());
272         $td = $tr->appendElement('td');
273         $td->appendText($page_title);
274         $td->appendElement('input', array('type' => 'hidden', 'name' => 'page_title', 'value' => $page_title));
275
276         // report date
277         $tr = $table->appendElement('tr');
278         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-date')->text());
279         $td = $tr->appendElement('td');
280         $select = $td->appendElement('select', array('name' => 'date_report'));
281         $daynames = array(
282                 wfMessage('wrreport-date-today')->text(),
283                 wfMessage('wrreport-date-yesterday')->text(),
284                 wfMessage('wrreport-date-2daysbefore')->text(),
285                 wfMessage('wrreport-date-3daysbefore')->text(),
286                 wfMessage('wrreport-date-4daysbefore')->text());
287         $date_selected = false;
288         $time = time(); // number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
289         for ($day=0; $day!=5; ++$day) {
290                 $date = strtotime("-$day days", $time);
291                 $date_f = strftime("%Y-%m-%d", $date); // Formats it according to locale, that is set to CET.
292                 $option = $select->appendElement('option', array('value' => $date_f));
293                 if ((is_null($date_report) && $day == 0) || (!is_null($date_report) && $date_report == $date_f)) {
294                         $option->setAttribute('selected', 'selected');
295                         $date_selected = true;
296                 }
297                 $option->appendText($daynames[$day] . ' (' . strftime('%d.%m.', $date) . ')');
298         }
299         if (!$date_selected) { // note: if $date_report is null $date_selected is true here
300                 $option = $select->appendElement('option', array('value' => $date_report, 'selected' => 'selected'));
301                 $option->appendText($date_report);
302         }
303
304         // report time
305         $tr = $table->appendElement('tr');
306         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-time')->text());
307         $td = $tr->appendElement('td');
308         $td->appendElement('input', array('name' => 'time_report', 'maxlength' => '5', 'size' => '5', 'value' => $time_report));
309         $td->appendText(' ');
310         $td->appendText('Uhr');
311         $td->appendText(' ');
312         $td->appendElement('span', array('class' => 'wrcomment'))->appendText("(z.B. '14', '1400' oder '14:00'. Optional.)");
313
314         // condition
315         $tr = $table->appendElement('tr');
316         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-condition')->text());
317         $td = $tr->appendElement('td');
318         $select = $td->appendElement('select', array('name' => 'condition', 'required' => 'required'));
319         $option = $select->appendElement('option', array('value' => '0'));
320         $option->appendText(wfMessage('wrreport-condition-select')->text());
321         foreach (WrReport::$wrConditions as $condition_num => $condition_text) {
322                 $option = $select->appendElement('option', array('value' => (string) $condition_num));
323                 if ($condition == $condition_num) $option->setAttribute('selected', 'selected');
324                 $option->appendText($condition_text);
325         }
326         $option = $select->appendElement('option', array('value' => '-1'));
327         if ($condition == -1) $option->setAttribute('selected', 'selected');
328         $option->appendText(wfMessage('wrreport-condition-no')->text());
329
330         // description
331         $tr = $table->appendElement('tr');
332         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-description')->text());
333         $tr->appendElement('td')->appendElement('textarea', array('name' => 'description', 'class' => 'fullwidth', 'rows' => '7', 'required' => 'required'))->appendText($description);
334
335         // author
336         $tr = $table->appendElement('tr');
337         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-author')->text());
338         $tr->appendElement('td')->appendElement('input', array('name' => 'author_name', 'class' => 'fullwidth', 'maxlength' => '30', 'value' => $author_name));
339
340         // Buttons
341         // I would like to do it this way, but due to a bug of internet explorer, the <button> element is not useable.
342         //   $buttons = '<button name="action" type="submit" value="preview">Vorschau';
343         //   if ($hide_save_button) $buttons .= ' &amp; Speichern';
344         //   $buttons .= '</button>';
345         //   if (!$hide_save_button) $buttons .= '<button name="action" type="submit" value="store">Speichern</button>';
346         // Workaround: User <input type="submit"/>
347         $tr = $table->appendElement('tr');
348         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-submit')->text());
349         $td = $tr->appendElement('td');
350         $input = $td->appendElement('input', array('name' => 'preview', 'type' => 'submit'));
351         if ($hide_save_button)
352                 $input->setAttribute('value', wfMessage('wrreport-newreport-next')->text());
353         else {
354                 $input->setAttribute('value', wfMessage('wrreport-newreport-preview')->text());
355
356                 $td->appendElement('input', array('name' => 'store', 'type' => 'submit', 'value' => wfMessage('wrreport-newreport-save')->text()));
357         }
358
359         return $doc->saveHTML($form);
360 }
361
362
363 /// \brief Renders the form to delete a report
364 ///
365 /// All in and output strings should be/are UTF-8 encoded.
366 /// \param $reportid the id of the report that is going to be deleted
367 /// \param $delete_person_name name of the person that wants to delete the report (form field)
368 /// \param $delete_reason_public publically visible reason for deleting the entry.
369 /// \param $delete_invisible don't show the report as being deleted
370 /// \return UTF-8 encoded HTML form
371 function wrDeleteReportFormRender($reportid, $delete_person_name, $delete_reason_public, $delete_invisible) {
372         $doc = new WrDOMDocument();
373         $rownum = 0;
374
375         // Info about special page
376         $specialPageName = wfMessage('wrreport')->text(); // 'Bahnberichte'
377         $title = Title::newFromText($specialPageName, NS_SPECIAL);
378         $specialPageUrl = $title->getLocalURL(); // e.g. '/wiki/Spezial:Bahnberichte'
379
380         // form
381         $form = $doc->appendElement('form', array('action' => $specialPageUrl, 'method' => 'post'));
382
383         // table (for layout of form elements)
384         $table = $form->appendElement('table', array('class' => 'wrreportform', 'summary' => wfMessage('wrreport-deletereport-tablesummary')->text()));
385
386         // delete_reason_public
387         $tr = $table->appendElement('tr');
388         $tr->appendElement('th')->appendText(wfMessage('wrreport-deletereport-reason')->text());
389         $tr->appendElement('td')->appendElement('textarea', array('name' => 'delete_reason_public', 'cols' => '50', 'rows' => '7'))->appendText($delete_reason_public);
390
391         // delete_person_name
392         $tr = $table->appendElement('tr');
393         $tr->appendElement('th')->appendText(wfMessage('wrreport-deletereport-name')->text());
394         $tr->appendElement('td')->appendElement('input', array('name' => 'delete_person_name', 'maxlength' => '30', 'size' => '30', 'value' => $delete_person_name));
395
396         // buttons
397         $tr = $table->appendElement('tr');
398         $tr->appendElement('th')->appendText(wfMessage('wrreport-reports-action')->text());
399         $td = $tr->appendElement('td');
400         $td->appendElement('input', array('name' => 'deletepreview', 'type' => 'submit', 'value' => wfMessage('wrreport-newreport-preview')->text()));
401         $td->appendElement('input', array('name' => 'delete', 'type' => 'submit', 'value' => wfMessage('wrreport-deletereport-delete')->text()));
402
403         // hidden field
404         $input = $td->appendElement('input', array('name' => 'reportid', 'type' => 'hidden', 'value' => (string) $reportid));
405         // delete_invisible - who is allowed to do so?
406         // $td->appendElement('input', array('name' => 'delete_invisible', 'type' => 'hidden', 'value' => (string) $delete_invisible));
407
408         return $doc->saveHTML($form);
409 }
410
411
412 /// \brief Generates the DOM of the table header ("private" sub-function of wrReportTableRender)
413 ///
414 /// \param $table table WrDOMElement where the header should be appended
415 /// \param $format row format like WRREPORT_COMPACT
416 /// \param $showActions boolean to indicate whether an actions column should be created
417 /// \return WrDOMElement of the HTML table header
418 function wrReportTableTitleRender($table, $format, $showActions) {
419         $tr = $table->appendElement('tr');
420         $tr->appendElement('th')->appendText(wfMessage('wrreport-reports-date-trip')->text());
421         if ($format != WRREPORT_COMPACT) $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun')->text());
422         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-condition')->text());
423         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-description')->text());
424         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-author')->text());
425         if ($format == WRREPORT_DETAIL) $tr->appendElement('th')->appendText(wfMessage('wrreport-reports-date-entry')->text());
426         if ($showActions) $tr->appendElement('th')->appendText(wfMessage('wrreport-reports-action')->text());
427         return $tr;
428 }
429
430
431 /// \brief Generates the DOM of a table row ("private" sub-function of wrReportTableRender)
432 ///
433 /// \param $table table WrDOMElement where the row should be appended
434 /// \param $row associative array of table columns like one row in the wrreport table
435 /// \param $format row format like WRREPORT_COMPACT
436 /// \param $showActions boolean to indicate whether an actions column should be created
437 /// \param $parser Parser instance of a Parser class that can be used to parse the condition
438 /// \return WrDOMElement of the HTML table row
439 function wrReportTableRowRender($table, $row, $format, $showActions, $parser) {
440         $tr = $table->appendElement('tr');
441
442         // $row['date_report'] and $row['time_report']
443         $date_report = strtotime($row['date_report']);
444         $date_report = wfMessage(Language::$mWeekdayAbbrevMsgs[(int) date('w', $date_report)])->text() . strftime(', %d.%m.', $date_report);
445         $td = $tr->appendElement('td');
446         $td->appendText($date_report);
447         if ($row['time_report']) {
448                 $td->appendText(' ');
449                 $td->appendElement('span', array('class' => 'wrcomment'))->appendText(date('H:i', strtotime($row['time_report'])));
450         }
451
452         // $row['page_title']
453         if ($format != WRREPORT_COMPACT) {
454                 $title = Title::newFromText($row['page_title']);
455                 $tr->appendElement('td')->appendCDATA(Linker::link($title));
456         }
457
458         // $row['condition']
459         $condition_text = '---';
460         if (isset(WrReport::$wrConditions[$row['condition']])) $condition_text = WrReport::$wrConditions[$row['condition']];
461         $td = $tr->appendElement('td');
462         if ($row['delete_date']) $td->appendElement('em')->appendText(wfMessage('wrreport-deletereport-deleted')->text());
463         else $td->appendText($condition_text);
464
465         // $row['description']
466         $td = $tr->appendElement('td', array('class' => 'wrreportdescription'));
467         if ($row['delete_date']) $td->appendElement('em')->appendText(wfMessage('wrreport-deletereport-deleted')->text());
468         else {
469                 // for registered users, use wikitext formatting
470                 if (is_null($row['author_userid'])) $td->appendText($row['description']);
471                 else {
472                         $html = $parser->recursiveTagParse($row['description']);
473                         $td->appendCDATA($html);
474                 }
475         }
476
477         // $row['author_name']
478         $td = $tr->appendElement('td');
479         if ($row['delete_date']) $td->appendElement('em')->appendText(wfMessage('wrreport-deletereport-deleted')->text());
480         else {
481                 if (!is_null($row['author_userid'])) {
482                         // find user's email ($user->getEmail())
483                         $user = User::newFromId($row['author_userid']);
484
485                         // create avatar (using the Services_Libravatar library to get avatar URL)
486                         $sla = new WrServicesLibravatar();
487                         $url = $sla->getUrl($user->getEmail());
488
489                         // create img tag for the avatar
490                         $td->appendElement('img', array('src' => $url, 'alt' => $row['author_name'], 'width' => $sla->getSize(), 'height' => $sla->getSize()));
491                         $td->appendText(' ');
492
493                         // get user page
494                         $title = $user->getUserPage();
495                         if ($title->exists()) {
496                                 $td->appendCDATA(Linker::link($title, $row['author_name']));
497                         } else {
498                                 $td->appendText($row['author_name']);
499                         }
500                         $td->appendText(' ');
501                         
502                         // get number of sledrun reports
503                         $td->appendElement('span', array('class' => 'wrreportcount'))
504                                 ->appendText((string) WrReport::getUserSeldrunReportCount($user->getId()));
505
506                 } else {
507                         $td->appendText($row['author_name']);
508                 }
509         }
510
511         // $row['date_entry']
512         if ($format == WRREPORT_DETAIL) {
513                 $td = $tr->appendElement('td');
514                 $td->appendText(date('d.m. ', strtotime($row['date_entry'])));
515                 $td->appendElement('span', array('class' => 'wrcomment'))->appendText(date('H:i', strtotime($row['date_entry'])));
516         }
517
518         // actions
519         // wiki/Spezial:Bahnberichte?action=deletepreview&reportid=42
520         if ($showActions) {
521                 $td = $tr->appendElement('td');
522                 if (!isset($row['delete_date'])) {
523                         $specialPageName = wfMessage('wrreport')->text(); // 'Bahnberichte'
524                         $title = Title::newFromText($specialPageName, NS_SPECIAL);
525                         $specialPageUrl = $title->getLocalURL(); // e.g. '/wiki/Spezial:Bahnberichte'
526                         $td->appendElement('a', array('href' => $specialPageUrl . '?action=deletepreview&reportid=' . $row['id']))
527                                 ->appendText(wfMessage('wrreport-deletereport-delete')->text() . '...');
528                 }
529         }
530         return $tr;
531 }
532
533
534 /// \brief Renders the report table. Call wrReportGetReports for the $rows parameter.
535 ///
536 /// \param $rows array of associative row arrays
537 /// \param $format row format like WRREPORT_TABLE_SHORT
538 function wrReportTableRender($rows, $format, $showActions, $parser) {
539         $doc = new WrDOMDocument();
540         $table = $doc->appendElement('table', array('class' => 'wrreporttable'));
541         wrReportTableTitleRender($table, $format, $showActions);
542         foreach ($rows as $key => $row) wrReportTableRowRender($table, $row, $format, $showActions, $parser);
543         return $doc->saveHTML($table);
544 }
545
546
547 /// Returns an array with column names
548 function wrReportGetColumnNames() {
549         return array('id', 'page_id', 'page_title', 'date_report', 'time_report', 'date_entry', 'date_invalid', 'condition', 'description', 'author_name', 'author_userid', 'author_username', 'delete_date', 'delete_person_name', 'delete_person_ip', 'delete_person_userid', 'delete_person_username', 'delete_reason_public', 'delete_invisible');
550 }
551
552
553 /// \brief Returns reports as associative array.
554 ///
555 /// Examples:
556 ///   $conditions = array('page_title' => 'Birgitzer Alm', 'date_invalid > now()');
557 ///   $limit = 1; // or NULL for no limit
558 function wrReportGetReports($conditions, $limit=NULL) {
559         $dbr = wfGetDB(DB_SLAVE);
560         $columns = wrReportGetColumnNames();
561         global $wgDBtype;
562         if ($wgDBtype == "mysql") // "condition" is a reserved word in mysql
563                  for ($i = 0; $i != count($columns); ++$i) $columns[$i] = sprintf('`%s`', $columns[$i]);
564         $options = array('ORDER BY' => 'date_report desc, date_entry desc');
565         if (!is_null($limit)) $options['LIMIT'] = $limit;
566         $res = $dbr->select('wrreport', $columns, $conditions, 'wrReportGetReports', $options);
567         $result = array();
568         while ($row = $dbr->fetchRow($res)) $result[] = $row;
569         $dbr->freeResult($res);
570         return $result;
571 }
572
573
574 /// \brief It returns an array of the "condition" (as number) and the date of the "most recent" report of the specified page (to decode as list($condition, $date));
575 ///
576 /// If no condition is present, array(NULL, NULL) is returned
577 function wrReportConditionRender($page_title) {
578         $dbr = wfGetDB(DB_SLAVE);
579         $cond = 'condition';
580         global $wgDBtype;
581         // 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;
582         if ($wgDBtype == "mysql") $cond = "`$cond`"; // "condition" is a reserved word in mysql
583         $res = $dbr->select(
584                 'wrreport',
585                 array('wrreport.id as report_id', $cond, 'date_report'),
586                 array('page_title' => $page_title, "$cond is not null", 'date_invalid > now()', 'delete_date is null'),
587                 'wrReportConditionRender',
588                 array('ORDER BY' => 'date_report desc, date_entry desc', 'LIMIT' => '1')
589         );
590         if ($res->numRows() <= 0) {
591                 $dbr->freeResult($res);
592                 return array(NULL, NULL);
593         }
594         $row = $dbr->fetchObject($res);
595         $date = $row->date_report;
596         if ($date) $date = strtotime($date);
597         $dbr->freeResult($res);
598         return array($row->condition, $date);
599 }
600
601
602 /// \brief Returns true if the user is allowed to delete reports (in general)
603 function wrReportUserMayDelete() {
604         global $wgUser;
605         global $wgWrReportDeleteMode;
606         return $wgWrReportDeleteMode == 'allow' || ($wgWrReportDeleteMode == 'loggedin' && $wgUser->isLoggedIn());
607 }
608
609
610
611 // tag extension hooks
612 // -------------------
613
614 class WrReport {
615         // Conditions: array(1 => 'Sehr gut', 2 => 'Gut', 3 => 'Mittelmäßig', 4 => 'Schlecht', 5 => 'Geht nicht');
616         public static $wrConditions;
617
618
619         public static function initWrConditions() {
620                 WrReport::$wrConditions = array();
621                 for ($i = 1; $i != 6; ++$i) WrReport::$wrConditions[$i] = wfMessage('wrreport-condition-' . $i)->text();
622         }
623
624
625         /// Returns the number of sledrun reports issued by a user with the given id.
626         public static function getUserSeldrunReportCount($user_id) {
627                 $dbr = wfGetDB(DB_SLAVE);
628                 // select count(*) from wrreport where author_userid = 1 and delete_date is null and `condition` is not null;
629                 $res = $dbr->select('wrreport', 'count(*)', array('author_userid' => $user_id, 'delete_date' => null, '`condition` is not null'));
630                 $row = $res->fetchRow();
631                 $count = $row[0];
632                 $dbr->freeResult($res);
633                 return $count;
634         }
635
636
637         // Parser Hook Functions
638         // ---------------------
639
640         public static function ParserFirstCallInitHook(&$parser) {
641                 $parser->setHook('bahnberichtformular', 'WrReport::bahnberichtformularParserHook');
642                 $parser->setHook('bahnberichte', 'WrReport::bahnberichteParserHook');
643                 $parser->setHook('bahnentabelle', 'WrReport::bahnentabelleParserHook');
644                 $parser->setHook('bahnenregiontabelle', 'WrReport::bahnenregiontabelleParserHook');
645                 $parser->setHook('rodelbahntabelle', 'WrReport::rodelbahntabelleParserHook');
646                 $parser->setHook('avatar', 'WrReport::avatarParserHook');
647                 return true;
648         }
649
650         /// \brief Is called when the tag <bahnberichtformular/> is encountered.
651         ///
652         /// The current page name is taken.
653         public static function bahnberichtformularParserHook($input, $args, $parser) {
654                 // Username
655                 global $wgUser;
656                 $author_name = NULL;
657                 if ($wgUser->isLoggedIn()) {
658                         $author_name = $wgUser->getRealName();
659                         if (!$author_name) $author_name = $wgUser->getName();
660                 }
661
662                 global $wgWrReportMode;
663                 global $wgWrReportBlackListAll;
664                 global $wgWrReportBlackListStrangers;
665
666                 // is the form allowed to be shown?
667                 $error_key = null;
668                 if ($wgWrReportMode == 'summer') $error_key = 'wrreport-newreport-summer';
669                 elseif ($wgWrReportMode == 'deny') $error_key = 'wrreport-newreport-deny';
670                 elseif ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) $error_key = 'wrreport-newreport-loggedin';
671                 elseif (in_array($parser->getTitle()->getText(), $wgWrReportBlackListAll)) $error_key = 'wrreport-newreport-blacklist';
672                 elseif (!$wgUser->isLoggedIn() && in_array($parser->getTitle()->getText(), $wgWrReportBlackListStrangers)) $error_key = 'wrreport-newreport-blackliststrangers';
673
674                 // report error
675                 if (!is_null($error_key)) {
676                         $doc = new WrDOMDocument();
677                         $p = $doc->appendElement('p')->appendElement('em')->appendText(wfMessage($error_key)->text());
678                         return $doc->saveHTML($p);
679                 }
680
681                 // Calling "$title = $parser->getTitle(); $title->invalidateCache();" doesn't help here to force regeneration
682                 // However, this would not be the best solution because the page has to be re-rendered only at midnight
683
684                 // In the following line, $author_name was replaced by NULL to prevent a bug, where the wrong author_name
685                 // is shown due to caching (see ticket #35).
686                 return array(wrReportFormRender(TRUE, $parser->getTitle()->getText(), NULL, NULL, NULL, NULL, NULL), 'markerType' => 'nowiki');
687         }
688
689
690         /// \brief Is called when the tag <bahnberichte/> is encountered.
691         ///
692         /// The current page name is taken.
693         public static function bahnberichteParserHook($input, $args, $parser) {
694                 $parser->getOutput()->addModules('ext.wrreport'); // getOutput() returns class ParserOutput
695                 $title = $parser->getTitle();
696                 $page_id = $title->getArticleID();
697
698                 $rows = array();
699                 if ($page_id > 0) {
700                         global $wgOut; // class OutputPage
701                         global $wgWrReportFeedRoot;
702                         $wgOut->addFeedLink('atom', $wgWrReportFeedRoot . '/berichte/bahn/' . strtolower($title->getPartialURL()));
703
704                         $conditions = array('page_id' => $page_id, 'date_invalid > now()');
705                         $rows = wrReportGetReports($conditions);
706                 }
707                 if (count($rows) == 0) return wfMessage('wrreport-reports-none')->text();
708                 return array(wrReportTableRender($rows, WRREPORT_COMPACT, wrReportUserMayDelete(), $parser), 'markerType' => 'nowiki');
709         }
710
711
712         /// Returns the region details of the region specifed in $conditions.
713         /// region_id (as in the database), region name (as in the database), and region border (as WKB).
714         /// conditions is an array that's given to the where clause
715         private static function getRegionDetails($conditions) {
716                 // Example: SELECT name FROM wrregion WHERE page_id = 882;
717                 $dbr = wfGetDB(DB_SLAVE);
718                 $res = $dbr->select('wrregion', array('id', 'name', 'aswkb(border)'), $conditions);
719                 if ($dbr->numRows($res) == 1) {
720                         $row = $dbr->fetchRow($res);
721                         return array($row[0], $row[1], $row[2]); // region_id, region_name, region_border_wkb
722                 }
723                 $dbr->freeResult($res);
724                 return array(null, null, null);
725         }
726
727
728         /// Returns the region details if the specified title is one.
729         /// region_id (as in the database), region name (as in the database), and region border (as WKB).
730         private static function getPageRegion($title) {
731                 $categories = $title->getParentCategories(); // e.g. array('Kategorie:Region' => 'Osttirol')
732                 global $wgContLang;
733                 $key_region = $wgContLang->getNSText(NS_CATEGORY) . ':Region';
734                 if (array_key_exists($key_region, $categories)) {
735                         return WrReport::getRegionDetails(array('page_id' => $title->getArticleID()));
736                 }
737                 return array(null, null, null);
738         }
739
740
741         /// Adds a region feed to the current page
742         private static function addRegionFeedLink($title) {
743                 list($region_id, $region_name, $region_border_wkb) = WrReport::getPageRegion($title);
744                 if (is_null($region_name)) return;
745                 global $wgWrReportFeedRoot;
746                 global $wgOut; // class OutputPage
747                 $wgOut->addFeedLink('atom', $wgWrReportFeedRoot . '/berichte/region/' . strtolower($region_name));
748         }
749
750
751         /// Creates the HTML of the <bahnentabelle>, <bahnenregiontabelle> and <rodelbahntabelle> tags.
752         private static function createBahnentabelle($page_titles) {
753                 $dbr = wfGetDB(DB_SLAVE);
754
755                 // 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
756                 $where_array = array('page.page_id = wrsledruncache.page_id');
757                 if (count($page_titles) > 0) {
758                         $mysql_page_ids = array();
759                         foreach ($page_titles as $page_title) $mysql_page_ids[] = $page_title->getArticleID();
760                         $where_array[] = 'page.page_id in (' . implode(', ', $mysql_page_ids) . ')';
761                 } else $where_array[] = 'false';
762                 $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'));
763
764                 global $wgUser;
765                 global $wgWrReportMode; // e.g. 'summer'
766                 global $wgWrReportBlackListAll;
767                 global $wgWrReportBlackListStrangers;
768                 global $wgDBtype;
769
770                 // Determine, whether the user is allowed to make a new report
771                 $userMayReport = ($wgWrReportMode == 'allow' || ($wgWrReportMode == 'loggedin' && $wgUser->isLoggedIn()));
772
773                 // Generate DOM for HTML
774                 $doc = new WrDOMDocument();
775                 $table = $doc->appendElement('table', array('class' => 'wikitable'));
776
777                 // Title
778                 $tr = $table->appendElement('tr');
779                 $tr->appendElement('th')->appendElement('img', array('src' => '/vorlagen/s_rental.png', 'alt' => wfMessage('wrreport-icon-sledrental')->text(), 'title' => wfMessage('wrreport-icon-sledrental')->text()));
780                 $tr->appendElement('th')->appendElement('img', array('src' => '/vorlagen/s_light.png', 'alt' => wfMessage('wrreport-icon-nightlight')->text(), 'title' => wfMessage('wrreport-icon-nightlight')->text()));
781                 $tr->appendElement('th')->appendElement('img', array('src' => '/vorlagen/s_lift.png', 'alt' => wfMessage('wrreport-icon-lift')->text(), 'title' => wfMessage('wrreport-icon-lift')->text()));
782                 $tr->appendElement('th')->appendElement('img', array('src' => '/vorlagen/s_walk.png', 'alt' => wfMessage('wrreport-icon-walkupseparate')->text(), 'title' => wfMessage('wrreport-icon-walkupseparate')->text()));
783                 $tr->appendElement('th')->appendElement('img', array('src' => '/vorlagen/s_bus.png', 'alt' => wfMessage('wrreport-icon-publictransport')->text(), 'title' => wfMessage('wrreport-icon-publictransport')->text()));
784                 $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun')->text());
785                 if ($wgWrReportMode != 'summer') $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-condition')->text());
786                 $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun-information')->text());
787                 $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun-walkuptime')->text());
788                 $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun-height')->text());
789                 $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun-length')->text());
790
791                 // Rows
792                 while ($row = $dbr->fetchObject($res)) {
793                         $title = Title::newFromRow($row);
794                         $tr = $table->appendElement('tr');
795
796                         $td = $tr->appendElement('td');
797                         if ($row->sled_rental) $td->appendElement('img', array('src' => '/vorlagen/s_rental.png', 'alt' => wfMessage('wrreport-icon-sledrental')->text(), 'title' => wfMessage('wrreport-icon-sledrental')->text()));
798
799                         $td = $tr->appendElement('td');
800                         if ($row->night_light) $td->appendElement('img', array('src' => '/vorlagen/s_light.png', 'alt' => wfMessage('wrreport-icon-nightlight')->text(), 'title' => wfMessage('wrreport-icon-nightlight')->text()));
801
802                         $td = $tr->appendElement('td');
803                         if ($row->lift) $td->appendElement('img', array('src' => '/vorlagen/s_lift.png', 'alt' => wfMessage('wrreport-icon-lift')->text(), 'title' => wfMessage('wrreport-icon-lift')->text()));
804
805                         $td = $tr->appendElement('td');
806                         if (!is_null($row->walkup_possible)) {
807                                 if ($row->walkup_possible) {
808                                         if ($row->walkup_separate) $td->appendElement('img', array('src' => '/vorlagen/s_walk.png', 'alt' => wfMessage('wrreport-icon-walkupseparate')->text(), 'title' => wfMessage('wrreport-icon-walkupseparate')->text()));
809                                 } else $td->appendElement('img', array('src' => '/vorlagen/s_nowalk.png', 'alt' => wfMessage('wrreport-icon-walkupnotpossible')->text(), 'title' => wfMessage('wrreport-icon-walkupnotpossible')->text()));
810                         }
811
812                         $td = $tr->appendElement('td');
813                         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()));
814
815                         $tr->appendElement('td')->appendElement('a', array('href' => $title->getLocalURL()))->appendText($title->getPrefixedText());
816
817                         if ($wgWrReportMode != 'summer') {
818                                 // Check rights
819                                 $userMayReportThis = $userMayReport;
820                                 if ($userMayReportThis) {
821                                         if (in_array($title->getText(), $wgWrReportBlackListAll)) $userMayReportThis = FALSE;
822                                         if (!$wgUser->isLoggedIn() && in_array($title->getText(), $wgWrReportBlackListStrangers)) $userMayReportThis = FALSE; // Title::getText() uses spaces instead of underscores
823                                 }
824
825                                 // Select condition
826                                 list($report_id, $condition, $date_report) = wrGetSledrunCondition($dbr, $row->page_id);
827                                 if (is_null($report_id)) $date = '';
828                                 else $date = strftime('%d.%m.', $date_report);
829
830                                 $td = $tr->appendElement('td');
831                                 if (isset(WrReport::$wrConditions[$condition])) {
832                                         $td->appendElement('a', array('href' => $title->getLocalURL() . '#' . Title::escapeFragmentForURL(wfMessage('wrreport-reports-sectionname')->text())))->appendText(WrReport::$wrConditions[$condition]);
833                                         $td->appendText(' ');
834                                         $small = $td->appendElement('small');
835                                         $small->appendText($date);
836                                         if ($userMayReportThis) {
837                                                 $small->appendText(' ');
838                                                 $small->appendElement('em')->appendElement('a', array('href' =>$title->getLocalURL() . '#' . Title::escapeFragmentForURL(wfMessage('wrreport-newreport-sectionname')->text())))->appendText(wfMessage('wrreport-newreport-new')->text());
839                                         }
840                                 } else {
841                                         if ($userMayReportThis)
842                                                 $td->appendElement('small')->appendElement('em')->appendElement('a', array('href' =>$title->getLocalURL() . '#' . Title::escapeFragmentForURL(wfMessage('wrreport-newreport-sectionname')->text())))->appendText(wfMessage('wrreport-newreport-please')->text());
843                                         else $td->appendText('--');
844                                 }
845                         }
846
847                         $td = $tr->appendElement('td');
848                         $info_phone = $row->information_phone;
849                         if ($info_phone) {
850                                 $info_parts = explode(';', $info_phone);
851                                 $info_parts = explode('(', $info_parts[0], 2);
852                                 if (count($info_parts) == 2 && substr($info_parts[1], -1) == ')') {
853                                         $td->appendText($info_parts[0]);
854                                         $td->appendElement('span', array('class' => 'wrtelinfo'))->appendText(substr($info_parts[1], 0, -1));
855                                 } else $td->appendText($info_phone);
856                         }
857                         $info_web = $row->information_web;
858                         if ($info_web === 'Nein') $info_web = null;
859                         if ($info_phone && $info_web) $td->appendText('; ');
860                         if ($info_web) {
861                                 $td->appendElement('a', array('href' => $info_web))->appendText('web');
862                         }
863
864
865                         $tr->appendElement('td')->appendText($row->walkup_time ? $row->walkup_time . ' min' : '');
866
867                         $tr->appendElement('td')->appendText(
868                                 ($row->bottom_elevation ? $row->bottom_elevation  : '') .
869                                 ($row->bottom_elevation && $row->top_elevation ? ' - ' : '') .
870                                 ($row->top_elevation ? $row->top_elevation : '') .
871                                 ($row->bottom_elevation || $row->top_elevation ? ' m' : ''));
872
873                         $tr->appendElement('td')->appendText($row->length ? $row->length . ' m' : '');
874                 }
875                 $dbr->freeResult($res);
876
877                 return $doc->saveHTML();
878         }
879
880
881         /// \brief Is called when the tag <bahnentabelle/> is encountered.
882         ///
883         /// Example:
884         /// <bahnentabelle>
885         /// Birgitzer Alm (vom Adelshof)
886         /// Kemater Alm
887         /// Axamer Lizum
888         /// </bahnentabelle>
889         public static function bahnentabelleParserHook($input, $args, $parser) {
890                 $parser->getOutput()->addModules('ext.wrreport');
891
892                 // Add feed
893                 // 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.
894                 WrReport::addRegionFeedLink($parser->getTitle());
895
896                 // Add each page title that has been found
897                 $page_titles = array(); // array of Title objects
898                 foreach (explode("\n", $input) as $page_title) {
899                         $page_title = Title::newFromText(trim($page_title));
900                         if (!$page_title || !$page_title->exists()) continue;
901                         $page_titles[] = $page_title;
902                 }
903
904                 // Create bahnentabelle
905                 $html = WrReport::createBahnentabelle($page_titles);
906                 return array($html, 'markerType' => 'nowiki');
907         }
908
909
910         /// \brief Is called when the tag <bahnenregiontabelle/> is encountered.
911         ///
912         /// Example 1:
913         /// <bahnenregiontabelle />
914         ///
915         /// Example 2:
916         /// <bahnenregiontabelle wiki="Innsbruck" /> (refers to region represented by the MediaWiki Title name)
917         /// <bahnenregiontabelle region_id="3" /> (refers to id in the wrregion table)
918         /// <bahnenregiontabelle region_name="Innsbruck" /> (refers to name in the wrregion table)
919         public static function bahnenregiontabelleParserHook($input, $args, $parser) {
920                 $parser->getOutput()->addModules('ext.wrreport');
921
922                 try {
923                         // we accept 0 or 1 parameters
924                         if (count($args) > 1) throw new WrReportException(wfMessage('wrreport-bahnenregiontabelle-toomanyarguments')->text());
925
926                         if (count($args) == 0) {
927                                 // current page represents a region
928                                 $title = $parser->getTitle(); // default title: current page
929                                 list($region_id, $region_name, $region_border_wkb) = WrReport::getPageRegion($title);
930                                 if (is_null($region_id)) throw new WrReportException(wfMessage('wrreport-bahnenregiontabelle-thispagenoregion')->text());
931                         } elseif (isset($args['wiki'])) {
932                                 // other page represents a region
933                                 $title = Title::newFromText($args['wiki']);
934                                 list($region_id, $region_name, $region_border_wkb) = WrReport::getPageRegion($title);
935                                 if (is_null($region_id)) throw new WrReportException(wfMessage('wrreport-bahnenregiontabelle-pagenoregion')->text());
936                         } elseif (isset($args['region_id'])) {
937                                 list($region_id, $region_name, $region_border_wkb) = WrReport::getRegionDetails(array('id' => $args['region_id']));
938                                 if (is_null($region_id)) throw new WrReportException(wfMessage('wrreport-bahnenregiontabelle-noregionid')->text());
939                         } elseif (isset($args['region_name'])) {
940                                 list($region_id, $region_name, $region_border_wkb) = WrReport::getRegionDetails(array('name' => $args['region_name']));
941                                 if (is_null($region_id)) throw new WrReportException(wfMessage('wrreport-bahnenregiontabelle-noregionname')->text());
942                         } else {
943                                 throw new WrReportException(wfMessage('wrreport-bahnenregiontabelle-invalidargument', array_keys($args)[0])->text());
944                         }
945
946                         // get titles that are in the region
947                         $page_titles = array();
948                         $dbr = wfGetDB(DB_SLAVE);
949                         // the following line would work if MySQL 5.5 would implement a real geospatial version of CONTAINS.
950                         // $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');
951                         $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');
952                         foreach ($res as $row) {
953                                 $page_titles[] = Title::newFromId($row->page_id);
954                         }
955                         $dbr->freeResult($res);
956                         $html = WrReport::createBahnentabelle($page_titles);
957
958                 } catch (WrReportException $e) {
959                         $doc = new WrDOMDocument();
960                         $doc->appendElement('span', array('class' => 'error'))->appendText(wfMessage('wrreport-bahnenregiontabelle-error', $e->getMessage())->text());
961                         $html = $doc->saveHTML($doc->firstChild);
962                 }
963
964                 return array($html, 'markerType' => 'nowiki');
965         }
966
967
968         /// \brief Is called when the tag <rodelbahntabelle/> is encountered.
969         ///
970         /// Description: See description of wrreport.php
971         public static function rodelbahntabelleParserHook($input, $args, $parser) {
972                 $parser->getOutput()->addModules('ext.wrreport');
973
974                 // Add feed
975                 // 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.
976                 WrReport::addRegionFeedLink($parser->getTitle());
977
978                 $dbr = wfGetDB(DB_SLAVE);
979                 try {
980                         libxml_use_internal_errors(true); // without that, we get PHP Warnings if the $input is not well-formed
981                         try {
982                                 $xml_input = '<rodelbahntabelle>' . $input . '</rodelbahntabelle>';
983                                 $xml = new SimpleXMLElement($xml_input); // input
984                         } catch (Exception $e) {
985                                 throw new WrReportException(wfMessage('wrreport-rodelbahntabelle-invalid-xml', $xml_input)->text());
986                         }
987                         $whitespace = (string) $xml; // everything between <rodelbahntabelle> and </rodelbahntabelle> that's not a sub-element
988                         if (strlen($whitespace) > 0 && !ctype_space($whitespace)) // there must not be anythin except sub-elements or whitespace
989                                 throw new WrReportException(wfMessage('wrreport-rodelbahntabelle-textbetweenelements', trim($xml))->text());
990
991                         // page_ids of sledrun titles that that are going to be returned
992                         $page_ids = array();
993                         foreach ($xml as $entry) { // entry is <rodelbahn>, <region> or <rodelbahnen>
994                                 $entry_page_ids = array(); // page_ids selected (or un-selected) for this entry.
995                                 $tagname = $entry->getName();
996                                 $attributes = array();
997                                 foreach ($entry->attributes() as $key => $value) $attributes[(string) $key] = (string) $value;
998
999                                 // is the tagname valid?
1000                                 if (!in_array($tagname, array('rodelbahn', 'rodelbahnen', 'region')))
1001                                         throw new WrReportException(wfMessage('wrreport-rodelbahntabelle-invalid-element', $tagname)->text());
1002
1003                                 // parse operation attribute
1004                                 $operation = '+'; // '+' (append) or '-' (subtract)
1005                                 if (array_key_exists('operation', $attributes)) {
1006                                         $operation = $attributes['operation'];
1007                                         if (!in_array($operation, array('+', '-')))
1008                                                 throw new WrReportException(wfMessage('wrreport-rodelbahntabelle-invalid-attribute-value', $tagname, 'operation', $operation)->text());
1009                                         unset($attributes['operation']);
1010                                 }
1011
1012                                 // parse in_arbeit attribute
1013                                 $under_construction = false; // false (only sledruns that are not under construction), true (only sledruns under construction) or null (doen't matter)
1014                                 if ($tagname === 'rodelbahn') $under_construction = null; // different default value for tag <rodelbahn>.
1015                                 if (array_key_exists('in_arbeit', $attributes)) {
1016                                         if ($attributes['in_arbeit'] === 'nein') $under_construction = false;
1017                                         elseif ($attributes['in_arbeit'] === 'ja') $under_construction = true;
1018                                         elseif ($attributes['in_arbeit'] === '*') $under_construction = null;
1019                                         else throw new WrReportException(wfMessage('wrreport-rodelbahntabelle-invalid-attribute-value', $tagname, 'in_arbeit', $attributes['in_arbeit'])->text());
1020                                         unset($attributes['in_arbeit']);
1021                                 }
1022
1023                                 // any attributes left that are not handled yet?
1024                                 if (count($attributes) > 0)
1025                                         throw new WrReportException(wfMessage('wrreport-rodelbahntabelle-invalid-attribute-name', $tagname, array_keys($attributes)[0])->text());
1026
1027
1028                                 // build SQL query
1029                                 $tables = array('wrsledruncache');
1030                                 $where = array();
1031
1032                                 // <region>
1033                                 if ($tagname === 'region') {
1034                                         // the following line would work if MySQL 5.5 would implement a real geospatial version of CONTAINS.
1035                                         // $where[] = 'CONTAINS(GEOMFROMWKB(' . $dbr->addQuotes($region_border_wkb) . '), POINT(position_longitude, position_latitude))'
1036                                         $tables[] = 'wrregion';
1037                                         $tables[] = 'wrregioncache';
1038                                         $where[] = 'wrregioncache.region_id=wrregion.id';
1039                                         $where[] = 'wrregioncache.page_id=wrsledruncache.page_id';
1040                                         $where['wrregion.name'] = $entry;
1041                                 }
1042
1043                                 // <rodelbahn>
1044                                 if ($tagname == 'rodelbahn') {
1045                                         $page_title = Title::newFromText(trim($entry));
1046                                         $where['page_title'] = $page_title->getDBkey();
1047                                 }
1048
1049                                 // under contruction
1050                                 if ($under_construction === true) $where[] = 'wrsledruncache.under_construction';
1051                                 if ($under_construction === false) $where[] = 'not wrsledruncache.under_construction';
1052
1053                                 // query
1054                                 $res = $dbr->select($tables, array('page_id' => 'wrsledruncache.page_id'), $where, __METHOD__, 'page_id');
1055                                 foreach ($res as $row) {
1056                                         $entry_page_ids[] = $row->page_id;
1057                                 }
1058                                 $dbr->freeResult($res);
1059
1060
1061                                 // merge the entry page_ids with the page_ids
1062                                 if ($operation == '+') $page_ids = array_merge($page_ids, $entry_page_ids);
1063                                 elseif ($operation == '-') $page_ids = array_diff($page_ids, $entry_page_ids);
1064                         }
1065
1066                         // page_titles that are going to be returned
1067                         $page_titles = Title::newFromIDs($page_ids);
1068
1069                         $html = WrReport::createBahnentabelle($page_titles);
1070
1071                 } catch (WrReportException $e) {
1072                         $doc = new WrDOMDocument();
1073                         $doc->appendElement('span', array('class' => 'error'))->appendText(wfMessage('wrreport-rodelbahntabelle-error', $e->getMessage())->text());
1074                         $html = $doc->saveHTML($doc->firstChild);
1075                 }
1076
1077                 return array($html, 'markerType' => 'nowiki');
1078         }
1079
1080
1081         /// \brief Is called when the tag <avatar>username</avatar> is encountered.
1082         public static function avatarParserHook($input, $args, $parser, $frame) {
1083                 $doc = new WrDOMDocument();
1084                 $sla = new WrServicesLibravatar();
1085
1086                 try {
1087                         // check user name
1088                         if (is_null($input)) throw new WrReportException(wfMessage('wrreport-avatar-nousername')->text());
1089                         $username = $parser->recursiveTagParse($input, $frame);
1090                         $user = User::newFromName($username);
1091                         if ($user === false) throw new WrReportException(wfMessage('wrreport-avatar-invalidusername', $username)->text());
1092                         if ($user->getId() == 0) throw new WrReportException(wfMessage('wrreport-avatar-userunknown', $username)->text());
1093
1094                         // size attribute (optional)
1095                         if (isset($args['size'])) $sla->setSize((int) $parser->recursiveTagParse($args['size'], $frame));
1096
1097                         // alt attribute (optional)
1098                         $alt = wfMessage('wrreport-avatar-of', $username)->text();
1099
1100                         // create avatar (using the Services_Libravatar library to get avatar URL)
1101                         $url = $sla->getUrl($user->getEmail());
1102                         $doc->appendElement('img', array('src' => $url, 'alt' => $username, 'width' => $sla->getSize(), 'height' => $sla->getSize()));
1103
1104                 } catch (WrReportException $e) {
1105                         $doc->appendElement('span', array('class' => 'error'))->appendText(wfMessage('wrreport-avatar-error', $e->getMessage())->text());
1106                 }
1107
1108                 // return result (markerType => nowiki prevents wiki formatting of the result)
1109                 $html = $doc->saveHTML($doc->firstChild);
1110                 return array($html, 'markerType' => 'nowiki');
1111         }
1112
1113
1114
1115 }
1116 WrReport::initWrConditions();
1117
1118
1119
1120 // Special page
1121 // ------------
1122
1123 /// Specal Page to show reports
1124 class SpecialWrReport extends SpecialPage {
1125         function __construct() {
1126                 parent::__construct('wrreport');
1127         }
1128
1129
1130         function LanguageGetSpecialPageAliasesHook(&$specialPageArray, $languageCode) {
1131                 $text = wfMessage('wrreport')->text(); // 'Bahnberichte'
1132                 $title = Title::newFromText($text); // 'Bahnberichte'
1133                 $specialPageArray['wrreport'][] = $title->getDBKey(); // 'Bahnberichte'
1134                 return true;
1135         }
1136
1137
1138
1139         /// \param $par Possibilities:
1140         /// - action == 'view' (default)
1141         /// - action == 'preview': Preview new report
1142         /// - action == 'store': Store new report
1143         /// - action == 'deletepreview': Preview the deleted record
1144         /// - action == 'delete': Delete an existing report
1145         /// - action == 'showerror': Shows the error and exits
1146         /// \param $override_action If not NULL (default), it overrides the action in $par
1147         /// \param $errorMsg UFT-8 encoded error message (in WikiText) to show on top of the page or NULL (default):
1148         function execute($par, $override_action = NULL, $errorMsg = NULL) {
1149                 $request = $this->getRequest();
1150                 $output = $this->getOutput();
1151         
1152                 global $wgParser;
1153
1154                 $output->addModules('ext.wrreport');
1155                 $this->setHeaders();
1156
1157                 // Get request data
1158                 $action = $request->getText('action');
1159                 if (!$action) {
1160                         if ($request->getVal('preview')) $action = 'preview';
1161                         elseif ($request->getVal('store')) $action = 'store';
1162                         elseif ($request->getVal('deletepreview')) $action = 'deletepreview';
1163                         elseif ($request->getVal('delete')) $action = 'delete';
1164                         else $action = 'view';
1165                 }
1166                 if ($override_action) $action = $override_action;
1167
1168                 // Show error message
1169                 if ($errorMsg || $action == 'showerror') {
1170                         $output->addWikiText('<div class="errorbox">' . $errorMsg . "</div>\n");
1171                         if ($action == 'showerror') return;
1172                 }
1173
1174                 // Action view
1175                 if ($action == 'view') {
1176                         global $wgWrReportFeedRoot;
1177                         $output->addFeedLink('atom', $wgWrReportFeedRoot . '/berichte/alle');
1178                         $conditions = array('date_invalid > now()');
1179                         $rows = wrReportGetReports($conditions);
1180                         if (count($rows) == 0) $output->addHTML(wfMessage('wrreport-reports-none')->text());
1181                         else {
1182                                 $output->addWikiText(''); // this is necessary because otherwise $wgParser is not properly initialized but $wgParser is needed in the next line
1183                                 $output->addHTML(wrReportTableRender($rows, WRREPORT_DETAIL, wrReportUserMayDelete(), $wgParser));
1184                         }
1185                 }
1186
1187                 // Action deletepreview or delete
1188                 elseif ($action == 'deletepreview' || $action == 'delete') {
1189                         $reportid = (int) $request->getText('reportid');
1190                         if ($reportid == 0) {
1191                                 $this->execute($par, 'showerror', wfMessage('wrreport-deletereport-noreport')->text());
1192                                 return;
1193                         }
1194                         $rows = wrReportGetReports(array('id' => $reportid));
1195                         if (count($rows) != 1) {
1196                                 $this->execute($par, 'showerror', wfMessage('wrreport-deletereport-invalid')->text());
1197                                 return;
1198                         }
1199                         $row = $rows[0];
1200                         if (!is_null($row['delete_date'])) {
1201                                 $this->execute($par, 'showerror', wfMessage('wrreport-deletereport-alreadydeleted')->text());
1202                                 return;
1203                         }
1204                         $delete_reason_public = $request->getText('delete_reason_public');
1205                         $delete_person_name = $request->getText('delete_person_name');
1206                         $delete_invisible = $request->getText('delete_invisible') ? TRUE : FALSE;
1207                         if ($action == 'delete') {
1208                                 // page
1209                                 $title = Title::newFromId($row['page_id']);
1210
1211                                 // user
1212                                 global $wgUser;
1213                                 $delete_person_userid = $wgUser->getId();
1214                                 if ($delete_person_userid == 0) $delete_person_userid = NULL; // to store a NULL value in the database if no user is logged in instead of 0.
1215                                 $delete_person_username = $wgUser->getName();
1216
1217                                 // Check permissions - see also function wrReportUserMayDelete, that does also check permissions but does not return an error message.
1218                                 $errorMsg = NULL;
1219                                 global $wgWrReportDeleteMode;
1220                                 if ($wgWrReportDeleteMode == 'deny') $errorMsg = wfMessage('wrreport-deletereport-deny')->text();
1221                                 elseif ($wgWrReportDeleteMode == 'loggedin' && !$wgUser->isLoggedIn()) $errorMsg = wfMessage('wrreport-deletereport-loggedin')->text();
1222                                 elseif (!$delete_person_name || !$delete_reason_public) $errorMsg = wfMessage('wrreport-deletereport-incomplete')->text();
1223                                 if ($errorMsg) {
1224                                         $this->execute($par, 'deletepreview', $errorMsg);
1225                                         return;
1226                                 }
1227
1228                                 // "Delete" (update) entry
1229                                 $dbr = wfGetDB(DB_MASTER);
1230                                 $dbr->update(
1231                                         'wrreport',
1232                                         array(
1233                                                 'delete_date' => date('c'),
1234                                                 'delete_person_name' => $delete_person_name,
1235                                                 'delete_person_ip' => $_SERVER['REMOTE_ADDR'],
1236                                                 'delete_person_userid' => $delete_person_userid,
1237                                                 'delete_person_username' => $delete_person_username,
1238                                                 'delete_reason_public' => $delete_reason_public,
1239                                                 'delete_invisible' => $delete_invisible ? 't' : 'f'
1240                                         ),
1241                                         array('id' => $reportid)
1242                                 );
1243
1244                                 // Purge cache
1245                                 $title->invalidateCache();
1246                                 wrRecacheRegions();
1247
1248                                 // Show success message
1249                                 $output->addWikiText(wfMessage('wrreport-deletereport-success', '[[' . $row['page_title'] . '#' . wfMessage('wrreport-reports-sectionname')->text() . '|' . $row['page_title'] . ']]')->text());
1250                         }
1251                         if ($action == 'deletepreview') {
1252                                 $output->addWikiText(wfMessage('wrreport-deletereport-preview-before')->text());
1253                                 $format = WRREPORT_COMPACT_PAGE;
1254                                 $output->addHTML(wrReportTableRender(array($row), $format, FALSE, $wgParser));
1255                                 $output->addWikiText(wfMessage('wrreport-deletereport-preview-after')->text());
1256                                 $row['delete_date'] = date('c');
1257                                 $row['delete_reason_public'] = $delete_reason_public;
1258                                 $row['delete_person_name'] = $delete_person_name;
1259                                 $row['delete_invisible'] = $delete_invisible;
1260                                 $output->addHTML(wrReportTableRender(array($row), $format, FALSE, $wgParser));
1261                                 $output->addWikiText(wfMessage('wrreport-deletereport-preview-form')->text());
1262                                 $output->addHTML(wrDeleteReportFormRender($reportid, $delete_person_name, $delete_reason_public, $delete_invisible));
1263                                 $output->addWikiText(wfMessage('wrreport-deletereport-preview-bottom')->text());
1264                         }
1265                 }
1266
1267                 // Action preview or store
1268                 elseif ($action == 'preview' || $action == 'store') {
1269                         $page_title = $request->getText('page_title');
1270                         $date_report = $request->getText('date_report');
1271                         $time_report = $request->getText('time_report');
1272                         $condition = $request->getText('condition');
1273                         $description = $request->getText('description');
1274                         $author_name = $request->getText('author_name');
1275
1276                         // time_report
1277                         $time_report = trim($time_report); // strip whitespace
1278                         $matches = array();
1279                         $hour = NULL;
1280                         $minute = NULL;
1281                         if (preg_match('/^[0-2]?[0-9]$/', $time_report)) {
1282                                 // $time_report has 1 or 2 digits, e.g. 'h', or 'hh'.
1283                                 $hour = intval($time_report);
1284                                 $minute = 0;
1285                         } elseif (preg_match('/^([0-2]?[0-9]):?([0-9]{2})$/', $time_report, $matches)) {
1286                                 // $time_report has 3 or 4 digits, e.g. 'hmm' or 'hhmm' or 'h:mm' or 'hh:mm'.
1287                                 $hour = intval($matches[1]);
1288                                 $minute = intval($matches[2]);
1289                         }
1290                         if (!is_null($hour) && $hour >= 0 && $hour < 24 && $minute >= 0 && $minute < 60) $time_report = sprintf('%02d:%02d', $hour, $minute);
1291                         else $time_report = NULL;
1292
1293                         // condition
1294                         $condition = (int) $condition; // force to be nummeric. -1 ... "keine Bewertung", 0 ... "Bitte eingeben", 1 to 5 ... "Sehr gut" to "Geht nicht"
1295                         if ($condition < -1 or $condition > 5) $condition = 0; // invalid condition: Tread like 0.
1296                         $condition_sql = NULL;
1297                         if ($condition >= 1 and $condition <= 5) $condition_sql = $condition;
1298
1299                         // author_name
1300                         $author_name = trim($author_name);
1301
1302                         // page_id
1303                         $title = Title::newFromText($page_title);
1304                         $page_id = $title->getArticleID();
1305                         if ($page_id == 0) $page_id = NULL;
1306
1307                         // user_id
1308                         global $wgUser;
1309                         $author_userid = $wgUser->getId();
1310                         if ($author_userid == 0) $author_userid = NULL; // to store a NULL value in the database if no user is logged in instead of 0.
1311                         $author_username = $wgUser->getName();
1312
1313                         if ($action == 'store') {
1314                                 // check conditions/permissions
1315                                 $errorMsg = NULL;
1316                                 global $wgWrReportMode;
1317                                 global $wgWrReportBlackListAll;
1318                                 global $wgWrReportBlackListStrangers;
1319                                 if ($wgWrReportMode == 'summer') $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-summer')->text());
1320                                 elseif ($wgWrReportMode == 'deny') $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-deny')->text());
1321                                 elseif ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-loggedin')->text());
1322                                 elseif (!$page_id) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-pagenotfound')->text());
1323                                 elseif (in_array($page_title, $wgWrReportBlackListAll)) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-blacklist')->text());
1324                                 elseif (!$wgUser->isLoggedIn() && in_array($page_title, $wgWrReportBlackListStrangers)) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-blackliststrangers')->text());
1325                                 elseif ($condition == 0) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-choosecondition')->text());
1326                                 elseif (!$wgUser->isLoggedIn()) {
1327                                         if (!$description) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-enterdescription')->text());
1328                                         elseif (!(stripos($description, 'http') === FALSE)) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-nohttp')->text());
1329                                         elseif (!$author_name) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-enterauthor')->text());
1330                                 }
1331
1332                                 // check author name
1333                                 if (!$errorMsg) {
1334                                         $author_name_id = $wgUser->idFromName(strtolower($author_name));
1335                                         if ($wgUser->isLoggedIn()) {
1336                                                 if ($author_name_id != 0 && $author_name_id != $wgUser->getId())
1337                                                         $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-authorused')->text());
1338                                         } else {
1339                                                 if ($author_name_id != 0)
1340                                                         $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-authorlogin')->text());
1341                                         }
1342                                 }
1343
1344                                 // Chech whether identical reports are present
1345                                 if (!$errorMsg) {
1346                                         $dbr = wfGetDB(DB_SLAVE);
1347                                         $cond = 'condition';
1348                                         global $wgDBtype;
1349                                         if ($wgDBtype == "mysql") $cond = "`$cond`"; // "condition" is a reserved word in mysql
1350                                         $sqlConditions = array('page_id' => $page_id, 'date_report' => $date_report, 'time_report' => $time_report, $cond => $condition_sql, 'description' => $description, 'author_name' => $author_name);
1351                                         $res = $dbr->select('wrreport', 'id', $sqlConditions);
1352                                         if ($res->numRows() == 1) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-alreadysaved')->text());
1353                                         $dbr->freeResult($res);
1354                                 }
1355
1356                                 // Show error if any
1357                                 if ($errorMsg) {
1358                                         $this->execute($par, 'preview', $errorMsg);
1359                                         return;
1360                                 }
1361
1362                                 // Save entry
1363                                 $dbr = wfGetDB(DB_MASTER);
1364                                 $dbr->insert(
1365                                         'wrreport',
1366                                         array(
1367                                                 'page_id' => $page_id,
1368                                                 'page_title' => $page_title,
1369                                                 'date_report' => $date_report,
1370                                                 'time_report' => $time_report,
1371                                                 'date_entry' => date('c'),
1372                                                 'date_invalid' => date('c', strtotime('+9 days')),
1373                                                 $cond => $condition_sql,
1374                                                 'description' => $description,
1375                                                 'author_name' => $author_name,
1376                                                 'author_ip' => $_SERVER['REMOTE_ADDR'],
1377                                                 'author_userid' => $author_userid,
1378                                                 'author_username' => $author_username
1379                                                 // 'delete_*' => // use database defaults (NULL)
1380                                         )
1381                                 );
1382
1383                                 // Purge cache
1384                                 $title->invalidateCache();
1385                                 wrRecacheRegions();
1386                                 wrUpdateWrReportCacheTable($page_id);
1387
1388                                 // Show success message
1389                                 $output->addWikiText(wfMessage('wrreport-newreport-success', '[[' . $page_title . '#' . wfMessage('wrreport-reports-sectionname')->text() . '|' . $page_title . ']]')->text());
1390                                 // We could redirect to result with the following line but we don't want to.
1391                                 // $output->redirect($title->getFullURL() . '#' . wfMessage('wrreport-reports-sectionname')->text());
1392                         }
1393                         if ($action == 'preview') {
1394                                 $output->addWikiText(wfMessage('wrreport-newreport-preview-top')->text());
1395                                 $format = WRREPORT_COMPACT_PAGE;
1396                                 $row = array_fill_keys(wrReportGetColumnNames(), NULL);
1397                                 $row['page_id'] = $page_id;
1398                                 $row['page_title'] = $page_title;
1399                                 $row['date_report'] = $date_report;
1400                                 $row['time_report'] = $time_report;
1401                                 $row['condition'] = $condition_sql;
1402                                 $row['description'] = $description;
1403                                 $row['author_name'] = $author_name;
1404                                 $row['author_userid'] = $author_userid;
1405                                 $row['author_username'] = $author_username;
1406
1407                                 $output->addHTML(wrReportTableRender(array($row), $format, FALSE, $wgParser));
1408                                 $output->addWikiText(wfMessage('wrreport-newreport-preview-middle')->text());
1409                                 $output->addHTML(wrReportFormRender(FALSE, $page_title, $date_report, $time_report, $condition, $description, $author_name));
1410                                 $output->addWikiText(wfMessage('wrreport-newreport-preview-bottom')->text());
1411                                 if ($author_userid)
1412                                         $output->addWikiText(wfMessage('wrreport-newreport-preview-bottom-loggedin')->text());
1413                                 else
1414                                         $output->addWikiText(wfMessage('wrreport-newreport-preview-bottom-anonymous')->text());
1415                         }
1416
1417                 }
1418
1419                 else die('Wrong action');
1420         }
1421 }
1422
1423
1424 ?>