Removed ID and 'date invalid' columns from sledrun report detail view.
[philipp/winterrodeln/mediawiki_extensions/wrreport.git] / wrreport.body.php
1 <?php
2 // File encoding: utf-8
3 // Classes for the wrreport extension.
4
5
6 // Constants
7 // ---------
8
9 // Constants for wrReportTableRender
10 define('WRREPORT_COMPACT_PAGE', 1); ///< includes the page name
11 define('WRREPORT_COMPACT', 2);      ///< shown on a single page
12 define('WRREPORT_DETAIL', 3);       ///< more columns
13
14
15
16 // DOM helper classes
17 // ------------------
18
19 class WrDOMDocument extends DOMDocument {
20         function __construct() {
21                 parent::__construct('1.0', 'utf-8');
22                 $this->registerNodeClass('DOMElement', 'WrDOMElement');
23         }
24
25         /// Creates and adds the element with the given tag name and returns it.
26         /// Additionally, it calls setAttribute($key, $value) for every entry
27         /// in $attributes.
28         function appendElement($tagName, $attributes=array()) {
29                 $child = $this->appendChild($this->createElement($tagName));
30                 foreach ($attributes as $key => $value) $child->setAttribute($key, $value);
31                 return $child;
32         }
33
34 }
35
36
37 class WrDOMElement extends DOMElement {
38
39         /// Creates and adds the element with the given tag name and returns it
40         /// Additionally, it calls setAttribute($key, $value) for every entry
41         /// in $attributes.
42         function appendElement($tagName, $attributes=array()) {
43                 $child = $this->appendChild($this->ownerDocument->createElement($tagName));
44                 foreach ($attributes as $key => $value) $child->setAttribute($key, $value);
45                 return $child;
46         }
47
48         /// Adds any UTF-8 string as content of the element - it will be escaped.
49         function appendText($text) {
50                 return $this->appendChild($this->ownerDocument->createTextNode($text));
51         }
52 }
53
54
55
56 // Tool functions
57 // --------------
58
59 /// Forces a regeneration of region overview pages ('Tirol', 'Vorarlberg', ...)
60 function wrRecacheRegions() {
61         $dbr = wfGetDB(DB_SLAVE);
62         // SELECT cl_from FROM categorylinks where cl_to = 'Region'
63         $res = $dbr->select('categorylinks', 'cl_from', array('cl_to' => 'Region'));
64         $page_ids = array();
65         while ($row = $dbr->fetchObject($res)) $page_ids[] = $row->cl_from;
66         $dbr->freeResult($res);
67
68         $titles = Title::newFromIDs($page_ids);
69         foreach ($titles as $title) $title->invalidateCache();
70 }
71
72
73 /// Returns the tuple ($report_id, $sledrun_condition, $date_report)
74 /// $date_report is NULL or a time as returned by strtotime
75 /// Expects a database connection ($dbr = wfGetDB(DB_SLAVE);)
76 /// and a page_id describung the page where the condition should be returned.
77 /// If no condition is found, (NULL, NULL, NULL) is returned.
78 function wrGetSledrunCondition($dbr, $page_id) {
79         // 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;
80         $cres = $dbr->select(
81                 'wrreport',
82                 array('wrreport.id as report_id', '`condition`', 'date_report'),
83                 array('page_id' => $page_id, '`condition` is not null', 'date_invalid > now()', 'delete_date is null'),
84                 'wrReportConditionRender',
85                 array('ORDER BY' => 'date_report desc, date_entry desc', 'LIMIT' => '1')
86         );
87         if ($cres->numRows() <= 0) {
88                 $report_id = NULL;
89                 $condition = NULL;
90                 $date_report = NULL;
91         } else {
92                 $crow = $dbr->fetchObject($cres);
93                 $report_id = $crow->report_id;
94                 $condition = $crow->condition;
95                 $date_report = strtotime($crow->date_report);
96         }
97         $dbr->freeResult($cres);
98         return array($report_id, $condition, $date_report);
99 }
100
101
102 /// Updates the line of the wrreportcache table that corresponds to the $page_id parameter
103 function wrUpdateWrReportCacheTable($page_id) {
104         // Determine the new content for the row that should be updated
105         $dbr = wfGetDB(DB_SLAVE);
106         list($report_id, $condition, $date_report) = wrGetSledrunCondition($dbr, $page_id);
107         $rows = wrReportGetReports(array('id' => $report_id), 1);
108
109         // Delete the old content (if any)
110         $dbw = wfGetDB(DB_MASTER);
111         $dbw->begin();
112         $dbw->delete('wrreportcache', array('page_id' => $page_id));
113
114         // Insert the updated row
115         if (count($rows) == 1) {
116                 $row = $rows[0];
117                 $dbw->insert('wrreportcache', array(
118                         'page_id' => $row['page_id'],
119                         'page_title' => $row['page_title'],
120                         'report_id' => $row['id'],
121                         'date_report' => $row['date_report'],
122                         '`condition`' => $row['condition'],
123                         'description' => $row['description'],
124                         'author_name' => $row['author_name'],
125                         'author_username' => (is_null($row['author_userid']) ? NULL : $row['author_username'])));
126         }
127         $dbw->commit();
128 }
129
130
131
132 // Render Functions
133 // ----------------
134
135 /// \brief Returns a form to enter a report (string containing HTML).
136 ///
137 /// All parameters have to be UTF-8 encoded.
138 /// \param $page_title Name of the sledrun.
139 /// \param $condition 1 to 5 for normal condition, 0 or NULL for missing condition and -1 for intentionally no condition.
140 /// \return UTF-8 encoded HTML form
141 function wrReportFormRender($hide_save_button = TRUE, $page_title = NULL, $date_report = NULL, $condition = NULL, $description = NULL, $author_name = NULL, $page_title_list = NULL) {
142         $doc = new WrDOMDocument();
143         $evenoddrow = array('oddrow', 'evenrow');
144         $rownum = 0;
145
146         // Info about special page
147         $specialPageName = wfMessage('wrreport')->text(); // 'Bahnberichte'
148         $title = Title::newFromText($specialPageName, NS_SPECIAL);
149         $specialPageUrl = $title->getLocalURL(); // e.g. '/wiki/Spezial:Bahnberichte'
150
151         // form
152         $form = $doc->appendElement('form', array('action' => $specialPageUrl, 'method' => 'post'));
153
154         // table (for layout of form elements)
155         $table = $form->appendElement('table', array('class' => 'wrreportform', 'summary' => wfMessage('wrreport-newreport-tablesummary')->text()));
156
157         // sledrun name
158         $tr = $table->appendElement('tr', array('class' => $evenoddrow[$rownum++ % 2]));
159         $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun')->text());
160         $td = $tr->appendElement('td');
161         $td->appendText($page_title);
162         $input = $td->appendElement('input', array('type' => 'hidden', 'name' => 'page_title', 'value' => $page_title));
163
164         // report date
165         $tr = $table->appendElement('tr', array('class' => $evenoddrow[$rownum++ % 2]));
166         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-date')->text());
167         $td = $tr->appendElement('td');
168         $select = $td->appendElement('select', array('name' => 'date_report'));
169         $daynames = array(
170                 wfMessage('wrreport-date-today')->text(),
171                 wfMessage('wrreport-date-yesterday')->text(),
172                 wfMessage('wrreport-date-2daysbefore')->text(),
173                 wfMessage('wrreport-date-3daysbefore')->text(),
174                 wfMessage('wrreport-date-4daysbefore')->text());
175         $date_selected = false;
176         $time = time(); // number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
177         for ($day=0; $day!=5; ++$day) {
178                 $date = strtotime("-$day days", $time);
179                 $date_f = strftime("%Y-%m-%d", $date); // Formats it according to locale, that is set to CET.
180                 $option = $select->appendElement('option', array('value' => $date_f));
181                 if ((is_null($date_report) && $day == 0) || (!is_null($date_report) && $date_report == $date_f)) {
182                         $option->setAttribute('selected', 'selected');
183                         $date_selected = true;
184                 }
185                 $option->appendText($daynames[$day] . ' (' . strftime('%d.%m.', $date) . ')');
186         }
187         if (!$date_selected) { // note: if $date_report is null $date_selected is true here
188                 $option = $select->appendElement('option', array('value' => $date_report, 'selected' => 'selected'));
189                 $option->appendText($date_report);
190         }
191
192         // condition
193         $tr = $table->appendElement('tr', array('class' => $evenoddrow[$rownum++ % 2]));
194         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-condition')->text());
195         $td = $tr->appendElement('td');
196         $select = $td->appendElement('select', array('name' => 'condition'));
197         $option = $select->appendElement('option', array('value' => '0'));
198         $option->appendText(wfMessage('wrreport-condition-select')->text());
199         foreach (WrReport::$wrConditions as $condition_num => $condition_text) {
200                 $option = $select->appendElement('option', array('value' => (string) $condition_num));
201                 if ($condition == $condition_num) $option->setAttribute('selected', 'selected');
202                 $option->appendText($condition_text);
203         }
204         $option = $select->appendElement('option', array('value' => '-1'));
205         if ($condition == -1) $option->setAttribute('selected', 'selected');
206         $option->appendText(wfMessage('wrreport-condition-no')->text());
207
208         // description
209         $tr = $table->appendElement('tr', array('class' => $evenoddrow[$rownum++ % 2]));
210         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-description')->text());
211         $tr->appendElement('td')->appendElement('textarea', array('name' => 'description', 'cols' => '50', 'rows' => '7'))->appendText($description);
212
213         // author
214         $tr = $table->appendElement('tr', array('class' => $evenoddrow[$rownum++ % 2]));
215         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-author')->text());
216         $tr->appendElement('td')->appendElement('input', array('name' => 'author_name', 'maxlength' => '30', 'size' => '30', 'value' => $author_name));
217
218         // Buttons
219         // I would like to do it this way, but due to a bug of internet explorer, the <button> element is not useable.
220         //   $buttons = '<button name="action" type="submit" value="preview">Vorschau';
221         //   if ($hide_save_button) $buttons .= ' &amp; Speichern';
222         //   $buttons .= '</button>';
223         //   if (!$hide_save_button) $buttons .= '<button name="action" type="submit" value="store">Speichern</button>';
224         // Workaround: User <input type="submit"/>
225         $tr = $table->appendElement('tr', array('class' => $evenoddrow[$rownum++ % 2]));
226         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-submit')->text());
227         $td = $tr->appendElement('td');
228         $input = $td->appendElement('input', array('name' => 'preview', 'type' => 'submit'));
229         if ($hide_save_button)
230                 $input->setAttribute('value', wfMessage('wrreport-newreport-preview')->text() . ' & ' . wfMessage('wrreport-newreport-save')->text());
231         else {
232                 $input->setAttribute('value', wfMessage('wrreport-newreport-preview')->text());
233
234                 $td->appendElement('input', array('name' => 'store', 'type' => 'submit', 'value' => wfMessage('wrreport-newreport-save')->text()));
235         }
236
237         return $doc->saveHTML($form);
238 }
239
240
241 /// \brief Renders the form to delete a report
242 ///
243 /// All in and output strings should be/are UTF-8 encoded.
244 /// \param $reportid the id of the report that is going to be deleted
245 /// \param $delete_person_name name of the person that wants to delete the report (form field)
246 /// \param $delete_reason_public publically visible reason for deleting the entry.
247 /// \param $delete_invisible don't show the report as being deleted
248 /// \return UTF-8 encoded HTML form
249 function wrDeleteReportFormRender($reportid, $delete_person_name, $delete_reason_public, $delete_invisible) {
250         $doc = new WrDOMDocument();
251         $evenoddrow = array('oddrow', 'evenrow');
252         $rownum = 0;
253
254         // Info about special page
255         $specialPageName = wfMessage('wrreport')->text(); // 'Bahnberichte'
256         $title = Title::newFromText($specialPageName, NS_SPECIAL);
257         $specialPageUrl = $title->getLocalURL(); // e.g. '/wiki/Spezial:Bahnberichte'
258
259         // form
260         $form = $doc->appendElement('form', array('action' => $specialPageUrl, 'method' => 'post'));
261
262         // table (for layout of form elements)
263         $table = $form->appendElement('table', array('class' => 'wrreportform', 'summary' => wfMessage('wrreport-deletereport-tablesummary')->text()));
264
265         // delete_reason_public
266         $tr = $table->appendElement('tr', array('class' => $evenoddrow[$rownum++ % 2]));
267         $tr->appendElement('th')->appendText(wfMessage('wrreport-deletereport-reason')->text());
268         $tr->appendElement('td')->appendElement('textarea', array('name' => 'delete_reason_public', 'cols' => '50', 'rows' => '7'))->appendText($delete_reason_public);
269
270         // delete_person_name
271         $tr = $table->appendElement('tr', array('class' => $evenoddrow[$rownum++ % 2]));
272         $tr->appendElement('th')->appendText(wfMessage('wrreport-deletereport-name')->text());
273         $tr->appendElement('td')->appendElement('input', array('name' => 'delete_person_name', 'maxlength' => '30', 'size' => '30', 'value' => $delete_person_name));
274
275         // buttons
276         $tr = $table->appendElement('tr', array('class' => $evenoddrow[$rownum++ % 2]));
277         $tr->appendElement('th')->appendText(wfMessage('wrreport-reports-action')->text());
278         $td = $tr->appendElement('td');
279         $td->appendElement('input', array('name' => 'deletepreview', 'type' => 'submit', 'value' => wfMessage('wrreport-newreport-preview')->text()));
280         $td->appendElement('input', array('name' => 'delete', 'type' => 'submit', 'value' => wfMessage('wrreport-deletereport-delete')->text()));
281
282         // hidden field
283         $input = $td->appendElement('input', array('name' => 'reportid', 'type' => 'hidden', 'value' => (string) $reportid));
284         // delete_invisible - who is allowed to do so?
285         // $td->appendElement('input', array('name' => 'delete_invisible', 'type' => 'hidden', 'value' => (string) $delete_invisible));
286
287         return $doc->saveHTML($form);
288 }
289
290
291 /// \brief Generates the DOM of the table header ("private" sub-function of wrReportTableRender)
292 ///
293 /// \param $table table WrDOMElement where the header should be appended
294 /// \param $format row format like WRREPORT_COMPACT
295 /// \param $showActions boolean to indicate whether an actions column should be created
296 /// \return WrDOMElement of the HTML table header
297 function wrReportTableTitleRender($table, $format, $showActions) {
298         $tr = $table->appendElement('tr');
299         if ($format != WRREPORT_COMPACT) $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun')->text());
300         $tr->appendElement('th')->appendText(wfMessage('wrreport-reports-date')->text());
301         if ($format == WRREPORT_DETAIL) $tr->appendElement('th')->appendText(wfMessage('wrreport-reports-date-entry')->text());
302         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-condition')->text());
303         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-description')->text());
304         $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-author')->text());
305         if ($showActions) $tr->appendElement('th')->appendText(wfMessage('wrreport-reports-action')->text());
306         return $tr;
307 }
308
309
310 /// \brief Generates the DOM of a table row ("private" sub-function of wrReportTableRender)
311 ///
312 /// \param $table table WrDOMElement where the row should be appended
313 /// \param $row associative array of table columns like one row in the wrreport table
314 /// \param $format row format like WRREPORT_COMPACT
315 /// \param $showActions boolean to indicate whether an actions column should be created
316 /// \param $parser Parser instance of a Parser class that can be used to parse the condition
317 /// \return WrDOMElement of the HTML table row
318 function wrReportTableRowRender($table, $row, $format, $showActions, $parser) {
319         $tr = $table->appendElement('tr');
320
321         // $row['page_title']
322         if ($format != WRREPORT_COMPACT) {
323                 $title = Title::newFromText($row['page_title']);
324                 $tr->appendElement('td')->appendElement('a', array('href' => $title->getLocalURL()))->appendText($title->getPrefixedText());
325         }
326
327         // $row['date_report']
328         $date_report = strtotime($row['date_report']);
329         $date_report = wfMessage(Language::$mWeekdayAbbrevMsgs[(int) date('w', $date_report)])->text() . strftime(', %d.%m.', $date_report);
330         $tr->appendElement('td')->appendText($date_report);
331
332         // $row['date_entry']
333         if ($format == WRREPORT_DETAIL) $tr->appendElement('td')->appendText(date('d.m. H:i', strtotime($row['date_entry'])));
334
335         // $row['condition']
336         $condition_text = '---';
337         if (isset(WrReport::$wrConditions[$row['condition']])) $condition_text = WrReport::$wrConditions[$row['condition']];
338         $td = $tr->appendElement('td');
339         if ($row['delete_date']) $td->appendElement('em')->appendText(wfMessage('wrreport-deletereport-deleted')->text());
340         else $td->appendText($condition_text);
341
342         // $row['description']
343         $td = $tr->appendElement('td', array('class' => 'wrreportdescription'));
344         if ($row['delete_date']) $td->appendElement('em')->appendText(wfMessage('wrreport-deletereport-deleted')->text());
345         else {
346                 // for registered users, use wikitext formatting
347                 if (is_null($row['author_userid'])) $td->appendText($row['description']);
348                 else {
349                         $html = $parser->recursiveTagParse($row['description']);
350                         // $parser->recursiveTagParse does not always escape & characters.
351                         // (see https://bugzilla.wikimedia.org/show_bug.cgi?id=55526 )
352                         //
353                         // Workaround: Use a CDATA section. When serializing with $doc->saveHTML,
354                         // the <![CDATA[...]]> is returned as ... .
355                         // However, we end up having unescaped & in the output due to this bug in recursiveTagParse.
356                         $td->appendChild($tr->ownerDocument->createCDATASection($html));
357                 }
358         }
359
360         // $row['author_name']
361         $td = $tr->appendElement('td');
362         if ($row['delete_date']) $td->appendElement('em')->appendText(wfMessage('wrreport-deletereport-deleted')->text());
363         else $td->appendText($row['author_name']); # TODO: Make it special :-), e.g. link to user + avatar + number of reports
364
365         // actions
366         // wiki/Spezial:Bahnberichte?action=deletepreview&reportid=42
367         if ($showActions) {
368                 $td = $tr->appendElement('td');
369                 if (!isset($row['delete_date'])) {
370                         $specialPageName = wfMessage('wrreport')->text(); // 'Bahnberichte'
371                         $title = Title::newFromText($specialPageName, NS_SPECIAL);
372                         $specialPageUrl = $title->getLocalURL(); // e.g. '/wiki/Spezial:Bahnberichte'
373                         $td->appendElement('a', array('href' => $specialPageUrl . '?action=deletepreview&reportid=' . $row['id']))
374                                 ->appendText(wfMessage('wrreport-deletereport-delete')->text() . '...');
375                 }
376         }
377         return $tr;
378 }
379
380
381 /// \brief Renders the report table. Call wrReportGetReports for the $rows parameter.
382 ///
383 /// \param $rows array of associative row arrays
384 /// \param $format row format like WRREPORT_TABLE_SHORT
385 function wrReportTableRender($rows, $format, $showActions, $parser) {
386         $doc = new WrDOMDocument();
387         $table = $doc->appendElement('table', array('class' => 'wrreporttable'));
388         wrReportTableTitleRender($table, $format, $showActions);
389         foreach ($rows as $key => $row) wrReportTableRowRender($table, $row, $format, $showActions, $parser);
390         return $doc->saveHTML($table);
391 }
392
393
394 /// Returns an array with column names
395 function wrReportGetColumnNames() {
396         return array('id', 'page_id', 'page_title', 'date_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');
397 }
398
399
400 /// \brief Returns reports as associative array.
401 ///
402 /// Examples:
403 ///   $conditions = array('page_title' => 'Birgitzer Alm', 'date_invalid > now()');
404 ///   $limit = 1; // or NULL for no limit
405 function wrReportGetReports($conditions, $limit=NULL) {
406         $dbr = wfGetDB(DB_SLAVE);
407         $columns = wrReportGetColumnNames();
408         global $wgDBtype;
409         if ($wgDBtype == "mysql") // "condition" is a reserved word in mysql
410                  for ($i = 0; $i != count($columns); ++$i) $columns[$i] = sprintf('`%s`', $columns[$i]);
411         $options = array('ORDER BY' => 'date_report desc, date_entry desc');
412         if (!is_null($limit)) $options['LIMIT'] = $limit;
413         $res = $dbr->select('wrreport', $columns, $conditions, 'wrReportGetReports', $options);
414         $result = array();
415         while ($row = $dbr->fetchRow($res)) $result[] = $row;
416         $dbr->freeResult($res);
417         return $result;
418 }
419
420
421 /// \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));
422 ///
423 /// If no condition is present, array(NULL, NULL) is returned
424 function wrReportConditionRender($page_title) {
425         $dbr = wfGetDB(DB_SLAVE);
426         $cond = 'condition';
427         global $wgDBtype;
428         // 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;
429         if ($wgDBtype == "mysql") $cond = "`$cond`"; // "condition" is a reserved word in mysql
430         $res = $dbr->select(
431                 'wrreport',
432                 array('wrreport.id as report_id', $cond, 'date_report'),
433                 array('page_title' => $page_title, "$cond is not null", 'date_invalid > now()', 'delete_date is null'),
434                 'wrReportConditionRender',
435                 array('ORDER BY' => 'date_report desc, date_entry desc', 'LIMIT' => '1')
436         );
437         if ($res->numRows() <= 0) {
438                 $dbr->freeResult($res);
439                 return array(NULL, NULL);
440         }
441         $row = $dbr->fetchObject($res);
442         $date = $row->date_report;
443         if ($date) $date = strtotime($date);
444         $dbr->freeResult($res);
445         return array($row->condition, $date);
446 }
447
448
449 /// \brief Returns true if the user is allowed to delete reports (in general)
450 function wrReportUserMayDelete() {
451         global $wgUser;
452         global $wgWrReportDeleteMode;
453         return $wgWrReportDeleteMode == 'allow' || ($wgWrReportDeleteMode == 'loggedin' && $wgUser->isLoggedIn());
454 }
455
456
457
458 // Parser Hook Functions
459 // ---------------------
460
461 /// \brief Is called when the tag <bahnberichtformular/> is encountered.
462 ///
463 /// The current page name is taken.
464 function bahnberichtformularParserHook($input, $args, $parser) {
465         // Username
466         global $wgUser;
467         $author_name = NULL;
468         if ($wgUser->isLoggedIn()) {
469                 $author_name = $wgUser->getRealName();
470                 if (!$author_name) $author_name = $wgUser->getName();
471         }
472
473         global $wgWrReportMode;
474         global $wgWrReportBlackListAll;
475         global $wgWrReportBlackListStrangers;
476
477         // is the form allowed to be shown?
478         $error_key = null;
479         if ($wgWrReportMode == 'summer') $error_key = 'wrreport-newreport-summer';
480         elseif ($wgWrReportMode == 'deny') $error_key = 'wrreport-newreport-deny';
481         elseif ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) $error_key = 'wrreport-newreport-loggedin';
482         elseif (in_array($parser->getTitle()->getText(), $wgWrReportBlackListAll)) $error_key = 'wrreport-newreport-blacklist';
483         elseif (!$wgUser->isLoggedIn() && in_array($parser->getTitle()->getText(), $wgWrReportBlackListStrangers)) $error_key = 'wrreport-newreport-blackliststrangers';
484
485         // report error
486         if (!is_null($error_key)) {
487                 $doc = new WrDOMDocument();
488                 $doc->appendElement('p')->appendElement('em')->appendText(wfMessage($error_key)->text());
489                 return $doc->saveHTML($form);
490         }
491
492         // Calling "$title = $parser->getTitle(); $title->invalidateCache();" doesn't help here to force regeneration
493         // However, this would not be the best solution because the page has to be re-rendered only at midnight
494
495         // In the following line, $author_name was replaced by NULL to prevent a bug, where the wrong author_name
496         // is shown due to caching (see ticket #35).
497         return wrReportFormRender(TRUE, $parser->getTitle()->getText(), NULL, NULL, NULL, NULL);
498 }
499
500
501 /// \brief Is called when the tag <bahnberichte/> is encountered.
502 ///
503 /// The current page name is taken.
504 function bahnberichteParserHook($input, $args, $parser) {
505         $page_title = $parser->getTitle()->getText();
506         $conditions = array('page_title' => $page_title, 'date_invalid > now()');
507         $rows = wrReportGetReports($conditions);
508         if (count($rows) == 0) return wfMessage('wrreport-reports-none')->text();
509         return wrReportTableRender($rows, WRREPORT_COMPACT, wrReportUserMayDelete(), $parser);
510 }
511
512
513 /// \brief Is called when the tag <bahnentabelle/> is encountered.
514 ///
515 /// Example:
516 /// <bahnentabelle>
517 /// Birgitzer Alm (vom Adelshof)
518 /// Kemater Alm
519 /// Axamer Lizum
520 /// </bahnentabelle>
521 function bahnentabelleParserHook($input, $args, $parser) {
522         $page_titles = array(); // array of Title objects
523         foreach (explode("\n", $input) as $page_title) {
524                 $page_title = Title::newFromText(trim($page_title));
525                 if (!$page_title || !$page_title->exists()) continue;
526                 $page_titles[] = $page_title;
527         }
528
529         $dbr = wfGetDB(DB_SLAVE);
530         // 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
531         $where_array = array('page.page_id = wrsledruncache.page_id');
532         if (count($page_titles) > 0) {
533                 $mysql_page_titles = array();
534                 for ($i = 0; $i != count($page_titles); ++$i) $mysql_page_titles[] = "'" . mysql_real_escape_string($page_titles[$i]->getDBKey()) . "'";
535                 $where_array[] = 'page.page_title in (' . implode(', ', $mysql_page_titles) . ')';
536         } else $where_array[] = 'false';
537         $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'), $where_array, 'bahnentabelleParserHook', array('ORDER BY' => 'page.page_title'));
538
539         global $wgUser;
540         global $wgWrReportMode; // e.g. 'summer'
541         global $wgWrReportBlackListAll;
542         global $wgWrReportBlackListStrangers;
543         global $wgDBtype;
544
545         // Determine, whether the user is allowed to make a new report
546         $userMayReport = ($wgWrReportMode == 'allow' || ($wgWrReportMode == 'loggedin' && $wgUser->isLoggedIn()));
547
548         // Generate DOM for HTML
549         $doc = new WrDOMDocument();
550         $table = $doc->appendElement('table', array('class' => 'wikitable'));
551
552         // Title
553         $tr = $table->appendElement('tr');
554         $tr->appendElement('th')->appendElement('img', array('src' => '/vorlagen/s_rental.png', 'alt' => wfMessage('wrreport-icon-sledrental')->text(), 'title' => wfMessage('wrreport-icon-sledrental')->text()));
555         $tr->appendElement('th')->appendElement('img', array('src' => '/vorlagen/s_light.png', 'alt' => wfMessage('wrreport-icon-nightlight')->text(), 'title' => wfMessage('wrreport-icon-nightlight')->text()));
556         $tr->appendElement('th')->appendElement('img', array('src' => '/vorlagen/s_lift.png', 'alt' => wfMessage('wrreport-icon-lift')->text(), 'title' => wfMessage('wrreport-icon-lift')->text()));
557         $tr->appendElement('th')->appendElement('img', array('src' => '/vorlagen/s_walk.png', 'alt' => wfMessage('wrreport-icon-walkupseparate')->text(), 'title' => wfMessage('wrreport-icon-walkupseparate')->text()));
558         $tr->appendElement('th')->appendElement('img', array('src' => '/vorlagen/s_bus.png', 'alt' => wfMessage('wrreport-icon-publictransport')->text(), 'title' => wfMessage('wrreport-icon-publictransport')->text()));
559         $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun')->text());
560         if ($wgWrReportMode != 'summer') $tr->appendElement('th')->appendText(wfMessage('wrreport-newreport-condition')->text());
561         $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun-information')->text());
562         $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun-walkuptime')->text());
563         $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun-height')->text());
564         $tr->appendElement('th')->appendText(wfMessage('wrreport-sledrun-length')->text());
565
566         // Rows
567         while ($row = $dbr->fetchObject($res)) {
568                 $title = Title::newFromRow($row);
569                 $tr = $table->appendElement('tr');
570
571                 $td = $tr->appendElement('td');
572                 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()));
573
574                 $td = $tr->appendElement('td');
575                 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()));
576                    
577                 $td = $tr->appendElement('td');
578                 if ($row->lift) $td->appendElement('img', array('src' => '/vorlagen/s_lift.png', 'alt' => wfMessage('wrreport-icon-lift')->text(), 'title' => wfMessage('wrreport-icon-lift')->text()));
579
580                 $td = $tr->appendElement('td');
581                 if (!is_null($row->walkup_possible)) {
582                         if ($row->walkup_possible) {
583                                 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()));
584                         } else $td->appendElement('img', array('src' => '/vorlagen/s_nowalk.png', 'alt' => wfMessage('wrreport-icon-walkupnotpossible')->text(), 'title' => wfMessage('wrreport-icon-walkupnotpossible')->text()));
585                 }
586
587                 $td = $tr->appendElement('td');
588                 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()));
589
590                 $tr->appendElement('td')->appendElement('a', array('href' => $title->getLocalURL()))->appendText($title->getPrefixedText());
591
592                 if ($wgWrReportMode != 'summer') {
593                         // Check rights
594                         $userMayReportThis = $userMayReport;
595                         if ($userMayReportThis) {
596                                 if (in_array($title->getText(), $wgWrReportBlackListAll)) $userMayReportThis = FALSE;
597                                 if (!$wgUser->isLoggedIn() && in_array($title->getText(), $wgWrReportBlackListStrangers)) $userMayReportThis = FALSE; // Title::getText() uses spaces instead of underscores
598                         }
599
600                         // Select condition
601                         list($report_id, $condition, $date_report) = wrGetSledrunCondition($dbr, $row->page_id);
602                         if (is_null($report_id)) $date = '';
603                         else $date = strftime('%d.%m.', $date_report);
604
605                         $td = $tr->appendElement('td');
606                         if (isset(WrReport::$wrConditions[$condition])) {
607                                 $td->appendElement('a', array('href' => $title->getLocalURL() . '#' . Title::escapeFragmentForURL(wfMessage('wrreport-reports-sectionname')->text())))->appendText(WrReport::$wrConditions[$condition]);
608                                 $td->appendText(' ');
609                                 $small = $td->appendElement('small');
610                                 $small->appendText($date);
611                                 if ($userMayReportThis) {
612                                         $small->appendText(' ');
613                                         $small->appendElement('em')->appendElement('a', array('href' =>$title->getLocalURL() . '#' . Title::escapeFragmentForURL(wfMessage('wrreport-newreport-sectionname')->text())))->appendText(wfMessage('wrreport-newreport-new')->text());
614                                 }
615                         } else {
616                                 if ($userMayReportThis) 
617                                         $td->appendElement('small')->appendElement('em')->appendElement('a', array('href' =>$title->getLocalURL() . '#' . Title::escapeFragmentForURL(wfMessage('wrreport-newreport-sectionname')->text())))->appendText(wfMessage('wrreport-newreport-please')->text());
618                                 else $td->appendText('--');
619                         }
620                 }
621
622                 $td = $tr->appendElement('td');
623                 $info = $row->information_phone;
624                 if ($info) {
625                         $info_parts = explode(';', $info);
626                         $info_parts = explode('(', $info_parts[0], 2);
627                         if (count($info_parts) == 2) {
628                                 $td->appendText($info_parts[0]);
629                                 $td->appendElement('small')->appendText('(' . $info_parts[1]);
630                         } else $td->appendText($info);
631                 }
632
633                 $tr->appendElement('td')->appendText($row->walkup_time ? $row->walkup_time . ' min' : '');
634
635                 $tr->appendElement('td')->appendText(
636                         ($row->bottom_elevation ? $row->bottom_elevation  : '') . 
637                         ($row->bottom_elevation && $row->top_elevation ? ' - ' : '') . 
638                         ($row->top_elevation ? $row->top_elevation : '') . 
639                         ($row->bottom_elevation || $row->top_elevation ? ' m' : '')); 
640
641                 $tr->appendElement('td')->appendText($row->length ? $row->length . ' m' : '');
642         }
643         $dbr->freeResult($res);
644
645         return $doc->saveHTML();
646 }
647
648
649 // tag extension hooks
650 // -------------------
651
652 class WrReport {
653         // Conditions: array(1 => 'Sehr gut', 2 => 'Gut', 3 => 'Mittelmäßig', 4 => 'Schlecht', 5 => 'Geht nicht');
654         public static $wrConditions;
655
656
657         function initWrConditions() {
658                 WrReport::$wrConditions = array();
659                 for ($i = 1; $i != 6; ++$i) WrReport::$wrConditions[$i] = wfMessage('wrreport-condition-' . $i)->text();
660         }
661
662
663         public static function languageGetSpecialPageAliasesHook(&$specialPageArray, $languageCode) {
664                 $text = wfMessage('wrreport')->text(); // 'Bahnberichte'
665                 $title = Title::newFromText($text); // 'Bahnberichte'
666                 $specialPageArray['wrreport'][] = $title->getDBKey(); // 'Bahnberichte'
667                 return true;
668         }
669
670
671         public static function parserFirstCallInitHook(&$parser) {
672                 $parser->setHook('bahnberichtformular', 'bahnberichtformularParserHook');
673                 $parser->setHook('bahnberichte', 'bahnberichteParserHook');
674                 $parser->setHook('bahnentabelle', 'bahnentabelleParserHook');
675                 return true;
676         }
677 }
678 WrReport::initWrConditions();
679
680
681
682 // Special page
683 // ------------
684
685 /// Specal Page to show reports
686 class SpecialWrReport extends SpecialPage {
687         function __construct() {
688                 parent::__construct('wrreport');
689         }
690
691
692         /// \param $par Possibilities:
693         /// - action == 'view' (default)
694         /// - action == 'preview': Preview new report
695         /// - action == 'store': Store new report
696         /// - action == 'deletepreview': Preview the deleted record
697         /// - action == 'delete': Delete an existing report
698         /// - action == 'showerror': Shows the error and exits
699         /// \param $override_action If not NULL (default), it overrides the action in $par
700         /// \param $errorMsg UFT-8 encoded error message (in WikiText) to show on top of the page or NULL (default):
701         function execute($par, $override_action = NULL, $errorMsg = NULL) {
702                 // $request = $this->getRequest();
703                 // $output = $this->getOutput(); // see http://www.mediawiki.org/wiki/Manual:Special_pages
704                 // $param = $request->getText('param');
705         
706                 global $wgRequest, $wgOut, $wgParser;
707
708                 $this->setHeaders();
709
710                 // Get request data
711                 $action = $wgRequest->getText('action');
712                 if (!$action) {
713                         if ($wgRequest->getVal('preview')) $action = 'preview';
714                         elseif ($wgRequest->getVal('store')) $action = 'store';
715                         elseif ($wgRequest->getVal('deletepreview')) $action = 'deletepreview';
716                         elseif ($wgRequest->getVal('delete')) $action = 'delete';
717                         else $action = 'view';
718                 }
719                 if ($override_action) $action = $override_action;
720
721                 // Show error message
722                 if ($errorMsg || $action == 'showerror') {
723                         $wgOut->addWikiText('<div class="errorbox">' . $errorMsg . "</div>\n");
724                         if ($action == 'showerror') return;
725                 }
726
727                 // Action view
728                 if ($action == 'view') {
729                         $conditions = array('date_invalid > now()');
730                         $rows = wrReportGetReports($conditions);
731                         if (count($rows) == 0) $wgOut->addHTML(wfMessage('wrreport-reports-none')->text());
732                         else {
733                                 $wgOut->addWikiText(''); // this is necessary because otherwise $wgParser is not properly initialized but $wgParser is needed in the next line
734                                 $wgOut->addHTML(wrReportTableRender($rows, WRREPORT_DETAIL, wrReportUserMayDelete(), $wgParser));
735                         }
736                 }
737
738                 // Action deletepreview or delete
739                 elseif ($action == 'deletepreview' || $action == 'delete') {
740                         $reportid = (int) $wgRequest->getText('reportid');
741                         if ($reportid == 0) {
742                                 $this->execute($par, 'showerror', wfMessage('wrreport-deletereport-noreport')->text());
743                                 return;
744                         }
745                         $rows = wrReportGetReports(array('id' => $reportid));
746                         if (count($rows) != 1) {
747                                 $this->execute($par, 'showerror', wfMessage('wrreport-deletereport-invalid')->text());
748                                 return;
749                         }
750                         $row = $rows[0];
751                         if (!is_null($row['delete_date'])) {
752                                 $this->execute($par, 'showerror', wfMessage('wrreport-deletereport-alreadydeleted')->text());
753                                 return;
754                         }
755                         $delete_reason_public = $wgRequest->getText('delete_reason_public');
756                         $delete_person_name = $wgRequest->getText('delete_person_name');
757                         $delete_invisible = $wgRequest->getText('delete_invisible') ? TRUE : FALSE;
758                         if ($action == 'delete') {
759                                 // page
760                                 $title = Title::newFromId($row['page_id']);
761
762                                 // user
763                                 global $wgUser;
764                                 $delete_person_userid = $wgUser->getId();
765                                 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.
766                                 $delete_person_username = $wgUser->getName();
767
768                                 // Check permissions - see also function wrReportUserMayDelete, that does also check permissions but does not return an error message.
769                                 $errorMsg = NULL;
770                                 global $wgWrReportDeleteMode;
771                                 if ($wgWrReportDeleteMode == 'deny') $errorMsg = wfMessage('wrreport-deletereport-deny')->text();
772                                 elseif ($wgWrReportDeleteMode == 'loggedin' && !$wgUser->isLoggedIn()) $errorMsg = wfMessage('wrreport-deletereport-loggedin')->text();
773                                 elseif (!$delete_person_name || !$delete_reason_public) $errorMsg = wfMessage('wrreport-deletereport-incomplete')->text();
774                                 if ($errorMsg) {
775                                         $this->execute($par, 'deletepreview', $errorMsg);
776                                         return;
777                                 }
778
779                                 // "Delete" (update) entry
780                                 $dbr = wfGetDB(DB_MASTER);
781                                 $dbr->update(
782                                         'wrreport',
783                                         array(
784                                                 'delete_date' => date('c'),
785                                                 'delete_person_name' => $delete_person_name,
786                                                 'delete_person_ip' => $_SERVER['REMOTE_ADDR'],
787                                                 'delete_person_userid' => $delete_person_userid,
788                                                 'delete_person_username' => $delete_person_username,
789                                                 'delete_reason_public' => $delete_reason_public,
790                                                 'delete_invisible' => $delete_invisible ? 't' : 'f'
791                                         ),
792                                         array('id' => $reportid)
793                                 );
794
795                                 // Purge cache
796                                 $title->invalidateCache();
797                                 wrRecacheRegions();
798
799                                 // Show success message
800                                 $wgOut->addWikiText(wfMessage('wrreport-deletereport-success', '[[' . $row['page_title'] . '#' . wfMessage('wrreport-reports-sectionname')->text() . '|' . $row['page_title'] . ']]')->text());
801                         }
802                         if ($action == 'deletepreview') {
803                                 $wgOut->addWikiText(wfMessage('wrreport-deletereport-preview-before')->text());
804                                 $format = WRREPORT_COMPACT_PAGE;
805                                 $wgOut->addHTML(wrReportTableRender(array($row), $format, FALSE, $wgParser));
806                                 $wgOut->addWikiText(wfMessage('wrreport-deletereport-preview-after')->text());
807                                 $row['delete_date'] = date('c');
808                                 $row['delete_reason_public'] = $delete_reason_public;
809                                 $row['delete_person_name'] = $delete_person_name;
810                                 $row['delete_invisible'] = $delete_invisible;
811                                 $wgOut->addHTML(wrReportTableRender(array($row), $format, FALSE, $wgParser));
812                                 $wgOut->addWikiText(wfMessage('wrreport-deletereport-preview-form')->text());
813                                 $wgOut->addHTML(wrDeleteReportFormRender($reportid, $delete_person_name, $delete_reason_public, $delete_invisible));
814                                 $wgOut->addWikiText(wfMessage('wrreport-deletereport-preview-bottom')->text());
815                         }
816                 }
817
818                 // Action preview or store
819                 elseif ($action == 'preview' || $action == 'store') {
820                         $page_title = $wgRequest->getText('page_title');
821                         $date_report = $wgRequest->getText('date_report');
822                         $condition = $wgRequest->getText('condition');
823                         $description = $wgRequest->getText('description');
824                         $author_name = $wgRequest->getText('author_name');
825
826                         // condition
827                         $condition = (int) $condition; // force to be nummeric. -1 ... "keine Bewertung", 0 ... "Bitte eingeben", 1 to 5 ... "Sehr gut" to "Geht nicht"
828                         if ($condition < -1 or $condition > 5) $condition = 0; // invalid condition: Tread like 0.
829                         $condition_sql = NULL;
830                         if ($condition >= 1 and $condition <= 5) $condition_sql = $condition;
831
832                         // author_name
833                         $author_name = trim($author_name);
834
835                         // page_id
836                         $title = Title::newFromText($page_title);
837                         $page_id = $title->getArticleId();
838                         if ($page_id == 0) $page_id = NULL;
839
840                         // user_id
841                         global $wgUser;
842                         $author_userid = $wgUser->getId();
843                         if ($author_userid == 0) $author_userid = NULL; // to store a NULL value in the database if no user is logged in instead of 0.
844                         $author_username = $wgUser->getName();
845
846                         if ($action == 'store') {
847                                 // check conditions/permissions
848                                 $errorMsg = NULL;
849                                 global $wgWrReportMode;
850                                 global $wgWrReportBlackListAll;
851                                 global $wgWrReportBlackListStrangers;
852                                 if ($wgWrReportMode == 'summer') $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-summer')->text());
853                                 elseif ($wgWrReportMode == 'deny') $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-deny')->text());
854                                 elseif ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-loggedin')->text());
855                                 elseif (!$page_id) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-pagenotfound')->text());
856                                 elseif (in_array($page_title, $wgWrReportBlackListAll)) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-blacklist')->text());
857                                 elseif (!$wgUser->isLoggedIn() && in_array($page_title, $wgWrReportBlackListStrangers)) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-blackliststrangers')->text());
858                                 elseif ($condition == 0) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-choosecondition')->text());
859                                 elseif (!$wgUser->isLoggedIn()) {
860                                         if (!$description) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-enterdescription')->text());
861                                         elseif (!(stripos($description, 'http') === FALSE)) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-nohttp')->text());
862                                         elseif (!$author_name) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-enterauthor')->text());
863                                 }
864
865                                 // check author name
866                                 if (!$errorMsg) {
867                                         $author_name_id = $wgUser->idFromName(strtolower($author_name));
868                                         if ($wgUser->isLoggedIn()) {
869                                                 if ($author_name_id != 0 && $author_name_id != $wgUser->getId())
870                                                         $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-authorused')->text());
871                                         } else {
872                                                 if ($author_name_id != 0)
873                                                         $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-authorlogin')->text());
874                                         }
875                                 }
876
877                                 // Chech whether identical reports are present
878                                 if (!$errorMsg) {
879                                         $dbr = wfGetDB(DB_SLAVE);
880                                         $cond = 'condition';
881                                         global $wgDBtype;
882                                         if ($wgDBtype == "mysql") $cond = "`$cond`"; // "condition" is a reserved word in mysql
883                                         $sqlConditions = array('page_id' => $page_id, 'date_report' => $date_report, $cond => $condition_sql, 'description' => $description, 'author_name' => $author_name);
884                                         $res = $dbr->select('wrreport', 'id', $sqlConditions);
885                                         if ($res->numRows() == 1) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-alreadysaved')->text());
886                                         $dbr->freeResult($res);
887                                 }
888
889                                 // Show error if any
890                                 if ($errorMsg) {
891                                         $this->execute($par, 'preview', $errorMsg);
892                                         return;
893                                 }
894
895                                 // Save entry
896                                 $dbr = wfGetDB(DB_MASTER);
897                                 $dbr->insert(
898                                         'wrreport',
899                                         array(
900                                                 'page_id' => $page_id,
901                                                 'page_title' => $page_title,
902                                                 'date_report' => $date_report,
903                                                 'date_entry' => date('c'),
904                                                 'date_invalid' => date('c', strtotime('+9 days')),
905                                                 $cond => $condition_sql,
906                                                 'description' => $description,
907                                                 'author_name' => $author_name,
908                                                 'author_ip' => $_SERVER['REMOTE_ADDR'],
909                                                 'author_userid' => $author_userid,
910                                                 'author_username' => $author_username
911                                                 // 'delete_*' => // use database defaults (NULL)
912                                         )
913                                 );
914
915                                 // Purge cache
916                                 $title->invalidateCache();
917                                 wrRecacheRegions();
918                                 wrUpdateWrReportCacheTable($page_id);
919
920                                 // Show success message
921                                 $wgOut->addWikiText(wfMessage('wrreport-newreport-success', '[[' . $page_title . '#' . wfMessage('wrreport-reports-sectionname')->text() . '|' . $page_title . ']]')->text());
922                                 // We could redirect to result with the following line but we don't want to.
923                                 // $wgOut->redirect($title->getFullURL() . '#' . wfMessage('wrreport-reports-sectionname')->text());
924                         }
925                         if ($action == 'preview') {
926                                 $wgOut->addWikiText(wfMessage('wrreport-newreport-preview-top')->text());
927                                 $format = WRREPORT_COMPACT_PAGE;
928                                 $row = array_fill_keys(wrReportGetColumnNames(), NULL);
929                                 $row['page_id'] = $page_id;
930                                 $row['page_title'] = $page_title;
931                                 $row['date_report'] = $date_report;
932                                 $row['condition'] = $condition_sql;
933                                 $row['description'] = $description;
934                                 $row['author_name'] = $author_name;
935                                 $row['author_userid'] = $author_userid;
936                                 $row['author_username'] = $author_username;
937
938                                 $wgOut->addHTML(wrReportTableRender(array($row), $format, FALSE, $wgParser));
939                                 $wgOut->addWikiText(wfMessage('wrreport-newreport-preview-middle')->text());
940                                 $wgOut->addHTML(wrReportFormRender(FALSE, $page_title, $date_report, $condition, $description, $author_name));
941                                 $wgOut->addWikiText(wfMessage('wrreport-newreport-preview-bottom')->text());
942                         }
943
944                 }
945
946                 else die('Wrong action');
947         }
948 }
949
950
951 ?>