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 $buttons = '<button name="action" type="submit" value="preview">Vorschau';
98 if ($hide_save_button) $buttons .= ' & Speichern';
99 $buttons .= '</button>';
100 if (!$hide_save_button) $buttons .= '<button name="action" type="submit" value="store">Speichern</button>';
103 <form action="/wiki/Spezial:Bahnberichte" method="post">
104 <table class="wrreportform" summary="Formular zum Eintragen eines Rodelbahnberichtes">
105 <tr class="oddrow"><th>Rodelbahn</th><td>$page_title<input type="hidden" name="page_title" value="$page_title"/></td></tr>
106 <tr class="evenrow"><th>Datum des Rodelns</th>
108 <select name="date_report">
113 <tr class="oddrow"><th>Bahnzustand*</th><td><select name="condition">
116 <tr class="evenrow"><th>Meinung</th><td><textarea name="description" cols="50" rows="7">$description_html</textarea></td></tr>
117 <tr class="oddrow"><th>Autor/in</th><td><input name="author_name" maxlength="30" size="30" value="$author_name" /></td></tr>
118 <tr class="evenrow"><th>Bericht abschicken</th><td>$buttons</td></tr>
127 /// \brief sub-function of wrReportTableRender - renders the table head
129 /// The input variables are either NULL (-> the column is not present) or a UTF-8 encoded string (that is ignored ;-) ).
130 /// \return UTF-8 encoded titles of HTML table
131 function wrReportTableTitleRender($page_id, $page_title, $date_report, $date_entry, $date_invalid, $condition, $description, $author_name) {
133 if ($page_id) $out .= '<th>ID</th>';
134 if ($page_title) $out .= '<th>Bahn</th>';
135 if ($date_report) $out .= '<th>Datum</th>';
136 if ($date_entry) $out .= '<th>Datum Eintrag</th>';
137 if ($date_invalid) $out .= '<th>Datum Ungültig</th>';
138 if ($condition) $out .= '<th>Zustand</th>';
139 if ($description) $out .= '<th>Beschreibung</th>';
140 if ($author_name) $out .= '<th>Autor</th>';
141 return utf8_encode($out . "</tr>\n");
145 /// \brief sub-function of wrReportTableRender - renders a table row
147 /// The in- and output parameters are expected to be UTF-8 encoded.
148 function wrReportTableRowRender($page_id, $page_title, $date_report, $date_entry, $date_invalid, $condition, $description, $author_name) {
150 if (!is_null($page_id)) $out .= '<td>' . $page_id . '</td>';
151 if (!is_null($page_title)) $out .= '<td>' . wrReportSandboxParse('[[' . $page_title . ']]') . '</td>';
152 $dayOfWeek = array('Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So');
153 $date_report = strtotime($date_report);
154 $date_report = $dayOfWeek[strftime('%u', $date_report)-1] . strftime(', %d.%m.', $date_report);
155 $out .= '<td>' . $date_report . '</td>';
156 if (!is_null($date_entry)) $out .= '<td>' . date('Y-m-d, H:i', strtotime($date_entry)) . '</td>';
157 if (!is_null($date_invalid)) $out .= '<td>' . date('Y-m-d, H:i', strtotime($date_invalid)) . '</td>';
158 global $wrConditions;
159 $condition_text = '---';
160 if (isset($wrConditions[$condition])) $condition_text = $wrConditions[$condition];
161 $out .= '<td>' . htmlspecialchars($condition_text) . '</td>';
162 $out .= '<td class="wrreportdescription">' . wrReportSandboxParse($description) . '</td>';
163 $out .= '<td>' . htmlspecialchars($author_name) . '</td>';
169 /// \brief It returns a table (string containing HTML) showing the results.
171 /// Is called when the tag <bahnberichte/> is encounted and renders a table /
172 /// \param $page_title If the name is specified (UTF-8 encoded), only reports of this "page" (e.g. 'Birgitzer Alm') is shown. Default: NULL
173 /// \return UTF-8 encoded HTML result table
174 function wrReportTableRender($page_title = NULL) {
176 $dbr = wfGetDB(DB_SLAVE);
177 $conditions = array();
178 if ($page_title) $conditions['page_title'] = $page_title;
179 $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'));
180 $admin = is_null($page_title);
181 if ($res->numRows() > 0) {
182 $out .= "<table class=\"wrreporttable\">\n";
183 $out .= wrReportTableTitleRender($admin, $admin, TRUE, $admin, $admin, TRUE, TRUE, TRUE);
184 while ($row = $dbr->fetchObject($res)) {
185 $author = $row->author_name;
186 $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);
188 $out .= "</table>\n";
190 $out .= wrReportSandboxParse("''Es wurden keine Bahnberichte in der Datenbank gefunden.''\n\n");
192 $dbr->freeResult($res);
197 /// \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);
199 /// If no condition is present, array(NULL, NULL) is returned
200 function wrReportConditionRender($page_title) {
201 $dbr = wfGetDB(DB_SLAVE);
202 $res = $dbr->select('wrreport', array('max(wrreport.id) as max'), array('page_title' => $page_title, 'condition is not null', 'date_invalid > now()'));
203 // 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);
204 if ($res->numRows() <= 0) return array(NULL, NULL);
205 $row = $dbr->fetchObject($res);
206 $res = $dbr->select('wrreport', array('condition', 'date_report'), array('id' => $row->max));
207 if ($res->numRows() <= 0) return array(NULL, NULL);
208 $row = $dbr->fetchObject($res);
209 $date = $row->date_report;
210 if ($date) $date = strtotime($date);
211 return array($row->condition, $date);
215 // Parser Hook Functions
216 // ---------------------
218 /// \brief Is called when the tag <bahnberichtformular/> is encountered.
220 /// The current page name is taken.
221 function bahnberichtformularParserHook($input, $args, $parser) {
225 if ($wgUser->isLoggedIn()) {
226 $author_name = $wgUser->getRealName();
227 if (!$author_name) $author_name = $wgUser->getName();
230 global $wgWrReportMode;
231 global $wgWrReportBlackListAll;
232 global $wgWrReportBlackListStrangers;
233 if ($wgWrReportMode == 'summer') return wrReportSandboxParse(utf8_encode("''An dieser Stelle kann während des Winters die Schneelage von Rodelbahnen eingetragen werden.''\n\n"));
234 if ($wgWrReportMode == 'deny') return wrReportSandboxParse(utf8_encode("''Rodelbahnberichte sind derzeit leider nicht erlaubt.''\n\n"));
235 if ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) return wrReportSandboxParse(utf8_encode("''Derzeit sind Rodelbahnberichte nur für angemeldete Benutzer erlaubt.''\n\n"));
236 if (in_array($parser->getTitle()->getText(), $wgWrReportBlackListAll)) return wrReportSandboxParse(utf8_encode("''Bei dieser Rodelbahn dürfen derzeit leider keine Rodelbahnberichte abgegeben werden.''\n"));
237 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"));
238 return replaceByMarker(wrReportFormRender(TRUE, $parser->getTitle()->getText(), NULL, NULL, NULL, $author_name));
242 /// \brief Is called when the tag <bahnberichte/> is encountered.
244 /// The current page name is taken.
245 function bahnberichteParserHook($input, $args, &$parser) {
246 return wrReportTableRender($parser->getTitle()->getText());
250 /// \brief Is called when the tag <bahnbewertung/> is encountered.
252 /// * <bahnbewertung/>: The current page name is taken.
253 /// * <bahnbewertung>page_name</bahnbewertung>: The given page name is taken.
254 function bahnbewertungParserHook($input, $args, &$parser) {
256 if (!$titleText) $titleText = $parser->getTitle()->getText();
257 list($condition, $date) = wrReportConditionRender($titleText);
258 if ($date) $date = strftime('%d.%m.', $date);
259 global $wrConditions;
260 global $wrNewReportSection; // = utf8_encode('Eintragen');
261 global $wrShowReportsSection; // = utf8_encode('Einträge');
263 global $wgWrReportMode; // e.g. 'summer'
264 global $wgWrReportBlackListAll;
265 global $wgWrReportBlackListStrangers;
267 // Determine, whether the user is allowed to make a new report
268 $userMayReport = ($wgWrReportMode == 'allow' || ($wgWrReportMode == 'loggedin' && $wgUser->isLoggedIn()));
269 if ($userMayReport) {
270 if (in_array($titleText, $wgWrReportBlackListAll)) $userMayReport = false;
271 if (!$wgUser->isLoggedIn() && in_array($titleText, $wgWrReportBlackListStrangers)) $userMayReport = false;
274 // Get the condition and create the response wiki text
275 if (isset($wrConditions[$condition])) {
276 $wikiText = '[['. $titleText . '#' . $wrShowReportsSection . '|'. $wrConditions[$condition] . "]] <small>$date";
277 if ($userMayReport) $wikiText .= " ''[[" . $titleText . '#' . $wrNewReportSection . "|Neu]]''";
278 $wikiText .= "</small>";
280 if ($userMayReport) $wikiText = "<small>''[[" . $titleText . '#' . $wrNewReportSection . "|Bitte eintragen]]''</small>";
281 else $wikiText = '--';
283 return wrReportSandboxParse($wikiText);
291 /// Specal Page to show reports
292 class WrReport extends SpecialPage {
293 function WrReport() {
294 SpecialPage::SpecialPage('WrReport');
295 wfLoadExtensionMessages('WrReport');
299 /// \param $par Possibilities:
300 /// - action == 'view' (default)
301 /// - action == 'preview': Preview new report
302 /// - action == 'store': Store new report
303 /// \param $override_action If not NULL (defualt), it overrides the action in $par
304 /// \param $errorMsg UFT-8 encoded error message (in WikiText) to show on top of the page or NULL (default):
305 function execute($par, $override_action = NULL, $errorMsg = NULL) {
306 global $wgRequest, $wgOut;
311 $action = $wgRequest->getText('action');
312 if ($action === '') $action = 'view';
313 if ($override_action) $action = $override_action;
315 // Show error message
317 $wgOut->addWikiText(utf8_encode('<div class="errorbox">') . $errorMsg . utf8_encode("</div>\n"));
321 if ($action == 'view') {
322 $wgOut->addHTML(wrReportTableRender());
325 // Action preview or store
326 elseif ($action == 'preview' || $action == 'store') {
327 $page_title = $wgRequest->getText('page_title');
328 $date_report = $wgRequest->getText('date_report');
329 $condition = $wgRequest->getText('condition');
330 $description = $wgRequest->getText('description');
331 $author_name = $wgRequest->getText('author_name');
332 if ($action == 'store') {
334 $title = Title::newFromText($page_title);
335 $page_id = $title->getArticleId();
336 if ($page_id == 0) $page_id = NULL;
340 $author_userid = $wgUser->getId();
341 if ($author_userid == 0) $author_userid = NULL; // to store a NULL value in the database if no user is logged in instead of 0.
342 $author_username = $wgUser->getName();
345 $condition_int = (int) $condition;
346 if ($condition_int >= 1 and $condition_int <= 5) $condition = $condition_int;
347 else $condition = NULL;
349 // check conditions/permissions
351 global $wgWrReportMode;
352 global $wgWrReportBlackListAll;
353 global $wgWrReportBlackListStrangers;
354 if ($wgWrReportMode == 'summer') $errorMsg = utf8_encode('Rodelbahnberichte sind in der schneefreien Zeit nicht erlaubt.');
355 elseif ($wgWrReportMode == 'deny') $errorMsg = utf8_encode('Rodelbahnberichte sind derzeit leider nicht erlaubt.');
356 elseif ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) $errorMsg = utf8_encode('Derzeit sind Rodelbahnberichte nur für angemeldete Benutzer erlaubt.');
357 elseif (!$page_id) $errorMsg = utf8_encode('Die angegebene Seite wurde nicht gefunden.');
358 elseif (in_array($page_title, $wgWrReportBlackListAll)) $errorMsg = utf8_encode('Bei der angegebenen Rodelbahn dürfen derzeit keine Rodelbahnberichte abgegeben werden.');
359 elseif (!$wgUser->isLoggedIn() && in_array($page_title, $wgWrReportBlackListStrangers)) $errorMsg = utf8_encode('Bei der angegebenen Rodelbahn dürfen derzeit nur angemeldete Benutzer Rodelbahnberichte abgebe.');
360 elseif (!$condition && !$description) $errorMsg = utf8_encode('Es sind sowohl die Beschreibung als auch die Bewertung leer/nicht vergeben.');
361 elseif (!$wgUser->isLoggedIn()) {
362 if (!$description) $errorMsg = utf8_encode('Bitte bei der Beschreibung eine kurze Begründung für die Bewertung abgeben.');
363 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.');
366 $this->execute($par, 'preview', $errorMsg);
371 $dbr = wfGetDB(DB_MASTER);
375 'page_id' => $page_id,
376 'page_title' => $page_title,
377 'date_report' => $date_report,
378 // 'date_entry' => '', // use database default
379 // 'date_invalid' => '', // use database default
380 'condition' => $condition,
381 'description' => $description,
382 'author_name' => $author_name,
383 'author_ip' => $_SERVER['REMOTE_ADDR'],
384 'author_userid' => $author_userid,
385 'author_username' => $author_username
390 $title->invalidateCache();
391 // Show success message
392 global $wrShowReportsSection;
393 $wgOut->addWikiText(utf8_encode('<div class="successbox">Der Bahnbericht für [[') . $page_title . '#' . $wrShowReportsSection . '|' . $page_title . utf8_encode("]] wurde erfolgreich gespeichert.</div>\n"));
394 // We could redirect to result with the following line but we don't want to.
395 // $wgOut->redirect($title->getFullURL() . '#Eintr.C3.A4ge');
397 if ($action == 'preview') {
398 $wgOut->addWikiText(utf8_encode("== Vorschau (noch nicht gespeichert) ==\n"));
399 $wgOut->addHTML("<table class=\"wrreporttable\">\n");
400 $wgOut->addHTML(wrReportTableTitleRender(FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE));
401 $wgOut->addHTML(wrReportTableRowRender(NULL, $page_title, $date_report, NULL, NULL, $condition, $description, $author_name));
402 $wgOut->addHTML("</table>");
403 $wgOut->addWikiText("\n");
404 $wgOut->addWikiText(utf8_encode("== Speichern oder Ändern ==\n"));
405 $wgOut->addHTML(wrReportFormRender(FALSE, $page_title, $date_report, $condition, $description, $author_name));
410 else die('Wrong action');