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