caf32e07240fba6bbb5ac52df6b6643070e093a8
[lhc/web/wiklou.git] / includes / SpecialValidate.php
1 <?php
2 # Copyright (C) 2004 Magnus Manske <magnus.manske@web.de>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 *
22 * @package MediaWiki
23 * @subpackage SpecialPage
24 */
25
26 /**
27 *
28 * @package MediaWiki
29 * @subpackage SpecialPage
30 */
31 class Validation {
32
33 # Returns a HTML link to the specified article revision
34 function getVersionLink( &$article , $revision , $text = "" ) {
35 $t = $article->getTitle() ;
36 if ( $text == "" ) $text = wfMsg("val_view_version");
37 $ret = "<a href=\"" . $t->getLocalURL ( "oldid={$revision}" ) . "\">" . $text . "</a>" ;
38 return $ret ;
39 }
40
41 # Returns an array containing all topics you can vote on
42 function getTopicList () {
43 $ret = array () ;
44 $sql = "SELECT * FROM validate WHERE val_user=0" ;
45 $res = wfQuery( $sql, DB_READ );
46 while( $x = wfFetchObject( $res ) ) {
47 $ret[$x->val_type] = $x ;
48 }
49 ksort ( $ret ) ;
50 return $ret ;
51 }
52
53 # Generates a form for a single revision
54 function getRevisionForm ( &$article , $revision , &$data , $focus = false ) {
55 # Fill data with blank values
56 foreach ( $this->topicList AS $x => $y ) {
57 if ( !isset ( $data[$x] ) ) {
58 $data[$x]->value = 0 ;
59 $data[$x]->comment = "" ;
60 }
61 }
62 ksort ( $data ) ;
63
64 # Generate form
65 $ret = "<form method='post'>" ;
66 $ret .= "<table border='1' cellspacing='0' cellpadding='2'" ;
67 if ( $focus ) $ret .= " style='background-color:#00BBFF'" ;
68 $ret .= ">\n" ;
69 $head = "Revision #" . $revision ;
70 # if ( $focus ) $head = "<font color='red'>{$head}</font>" ;
71 $link = " " . $this->getVersionLink ( $article , $revision ) ;
72 $ret .= "<tr><th align='left' colspan='3'>" . $head . " ({$link})</th></tr>\n" ;
73 $line = 0 ;
74 foreach ( $data AS $x => $y ) {
75 $line = 1 - $line ;
76 $col = $line == 1 ? "#DDDDDD" : "#EEEEEE" ;
77 $idx = "_{$revision}[{$x}]" ;
78 $ret .= "<tr bgcolor='{$col}'>\n" ;
79 $ret .= "<th nowrap>" ;
80 $ret .= $this->topicList[$x]->val_comment ;
81 $ret .= "</th>\n" ;
82
83 $tlx = $this->topicList[$x] ;
84 $vote = "" ;
85 $max = $tlx->val_value ;
86 for ( $a = 0 ; $a <= $max ; $a++ ) {
87 if ( $a == 0 ) $vote .= wfMsg ( "val_noop" ) ;
88 $vote .= "<input type='radio' name='re_v{$idx}' value='{$a}'" ;
89 if ( $a == $y->value ) $vote .= " checked" ;
90 $vote .= "/>" ;
91 if ( $max == 2 && $a == 1 ) $vote .= wfMsg ( "val_no" ) . " " ;
92 else if ( $max == 2 && $a == 2 ) $vote .= wfMsg ( "val_yes" ) ;
93 else if ( $a != 0 ) $vote .= $a . " " ;
94 if ( $a == 0 ) $vote .= " &nbsp; " ;
95 }
96 $ret .= "<td nowrap valign='center'>{$vote}</td>\n" ;
97
98 $ret .= "<td width='100%' align='center'><input size='50' style='width:98%' maxlength='250' type='text' name='re_c{$idx}' value='{$y->comment}'/>" ;
99 $ret .= "</td></tr>\n" ;
100 }
101 $checked = $focus ? " checked" : "" ;
102 $ret .= "<tr><td colspan='3' valign='center'>\n" ;
103 $ret .= "<input type='checkbox' name='re_merge_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_merge_old' ) . " \n" ;
104 $ret .= "<input type='checkbox' name='re_clear_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_clear_old' ) . " \n" ;
105 $ret .= "<input type='submit' name='re_submit[{$revision}]' value='" . htmlspecialchars( wfMsg("ok") ) . "'/>\n" ;
106 if ( $focus ) $ret .= "<br/>\n<small>" . wfMsg ( "val_form_note" ) . "</small>" ;
107 $ret .= "</td></tr>\n" ;
108 $ret .= "</table>\n</form>\n\n" ;
109 return $ret ;
110 }
111
112 # Merges one dataset into another
113 function mergeInto ( &$source , &$dest ) {
114 $ret = false ;
115 foreach ( $source AS $x => $y ) {
116 $doit = false ;
117 if ( !isset ( $dest[$x] ) ) $doit = true ;
118 else if ( $dest[$x]->value == 0 ) $doit = true ;
119 if ( $doit ) {
120 $dest[$x] = $y ;
121 $ret = true ;
122 }
123 }
124 if ( $ret ) ksort ( $dest ) ;
125 return $ret ;
126 }
127
128 # Merges all votes prior to the given revision into it
129 function mergeOldRevisions ( &$article , $revision ) {
130 $tmp = $this->voteCache ;
131 krsort ( $tmp ) ;
132 $update = false ;
133 $data = $this->voteCache[$revision] ;
134 foreach ( $tmp AS $x => $y ) {
135 if ( $x < $revision ) {
136 if ( $this->mergeInto ( $y , $data ) ) $update = true ;
137 }
138 }
139 if ( $update ) $this->setRevision ( $article , $revision , $data ) ;
140 }
141
142 # Clears all votes prior to the given revision
143 function clearOldRevisions ( &$article , $revision ) {
144 $tmp = $this->voteCache ;
145 foreach ( $tmp AS $x => $y ) {
146 if ( $x < $revision ) $this->deleteRevision ( $article , $x ) ;
147 }
148 }
149
150 # Updates the votes for the given revision from the FORM data
151 function updateRevision ( &$article , $revision ) {
152 global $wgUser, $wgRequest ;
153
154 if ( isset ( $this->voteCache[$revision] ) ) $data = $this->voteCache[$revision] ;
155 else $data = array () ;
156 $nv = $wgRequest->getArray ( "re_v_{$revision}" , array() ) ;
157 $nc = $wgRequest->getArray ( "re_c_{$revision}" , array() ) ;
158
159 foreach ( $nv AS $x => $y ) {
160 # if ( $y > 0 ) {
161 $data[$x]->value = $y ;
162 $data[$x]->comment = $nc[$x] ;
163 # }
164 }
165 krsort ( $data ) ;
166
167 $this->setRevision ( $article , $revision , $data ) ;
168 }
169
170 # Sets a specific revision to both cache and database
171 function setRevision ( &$article , $revision , &$data ) {
172 global $wgUser ;
173 $this->deleteRevision ( $article , $revision ) ;
174 $this->voteCache[$revision] = $data ;
175 foreach ( $data AS $x => $y ) {
176 if ( $y->value > 0 ) {
177 $sql = "INSERT INTO validate (val_user,val_page,val_revision,val_type,val_value,val_comment) VALUES ('" ;
178 $sql .= $wgUser->getID() . "','" ;
179 $sql .= $article->getID() . "','" ;
180 $sql .= $revision . "','" ;
181 $sql .= $x . "','" ;
182 $sql .= $y->value . "','" ;
183 $sql .= Database::strencode ( $y->comment ) . "')" ;
184 $res = wfQuery( $sql, DB_WRITE );
185 }
186 }
187 }
188
189 # Deletes a specific vote set in both cache and database
190 function deleteRevision ( &$article , $revision ) {
191 global $wgUser ;
192 if ( !isset ( $this->voteCache[$revision] ) ) return ; # Nothing to do
193 $sql = "DELETE FROM validate WHERE val_user='" . $wgUser->GetID() . "' AND " ;
194 $sql .= " val_page='" . $article->getID() . "' AND val_revision='{$revision}'" ;
195 $res = wfQuery( $sql, DB_WRITE );
196 unset ( $this->voteCache[$revision] ) ;
197 }
198
199 # Reads the entire vote list for this user for the given article
200 function getVoteList ( $id ) {
201 global $wgUser ;
202 $r = array () ; # Revisions
203 $sql = "SELECT * FROM validate WHERE val_page=" . $id . " AND val_user=" . $wgUser->getID() ;
204 $res = wfQuery( $sql, DB_READ );
205 while( $x = wfFetchObject( $res ) ) {
206 if ( !isset($r[$x->val_revision]) ) $r[$x->val_revision] = array () ;
207 $r[$x->val_revision][$x->val_type]->value = $x->val_value ;
208 $r[$x->val_revision][$x->val_type]->comment = $x->val_comment ;
209 }
210 return $r ;
211 }
212
213 # This functions adds a topic to the database
214 function addTopic ( $topic , $limit ) {
215 $a = 1 ;
216 while ( isset ( $this->topicList[$a] ) ) $a++ ;
217 $sql = "INSERT INTO validate (val_user,val_page,val_revision,val_type,val_value,val_comment) VALUES (" ;
218 $sql .= "'0','0','0','{$a}','{$limit}','" ;
219 $sql .= Database::strencode ( $topic ) . "')" ;
220 $res = wfQuery( $sql, DB_WRITE );
221 $x->val_user = $x->val_page = $x->val_revision = 0 ;
222 $x->val_type = $a ;
223 $x->val_value = $limit ;
224 $x->val_comment = $topic ;
225 $this->topicList[$a] = $x ;
226 ksort ( $this->topicList ) ;
227 }
228
229 # This functions adds a topic to the database
230 function deleteTopic ( $id ) {
231 $sql = "DELETE FROM validate WHERE val_type='{$id}'" ;
232 $res = wfQuery( $sql, DB_WRITE );
233 unset ( $this->topicList[$id] ) ;
234 }
235
236 # This function returns a link text to the page validation statistics
237 function link2statistics ( &$article ) {
238 $ret = wfMsg ( 'val_rev_stats_link' ) ;
239 $nt = $article->getTitle() ;
240 $ret = str_replace ( "$1" , $nt->getPrefixedText() , $ret ) ;
241
242 # $t = Title::newFromText ( "Special:Validate" ) ;
243 # $url = $t->getLocalURL ( "mode=list&id=" . $article->getID() ) ;
244 $url = $nt->getLocalURL ( "action=validate&mode=list" ) ;
245 $ret = str_replace ( "$2" , $url , $ret ) ;
246
247 return $ret ;
248 }
249
250
251
252 # HTML generation functions from this point on
253
254
255 # Generates the page from the validation tab
256 function validatePageForm ( &$article , $revision ) {
257 global $wgOut, $wgRequest ;
258
259 $this->topicList = $this->getTopicList() ;
260 $this->voteCache = $this->getVoteList ( $article->getID() ) ;
261
262 # Check for POST data
263 $re = $wgRequest->getArray( 're_submit' );
264 if ( isset ( $re ) )
265 {
266 $id = array_keys ( $re ) ;
267 $id = $id[0] ; # $id is now the revision number the user clicked "OK" for
268 $clearOldRev = $wgRequest->getVal( "re_clear_{$id}" , 0 );
269 $mergeOldRev = $wgRequest->getVal( "re_merge_{$id}" , 0 );
270 $this->updateRevision ( $article , $id ) ;
271 if ( $mergeOldRev ) $this->mergeOldRevisions ( $article , $id ) ;
272 if ( $clearOldRev ) $this->clearOldRevisions ( $article , $id ) ;
273 }
274
275 # Make sure the requested revision exists
276 if ( !isset ( $this->voteCache[$revision] ) ) $this->voteCache[$revision] = array () ;
277
278 # Sort revisions list, newest first
279 krsort ( $this->voteCache ) ;
280
281 # Output
282 $ret = "" ;
283 $title = $article->getTitle();
284 $title = $title->getPrefixedText() ;
285 $wgOut->setPageTitle ( wfMsg ( 'val_rev_for' ) . $title ) ;
286 foreach ( $this->voteCache AS $x => $y )
287 {
288 $ret .= $this->getRevisionForm ( $article , $x , $y , $x == $revision ) ;
289 $ret .= "<br/>\n" ;
290 }
291 $ret .= $this->link2statistics ( $article ) ;
292 return $ret ;
293 }
294
295 # This function performs the "management" mode on Special:Validate
296 function manageTopics () {
297 global $wgRequest ;
298 $this->topicList = $this->getTopicList() ;
299
300 $iamsure = $wgRequest->getVal ( "iamsure" , "0" ) == 1 ;
301
302 if ( $iamsure && $wgRequest->getVal ( "m_add" , "--" ) != "--" ) {
303 $new_topic = $wgRequest->getVal ( "m_topic" ) ;
304 $new_limit = $wgRequest->getVal ( "m_limit" ) ;
305 if ( $new_topic != "" && $new_limit > 1 )
306 $this->addTopic ( $new_topic , $new_limit ) ;
307 }
308
309 $da = $wgRequest->getArray ( "m_del" ) ;
310 if ( $iamsure && isset ( $da ) && count ( $da ) > 0 ) {
311 $id = array_keys ( $da ) ;
312 $id = array_shift ( $id ) ;
313 $this->deleteTopic ( $id ) ;
314 }
315
316 $r = "<p>" . wfMsg ( 'val_warning' ) . "</p>\n" ;
317 $r .= "<form method='post'>\n" ;
318 $r .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
319 $r .= "<tr>" . wfMsg ( 'val_list_header' ) . "</tr>\n" ;
320 foreach ( $this->topicList AS $x => $y ) {
321 $r .= "<tr>\n" ;
322 $r .= "<th>" . $y->val_type . "</th>\n" ;
323 $r .= "<td>{$y->val_comment}</td>\n" ;
324 $r .= "<td>1 .. <b>{$y->val_value}</b></td>\n" ;
325 $r .= "<td><input type='submit' name='m_del[{$x}]' value='" . wfMsg ( 'val_del' ) . "'/></td>\n" ;
326 $r .= "</tr>\n" ;
327 }
328 $r .= "<tr>\n" ;
329 $r .= "<td/>\n" ;
330 $r .= "<td><input type='text' name='m_topic' value=''/></td>\n" ;
331 $r .= "<td><input type='text' name='m_limit' value=''/></td>\n" ;
332 $r .= "<td><input type='submit' name='m_add' value='" . wfMsg ( 'val_add' ) . "'/></td>\n" ;
333 $r .= "</tr>\n" ;
334 $r .= "</table>\n" ;
335 $r .= "<input type='checkbox' name='iamsure' value='1'/>" . wfMsg ( 'val_iamsure' ) . "\n" ;
336 $r .= "</form>\n" ;
337 return $r ;
338 }
339
340 function showList ( &$article ) {
341 $this->topicList = $this->getTopicList() ;
342
343 # Collecting statistic data
344 $id = $article->getID() ;
345 $sql = "SELECT * FROM validate WHERE val_page='{$id}'" ;
346 $res = wfQuery( $sql, DB_READ );
347 $data = array () ;
348 while( $x = wfFetchObject( $res ) ) {
349 if ( !isset ( $data[$x->val_revision] ) )
350 $data[$x->val_revision] = array () ;
351 if ( !isset ( $data[$x->val_revision][$x->val_type] ) ) {
352 $data[$x->val_revision][$x->val_type]->count = 0 ;
353 $data[$x->val_revision][$x->val_type]->sum = 0 ;
354 }
355 $data[$x->val_revision][$x->val_type]->count++ ;
356 $data[$x->val_revision][$x->val_type]->sum += $x->val_value ;
357 }
358
359 $ret = "" ;
360 $ret .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
361 $ret .= "<tr><th>Revision</th>" ;
362 foreach ( $this->topicList AS $x => $y )
363 $ret .= "<th>{$y->val_comment}</th>" ;
364 $ret .= "</tr>\n" ;
365 foreach ( $data AS $revision => $y ) {
366 $url = $this->getVersionLink ( $article , $revision , $revision ) ;
367 $ret .= "<tr><th>{$url}</th>" ;
368 foreach ( $this->topicList AS $topicID => $dummy ) {
369 if ( isset ( $y[$topicID] ) ) {
370 $z = $y[$topicID] ;
371 if ( $z->count == 0 ) $a = 0 ;
372 else $a = $z->sum / $z->count ;
373 $ret .= sprintf ( "<td><b>%1.1f</b> (%d)</td>" , $a , $z->count ) ;
374 } else $ret .= "<td/>" ;
375 }
376 $ret .= "</tr>\n" ;
377 }
378 $ret .= "</table>\n" ;
379 return $ret ;
380 }
381
382 }
383
384 /**
385 * constructor
386 */
387 function wfSpecialValidate( $page = '' ) {
388 global $wgOut, $wgRequest, $wgUseValidation, $wgUser, $wgContLang;
389
390 if( !$wgUseValidation ) {
391 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
392 return;
393 }
394
395 /*
396 # Can do?
397 if ( ! $wgUser->isAllowed('change_validation') ) {
398 $wgOut->sysopRequired();
399 return;
400 }
401 */
402
403 $mode = $wgRequest->getVal ( "mode" ) ;
404 $skin = $wgUser->getSkin() ;
405
406
407 if ( $mode == "manage" ) {
408 $v = new Validation ;
409 $html = $v->manageTopics () ;
410 # } else if ( $mode == "list" ) {
411 # $v = new Validation ;
412 # $html = $v->showList ( $wgRequest->getVal ( "id" ) ) ;
413 } else {
414 $html = "$mode" ;
415 $html .= "<ul>\n" ;
416
417 $t = Title::newFromText ( "Special:Validate" ) ;
418 $url = $t->getLocalURL ( "mode=manage" ) ;
419 $html .= "<li><a href=\"" . $url . "\">Manage</a></li>\n" ;
420
421 $html .= "</ul>\n" ;
422 }
423
424 $wgOut->addHTML( $html );
425 }
426
427 ?>