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 = 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</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 wrRenderReportTable
128 /// \return UTF-8 encoded titles of HTML table
129 function wrReportTableTitleRender($page_id, $page_title, $date_report, $date_entry, $date_invalid, $condition, $description, $author_name) {
131 if ($page_id) $out .= '<th>ID</th>';
132 if ($page_title) $out .= '<th>Bahn</th>';
133 if ($date_report) $out .= '<th>Datum</th>';
134 if ($date_entry) $out .= '<th>Datum Eintrag</th>';
135 if ($date_invalid) $out .= '<th>Datum Ungültig</th>';
136 if ($condition) $out .= '<th>Zustand</th>';
137 if ($description) $out .= '<th>Beschreibung</th>';
138 if ($author_name) $out .= '<th>Autor</th>';
139 return utf8_encode($out . "</tr>\n");
143 /// sub-function of wrRenderReportTable
144 /// The in- and output parameters are expected to be UTF-8 encoded
145 function wrReportTableRowRender($page_id, $page_title, $date_report, $date_entry, $date_invalid, $condition, $description, $author_name) {
147 if (!is_null($page_id)) $out .= '<td>' . $page_id . '</td>';
148 if (!is_null($page_title)) $out .= '<td>' . wrReportSandboxParse('[[' . $page_title . ']]') . '</td>';
149 $out .= '<td>' . $date_report . '</td>';
150 if (!is_null($date_entry)) $out .= '<td>' . date('Y-m-d, H:i', strtotime($date_entry)) . '</td>';
151 if (!is_null($date_invalid)) $out .= '<td>' . date('Y-m-d, H:i', strtotime($date_invalid)) . '</td>';
152 $out .= '<td>' . $condition . '</td>';
153 $out .= '<td>' . htmlspecialchars($description) . '</td>';
154 $out .= '<td>' . htmlspecialchars($author_name) . '</td>';
160 /// \brief It returns a table (string containing HTML) showing the results.
162 /// Is called when the tag <bahnberichte/> is encounted and renders a table /
163 /// \param $page_title If the name is specified (UTF-8 encoded), only reports of this "page" (e.g. 'Birgitzer Alm') is shown. Default: NULL
164 /// \return UTF-8 encoded HTML result table
165 function wrReportTableRender($page_title = NULL) {
167 $dbr = wfGetDB(DB_SLAVE);
168 $conditions = array();
169 if ($page_title) $conditions['page_title'] = $page_title;
170 $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'));
171 $admin = is_null($page_title);
172 if ($res->numRows() > 0) {
173 $out .= "<table class=\"wrreporttable\">\n";
174 $out .= wrReportTableTitleRender($admin, $admin, TRUE, $admin, $admin, TRUE, TRUE, TRUE);
175 while ($row = $dbr->fetchObject($res)) {
176 $author = $row->author_name;
177 $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);
179 $out .= "</table>\n";
181 $out .= "<p>Es wurden keine Bahnberichte in der Datenbank gefunden.</p>\n";
183 $dbr->freeResult($res);
188 /// \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);
190 /// If no condition is present, array(NULL, NULL) is returned
191 function wrReportConditionRender($page_title) {
192 $dbr = wfGetDB(DB_SLAVE);
193 $res = $dbr->select('wrreport', array('max(wrreport.id) as max'), array('page_title' => $page_title, 'condition is not null', 'date_invalid > now()'));
194 // 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);
195 if ($res->numRows() <= 0) return array(NULL, NULL);
196 $row = $dbr->fetchObject($res);
197 $res = $dbr->select('wrreport', array('condition', 'date_report'), array('id' => $row->max));
198 if ($res->numRows() <= 0) return array(NULL, NULL);
199 $row = $dbr->fetchObject($res);
200 return array($row->condition, $row->date_report);
204 // Parser Hook Functions
205 // ---------------------
207 /// \brief Is called when the tag <bahnberichtformular/> is encountered.
209 /// The current page name is taken.
210 function bahnberichtformularParserHook($input, $args, $parser) {
213 if ($wgUser->isLoggedIn()) {
214 $author_name = $wgUser->getRealName();
215 if (!$author_name) $author_name = $wgUser->getName();
217 return replaceByMarker(wrReportFormRender(TRUE, $parser->getTitle()->getText(), NULL, NULL, NULL, $author_name));
221 /// \brief Is called when the tag <bahnberichte/> is encountered.
223 /// The current page name is taken.
224 function bahnberichteParserHook($input, $args, &$parser) {
225 return wrReportTableRender($parser->getTitle()->getText());
229 /// \brief Is called when the tag <bahnbewertung/> is encountered.
231 /// * <bahnbewertung/>: The current page name is taken.
232 /// * <bahnbewertung>page_name</bahnbewertung>: The given page name is taken.
233 function bahnbewertungParserHook($input, $args, &$parser) {
234 if (!$input) $input = $parser->getTitle()->getText();
235 list($condition, $date) = wrReportConditionRender($input);
236 return "$condition ($date)";
244 /// Specal Page to show reports
245 class WrReport extends SpecialPage {
246 function WrReport() {
247 SpecialPage::SpecialPage('WrReport');
248 wfLoadExtensionMessages('WrReport');
252 /// Possibilities for $par:
253 /// - action == 'view' (default)
254 /// - action == 'preview': Preview new report
255 /// - action == 'store': Store new report
256 /// - action == 'success': Show a success message
257 function execute($par) {
258 global $wgRequest, $wgOut;
262 # Get request data from, e.g.
263 $action = $wgRequest->getText('action');
264 if ($action === '') $action = 'view';
266 if ($action == 'view') {
267 $wgOut->addHTML(wrRenderReportTable());
270 elseif ($action == 'preview' || $action == 'store') {
271 $page_title = $wgRequest->getText('page_title');
272 $date_report = $wgRequest->getText('date_report');
273 $condition = $wgRequest->getText('condition');
274 $description = $wgRequest->getText('description');
275 $author_name = $wgRequest->getText('author_name');
276 if ($action == 'store') {
278 $title = Title::newFromText($page_title);
279 $page_id = $title->getArticleId();
283 $author_userid = $wgUser->getId();
284 $author_username = $wgUser->getName();
287 $condition_int = (int) $condition;
288 if ($condition_int >= 1 and $condition_int <= 5) $condition = $condition_int;
289 else $condition = NULL;
291 // TODO: check conditions/permissions
292 $dbr = wfGetDB(DB_MASTER);
296 'page_id' => $page_id,
297 'page_title' => $page_title,
298 'date_report' => $date_report,
299 // 'date_entry' => '', // use database default
300 // 'date_invalid' => '', // use database default
301 'condition' => $condition,
302 'description' => $description,
303 'author_name' => $author_name,
304 'author_ip' => $_SERVER['REMOTE_ADDR'],
305 'author_userid' => $author_userid,
306 'author_username' => $author_username
311 $title->invalidateCache();
312 // Show success message
313 $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>'));
314 // We could redirect to result with the following line but we don't want to.
315 // $wgOut->redirect($title->getFullURL() . '#Eintr.C3.A4ge');
317 if ($action == 'preview') {
318 $wgOut->addHTML("<h3>Vorschau (noch nicht gespeichert)</h3>\n");
319 $wgOut->addHTML("<table class=\"wrreporttable\">\n");
320 $wgOut->addHTML(wrReportTableTitleRender(FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE));
321 $wgOut->addHTML(wrReportTableRowRender(NULL, $page_title, $date_report, NULL, NULL, $condition, $description, $author_name));
322 $wgOut->addHTML("</table>\n\n\n");
323 $wgOut->addHTML(utf8_encode("<h3>Speichern oder Ändern</h3>\n"));
324 $wgOut->addHTML(wrReportFormRender(FALSE, $page_title, $date_report, $condition, $description, $author_name));
329 else die('Wrong action');