7 function wrReportParserInit() {
9 $wgParser->setHook('bahnberichtformular', 'bahnberichtformularParserHook');
10 $wgParser->setHook('bahnberichte', 'bahnberichteParserHook');
11 $wgParser->setHook('bahnbewertung', 'bahnbewertungParserHook');
19 /// \brief This function is used to translate WikiText to HTML. Normally it should be avoided to do this
20 /// but I found situations where I did not find an other possibility.
21 function wrReportSandboxParse($wikiText) {
22 global $wgTitle, $wgUser;
23 $myParser = new Parser();
24 $myParserOptions = new ParserOptions();
25 $myParserOptions->initialiseFromUser($wgUser);
26 $result = $myParser->parse($wikiText, $wgTitle, $myParserOptions);
27 return $result->getText();
31 /// List of markers - used by the functions replaceByMarker and wrReportAfterTidy
32 $wrReportMarkerList = array();
35 /// Returns a marker for a text and back-replaces the text in wrReportAfterTidy
36 function replaceByMarker($text, $marker = 'marker') {
37 $marker = $marker . mt_rand(1e5, 1e7);
38 global $wrReportMarkerList;
39 $wrReportMarkerList[$marker] = $text;
44 /// Replaces the markers by its contents
45 function wrReportAfterTidy(&$parser, &$text) {
46 // find markers in $text
47 // replace markers with actual output
48 global $wrReportMarkerList;
49 foreach ($wrReportMarkerList as $marker => $html) $text = str_replace($marker, $html, $text);
58 /// \brief Returns a form to enter a report (string containing HTML).
60 /// All parameters have to be UTF-8 encoded.
61 /// \param $page_title Name of the sledding run.
62 /// \return UTF-8 encoded HTML form
63 function wrReportFormRender($hide_save_button = TRUE, $page_title = NULL, $date_report = NULL, $condition = NULL, $description = NULL, $author_name = NULL, $page_title_list = NULL) {
64 if ($page_title) $page_title = htmlspecialchars($page_title);
66 $tagnamen = array('Heute', 'Gestern', 'Vorgestern', 'Vor 3 Tagen', 'Vor 4 Tagen');
68 $date_selected = FALSE;
69 for ($tag=0; $tag!=5; ++$tag) {
70 $datum = time() - $tag*24*60*60;
71 $datum_f = strftime("%Y-%m-%d", $datum);
72 $date_options .= '<option value="' . $datum_f . '"';
73 // var_dump($date_report);
74 // var_dump($datum_f);
75 if ((is_null($date_report) && $tag == 0) || (!is_null($date_report) && $date_report == $datum_f)) {
76 $date_options .= ' selected="selected"';
77 $date_selected = TRUE;
79 $date_options .= '>' . htmlspecialchars($tagnamen[$tag]) . ' (' . strftime('%d.%m.', $datum) . ')</option>' . "\n";
81 if (!$date_selected) {
82 $date_options = '<option value="' . $date_report . '" selected="selected">' . htmlspecialchars($date_report) . "</option>\n" . $date_options;
86 $condition_options = '';
87 global $wrConditions; // $wrConditions = array(1 => 'Sehr gut', 2 => 'Gut', 3 => utf8_encode('Mittelmäßig'), 4 => 'Schlecht', 5 => 'Geht nicht');
88 $condition_options .= utf8_encode('<option value="">(keine Beurteilung)</option>') . "\n";
89 foreach ($wrConditions as $condition_num => $condition_text) {
90 $condition_options .= "<option value=\"$condition_num\"";
91 if ($condition == $condition_num) $condition_options .= ' selected="selected"';
92 $condition_options .= '>' . htmlspecialchars($condition_text) . "</option>\n";
94 $description_html = htmlspecialchars($description);
97 // I would like to do it this way, but due to a bug of internet explorer, the <button> element is not useable.
98 // $buttons = '<button name="action" type="submit" value="preview">Vorschau';
99 // if ($hide_save_button) $buttons .= ' & Speichern';
100 // $buttons .= '</button>';
101 // if (!$hide_save_button) $buttons .= '<button name="action" type="submit" value="store">Speichern</button>';
102 // Workaround: User <input type="submit"/>
103 $buttons = '<input name="preview" type="submit" value="Vorschau';
104 if ($hide_save_button) $buttons .= ' & Speichern';
106 if (!$hide_save_button) $buttons .= '<input name="store" type="submit" value="Speichern"/>';
109 <form action="/wiki/Spezial:Bahnberichte" method="post">
110 <table class="wrreportform" summary="Formular zum Eintragen eines Rodelbahnberichtes">
111 <tr class="oddrow"><th>Rodelbahn</th><td>$page_title<input type="hidden" name="page_title" value="$page_title"/></td></tr>
112 <tr class="evenrow"><th>Datum des Rodelns</th>
114 <select name="date_report">
119 <tr class="oddrow"><th>Bahnzustand*</th><td><select name="condition">
122 <tr class="evenrow"><th>Meinung</th><td><textarea name="description" cols="50" rows="7">$description_html</textarea></td></tr>
123 <tr class="oddrow"><th>Autor/in</th><td><input name="author_name" maxlength="30" size="30" value="$author_name" /></td></tr>
124 <tr class="evenrow"><th>Bericht abschicken</th><td>$buttons</td></tr>
133 /// \brief sub-function of wrReportTableRender - renders the table head
135 /// The input variables are either NULL (-> the column is not present) or a UTF-8 encoded string (that is ignored ;-) ).
136 /// \return UTF-8 encoded titles of HTML table
137 function wrReportTableTitleRender($page_id, $page_title, $date_report, $date_entry, $date_invalid, $condition, $description, $author_name) {
139 if ($page_id) $out .= '<th>ID</th>';
140 if ($page_title) $out .= '<th>Bahn</th>';
141 if ($date_report) $out .= '<th>Datum</th>';
142 if ($date_entry) $out .= '<th>Datum Eintrag</th>';
143 if ($date_invalid) $out .= '<th>Datum Ungültig</th>';
144 if ($condition) $out .= '<th>Zustand</th>';
145 if ($description) $out .= '<th>Beschreibung</th>';
146 if ($author_name) $out .= '<th>Autor</th>';
147 return utf8_encode($out . "</tr>\n");
151 /// \brief sub-function of wrReportTableRender - renders a table row
153 /// The in- and output parameters are expected to be UTF-8 encoded.
154 function wrReportTableRowRender($page_id, $page_title, $date_report, $date_entry, $date_invalid, $condition, $description, $author_name) {
156 if (!is_null($page_id)) $out .= '<td>' . $page_id . '</td>';
157 if (!is_null($page_title)) $out .= '<td>' . wrReportSandboxParse('[[' . $page_title . ']]') . '</td>';
158 $dayOfWeek = array('Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So');
159 $date_report = strtotime($date_report);
160 $date_report = $dayOfWeek[strftime('%u', $date_report)-1] . strftime(', %d.%m.', $date_report);
161 $out .= '<td>' . $date_report . '</td>';
162 if (!is_null($date_entry)) $out .= '<td>' . date('Y-m-d, H:i', strtotime($date_entry)) . '</td>';
163 if (!is_null($date_invalid)) $out .= '<td>' . date('Y-m-d, H:i', strtotime($date_invalid)) . '</td>';
164 global $wrConditions;
165 $condition_text = '---';
166 if (isset($wrConditions[$condition])) $condition_text = $wrConditions[$condition];
167 $out .= '<td>' . htmlspecialchars($condition_text) . '</td>';
168 $out .= '<td class="wrreportdescription">' . wrReportSandboxParse($description) . '</td>';
169 $out .= '<td>' . htmlspecialchars($author_name) . '</td>';
175 /// \brief It returns a table (string containing HTML) showing the results.
177 /// Is called when the tag <bahnberichte/> is encounted and renders a table /
178 /// \param $page_title If the name is specified (UTF-8 encoded), only reports of this "page" (e.g. 'Birgitzer Alm') is shown. Default: NULL
179 /// \return UTF-8 encoded HTML result table
180 function wrReportTableRender($page_title = NULL) {
182 $dbr = wfGetDB(DB_SLAVE);
183 $conditions = array();
184 if ($page_title) $conditions['page_title'] = $page_title;
185 $res = $dbr->select('wrreport', array('id', 'page_title', 'date_report', 'date_entry', 'date_invalid', 'condition', 'description', 'author_name', 'author_username'), $conditions, $fname = 'Database::select', $options = array('ORDER BY' => 'date_report desc, date_entry desc'));
186 $admin = is_null($page_title);
187 if ($res->numRows() > 0) {
188 $out .= "<table class=\"wrreporttable\">\n";
189 $out .= wrReportTableTitleRender($admin, $admin, TRUE, $admin, $admin, TRUE, TRUE, TRUE);
190 while ($row = $dbr->fetchObject($res)) {
191 $author = $row->author_name;
192 $out .= wrReportTableRowRender($admin ? $row->id : NULL, $admin ? $row->page_title : NULL, $row->date_report, $admin ? $row->date_entry : NULL, $admin ? $row->date_invalid : NULL, $row->condition, $row->description, $author);
194 $out .= "</table>\n";
196 $out .= wrReportSandboxParse("''Es wurden keine Bahnberichte in der Datenbank gefunden.''\n\n");
198 $dbr->freeResult($res);
203 /// \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);
205 /// If no condition is present, array(NULL, NULL) is returned
206 function wrReportConditionRender($page_title) {
207 $dbr = wfGetDB(DB_SLAVE);
208 $res = $dbr->select('wrreport', array('max(wrreport.id) as max'), array('page_title' => $page_title, 'condition is not null', 'date_invalid > now()'));
209 // select condition, date_report from wrreport where id = (select max(wrreport.id) as max from wrreport where page_title='Birgitzer Alm (vom Adelshof)' and date_invalid > now() and condition is not null);
210 if ($res->numRows() <= 0) return array(NULL, NULL);
211 $row = $dbr->fetchObject($res);
212 $res = $dbr->select('wrreport', array('condition', 'date_report'), array('id' => $row->max));
213 if ($res->numRows() <= 0) return array(NULL, NULL);
214 $row = $dbr->fetchObject($res);
215 $date = $row->date_report;
216 if ($date) $date = strtotime($date);
217 return array($row->condition, $date);
221 // Parser Hook Functions
222 // ---------------------
224 /// \brief Is called when the tag <bahnberichtformular/> is encountered.
226 /// The current page name is taken.
227 function bahnberichtformularParserHook($input, $args, $parser) {
231 if ($wgUser->isLoggedIn()) {
232 $author_name = $wgUser->getRealName();
233 if (!$author_name) $author_name = $wgUser->getName();
236 global $wgWrReportMode;
237 global $wgWrReportBlackListAll;
238 global $wgWrReportBlackListStrangers;
239 if ($wgWrReportMode == 'summer') return wrReportSandboxParse(utf8_encode("''An dieser Stelle kann während des Winters die Schneelage von Rodelbahnen eingetragen werden.''\n\n"));
240 if ($wgWrReportMode == 'deny') return wrReportSandboxParse(utf8_encode("''Rodelbahnberichte sind derzeit leider nicht erlaubt.''\n\n"));
241 if ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) return wrReportSandboxParse(utf8_encode("''Derzeit sind Rodelbahnberichte nur für angemeldete Benutzer erlaubt.''\n\n"));
242 if (in_array($parser->getTitle()->getText(), $wgWrReportBlackListAll)) return wrReportSandboxParse(utf8_encode("''Bei dieser Rodelbahn dürfen derzeit leider keine Rodelbahnberichte abgegeben werden.''\n"));
243 if (!$wgUser->isLoggedIn() && in_array($parser->getTitle()->getText(), $wgWrReportBlackListStrangers)) return wrReportSandboxParse(utf8_encode("''Bei der angegebenen Rodelbahn dürfen derzeit nur angemeldete Benutzer Rodelbahnberichte abgebe.''\n\n"));
244 return replaceByMarker(wrReportFormRender(TRUE, $parser->getTitle()->getText(), NULL, NULL, NULL, $author_name));
248 /// \brief Is called when the tag <bahnberichte/> is encountered.
250 /// The current page name is taken.
251 function bahnberichteParserHook($input, $args, &$parser) {
252 return wrReportTableRender($parser->getTitle()->getText());
256 /// \brief Is called when the tag <bahnbewertung/> is encountered.
258 /// * <bahnbewertung/>: The current page name is taken.
259 /// * <bahnbewertung>page_name</bahnbewertung>: The given page name is taken.
260 function bahnbewertungParserHook($input, $args, &$parser) {
262 if (!$titleText) $titleText = $parser->getTitle()->getText();
263 list($condition, $date) = wrReportConditionRender($titleText);
264 if ($date) $date = strftime('%d.%m.', $date);
265 global $wrConditions;
266 global $wrNewReportSection; // = utf8_encode('Eintragen');
267 global $wrShowReportsSection; // = utf8_encode('Einträge');
269 global $wgWrReportMode; // e.g. 'summer'
270 global $wgWrReportBlackListAll;
271 global $wgWrReportBlackListStrangers;
273 // Determine, whether the user is allowed to make a new report
274 $userMayReport = ($wgWrReportMode == 'allow' || ($wgWrReportMode == 'loggedin' && $wgUser->isLoggedIn()));
275 if ($userMayReport) {
276 if (in_array($titleText, $wgWrReportBlackListAll)) $userMayReport = false;
277 if (!$wgUser->isLoggedIn() && in_array($titleText, $wgWrReportBlackListStrangers)) $userMayReport = false;
280 // Get the condition and create the response wiki text
281 if (isset($wrConditions[$condition])) {
282 $wikiText = '[['. $titleText . '#' . $wrShowReportsSection . '|'. $wrConditions[$condition] . "]] <small>$date";
283 if ($userMayReport) $wikiText .= " ''[[" . $titleText . '#' . $wrNewReportSection . "|Neu]]''";
284 $wikiText .= "</small>";
286 if ($userMayReport) $wikiText = "<small>''[[" . $titleText . '#' . $wrNewReportSection . "|Bitte eintragen]]''</small>";
287 else $wikiText = '--';
289 return wrReportSandboxParse($wikiText);
297 /// Specal Page to show reports
298 class WrReport extends SpecialPage {
299 function WrReport() {
300 SpecialPage::SpecialPage('WrReport');
301 wfLoadExtensionMessages('WrReport');
305 /// \param $par Possibilities:
306 /// - action == 'view' (default)
307 /// - action == 'preview': Preview new report
308 /// - action == 'store': Store new report
309 /// \param $override_action If not NULL (defualt), it overrides the action in $par
310 /// \param $errorMsg UFT-8 encoded error message (in WikiText) to show on top of the page or NULL (default):
311 function execute($par, $override_action = NULL, $errorMsg = NULL) {
312 global $wgRequest, $wgOut;
317 $action = $wgRequest->getText('action');
319 if ($wgRequest->getVal('preview')) $action = 'preview';
320 elseif ($wgRequest->getVal('store')) $action = 'store';
321 else $action = 'view';
323 if ($override_action) $action = $override_action;
325 // Show error message
327 $wgOut->addWikiText(utf8_encode('<div class="errorbox">') . $errorMsg . utf8_encode("</div>\n"));
331 if ($action == 'view') {
332 $wgOut->addHTML(wrReportTableRender());
335 // Action preview or store
336 elseif ($action == 'preview' || $action == 'store') {
337 $page_title = $wgRequest->getText('page_title');
338 $date_report = $wgRequest->getText('date_report');
339 $condition = $wgRequest->getText('condition');
340 $description = $wgRequest->getText('description');
341 $author_name = $wgRequest->getText('author_name');
342 if ($action == 'store') {
344 $title = Title::newFromText($page_title);
345 $page_id = $title->getArticleId();
346 if ($page_id == 0) $page_id = NULL;
350 $author_userid = $wgUser->getId();
351 if ($author_userid == 0) $author_userid = NULL; // to store a NULL value in the database if no user is logged in instead of 0.
352 $author_username = $wgUser->getName();
355 $condition_int = (int) $condition;
356 if ($condition_int >= 1 and $condition_int <= 5) $condition = $condition_int;
357 else $condition = NULL;
359 // check conditions/permissions
361 global $wgWrReportMode;
362 global $wgWrReportBlackListAll;
363 global $wgWrReportBlackListStrangers;
364 if ($wgWrReportMode == 'summer') $errorMsg = utf8_encode('Rodelbahnberichte sind in der schneefreien Zeit nicht erlaubt.');
365 elseif ($wgWrReportMode == 'deny') $errorMsg = utf8_encode('Rodelbahnberichte sind derzeit leider nicht erlaubt.');
366 elseif ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) $errorMsg = utf8_encode('Derzeit sind Rodelbahnberichte nur für angemeldete Benutzer erlaubt.');
367 elseif (!$page_id) $errorMsg = utf8_encode('Die angegebene Seite wurde nicht gefunden.');
368 elseif (in_array($page_title, $wgWrReportBlackListAll)) $errorMsg = utf8_encode('Bei der angegebenen Rodelbahn dürfen derzeit keine Rodelbahnberichte abgegeben werden.');
369 elseif (!$wgUser->isLoggedIn() && in_array($page_title, $wgWrReportBlackListStrangers)) $errorMsg = utf8_encode('Bei der angegebenen Rodelbahn dürfen derzeit nur angemeldete Benutzer Rodelbahnberichte abgebe.');
370 elseif (!$condition && !$description) $errorMsg = utf8_encode('Es sind sowohl die Beschreibung als auch die Bewertung leer/nicht vergeben.');
371 elseif (!$wgUser->isLoggedIn()) {
372 if (!$description) $errorMsg = utf8_encode('Bitte bei der Beschreibung eine kurze Begründung für die Bewertung abgeben.');
373 elseif (!(stripos($description, 'http') === FALSE)) $errorMsg = utf8_encode('Der Text "http" ist leider für nicht-angemeldete Benutzer nicht erlaubt, weil wir in der Vergangenheit Fälle hatten, bei denen externe Werbelinks automatisiert eingetragen wurden.');
376 $this->execute($par, 'preview', $errorMsg);
381 $dbr = wfGetDB(DB_MASTER);
385 'page_id' => $page_id,
386 'page_title' => $page_title,
387 'date_report' => $date_report,
388 // 'date_entry' => '', // use database default
389 // 'date_invalid' => '', // use database default
390 'condition' => $condition,
391 'description' => $description,
392 'author_name' => $author_name,
393 'author_ip' => $_SERVER['REMOTE_ADDR'],
394 'author_userid' => $author_userid,
395 'author_username' => $author_username
400 $title->invalidateCache();
401 // Show success message
402 global $wrShowReportsSection;
403 $wgOut->addWikiText(utf8_encode('<div class="successbox">Der Bahnbericht für [[') . $page_title . '#' . $wrShowReportsSection . '|' . $page_title . utf8_encode("]] wurde erfolgreich gespeichert.</div>\n"));
404 // We could redirect to result with the following line but we don't want to.
405 // $wgOut->redirect($title->getFullURL() . '#Eintr.C3.A4ge');
407 if ($action == 'preview') {
408 $wgOut->addWikiText(utf8_encode("== Vorschau (noch nicht gespeichert) ==\n"));
409 $wgOut->addHTML("<table class=\"wrreporttable\">\n");
410 $wgOut->addHTML(wrReportTableTitleRender(FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE));
411 $wgOut->addHTML(wrReportTableRowRender(NULL, $page_title, $date_report, NULL, NULL, $condition, $description, $author_name));
412 $wgOut->addHTML("</table>");
413 $wgOut->addWikiText("\n");
414 $wgOut->addWikiText(utf8_encode("== Speichern oder Ändern ==\n"));
415 $wgOut->addHTML(wrReportFormRender(FALSE, $page_title, $date_report, $condition, $description, $author_name));
420 else die('Wrong action');