From: philipp Date: Tue, 18 Nov 2008 21:39:00 +0000 (+0000) Subject: On the way to close ticket #9, but many things remain to be done. X-Git-Url: https://git.toastfreeware.priv.at/philipp/winterrodeln/mediawiki_extensions/wrreport.git/commitdiff_plain/6131dca74a92c823decfc6999ceeff1f2453c1c3?ds=sidebyside On the way to close ticket #9, but many things remain to be done. git-svn-id: http://www.winterrodeln.org/svn/servermediawiki/trunk/extensions/wrreport@328 7aebc617-e5e2-0310-91dc-80fb5f6d2477 --- diff --git a/upgrade_1.0_to_1.1.sql b/upgrade_1.0_to_1.1.sql index 54fe6fc..0305e3d 100644 --- a/upgrade_1.0_to_1.1.sql +++ b/upgrade_1.0_to_1.1.sql @@ -5,6 +5,6 @@ alter table wrreport add delete_person_ip inet; alter table wrreport add delete_person_userid integer; alter table wrreport add delete_person_username varchar(30); alter table wrreport add delete_reason_public text; -alter table wrreport add delete_invisble boolean; +alter table wrreport add delete_invisible boolean; commit; diff --git a/wrreport.body.php b/wrreport.body.php index 06461cb..f818418 100644 --- a/wrreport.body.php +++ b/wrreport.body.php @@ -128,12 +128,40 @@ EOT; } +/// \brief Renders the form to delete a report +/// +/// All in and output strings should be/are UTF-8 encoded. +function wrDeleteReportFormRender($reportid, $delete_person_name, $delete_reason_public, $delete_invisible) { + // Buttons + $buttons = ' '; + $buttons .= utf8_encode(''); + $buttons .= ''; + // $buttons .= ''; // who is allowed to do so? + $summary = utf8_encode('Formular zum Löschen eines Schneelageberichtes'); + $reason = utf8_encode('Begründung'); + $delete_reason_public_html = htmlspecialchars($delete_reason_public); + $delete_person_name_html = htmlspecialchars($delete_person_name); + + $form = << + + + + +
$reason
Name
Aktion$buttons
+ +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 -function wrReportTableTitleRender($page_id, $page_title, $date_report, $date_entry, $date_invalid, $condition, $description, $author_name) { +/// \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'; @@ -143,14 +171,26 @@ function wrReportTableTitleRender($page_id, $page_title, $date_report, $date_ent 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. -function wrReportTableRowRender($page_id, $page_title, $date_report, $date_entry, $date_invalid, $condition, $description, $author_name) { +/// * 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 . ']]') . ''; @@ -166,6 +206,7 @@ function wrReportTableRowRender($page_id, $page_title, $date_report, $date_entry $out .= '' . htmlspecialchars($condition_text) . ''; $out .= '' . wrReportSandboxParse($description) . ''; $out .= '' . htmlspecialchars($author_name) . ''; + if (!is_null($delete)) $out .= '(Löschen)'; $out .= "\n"; return $out; } @@ -176,6 +217,7 @@ function wrReportTableRowRender($page_id, $page_title, $date_report, $date_entry /// 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); @@ -199,6 +241,111 @@ function wrReportTableRender($page_title = NULL) { } +// 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 @@ -237,7 +384,11 @@ function bahnberichtformularParserHook($input, $args, $parser) { 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")); - if ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) return wrReportSandboxParse(utf8_encode("''Derzeit sind Rodelbahnberichte nur für angemeldete Benutzer 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)); @@ -248,7 +399,7 @@ function bahnberichtformularParserHook($input, $args, $parser) { /// /// The current page name is taken. function bahnberichteParserHook($input, $args, &$parser) { - return wrReportTableRender($parser->getTitle()->getText()); + return wrReportTableRender($parser->getTitle()->getText()); // TODO: Replace } @@ -305,7 +456,10 @@ class WrReport extends SpecialPage { /// - action == 'view' (default) /// - action == 'preview': Preview new report /// - action == 'store': Store new report - /// \param $override_action If not NULL (defualt), it overrides the action in $par + /// - 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; @@ -317,18 +471,114 @@ class WrReport extends SpecialPage { 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) { - $wgOut->addWikiText(utf8_encode('
') . $errorMsg . utf8_encode("
\n")); + if ($errorMsg || $action == 'showerror') { + $wgOut->addWikiText('
' . $errorMsg . "
\n"); + if ($action == 'showerror') return; } // Action view if ($action == 'view') { - $wgOut->addHTML(wrReportTableRender()); + $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 diff --git a/wrreport.php b/wrreport.php index cab6104..6aee14f 100644 --- a/wrreport.php +++ b/wrreport.php @@ -31,7 +31,7 @@ function wrReportLocalizedPageName(&$specialPageArray, $code) { $wgExtensionCredits['parserhook'][] = array( 'name' => 'Winterrodeln Report', - 'version' => '1.0.0', + 'version' => '1.1.0', 'author' =>'Philipp Spitzer', 'url' => 'http://www.winterrodeln.org', 'description' => 'This extension interprets the tag in sledding pages and creates report summaries' diff --git a/wrreport.sql b/wrreport.sql index a66ea89..9525eaa 100644 --- a/wrreport.sql +++ b/wrreport.sql @@ -18,7 +18,7 @@ CREATE TABLE wrreport ( delete_person_userid integer, -- mediawiki user id of the person who deleted the report delete_person_username varchar(30), -- mediawiki user name of the person who deleted the report delete_reason_public text, -- reason for the deletion (wikitext) - delete_invisble boolean, -- do not mention that someone deleted this recored + delete_invisible boolean, -- do not mention that someone deleted this recored CONSTRAINT condition_check CHECK (condition >= 1 AND condition <= 5) );