* Using a white background instead of a gray one
[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 var $topicList ;
33 var $voteCache ;
34 var $rev2date ;
35 var $date2ref ;
36
37 # Reads all revision information of the specified article
38 function prepareRevisions ( $id ) {
39 global $wgDBprefix ;
40 $this->rev2date = array () ;
41 $this->date2rev = array () ;
42 $sql = "SELECT * FROM {$wgDBprefix}revision WHERE rev_page='{$id}'" ;
43 $res = wfQuery( $sql, DB_READ );
44 while( $x = wfFetchObject( $res ) ) {
45 $this->rev2date[$x->rev_id] = $x ;
46 $this->date2rev[$x->rev_timestamp] = $x ;
47 }
48 }
49
50 # Returns a HTML link to the specified article revision
51 function getVersionLink( &$article , $revision , $text = "" ) {
52 $t = $article->getTitle() ;
53 if ( $text == "" ) $text = wfMsg("val_view_version");
54 $ret = "<a href=\"" . $t->getLocalURL ( "oldid={$revision}" ) . "\">" . $text . "</a>" ;
55 return $ret ;
56 }
57
58 # Returns an array containing all topics you can vote on
59 function getTopicList () {
60 global $wgDBprefix ;
61 $ret = array () ;
62 $sql = "SELECT * FROM {$wgDBprefix}validate WHERE val_user=0" ;
63 $res = wfQuery( $sql, DB_READ );
64 while( $x = wfFetchObject( $res ) ) {
65 $ret[$x->val_type] = $x ;
66 }
67 ksort ( $ret ) ;
68 return $ret ;
69 }
70
71 # Merges one dataset into another
72 function mergeInto ( &$source , &$dest ) {
73 $ret = false ;
74 foreach ( $source AS $x => $y ) {
75 $doit = false ;
76 if ( !isset ( $dest[$x] ) ) $doit = true ;
77 else if ( $dest[$x]->value == 0 ) $doit = true ;
78 if ( $doit ) {
79 $dest[$x] = $y ;
80 $ret = true ;
81 }
82 }
83 if ( $ret ) ksort ( $dest ) ;
84 return $ret ;
85 }
86
87 # Merges all votes prior to the given revision into it
88 function mergeOldRevisions ( &$article , $revision ) {
89 $tmp = $this->voteCache ;
90 krsort ( $tmp ) ;
91 $update = false ;
92 $ts = $this->getTimestamp($revision) ;
93 $data = $this->voteCache[$ts] ;
94 foreach ( $tmp AS $x => $y ) {
95 if ( $x < $ts ) {
96 if ( $this->mergeInto ( $y , $data ) ) $update = true ;
97 }
98 }
99 if ( $update ) $this->setRevision ( $article , $revision , $data ) ;
100 }
101
102 # Clears all votes prior to the given revision
103 function clearOldRevisions ( &$article , $revision ) {
104 $tmp = $this->voteCache ;
105 $ts = $this->getTimestamp($revision);
106 foreach ( $tmp AS $x => $y ) {
107 if ( $x < $ts ) $this->deleteRevision ( $article , $this->getRevisionNumber($x) ) ;
108 }
109 }
110
111 # Updates the votes for the given revision from the FORM data
112 function updateRevision ( &$article , $revision ) {
113 global $wgUser, $wgRequest ;
114
115 if ( isset ( $this->voteCache[$this->getTimestamp($revision)] ) ) $data = $this->voteCache[$this->getTimestamp($revision)] ;
116 else $data = array () ;
117 $nv = $wgRequest->getArray ( "re_v_{$revision}" , array() ) ;
118 $nc = $wgRequest->getArray ( "re_c_{$revision}" , array() ) ;
119
120 foreach ( $nv AS $x => $y ) {
121 $data[$x]->value = $y ;
122 $data[$x]->comment = $nc[$x] ;
123 }
124 krsort ( $data ) ;
125
126 $this->setRevision ( $article , $revision , $data ) ;
127 }
128
129 # Sets a specific revision to both cache and database
130 function setRevision ( &$article , $revision , &$data ) {
131 global $wgUser , $wgDBprefix ;
132 $this->deleteRevision ( $article , $revision ) ;
133 $this->voteCache[$this->getTimestamp($revision)] = $data ;
134 foreach ( $data AS $x => $y ) {
135 if ( $y->value > 0 ) {
136 $sql = "INSERT INTO {$wgDBprefix}validate (val_user,val_page,val_revision,val_type,val_value,val_comment) VALUES ('" ;
137 $sql .= $wgUser->getID() . "','" ;
138 $sql .= $article->getID() . "','" ;
139 $sql .= $revision . "','" ;
140 $sql .= $x . "','" ;
141 $sql .= $y->value . "','" ;
142 $sql .= Database::strencode ( $y->comment ) . "')" ;
143 $res = wfQuery( $sql, DB_WRITE );
144 }
145 }
146 }
147
148 # Deletes a specific vote set in both cache and database
149 function deleteRevision ( &$article , $revision ) {
150 global $wgUser , $wgDBprefix ;
151 $ts = $this->getTimestamp ( $revision ) ;
152 if ( !isset ( $this->voteCache[$ts] ) ) return ; # Nothing to do
153 $sql = "DELETE FROM {$wgDBprefix}validate WHERE val_user='" . $wgUser->GetID() . "' AND " ;
154 $sql .= " val_page='" . $article->getID() . "' AND val_revision='{$revision}'" ;
155 $res = wfQuery( $sql, DB_WRITE );
156 unset ( $this->voteCache[$ts] ) ;
157 }
158
159 # Reads the entire vote list for this user for the given article
160 function getVoteList ( $id ) {
161 global $wgUser , $wgDBprefix ;
162 $r = array () ; # Revisions
163 $sql = "SELECT * FROM {$wgDBprefix}validate WHERE val_page=" . $id . " AND val_user=" . $wgUser->getID() ;
164 $res = wfQuery( $sql, DB_READ );
165 while( $x = wfFetchObject( $res ) ) {
166 #$y = $x->val_revision ;
167 $y = $this->rev2date[$x->val_revision] ;
168 $y = $y->rev_timestamp ;
169 if ( !isset($r[$y]) ) $r[$y] = array () ;
170 $r[$y][$x->val_type]->value = $x->val_value ;
171 $r[$y][$x->val_type]->comment = $x->val_comment ;
172 }
173 return $r ;
174 }
175
176 # This functions adds a topic to the database
177 function addTopic ( $topic , $limit ) {
178 global $wgDBprefix ;
179 $a = 1 ;
180 while ( isset ( $this->topicList[$a] ) ) $a++ ;
181 $sql = "INSERT INTO {$wgDBprefix}validate (val_user,val_page,val_revision,val_type,val_value,val_comment) VALUES (" ;
182 $sql .= "'0','0','0','{$a}','{$limit}','" ;
183 $sql .= Database::strencode ( $topic ) . "')" ;
184 $res = wfQuery( $sql, DB_WRITE );
185 $x->val_user = $x->val_page = $x->val_revision = 0 ;
186 $x->val_type = $a ;
187 $x->val_value = $limit ;
188 $x->val_comment = $topic ;
189 $this->topicList[$a] = $x ;
190 ksort ( $this->topicList ) ;
191 }
192
193 # This functions adds a topic to the database
194 function deleteTopic ( $id ) {
195 global $wgDBprefix ;
196 $sql = "DELETE FROM {$wgDBprefix}validate WHERE val_type='{$id}'" ;
197 $res = wfQuery( $sql, DB_WRITE );
198 unset ( $this->topicList[$id] ) ;
199 }
200
201 # This function returns a link text to the page validation statistics
202 function link2statistics ( &$article ) {
203 $ret = wfMsg ( 'val_rev_stats_link' ) ;
204 $nt = $article->getTitle() ;
205 $ret = str_replace ( "$1" , $nt->getPrefixedText() , $ret ) ;
206
207 $url = $nt->getLocalURL ( "action=validate&mode=list" ) ;
208 $ret = str_replace ( "$2" , $url , $ret ) ;
209
210 return $ret ;
211 }
212
213 # Returns the timestamp of a revision based on the revision number
214 function getTimestamp ( $revision ) {
215 $ts = $this->rev2date[$revision] ;
216 $ts = $ts->rev_timestamp ;
217 return $ts ;
218 }
219
220 # Returns the revision number of a revision based on the timestamp
221 function getRevisionNumber ( $ts ) {
222 $revision = $this->date2rev[$ts] ;
223 $revision = $revision->rev_id ;
224 return $revision ;
225 }
226
227
228 # HTML generation functions from this point on
229
230 # Returns the metadata string for a revision
231 function getMetadata ( $idx ) {
232 $metadata = "" ;
233 $x = $this->rev2date[$idx] ;
234 $metadata .= wfTimestamp ( TS_DB , $x->rev_timestamp ) ;
235 $metadata .= " by " ;
236 if ( $x->rev_user == 0 ) {
237 $metadata .= $x->rev_user_text ;
238 } else {
239 $u = new User ;
240 $u->setId ( $x->rev_user ) ;
241 $u->setName ( $x->rev_user_text ) ;
242 $nt = $u->getUserPage() ;
243 $url = "<a href='" . $nt->getLocalUrl () . "'>" . $nt->getText() . "</a>" ;
244 $metadata .= $url ;
245 }
246 $metadata .= " : <small>\"" . htmlspecialchars ( $x->rev_comment ) . "\"</small>" ;
247 return $metadata ;
248 }
249
250 # Generates a form for a single revision
251 function getRevisionForm ( &$article , $idx , &$data , $focus = false ) {
252 # Fill data with blank values
253 $ts = $idx ;
254 $revision = $this->getRevisionNumber ( $ts ) ;
255 foreach ( $this->topicList AS $x => $y ) {
256 if ( !isset ( $data[$x] ) ) {
257 $data[$x]->value = 0 ;
258 $data[$x]->comment = "" ;
259 }
260 }
261 ksort ( $data ) ;
262
263 # Generate form
264 $ret = "<form method='post'>" ;
265 $ret .= "<table border='1' cellspacing='0' cellpadding='2'" ;
266 if ( $focus ) $ret .= " style='background-color:#00BBFF'" ;
267 $ret .= ">\n" ;
268 $head = "Revision #" . $revision ;
269 $link = " " . $this->getVersionLink ( $article , $revision ) ;
270 $metadata = $this->getMetadata ( $revision ) ;
271 $ret .= "<tr><th align='left' colspan='3'>" . $head . " ({$link}) {$metadata}</th></tr>\n" ;
272 $line = 0 ;
273 foreach ( $data AS $x => $y ) {
274 $line = 1 - $line ;
275 $col = $line == 1 ? "#DDDDDD" : "#EEEEEE" ;
276 $idx = "_{$revision}[{$x}]" ;
277 $ret .= "<tr bgcolor='{$col}'>\n" ;
278 $ret .= "<th nowrap>" ;
279 $ret .= $this->topicList[$x]->val_comment ;
280 $ret .= "</th>\n" ;
281
282 $tlx = $this->topicList[$x] ;
283 $vote = "" ;
284 $max = $tlx->val_value ;
285 for ( $a = 0 ; $a <= $max ; $a++ ) {
286 if ( $a == 0 ) $vote .= wfMsg ( "val_noop" ) ;
287 $vote .= "<input type='radio' name='re_v{$idx}' value='{$a}'" ;
288 if ( $a == $y->value ) $vote .= " checked" ;
289 $vote .= "/>" ;
290 if ( $max == 2 && $a == 1 ) $vote .= wfMsg ( "val_no" ) . " " ;
291 else if ( $max == 2 && $a == 2 ) $vote .= wfMsg ( "val_yes" ) ;
292 else if ( $a != 0 ) $vote .= $a . " " ;
293 if ( $a == 0 ) $vote .= " &nbsp; " ;
294 }
295 $ret .= "<td nowrap valign='center'>{$vote}</td>\n" ;
296
297 $ret .= "<td width='100%' align='center'><input size='50' style='width:98%' maxlength='250' type='text' name='re_c{$idx}' value='{$y->comment}'/>" ;
298 $ret .= "</td></tr>\n" ;
299 }
300 $checked = $focus ? " checked" : "" ;
301 $ret .= "<tr><td colspan='3' valign='center'>\n" ;
302 $ret .= "<input type='checkbox' name='re_merge_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_merge_old' ) . " \n" ;
303 $ret .= "<input type='checkbox' name='re_clear_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_clear_old' ) . " \n" ;
304 $ret .= "<input type='submit' name='re_submit[{$revision}]' value='" . htmlspecialchars( wfMsg("ok") ) . "'/>\n" ;
305 if ( $focus ) $ret .= "<br/>\n<small>" . wfMsg ( "val_form_note" ) . "</small>" ;
306 $ret .= "</td></tr>\n" ;
307 $ret .= "</table>\n</form>\n\n" ;
308 return $ret ;
309 }
310
311
312 # Generates the page from the validation tab
313 function validatePageForm ( &$article , $revision ) {
314 global $wgOut, $wgRequest ;
315
316 $this->prepareRevisions ( $article->getID() ) ;
317 $this->topicList = $this->getTopicList() ;
318 $this->voteCache = $this->getVoteList ( $article->getID() ) ;
319
320 # Check for POST data
321 $re = $wgRequest->getArray( 're_submit' );
322 if ( isset ( $re ) )
323 {
324 $id = array_keys ( $re ) ;
325 $id = $id[0] ; # $id is now the revision number the user clicked "OK" for
326 $clearOldRev = $wgRequest->getVal( "re_clear_{$id}" , 0 );
327 $mergeOldRev = $wgRequest->getVal( "re_merge_{$id}" , 0 );
328 $this->updateRevision ( $article , $id ) ;
329 if ( $mergeOldRev ) $this->mergeOldRevisions ( $article , $id ) ;
330 if ( $clearOldRev ) $this->clearOldRevisions ( $article , $id ) ;
331 }
332
333 # Make sure the requested revision exists
334 $ts = $this->rev2date[$revision]->rev_timestamp ;
335 if ( !isset ( $this->voteCache[$ts] ) ) $this->voteCache[$ts] = array () ;
336
337 # Sort revisions list, newest first
338 krsort ( $this->voteCache ) ;
339
340 # Output
341 $ret = "" ;
342 $title = $article->getTitle();
343 $title = $title->getPrefixedText() ;
344 $wgOut->setPageTitle ( wfMsg ( 'val_rev_for' ) . $title ) ;
345 foreach ( $this->voteCache AS $x => $y )
346 {
347 $ret .= $this->getRevisionForm ( $article , $x , $y , $x == $ts ) ;
348 $ret .= "<br/>\n" ;
349 }
350 $ret .= $this->link2statistics ( $article ) ;
351 return $ret ;
352 }
353
354 # This function performs the "management" mode on Special:Validate
355 function manageTopics () {
356 global $wgRequest ;
357 $this->topicList = $this->getTopicList() ;
358
359 $iamsure = $wgRequest->getVal ( "iamsure" , "0" ) == 1 ;
360
361 if ( $iamsure && $wgRequest->getVal ( "m_add" , "--" ) != "--" ) {
362 $new_topic = $wgRequest->getVal ( "m_topic" ) ;
363 $new_limit = $wgRequest->getVal ( "m_limit" ) ;
364 if ( $new_topic != "" && $new_limit > 1 )
365 $this->addTopic ( $new_topic , $new_limit ) ;
366 }
367
368 $da = $wgRequest->getArray ( "m_del" ) ;
369 if ( $iamsure && isset ( $da ) && count ( $da ) > 0 ) {
370 $id = array_keys ( $da ) ;
371 $id = array_shift ( $id ) ;
372 $this->deleteTopic ( $id ) ;
373 }
374
375 $r = "<p>" . wfMsg ( 'val_warning' ) . "</p>\n" ;
376 $r .= "<form method='post'>\n" ;
377 $r .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
378 $r .= "<tr>" . wfMsg ( 'val_list_header' ) . "</tr>\n" ;
379 foreach ( $this->topicList AS $x => $y ) {
380 $r .= "<tr>\n" ;
381 $r .= "<th>" . $y->val_type . "</th>\n" ;
382 $r .= "<td>{$y->val_comment}</td>\n" ;
383 $r .= "<td>1 .. <b>{$y->val_value}</b></td>\n" ;
384 $r .= "<td><input type='submit' name='m_del[{$x}]' value='" . wfMsg ( 'val_del' ) . "'/></td>\n" ;
385 $r .= "</tr>\n" ;
386 }
387 $r .= "<tr>\n" ;
388 $r .= "<td/>\n" ;
389 $r .= "<td><input type='text' name='m_topic' value=''/></td>\n" ;
390 $r .= "<td><input type='text' name='m_limit' value=''/></td>\n" ;
391 $r .= "<td><input type='submit' name='m_add' value='" . wfMsg ( 'val_add' ) . "'/></td>\n" ;
392 $r .= "</tr>\n" ;
393 $r .= "</table>\n" ;
394 $r .= "<input type='checkbox' name='iamsure' value='1'/>" . wfMsg ( 'val_iamsure' ) . "\n" ;
395 $r .= "</form>\n" ;
396 return $r ;
397 }
398
399 function showList ( &$article ) {
400 global $wgDBprefix ;
401 $this->prepareRevisions ( $article->getID() ) ;
402 $this->topicList = $this->getTopicList() ;
403
404 # Collecting statistic data
405 $id = $article->getID() ;
406 $sql = "SELECT * FROM {$wgDBprefix}validate WHERE val_page='{$id}'" ;
407 $res = wfQuery( $sql, DB_READ );
408 $data = array () ;
409 while( $x = wfFetchObject( $res ) ) {
410 #$idx = $x->val_revision ;
411 $idx = $this->getTimestamp ( $x->val_revision ) ;
412 if ( !isset ( $data[$idx] ) )
413 $data[$idx] = array () ;
414 if ( !isset ( $data[$idx][$x->val_type] ) ) {
415 $data[$idx][$x->val_type]->count = 0 ;
416 $data[$idx][$x->val_type]->sum = 0 ;
417 }
418 $data[$idx][$x->val_type]->count++ ;
419 $data[$idx][$x->val_type]->sum += $x->val_value ;
420 }
421
422 krsort ( $data ) ;
423
424 $ret = "" ;
425 $ret .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
426 $ret .= "<tr><th>" . wfMsg("val_revision") . "</th>" ;
427 # $ret .= "<th>" . wfMsg("val_time") . "</th>" ;
428 foreach ( $this->topicList AS $x => $y )
429 $ret .= "<th>{$y->val_comment}</th>" ;
430 $ret .= "</tr>\n" ;
431 foreach ( $data AS $ts => $y ) {
432 $revision = $this->getRevisionNumber ( $ts ) ;
433 # $url = $this->getVersionLink ( $article , $revision , $revision ) ;
434 $url = $this->getVersionLink ( $article , $revision , wfTimestamp ( TS_DB , $ts ) ) ;
435 $ret .= "<tr><th>{$url}</th>" ;
436 # $ret .= "<td nowrap>" . wfTimestamp ( TS_DB , $ts ) . "</td>" ;
437 foreach ( $this->topicList AS $topicID => $dummy ) {
438 if ( isset ( $y[$topicID] ) ) {
439 $z = $y[$topicID] ;
440 if ( $z->count == 0 ) $a = 0 ;
441 else $a = $z->sum / $z->count ;
442 $ret .= sprintf ( "<td><b>%1.1f</b> (%d)</td>" , $a , $z->count ) ;
443 } else $ret .= "<td/>" ;
444 }
445 $ret .= "</tr>\n" ;
446 }
447 $ret .= "</table>\n" ;
448 return $ret ;
449 }
450
451 }
452
453 /**
454 * constructor
455 */
456 function wfSpecialValidate( $page = '' ) {
457 global $wgOut, $wgRequest, $wgUseValidation, $wgUser, $wgContLang;
458
459 if( !$wgUseValidation ) {
460 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
461 return;
462 }
463
464 /*
465 # Can do?
466 if ( ! $wgUser->isAllowed('change_validation') ) {
467 $wgOut->sysopRequired();
468 return;
469 }
470 */
471
472 $mode = $wgRequest->getVal ( "mode" ) ;
473 $skin = $wgUser->getSkin() ;
474
475
476 if ( $mode == "manage" ) {
477 $v = new Validation ;
478 $html = $v->manageTopics () ;
479 # } else if ( $mode == "list" ) {
480 # $v = new Validation ;
481 # $html = $v->showList ( $wgRequest->getVal ( "id" ) ) ;
482 } else {
483 $html = "$mode" ;
484 $html .= "<ul>\n" ;
485
486 $t = Title::newFromText ( "Special:Validate" ) ;
487 $url = $t->getLocalURL ( "mode=manage" ) ;
488 $html .= "<li><a href=\"" . $url . "\">Manage</a></li>\n" ;
489
490 $html .= "</ul>\n" ;
491 }
492
493 $wgOut->addHTML( $html );
494 }
495
496 ?>