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