EOT;
return $form;
}
/// \brief sub-function of wrReportTableRender - renders the table head
///
/// The input variables are either NULL (-> the column is not present) or a UTF-8 encoded string (that is ignored ;-) ).
/// \return UTF-8 encoded titles of HTML table
/// \todo: Delete
function wrReportTableTitleRender($page_id, $page_title, $date_report, $date_entry, $date_invalid, $condition, $description, $author_name, $delete = NULL) {
$out = '';
if ($page_id) $out .= 'ID | ';
if ($page_title) $out .= 'Bahn | ';
if ($date_report) $out .= 'Datum | ';
if ($date_entry) $out .= 'Datum Eintrag | ';
if ($date_invalid) $out .= 'Datum Ungültig | ';
if ($condition) $out .= 'Zustand | ';
if ($description) $out .= 'Beschreibung | ';
if ($author_name) $out .= 'Autor | ';
if ($delete) $out .= 'Aktion | ';
return utf8_encode($out . "
\n");
}
/// \brief sub-function of wrReportTableRender - renders a table row
///
/// * The in- and output parameters are expected to be UTF-8 encoded.
/// * If one parameter is NULL, the corresponding column is omitted.
/// \param $page_id
/// \param $page_title
/// \param $date_report
/// \param $date_entry
/// \param $date_invalid
/// \param $condition
/// \param $description
/// \param $author_name
/// \param $delete
/// \todo delete
function wrReportTableRowRender($page_id, $page_title, $date_report, $date_entry, $date_invalid, $condition, $description, $author_name, $delete = NULL) {
$out = "";
if (!is_null($page_id)) $out .= '' . $page_id . ' | ';
if (!is_null($page_title)) $out .= '' . wrReportSandboxParse('[[' . $page_title . ']]') . ' | ';
$dayOfWeek = array('Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So');
$date_report = strtotime($date_report);
$date_report = $dayOfWeek[strftime('%u', $date_report)-1] . strftime(', %d.%m.', $date_report);
$out .= '' . $date_report . ' | ';
if (!is_null($date_entry)) $out .= '' . date('Y-m-d, H:i', strtotime($date_entry)) . ' | ';
if (!is_null($date_invalid)) $out .= '' . date('Y-m-d, H:i', strtotime($date_invalid)) . ' | ';
global $wrConditions;
$condition_text = '---';
if (isset($wrConditions[$condition])) $condition_text = $wrConditions[$condition];
$out .= '' . htmlspecialchars($condition_text) . ' | ';
$out .= '' . wrReportSandboxParse($description) . ' | ';
$out .= '' . htmlspecialchars($author_name) . ' | ';
if (!is_null($delete)) $out .= '(Löschen) | ';
$out .= "
\n";
return $out;
}
/// \brief It returns a table (string containing HTML) showing the results.
///
/// Is called when the tag is encounted and renders a table /
/// \param $page_title If the name is specified (UTF-8 encoded), only reports of this "page" (e.g. 'Birgitzer Alm') is shown. Default: NULL
/// \return UTF-8 encoded HTML result table
/// \todo delte
function wrReportTableRender($page_title = NULL) {
$out = '';
$dbr = wfGetDB(DB_SLAVE);
$conditions = array();
if ($page_title) $conditions['page_title'] = $page_title;
$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'));
$admin = is_null($page_title);
if ($res->numRows() > 0) {
$out .= "\n";
$out .= wrReportTableTitleRender($admin, $admin, TRUE, $admin, $admin, TRUE, TRUE, TRUE);
while ($row = $dbr->fetchObject($res)) {
$author = $row->author_name;
$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);
}
$out .= "
\n";
} else {
$out .= wrReportSandboxParse("''Es wurden keine Bahnberichte in der Datenbank gefunden.''\n\n");
}
$dbr->freeResult($res);
return $out;
}
// Constants for wrReportTableRender2
define(WRREPORT_COMPACT_PAGE, 1); ///< includes the page name
define(WRREPORT_COMPACT, 2); ///< shown on a single page
define(WRREPORT_DETAIL, 3); ///< more columns
/// \brief Renders a table header ("private" sub-function of wrReportTableRender2)
///
/// \param $format row format like WRREPORT_COMPACT
/// \param $showActions boolean to indicate whether an actions column should be created
/// \return UTF-8 encoded titles of HTML table
function wrReportTableTitleRender2($format, $showActions) {
$out = '';
if ($format == WRREPORT_DETAIL) $out .= 'ID | ';
if ($format != WRREPORT_COMPACT) $out .= 'Bahn | ';
$out .= 'Datum | ';
if ($format == WRREPORT_DETAIL) $out .= 'Datum Eintrag | ';
if ($format == WRREPORT_DETAIL) $out .= 'Datum Ungültig | ';
$out .= 'Zustand | ';
$out .= 'Beschreibung | ';
$out .= 'Autor | ';
if ($showActions) $out .= 'Aktion | ';
return utf8_encode($out . "
\n");
}
/// \brief Renders a table row ("private" sub-function of wrReportTableRender2)
///
/// \param $row associative array of table columns like one row in the wrreport table
/// \param $format row format like WRREPORT_COMPACT
/// \param $showActions boolean to indicate whether an actions column should be created
/// \return UTF-8 encoded titles of HTML table
function wrReportTableRowRender2($row, $format, $showActions) {
extract($row);
$out = '';
// $id
if ($format == WRREPORT_DETAIL) $out .= '' . $id . ' | ';
// $page_title
if ($format != WRREPORT_COMPACT) $out .= '' . wrReportSandboxParse('[[' . $page_title . ']]') . ' | ';
// $date_report
$dayOfWeek = array('Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So');
$date_report = strtotime($date_report);
$date_report = $dayOfWeek[strftime('%u', $date_report)-1] . strftime(', %d.%m.', $date_report);
$out .= '' . $date_report . ' | ';
// $date_entry
if ($format == WRREPORT_DETAIL) $out .= '' . date('Y-m-d, H:i', strtotime($date_entry)) . ' | ';
// $date_invalid
if ($format == WRREPORT_DETAIL) $out .= '' . date('Y-m-d, H:i', strtotime($date_invalid)) . ' | ';
// $condition
global $wrConditions;
$condition_text = '---';
if (isset($wrConditions[$condition])) $condition_text = $wrConditions[$condition];
$out .= '';
if ($delete_date) $out .= utf8_encode('Gelöscht');
else $out .= htmlspecialchars($condition_text);
$out .= ' | ';
// $description
$out .= '';
if ($delete_date) $out .= utf8_encode('Gelöscht');
else $out .= wrReportSandboxParse($description);
$out .= ' | ';
// $author_name
$out .= '';
if ($delete_date) $out .= utf8_encode('Gelöscht');
else $out .= htmlspecialchars($author_name);
$out .= ' | ';
// actions
if ($showActions) $out .= '' . utf8_encode('(Löschen)') . ' | ';
return $out . "
\n";
}
/// \brief Renders the report table. Call wrReportGetReports for the $rows parameter.
///
/// \param $rows array of associative row arrays
/// \param $format row format like WRREPORT_TABLE_SHORT
function wrReportTableRender2($rows, $format, $showActions) {
$out = "\n" . wrReportTableTitleRender2($format, $showActions);
foreach ($rows as $key => $row) $out .= wrReportTableRowRender2($row, $format, $showActions);
return $out . "
\n";
}
/// Returns an array with column names
function wrReportGetColumnNames() {
return array('id', 'page_id', 'page_title', 'date_report', 'date_entry', 'date_invalid', 'condition', 'description', 'author_name', 'author_username', 'delete_date', 'delete_person_name', 'delete_person_ip', 'delete_person_userid', 'delete_person_username', 'delete_reason_public', 'delete_invisible');
}
/// \brief Returns reports as associative array.
///
/// Examples:
/// $conditions = array('page_title' => 'Birgitzer Alm', 'date_invalid > now()');
/// $order = 'date_report desc, date_entry desc';
function wrReportGetReports($conditions, $order) {
$dbr = wfGetDB(DB_SLAVE);
$res = $dbr->select('wrreport', wrReportGetColumnNames(), $conditions, $fname = 'Database::select', $options = array('ORDER BY' => 'date_report desc, date_entry desc'));
$result = array();
while ($row = $dbr->fetchRow($res)) $result[] = $row;
$dbr->freeResult($res);
return $result;
}
/// \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);
///
/// If no condition is present, array(NULL, NULL) is returned
function wrReportConditionRender($page_title) {
$dbr = wfGetDB(DB_SLAVE);
$res = $dbr->select('wrreport', array('max(wrreport.id) as max'), array('page_title' => $page_title, 'condition is not null', 'date_invalid > now()'));
// 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);
if ($res->numRows() <= 0) return array(NULL, NULL);
$row = $dbr->fetchObject($res);
$res = $dbr->select('wrreport', array('condition', 'date_report'), array('id' => $row->max));
if ($res->numRows() <= 0) return array(NULL, NULL);
$row = $dbr->fetchObject($res);
$date = $row->date_report;
if ($date) $date = strtotime($date);
return array($row->condition, $date);
}
// Parser Hook Functions
// ---------------------
/// \brief Is called when the tag is encountered.
///
/// The current page name is taken.
function bahnberichtformularParserHook($input, $args, $parser) {
// Username
global $wgUser;
$author_name = NULL;
if ($wgUser->isLoggedIn()) {
$author_name = $wgUser->getRealName();
if (!$author_name) $author_name = $wgUser->getName();
}
global $wgWrReportMode;
global $wgWrReportBlackListAll;
global $wgWrReportBlackListStrangers;
if ($wgWrReportMode == 'summer') return wrReportSandboxParse(utf8_encode("''An dieser Stelle kann während des Winters die Schneelage von Rodelbahnen eingetragen werden.''\n\n"));
if ($wgWrReportMode == 'deny') return wrReportSandboxParse(utf8_encode("''Rodelbahnberichte sind derzeit leider nicht erlaubt.''\n\n"));
// The following was changed because for testing purposes, we have 'loggedin' instead of 'summer' in the $wgWrReportMode. TODO: Change back!
// if ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) return wrReportSandboxParse(utf8_encode("''Derzeit sind Rodelbahnberichte nur für angemeldete Benutzer erlaubt.''\n\n"));
if ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) return wrReportSandboxParse(utf8_encode("''An dieser Stelle kann während des Winters die Schneelage von Rodelbahnen eingetragen werden.''\n\n"));
if (in_array($parser->getTitle()->getText(), $wgWrReportBlackListAll)) return wrReportSandboxParse(utf8_encode("''Bei dieser Rodelbahn dürfen derzeit leider keine Rodelbahnberichte abgegeben werden.''\n"));
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"));
return replaceByMarker(wrReportFormRender(TRUE, $parser->getTitle()->getText(), NULL, NULL, NULL, $author_name));
}
/// \brief Is called when the tag is encountered.
///
/// The current page name is taken.
function bahnberichteParserHook($input, $args, &$parser) {
return wrReportTableRender($parser->getTitle()->getText()); // TODO: Replace
}
/// \brief Is called when the tag is encountered.
///
/// * : The current page name is taken.
/// * page_name: The given page name is taken.
function bahnbewertungParserHook($input, $args, &$parser) {
$titleText = $input;
if (!$titleText) $titleText = $parser->getTitle()->getText();
list($condition, $date) = wrReportConditionRender($titleText);
if ($date) $date = strftime('%d.%m.', $date);
global $wrConditions;
global $wrNewReportSection; // = utf8_encode('Eintragen');
global $wrShowReportsSection; // = utf8_encode('Einträge');
global $wgUser;
global $wgWrReportMode; // e.g. 'summer'
global $wgWrReportBlackListAll;
global $wgWrReportBlackListStrangers;
// Determine, whether the user is allowed to make a new report
$userMayReport = ($wgWrReportMode == 'allow' || ($wgWrReportMode == 'loggedin' && $wgUser->isLoggedIn()));
if ($userMayReport) {
if (in_array($titleText, $wgWrReportBlackListAll)) $userMayReport = false;
if (!$wgUser->isLoggedIn() && in_array($titleText, $wgWrReportBlackListStrangers)) $userMayReport = false;
}
// Get the condition and create the response wiki text
if (isset($wrConditions[$condition])) {
$wikiText = '[['. $titleText . '#' . $wrShowReportsSection . '|'. $wrConditions[$condition] . "]] $date";
if ($userMayReport) $wikiText .= " ''[[" . $titleText . '#' . $wrNewReportSection . "|Neu]]''";
$wikiText .= "";
} else {
if ($userMayReport) $wikiText = "''[[" . $titleText . '#' . $wrNewReportSection . "|Bitte eintragen]]''";
else $wikiText = '--';
}
return wrReportSandboxParse($wikiText);
}
// Special page
// ------------
/// Specal Page to show reports
class WrReport extends SpecialPage {
function WrReport() {
SpecialPage::SpecialPage('WrReport');
wfLoadExtensionMessages('WrReport');
}
/// \param $par Possibilities:
/// - action == 'view' (default)
/// - action == 'preview': Preview new report
/// - action == 'store': Store new report
/// - action == 'deletepreview': Preview the deleted record
/// - action == 'delete': Delete an existing report
/// - action == 'showerror': Shows the error and exits
/// \param $override_action If not NULL (default), it overrides the action in $par
/// \param $errorMsg UFT-8 encoded error message (in WikiText) to show on top of the page or NULL (default):
function execute($par, $override_action = NULL, $errorMsg = NULL) {
global $wgRequest, $wgOut;
$this->setHeaders();
// Get request data
$action = $wgRequest->getText('action');
if (!$action) {
if ($wgRequest->getVal('preview')) $action = 'preview';
elseif ($wgRequest->getVal('store')) $action = 'store';
elseif ($wgRequest->getVal('deletepreview')) $action = 'deletepreview';
elseif ($wgRequest->getVal('delete')) $action = 'delete';
else $action = 'view';
}
if ($override_action) $action = $override_action;
// Show error message
if ($errorMsg || $action == 'showerror') {
$wgOut->addWikiText('' . $errorMsg . "
\n");
if ($action == 'showerror') return;
}
// Action view
if ($action == 'view') {
$wgOut->addHTML(wrReportTableRender()); // TODO: Replace
}
elseif ($action == 'test') { // TODO: delete
$conditions = array('page_title' => 'Axamer Lizum', 'date_invalid > now()');
$order = 'date_report desc, date_entry desc';
$rows = wrReportGetReports($conditions, $order);
$format = WRREPORT_COMPACT_PAGE; // WRREPORT_COMPACT WRREPORT_DETAIL
$format = WRREPORT_DETAIL;
$wgOut->addHTML(wrReportTableRender2($rows, $format, True));
$wgOut->addHTML('OK');
}
// Action deletepreview or delete
elseif ($action == 'deletepreview' || $action == 'delete') {
$reportid = (int) $wgRequest->getText('reportid');
if ($reportid == 0) {
$this->execute($par, 'showerror', utf8_encode('Es wurde kein Bericht zum Löschen ausgewählt.'));
return;
}
$rows = wrReportGetReports(array('id' => $reportid), '');
if (count($rows) != 1) {
$this->execute($par, 'showerror', utf8_encode('Es wurde ein ungültiger Bericht zum Löschen ausgewählt.'));
return;
}
$row = $rows[0];
if (!is_null($row['delete_date'])) {
$this->execute($par, 'showerror', utf8_encode('Der angegebende Bericht ist bereits gelöscht.'));
return;
}
// $row = array_fill_keys(wrReportGetColumnNames(), NULL); // TODO: Delete
$delete_reason_public = $wgRequest->getText('delete_reason_public');
$delete_person_name = $wgRequest->getText('delete_person_name');
$delete_invisible = $wgRequest->getText('delete_invisible') ? TRUE : FALSE;
if ($action == 'delete') {
// page
$title = Title::newFromId($row['page_id']);
// user
global $wgUser;
$delete_person_userid = $wgUser->getId();
if ($delete_person_userid == 0) $delete_person_userid = NULL; // to store a NULL value in the database if no user is logged in instead of 0.
$delete_person_username = $wgUser->getName();
// Check permissions
$errorMsg = NULL;
global $wgWrReportDeleteMode;
if ($wgWrReportDeleteMode == 'deny') $errorMsg = utf8_encode('Das Löschen von Rodelbahnberichten ist derzeit leider nicht erlaubt.');
elseif ($wgWrReportDeleteMode == 'loggedin' && !$wgUser->isLoggedIn()) $errorMsg = utf8_encode('Derzeit dürfen nur angemeldete Benutzer Rodelbahnberichte löschen.');
elseif (!$delete_person_name && !$delete_reason_public) $errorMsg = utf8_encode('Es müssen sowohl die Begründung als auch der Name angegeben werden.');
if ($errorMsg) {
$this->execute($par, 'preview', $errorMsg);
return;
}
// "Delete" (update) entry
$dbr = wfGetDB(DB_MASTER);
$dbr->update(
'wrreport',
array(
'delete_date' => date('c'),
'delete_person_name' => $delete_person_name,
'delete_person_ip' => $_SERVER['REMOTE_ADDR'],
'delete_person_userid' => $delete_person_userid,
'delete_person_username' => $delete_person_username,
'delete_reason_public' => $delete_reason_public,
'delete_invisible' => $delete_invisible ? 't' : 'f'
),
array('id' => $reportid)
);
// Purge cache
$title->invalidateCache();
// Show success message
global $wrShowReportsSection;
$wgOut->addWikiText(utf8_encode('Der Bahnbericht für [[') . $row['page_title'] . '#' . $wrShowReportsSection . '|' . $row['page_title'] . utf8_encode("]] wurde erfolgreich gelöscht.
\n"));
}
if ($action == 'deletepreview') {
$wgOut->addWikiText(utf8_encode("== Schneelagebericht, um den es beim Löschen geht ==\n"));
$format = WRREPORT_COMPACT_PAGE;
$wgOut->addHTML(wrReportTableRender2(array($row), $format, FALSE));
$wgOut->addWikiText(utf8_encode("\n\n== Vorschau (noch nicht gelöscht) ==\n"));
$row['delete_date'] = date('c');
$row['delete_reason_public'] = $delete_reason_public;
$row['delete_person_name'] = $delete_person_name;
$row['delete_invisible'] = $delete_invisible;
$wgOut->addHTML("\n");
$wgOut->addHTML(wrReportTableRender2(array($row), $format, FALSE));
$wgOut->addHTML("
");
$wgOut->addWikiText("\n");
$wgOut->addWikiText(utf8_encode("== Löschen ==\n"));
$wgOut->addWikiText(utf8_encode("Die Begründung und der Name scheinen nicht in den normalen Listen auf, allerdings dienen sie den Administratoren dazu, sich schnell einen Überblick zu verschaffen, wer was warum gelöscht hat.\n"));
$wgOut->addHTML(wrDeleteReportFormRender($reportid, $delete_person_name, $delete_reason_public, $delete_invisible));
}
}
// Action preview or store
elseif ($action == 'preview' || $action == 'store') {
$page_title = $wgRequest->getText('page_title');
$date_report = $wgRequest->getText('date_report');
$condition = $wgRequest->getText('condition');
$description = $wgRequest->getText('description');
$author_name = $wgRequest->getText('author_name');
if ($action == 'store') {
// page_id
$title = Title::newFromText($page_title);
$page_id = $title->getArticleId();
if ($page_id == 0) $page_id = NULL;
// user_id
global $wgUser;
$author_userid = $wgUser->getId();
if ($author_userid == 0) $author_userid = NULL; // to store a NULL value in the database if no user is logged in instead of 0.
$author_username = $wgUser->getName();
// condition
$condition_int = (int) $condition;
if ($condition_int >= 1 and $condition_int <= 5) $condition = $condition_int;
else $condition = NULL;
// check conditions/permissions
$errorMsg = NULL;
global $wgWrReportMode;
global $wgWrReportBlackListAll;
global $wgWrReportBlackListStrangers;
if ($wgWrReportMode == 'summer') $errorMsg = utf8_encode('Rodelbahnberichte sind in der schneefreien Zeit nicht erlaubt.');
elseif ($wgWrReportMode == 'deny') $errorMsg = utf8_encode('Rodelbahnberichte sind derzeit leider nicht erlaubt.');
elseif ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) $errorMsg = utf8_encode('Derzeit sind Rodelbahnberichte nur für angemeldete Benutzer erlaubt.');
elseif (!$page_id) $errorMsg = utf8_encode('Die angegebene Seite wurde nicht gefunden.');
elseif (in_array($page_title, $wgWrReportBlackListAll)) $errorMsg = utf8_encode('Bei der angegebenen Rodelbahn dürfen derzeit keine Rodelbahnberichte abgegeben werden.');
elseif (!$wgUser->isLoggedIn() && in_array($page_title, $wgWrReportBlackListStrangers)) $errorMsg = utf8_encode('Bei der angegebenen Rodelbahn dürfen derzeit nur angemeldete Benutzer Rodelbahnberichte abgebe.');
elseif (!$condition && !$description) $errorMsg = utf8_encode('Es sind sowohl die Beschreibung als auch die Bewertung leer/nicht vergeben.');
elseif (!$wgUser->isLoggedIn()) {
if (!$description) $errorMsg = utf8_encode('Bitte bei der Beschreibung eine kurze Begründung für die Bewertung abgeben.');
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.');
}
if ($errorMsg) {
$this->execute($par, 'preview', $errorMsg);
return;
}
// Save entry
$dbr = wfGetDB(DB_MASTER);
$dbr->insert(
'wrreport',
array(
'page_id' => $page_id,
'page_title' => $page_title,
'date_report' => $date_report,
// 'date_entry' => '', // use database default
// 'date_invalid' => '', // use database default
'condition' => $condition,
'description' => $description,
'author_name' => $author_name,
'author_ip' => $_SERVER['REMOTE_ADDR'],
'author_userid' => $author_userid,
'author_username' => $author_username
)
);
// Purge cache
$title->invalidateCache();
// Show success message
global $wrShowReportsSection;
$wgOut->addWikiText(utf8_encode('Der Bahnbericht für [[') . $page_title . '#' . $wrShowReportsSection . '|' . $page_title . utf8_encode("]] wurde erfolgreich gespeichert.
\n"));
// We could redirect to result with the following line but we don't want to.
// $wgOut->redirect($title->getFullURL() . '#Eintr.C3.A4ge');
}
if ($action == 'preview') {
$wgOut->addWikiText(utf8_encode("== Vorschau (noch nicht gespeichert) ==\n"));
$wgOut->addHTML("\n");
$wgOut->addHTML(wrReportTableTitleRender(FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE));
$wgOut->addHTML(wrReportTableRowRender(NULL, $page_title, $date_report, NULL, NULL, $condition, $description, $author_name));
$wgOut->addHTML("
");
$wgOut->addWikiText("\n");
$wgOut->addWikiText(utf8_encode("== Speichern oder Ändern ==\n"));
$wgOut->addHTML(wrReportFormRender(FALSE, $page_title, $date_report, $condition, $description, $author_name));
}
}
else die('Wrong action');
}
}
?>