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 $condition_array = array(1 => 'Sehr gut', 2 => 'Gut', 3 => 'Mittelmäßig', 4 => 'Schlecht', 5 => 'Geht nicht');
88 $condition_options .= '<option value="">(keine Beurteilung)</option>' . "\n";
89 foreach ($condition_array 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(utf8_encode($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 $out .= '<td>' . $date_report . '</td>';
153 if (!is_null($date_entry)) $out .= '<td>' . date('Y-m-d, H:i', strtotime($date_entry)) . '</td>';
154 if (!is_null($date_invalid)) $out .= '<td>' . date('Y-m-d, H:i', strtotime($date_invalid)) . '</td>';
155 $out .= '<td>' . $condition . '</td>';
156 $out .= '<td class="wrreportdescription">' . wrReportSandboxParse($description) . '</td>';
157 $out .= '<td>' . htmlspecialchars($author_name) . '</td>';
163 /// \brief It returns a table (string containing HTML) showing the results.
165 /// Is called when the tag <bahnberichte/> is encounted and renders a table /
166 /// \param $page_title If the name is specified (UTF-8 encoded), only reports of this "page" (e.g. 'Birgitzer Alm') is shown. Default: NULL
167 /// \return UTF-8 encoded HTML result table
168 function wrReportTableRender($page_title = NULL) {
170 $dbr = wfGetDB(DB_SLAVE);
171 $conditions = array();
172 if ($page_title) $conditions['page_title'] = $page_title;
173 $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, date_entry'));
174 $admin = is_null($page_title);
175 if ($res->numRows() > 0) {
176 $out .= "<table class=\"wrreporttable\">\n";
177 $out .= wrReportTableTitleRender($admin, $admin, TRUE, $admin, $admin, TRUE, TRUE, TRUE);
178 while ($row = $dbr->fetchObject($res)) {
179 $author = $row->author_name;
180 $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);
182 $out .= "</table>\n";
184 $out .= wrReportSandboxParse("''Es wurden keine Bahnberichte in der Datenbank gefunden.''\n\n");
186 $dbr->freeResult($res);
191 /// \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);
193 /// If no condition is present, array(NULL, NULL) is returned
194 function wrReportConditionRender($page_title) {
195 $dbr = wfGetDB(DB_SLAVE);
196 $res = $dbr->select('wrreport', array('max(wrreport.id) as max'), array('page_title' => $page_title, 'condition is not null', 'date_invalid > now()'));
197 // 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);
198 if ($res->numRows() <= 0) return array(NULL, NULL);
199 $row = $dbr->fetchObject($res);
200 $res = $dbr->select('wrreport', array('condition', 'date_report'), array('id' => $row->max));
201 if ($res->numRows() <= 0) return array(NULL, NULL);
202 $row = $dbr->fetchObject($res);
203 return array($row->condition, $row->date_report);
207 // Parser Hook Functions
208 // ---------------------
210 /// \brief Is called when the tag <bahnberichtformular/> is encountered.
212 /// The current page name is taken.
213 function bahnberichtformularParserHook($input, $args, $parser) {
217 if ($wgUser->isLoggedIn()) {
218 $author_name = $wgUser->getRealName();
219 if (!$author_name) $author_name = $wgUser->getName();
222 global $wgWrReportMode;
223 global $wgWrReportBlackListAll;
224 global $wgWrReportBlackListStrangers;
225 if ($wgWrReportMode == 'summer') return wrReportSandboxParse(utf8_encode("''An dieser Stelle kann während des Winters die Schneelage von Rodelbahnen eingetragen werden.''\n\n"));
226 if ($wgWrReportMode == 'deny') return wrReportSandboxParse(utf8_encode("''Rodelbahnberichte sind derzeit leider nicht erlaubt.''\n\n"));
227 if ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) return wrReportSandboxParse(utf8_encode("''Derzeit sind Rodelbahnberichte nur für angemeldete Benutzer erlaubt.''\n\n"));
228 if (in_array($parser->getTitle()->getText(), $wgWrReportBlackListAll)) return wrReportSandboxParse(utf8_encode("''Bei dieser Rodelbahn dürfen derzeit leider keine Rodelbahnberichte abgegeben werden.''\n"));
229 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"));
230 return replaceByMarker(wrReportFormRender(TRUE, $parser->getTitle()->getText(), NULL, NULL, NULL, $author_name));
234 /// \brief Is called when the tag <bahnberichte/> is encountered.
236 /// The current page name is taken.
237 function bahnberichteParserHook($input, $args, &$parser) {
238 return wrReportTableRender($parser->getTitle()->getText());
242 /// \brief Is called when the tag <bahnbewertung/> is encountered.
244 /// * <bahnbewertung/>: The current page name is taken.
245 /// * <bahnbewertung>page_name</bahnbewertung>: The given page name is taken.
246 function bahnbewertungParserHook($input, $args, &$parser) {
247 if (!$input) $input = $parser->getTitle()->getText();
248 list($condition, $date) = wrReportConditionRender($input);
249 return "$condition ($date)";
257 /// Specal Page to show reports
258 class WrReport extends SpecialPage {
259 function WrReport() {
260 SpecialPage::SpecialPage('WrReport');
261 wfLoadExtensionMessages('WrReport');
265 /// \param $par Possibilities:
266 /// - action == 'view' (default)
267 /// - action == 'preview': Preview new report
268 /// - action == 'store': Store new report
269 /// \param $override_action If not NULL (defualt), it overrides the action in $par
270 /// \param $errorMsg UFT-8 encoded error message (in WikiText) to show on top of the page or NULL (default):
271 function execute($par, $override_action = NULL, $errorMsg = NULL) {
272 global $wgRequest, $wgOut;
277 $action = $wgRequest->getText('action');
278 if ($action === '') $action = 'view';
279 if ($override_action) $action = $override_action;
281 // Show error message
283 $wgOut->addWikiText(utf8_encode('<div class="errorbox">') . $errorMsg . utf8_encode("</div>\n"));
287 if ($action == 'view') {
288 $wgOut->addHTML(wrReportTableRender());
291 // Action preview or store
292 elseif ($action == 'preview' || $action == 'store') {
293 $page_title = $wgRequest->getText('page_title');
294 $date_report = $wgRequest->getText('date_report');
295 $condition = $wgRequest->getText('condition');
296 $description = $wgRequest->getText('description');
297 $author_name = $wgRequest->getText('author_name');
298 if ($action == 'store') {
300 $title = Title::newFromText($page_title);
301 $page_id = $title->getArticleId();
302 if ($page_id == 0) $page_id = NULL;
306 $author_userid = $wgUser->getId();
307 if ($author_userid == 0) $author_userid = NULL; // to store a NULL value in the database if no user is logged in instead of 0.
308 $author_username = $wgUser->getName();
311 $condition_int = (int) $condition;
312 if ($condition_int >= 1 and $condition_int <= 5) $condition = $condition_int;
313 else $condition = NULL;
315 // check conditions/permissions
317 global $wgWrReportMode;
318 global $wgWrReportBlackListAll;
319 global $wgWrReportBlackListStrangers;
320 if ($wgWrReportMode == 'summer') $errorMsg = utf8_encode('Rodelbahnberichte sind in der schneefreien Zeit nicht erlaubt.');
321 elseif ($wgWrReportMode == 'deny') $errorMsg = utf8_encode('Rodelbahnberichte sind derzeit leider nicht erlaubt.');
322 elseif ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) $errorMsg = utf8_encode('Derzeit sind Rodelbahnberichte nur für angemeldete Benutzer erlaubt.');
323 elseif (!$page_id) $errorMsg = utf8_encode('Die angegebene Seite wurde nicht gefunden.');
324 elseif (in_array($page_title, $wgWrReportBlackListAll)) $errorMsg = utf8_encode('Bei der angegebenen Rodelbahn dürfen derzeit keine Rodelbahnberichte abgegeben werden.');
325 elseif (!$wgUser->isLoggedIn() && in_array($page_title, $wgWrReportBlackListStrangers)) $errorMsg = utf8_encode('Bei der angegebenen Rodelbahn dürfen derzeit nur angemeldete Benutzer Rodelbahnberichte abgebe.');
326 elseif (!$condition && !$description) $errorMsg = utf8_encode('Es sind sowohl die Beschreibung als auch die Bewertung leer/nicht vergeben.');
327 elseif (!$wgUser->isLoggedIn()) {
328 if (!$description) $errorMsg = utf8_encode('Bitte bei der Beschreibung eine kurze Begründung für die Bewertung abgeben.');
329 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.');
332 $this->execute($par, 'preview', $errorMsg);
337 $dbr = wfGetDB(DB_MASTER);
341 'page_id' => $page_id,
342 'page_title' => $page_title,
343 'date_report' => $date_report,
344 // 'date_entry' => '', // use database default
345 // 'date_invalid' => '', // use database default
346 'condition' => $condition,
347 'description' => $description,
348 'author_name' => $author_name,
349 'author_ip' => $_SERVER['REMOTE_ADDR'],
350 'author_userid' => $author_userid,
351 'author_username' => $author_username
356 $title->invalidateCache();
357 // Show success message
358 $wgOut->addWikiText(utf8_encode('<div class="successbox">Der Bahnbericht für [[') . $page_title . utf8_encode('#Einträge|') . $page_title . utf8_encode("]] wurde erfolgreich gespeichert.</div>\n"));
359 // We could redirect to result with the following line but we don't want to.
360 // $wgOut->redirect($title->getFullURL() . '#Eintr.C3.A4ge');
362 if ($action == 'preview') {
363 $wgOut->addWikiText(utf8_encode("== Vorschau (noch nicht gespeichert) ==\n"));
364 $wgOut->addHTML("<table class=\"wrreporttable\">\n");
365 $wgOut->addHTML(wrReportTableTitleRender(FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE));
366 $wgOut->addHTML(wrReportTableRowRender(NULL, $page_title, $date_report, NULL, NULL, $condition, $description, $author_name));
367 $wgOut->addHTML("</table>");
368 $wgOut->addWikiText("\n");
369 $wgOut->addWikiText(utf8_encode("== Speichern oder Ändern ==\n"));
370 $wgOut->addHTML(wrReportFormRender(FALSE, $page_title, $date_report, $condition, $description, $author_name));
375 else die('Wrong action');