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_id = NULL, $page_title = NULL, $date_report = NULL, $condition = NULL, $description = NULL, $author_name = NULL, $page_title_list = NULL) {
64 if ($page_id) $page_id = (int) $page_id;
65 if ($page_title) $page_title = htmlspecialchars($page_title);
67 $tagnamen = array('Heute', 'Gestern', 'Vorgestern', 'Vor 3 Tagen', 'Vor 4 Tagen');
69 $date_selected = FALSE;
70 for ($tag=0; $tag!=5; ++$tag) {
71 $datum = time() - $tag*24*60*60;
72 $datum_f = strftime("%Y-%m-%d", $datum);
73 $date_options .= '<option value="' . $datum_f . '"';
74 // var_dump($date_report);
75 // var_dump($datum_f);
76 if ((is_null($date_report) && $tag == 0) || (!is_null($date_report) && $date_report == $datum_f)) {
77 $date_options .= ' selected="selected"';
78 $date_selected = TRUE;
80 $date_options .= '>' . htmlspecialchars($tagnamen[$tag]) . ' (' . strftime('%d.%m.', $datum) . ')</option>' . "\n";
82 if (!$date_selected) {
83 $date_options = '<option value="' . $date_report . '" selected="selected">' . htmlspecialchars($date_report) . "</option>\n" . $date_options;
87 $condition_options = '';
88 $condition_array = array(1 => 'Sehr gut', 2 => 'Gut', 3 => 'Mittelmäßig', 4 => 'Schlecht', 5 => 'Geht nicht');
89 $condition_options .= '<option value="">(keine Beurteilung)</option>' . "\n";
90 foreach ($condition_array as $condition_num => $condition_text) {
91 $condition_options .= "<option value=\"$condition_num\"";
92 if ($condition == $condition_num) $condition_options .= ' selected="selected"';
93 $condition_options .= '>' . htmlspecialchars(utf8_encode($condition_text)) . "</option>\n";
95 $description = htmlspecialchars($description);
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>';
104 <form action="/wiki/Spezial:Bahnberichte" method="post">
105 <table class="wrreportform" summary="Formular zum Eintragen eines Rodelbahnberichtes">
106 <tr class="oddrow"><th>Rodelbahn</th><td>$page_title<input type="hidden" name="page_id" value="$page_id"/><input type="hidden" name="page_title" value="$page_title"/></td></tr>
107 <tr class="evenrow"><th>Datum des Rodelns</th>
109 <select name="date_report">
114 <tr class="oddrow"><th>Bahnzustand*</th><td><select name="condition">
117 <tr class="evenrow"><th>Meinung</th><td><textarea name="description" cols="50" rows="7">$description</textarea></td></tr>
118 <tr class="oddrow"><th>Autor/in</th><td><input name="author_name" maxlength="30" size="30" value="$author_name" /></td></tr>
119 <tr class="evenrow"><th>Bericht abschicken</th><td>$buttons</td></tr>
128 /// \brief sub-function of wrRenderReportTable
129 /// \return UTF-8 encoded titles of HTML table
130 function wrReportTableTitleRender($page_id, $page_title, $date_report, $date_entry, $date_invalid, $condition, $description, $author_name) {
132 if ($page_id) $out .= '<th>ID</th>';
133 if ($page_title) $out .= '<th>Bahn</th>';
134 if ($date_report) $out .= '<th>Datum</th>';
135 if ($date_entry) $out .= '<th>Datum Eintrag</th>';
136 if ($date_invalid) $out .= '<th>Datum Ungültig</th>';
137 if ($condition) $out .= '<th>Zustand</th>';
138 if ($description) $out .= '<th>Beschreibung</th>';
139 if ($author_name) $out .= '<th>Autor</th>';
140 return utf8_encode($out . "</tr>\n");
144 /// sub-function of wrRenderReportTable
145 /// The in- and output parameters are expected to be UTF-8 encoded
146 function wrReportTableRowRender($page_id, $page_title, $date_report, $date_entry, $date_invalid, $condition, $description, $author_name) {
148 if (!is_null($page_id)) $out .= '<td>' . $page_id . '</td>';
149 if (!is_null($page_title)) $out .= '<td>' . wrReportSandboxParse('[[' . $page_title . ']]') . '</td>';
150 $out .= '<td>' . $date_report . '</td>';
151 if (!is_null($date_entry)) $out .= '<td>' . date('Y-m-d, H:i', strtotime($date_entry)) . '</td>';
152 if (!is_null($date_invalid)) $out .= '<td>' . date('Y-m-d, H:i', strtotime($date_invalid)) . '</td>';
153 $out .= '<td>' . $condition . '</td>';
154 $out .= '<td>' . htmlspecialchars($description) . '</td>';
155 $out .= '<td>' . htmlspecialchars($author_name) . '</td>';
161 /// \brief It returns a table (string containing HTML) showing the results.
163 /// Is called when the tag <bahnberichte/> is encounted and renders a table /
164 /// \param $page_title If the name is specified (UTF-8 encoded), only reports of this "page" (e.g. 'Birgitzer Alm') is shown. Default: NULL
165 /// \return UTF-8 encoded HTML result table
166 function wrReportTableRender($page_title = NULL) {
168 $dbr = wfGetDB(DB_SLAVE);
169 $conditions = array();
170 if ($page_title) $conditions['page_title'] = $page_title;
171 $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'));
172 $admin = is_null($page_title);
173 if ($res->numRows() > 0) {
174 $out .= "<table class=\"wrreporttable\">\n";
175 $out .= wrReportTableTitleRender($admin, $admin, TRUE, $admin, $admin, TRUE, TRUE, TRUE);
176 while ($row = $dbr->fetchObject($res)) {
177 $author = $row->author_name;
178 if ($row->author_username) $author .= ' (' . $row->author_username . ')';
179 $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);
181 $out .= "</table>\n";
183 $out .= "<p>Es wurden keine Bahnberichte in der Datenbank gefunden.</p>\n";
185 $dbr->freeResult($res);
190 /// \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);
192 /// If no condition is present, array(NULL, NULL) is returned
193 function wrReportConditionRender($page_title) {
194 $dbr = wfGetDB(DB_SLAVE);
195 $res = $dbr->select('wrreport', array('max(wrreport.id) as max'), array('page_title' => $page_title, 'condition is not null', 'date_invalid > now()'));
196 // 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);
197 if ($res->numRows() <= 0) return array(NULL, NULL);
198 $row = $dbr->fetchObject($res);
199 $res = $dbr->select('wrreport', array('condition', 'date_report'), array('id' => $row->max));
200 if ($res->numRows() <= 0) return array(NULL, NULL);
201 $row = $dbr->fetchObject($res);
202 return array($row->condition, $row->date_report);
206 // Parser Hook Functions
207 // ---------------------
209 /// \brief Is called when the tag <bahnberichtformular/> is encountered.
211 /// The current page name is taken.
212 function bahnberichtformularParserHook($input, $args, $parser) {
213 return replaceByMarker(wrReportFormRender(TRUE, $parser->getTitle()->getArticleID(), $parser->getTitle()->getText()));
217 /// \brief Is called when the tag <bahnberichte/> is encountered.
219 /// The current page name is taken.
220 function bahnberichteParserHook($input, $args, &$parser) {
221 return wrReportTableRender($parser->getTitle()->getText());
225 /// \brief Is called when the tag <bahnbewertung/> is encountered.
227 /// * <bahnbewertung/>: The current page name is taken.
228 /// * <bahnbewertung>page_name</bahnbewertung>: The given page name is taken.
229 function bahnbewertungParserHook($input, $args, &$parser) {
230 if (!$input) $input = $parser->getTitle()->getText();
231 list($condition, $date) = wrReportConditionRender($input);
232 return "$condition ($date)";
240 /// Specal Page to show reports
241 class WrReport extends SpecialPage {
242 function WrReport() {
243 SpecialPage::SpecialPage('WrReport');
244 wfLoadExtensionMessages('WrReport');
248 /// Possibilities for $par:
249 /// - action == 'view' (default)
250 /// - action == 'preview': Preview new report
251 /// - action == 'store': Store new report
252 /// - action == 'success': Show a success message
253 function execute($par) {
254 global $wgRequest, $wgOut;
258 # Get request data from, e.g.
259 $action = $wgRequest->getText('action');
260 if ($action === '') $action = 'view';
262 if ($action == 'view') {
263 $wgOut->addHTML(wrRenderReportTable());
266 elseif ($action == 'preview' || $action == 'store') {
267 $page_id = $wgRequest->getText('page_id');
268 $page_title = $wgRequest->getText('page_title');
269 $date_report = $wgRequest->getText('date_report');
270 $condition = $wgRequest->getText('condition');
271 $description = $wgRequest->getText('description');
272 $author_name = $wgRequest->getText('author_name');
273 if ($action == 'store') {
274 // TODO: check conditions/permissions
275 $dbr = wfGetDB(DB_MASTER);
279 'page_id' => (int) $page_id,
280 'page_title' => $page_title,
281 'date_report' => $date_report,
282 // 'date_entry' => '', // use database default
283 // 'date_invalid' => '', // use database default
284 'condition' => $condition,
285 'description' => $description,
286 'author_name' => $author_name,
287 'author_username' => '',
288 'author_ip' => $_SERVER['REMOTE_ADDR']
291 $wgOut->addHTML('<p>Bahnbericht gespeichert! :-)</p>');
293 if ($action == 'preview') {
294 $wgOut->addHTML("<h3>Vorschau (noch nicht gespeichert)</h3>\n");
295 $wgOut->addHTML("<table class=\"wrreporttable\">\n");
296 $wgOut->addHTML(wrReportTableTitleRender(FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE));
297 $wgOut->addHTML(wrReportTableRowRender(NULL, $page_title, $date_report, NULL, NULL, $condition, $description, $author_name));
298 $wgOut->addHTML("</table>\n\n\n");
299 $wgOut->addHTML(utf8_encode("<h3>Speichern oder Ändern</h3>\n"));
300 $wgOut->addHTML(wrReportFormRender(FALSE, $page_id, $page_title, $date_report, $condition, $description, $author_name));
305 else die('Wrong action');