wfMsg($key) was deprecated. Replaced it by wfMessage($key)->text().
[philipp/winterrodeln/mediawiki_extensions/wrreport.git] / wrreport.body.php
1 <?php
2 // File encoding: utf-8
3 // Classes for the wrreport extension.
4
5
6 // Special page
7 // ------------
8
9 /// Specal Page to show reports
10 class SpecialWrReport extends SpecialPage {
11         function __construct() {
12                 parent::__construct('wrreport');
13         }
14
15
16         /// \param $par Possibilities:
17         /// - action == 'view' (default)
18         /// - action == 'preview': Preview new report
19         /// - action == 'store': Store new report
20         /// - action == 'deletepreview': Preview the deleted record
21         /// - action == 'delete': Delete an existing report
22         /// - action == 'showerror': Shows the error and exits
23         /// \param $override_action If not NULL (default), it overrides the action in $par
24         /// \param $errorMsg UFT-8 encoded error message (in WikiText) to show on top of the page or NULL (default):
25         function execute($par, $override_action = NULL, $errorMsg = NULL) {
26                 global $wgRequest, $wgOut, $wgParser;
27
28                 $this->setHeaders();
29
30                 // Get request data
31                 $action = $wgRequest->getText('action');
32                 if (!$action) {
33                         if ($wgRequest->getVal('preview')) $action = 'preview';
34                         elseif ($wgRequest->getVal('store')) $action = 'store';
35                         elseif ($wgRequest->getVal('deletepreview')) $action = 'deletepreview';
36                         elseif ($wgRequest->getVal('delete')) $action = 'delete';
37                         else $action = 'view';
38                 }
39                 if ($override_action) $action = $override_action;
40
41                 // Show error message
42                 if ($errorMsg || $action == 'showerror') {
43                         $wgOut->addWikiText('<div class="errorbox">' . $errorMsg . "</div>\n");
44                         if ($action == 'showerror') return;
45                 }
46
47                 // Action view
48                 if ($action == 'view') {                
49                         $conditions = array('date_invalid > now()');
50                         $rows = wrReportGetReports($conditions);
51                         if (count($rows) == 0) $wgOut->addHTML(wfMessage('wrreport-reports-none')->text());
52                         else {
53                                 $wgOut->addWikiText(''); // this is necessary because otherwise $wgParser is not properly initialized but $wgParser is needed in the next line
54                                 $wgOut->addHTML(wrReportTableRender2($rows, WRREPORT_DETAIL, wrReportUserMayDelete(), $wgParser)); 
55                         }
56                 }
57
58                 // Action deletepreview or delete
59                 elseif ($action == 'deletepreview' || $action == 'delete') {
60                         $reportid = (int) $wgRequest->getText('reportid');
61                         if ($reportid == 0) {
62                                 $this->execute($par, 'showerror', wfMessage('wrreport-deletereport-noreport')->text());
63                                 return;
64                         }
65                         $rows = wrReportGetReports(array('id' => $reportid));
66                         if (count($rows) != 1) {
67                                 $this->execute($par, 'showerror', wfMessage('wrreport-deletereport-invalid')->text());
68                                 return;
69                         }
70                         $row = $rows[0];
71                         if (!is_null($row['delete_date'])) {
72                                 $this->execute($par, 'showerror', wfMessage('wrreport-deletereport-alreadydeleted')->text());
73                                 return;
74                         }
75                         $delete_reason_public = $wgRequest->getText('delete_reason_public');
76                         $delete_person_name = $wgRequest->getText('delete_person_name');
77                         $delete_invisible = $wgRequest->getText('delete_invisible') ? TRUE : FALSE;
78                         if ($action == 'delete') {
79                                 // page
80                                 $title = Title::newFromId($row['page_id']);
81
82                                 // user
83                                 global $wgUser;
84                                 $delete_person_userid = $wgUser->getId();
85                                 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.
86                                 $delete_person_username = $wgUser->getName();
87
88                                 // Check permissions - see also function wrReportUserMayDelete, that does also check permissions but does not return an error message.
89                                 $errorMsg = NULL;
90                                 global $wgWrReportDeleteMode;
91                                 if ($wgWrReportDeleteMode == 'deny') $errorMsg = wfMessage('wrreport-deletereport-deny')->text();
92                                 elseif ($wgWrReportDeleteMode == 'loggedin' && !$wgUser->isLoggedIn()) $errorMsg = wfMessage('wrreport-deletereport-loggedin')->text();
93                                 elseif (!$delete_person_name || !$delete_reason_public) $errorMsg = wfMessage('wrreport-deletereport-incomplete')->text();
94                                 if ($errorMsg) {
95                                         $this->execute($par, 'deletepreview', $errorMsg);
96                                         return;
97                                 }
98                                 
99                                 // "Delete" (update) entry
100                                 $dbr = wfGetDB(DB_MASTER);
101                                 $dbr->update(
102                                         'wrreport', 
103                                         array(
104                                                 'delete_date' => date('c'),
105                                                 'delete_person_name' => $delete_person_name,
106                                                 'delete_person_ip' => $_SERVER['REMOTE_ADDR'],
107                                                 'delete_person_userid' => $delete_person_userid,
108                                                 'delete_person_username' => $delete_person_username,
109                                                 'delete_reason_public' => $delete_reason_public,
110                                                 'delete_invisible' => $delete_invisible ? 't' : 'f'
111                                         ),
112                                         array('id' => $reportid)
113                                 );
114
115                                 // Purge cache
116                                 $title->invalidateCache();
117                                 wrRecacheRegions();                             
118
119                                 // Show success message
120                                 $wgOut->addWikiText(wfMessage('wrreport-deletereport-success', '[[' . $row['page_title'] . '#' . wfMessage('wrreport-reports-sectionname')->text() . '|' . $row['page_title'] . ']]')->text());
121                         }
122                         if ($action == 'deletepreview') {
123                                 $wgOut->addWikiText(wfMessage('wrreport-deletereport-preview-before')->text());
124                                 $format = WRREPORT_COMPACT_PAGE;
125                                 $wgOut->addHTML(wrReportTableRender2(array($row), $format, FALSE, $wgParser));
126                                 $wgOut->addWikiText(wfMessage('wrreport-deletereport-preview-after')->text());
127                                 $row['delete_date'] = date('c');
128                                 $row['delete_reason_public'] = $delete_reason_public;
129                                 $row['delete_person_name'] = $delete_person_name;
130                                 $row['delete_invisible'] = $delete_invisible;
131                                 $wgOut->addHTML(wrReportTableRender2(array($row), $format, FALSE, $wgParser));
132                                 $wgOut->addWikiText(wfMessage('wrreport-deletereport-preview-form')->text());
133                                 $wgOut->addHTML(wrDeleteReportFormRender($reportid, $delete_person_name, $delete_reason_public, $delete_invisible));
134                                 $wgOut->addWikiText(wfMessage('wrreport-deletereport-preview-bottom')->text());
135                         }
136                 }
137
138                 // Action preview or store
139                 elseif ($action == 'preview' || $action == 'store') {
140                         $page_title = $wgRequest->getText('page_title');
141                         $date_report = $wgRequest->getText('date_report');
142                         $condition = $wgRequest->getText('condition');
143                         $description = $wgRequest->getText('description');
144                         $author_name = $wgRequest->getText('author_name');
145                         
146                         // condition
147                         $condition = (int) $condition; // force to be nummeric. -1 ... "keine Bewertung", 0 ... "Bitte eingeben", 1 to 5 ... "Sehr gut" to "Geht nicht"
148                         if ($condition < -1 or $condition > 5) $condition = 0; // invalid condition: Tread like 0.
149                         $condition_sql = NULL;
150                         if ($condition >= 1 and $condition <= 5) $condition_sql = $condition;
151
152                         // author_name
153                         $author_name = trim($author_name);
154
155                         // page_id
156                         $title = Title::newFromText($page_title);
157                         $page_id = $title->getArticleId();
158                         if ($page_id == 0) $page_id = NULL;
159                         
160                         // user_id
161                         global $wgUser;
162                         $author_userid = $wgUser->getId();
163                         if ($author_userid == 0) $author_userid = NULL; // to store a NULL value in the database if no user is logged in instead of 0.
164                         $author_username = $wgUser->getName();
165
166                         if ($action == 'store') {
167                                 // check conditions/permissions
168                                 $errorMsg = NULL;
169                                 global $wgWrReportMode;
170                                 global $wgWrReportBlackListAll;
171                                 global $wgWrReportBlackListStrangers;
172                                 if ($wgWrReportMode == 'summer') $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-summer')->text());
173                                 elseif ($wgWrReportMode == 'deny') $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-deny')->text());
174                                 elseif ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-loggedin')->text());
175                                 elseif (!$page_id) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-pagenotfound')->text());
176                                 elseif (in_array($page_title, $wgWrReportBlackListAll)) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-blacklist')->text());
177                                 elseif (!$wgUser->isLoggedIn() && in_array($page_title, $wgWrReportBlackListStrangers)) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-blackliststrangers')->text());
178                                 elseif ($condition == 0) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-choosecondition')->text());
179                                 elseif (!$wgUser->isLoggedIn()) {
180                                         if (!$description) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-enterdescription')->text());
181                                         elseif (!(stripos($description, 'http') === FALSE)) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-nohttp')->text());
182                                         elseif (!$author_name) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-enterauthor')->text());
183                                 }
184
185                                 // check author name
186                                 if (!$errorMsg) {
187                                         $author_name_id = $wgUser->idFromName(strtolower($author_name));
188                                         if ($wgUser->isLoggedIn()) {
189                                                 if ($author_name_id != 0 && $author_name_id != $wgUser->getId())
190                                                         $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-authorused')->text());
191                                         } else {
192                                                 if ($author_name_id != 0)
193                                                         $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-authorlogin')->text());
194                                         }
195                                 }
196
197                                 // Chech whether identical reports are present
198                                 if (!$errorMsg) {
199                                         $dbr = wfGetDB(DB_SLAVE);
200                                         $cond = 'condition';
201                                         global $wgDBtype;
202                                         if ($wgDBtype == "mysql") $cond = "`$cond`"; // "condition" is a reserved word in mysql
203                                         $sqlConditions = array('page_id' => $page_id, 'date_report' => $date_report, $cond => $condition_sql, 'description' => $description, 'author_name' => $author_name);
204                                         $res = $dbr->select('wrreport', 'id', $sqlConditions);
205                                         if ($res->numRows() == 1) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-alreadysaved')->text());
206                                         $dbr->freeResult($res);
207                                 }
208
209                                 // Show error if any
210                                 if ($errorMsg) {
211                                         $this->execute($par, 'preview', $errorMsg);
212                                         return;
213                                 }
214
215                                 // Save entry
216                                 $dbr = wfGetDB(DB_MASTER);
217                                 $dbr->insert(
218                                         'wrreport', 
219                                         array(
220                                                 'page_id' => $page_id, 
221                                                 'page_title' => $page_title, 
222                                                 'date_report' => $date_report, 
223                                                 'date_entry' => date('c'),
224                                                 'date_invalid' => date('c', strtotime('+9 days')),
225                                                 $cond => $condition_sql,
226                                                 'description' => $description,
227                                                 'author_name' => $author_name,
228                                                 'author_ip' => $_SERVER['REMOTE_ADDR'],
229                                                 'author_userid' => $author_userid,
230                                                 'author_username' => $author_username
231                                                 // 'delete_*' => // use database defaults (NULL)
232                                         )
233                                 );
234
235                                 // Purge cache
236                                 $title->invalidateCache();
237                                 wrRecacheRegions();                             
238                                 wrUpdateWrReportCacheTable($page_id);
239
240                                 // Show success message
241                                 $wgOut->addWikiText(wfMessage('wrreport-newreport-success', '[[' . $page_title . '#' . wfMessage('wrreport-reports-sectionname')->text() . '|' . $page_title . ']]')->text());
242                                 // We could redirect to result with the following line but we don't want to.
243                                 // $wgOut->redirect($title->getFullURL() . '#' . wfMessage('wrreport-reports-sectionname')->text());
244                         }
245                         if ($action == 'preview') {
246                                 $wgOut->addWikiText(wfMessage('wrreport-newreport-preview-top')->text());
247                                 $format = WRREPORT_COMPACT_PAGE;
248                                 $row = array_fill_keys(wrReportGetColumnNames(), NULL);
249                                 $row['page_id'] = $page_id;
250                                 $row['page_title'] = $page_title;
251                                 $row['date_report'] = $date_report;
252                                 $row['condition'] = $condition_sql;
253                                 $row['description'] = $description;
254                                 $row['author_name'] = $author_name;
255                                 $row['author_userid'] = $author_userid;
256                                 $row['author_username'] = $author_username;
257                                 
258                                 $wgOut->addHTML(wrReportTableRender2(array($row), $format, FALSE, $wgParser));
259                                 $wgOut->addWikiText(wfMessage('wrreport-newreport-preview-middle')->text());
260                                 $wgOut->addHTML(wrReportFormRender(FALSE, $page_title, $date_report, $condition, $description, $author_name));
261                                 $wgOut->addWikiText(wfMessage('wrreport-newreport-preview-bottom')->text());
262                         } 
263                         
264                 }
265
266                 else die('Wrong action');
267         }
268 }
269
270
271 ?>