validation updates
[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 # Reads the entire vote list for this user for all articles
177 function getAllVoteLists ( $user ) {
178 global $wgDBprefix ;
179 $r = array () ; # Revisions
180 $sql = "SELECT * FROM {$wgDBprefix}validate WHERE val_user=" . $user ;
181 $res = wfQuery( $sql, DB_READ );
182 while( $x = wfFetchObject( $res ) ) {
183 $a = $x->val_page ;
184 $y = $x->val_revision ;
185 if ( !isset ( $r[$a] ) ) $r[$a] = array () ;
186 if ( !isset($r[$a][$y]) ) $r[$a][$y] = array () ;
187 $r[$a][$y][$x->val_type] = $x ;
188 }
189 return $r ;
190 }
191
192 # This functions adds a topic to the database
193 function addTopic ( $topic , $limit ) {
194 global $wgDBprefix ;
195 $a = 1 ;
196 while ( isset ( $this->topicList[$a] ) ) $a++ ;
197 $sql = "INSERT INTO {$wgDBprefix}validate (val_user,val_page,val_revision,val_type,val_value,val_comment) VALUES (" ;
198 $sql .= "'0','0','0','{$a}','{$limit}','" ;
199 $sql .= Database::strencode ( $topic ) . "')" ;
200 $res = wfQuery( $sql, DB_WRITE );
201 $x->val_user = $x->val_page = $x->val_revision = 0 ;
202 $x->val_type = $a ;
203 $x->val_value = $limit ;
204 $x->val_comment = $topic ;
205 $this->topicList[$a] = $x ;
206 ksort ( $this->topicList ) ;
207 }
208
209 # This functions adds a topic to the database
210 function deleteTopic ( $id ) {
211 global $wgDBprefix ;
212 $sql = "DELETE FROM {$wgDBprefix}validate WHERE val_type='{$id}'" ;
213 $res = wfQuery( $sql, DB_WRITE );
214 unset ( $this->topicList[$id] ) ;
215 }
216
217 # This function returns a link text to the page validation statistics
218 function link2statistics ( &$article ) {
219 $nt = $article->getTitle();
220 $url = htmlspecialchars( $nt->getLocalURL( "action=validate&mode=list" ) );
221 return wfMsg ( 'val_rev_stats_link', $nt->getPrefixedText(), $url );
222 }
223
224 # This function returns a link text to the page validation statistics of a single revision
225 function link2revisionstatistics ( &$article , $revision ) {
226 $nt = $article->getTitle();
227 $url = htmlspecialchars( $nt->getLocalURL( "action=validate&mode=details&revision={$revision}" ) );
228 return wfMsg ( 'val_revision_stats_link', $url );
229 }
230
231 # This function returns a link text to the user rating statistics page
232 function link2userratings ( $user , $text ) {
233 $nt = Title::newFromText ( "Special:Validate" ) ;
234 $url = htmlspecialchars( $nt->getLocalURL( "mode=userstats&user={$user}" ) );
235 return "<a href=\"{$url}\">{$text}</a>" ;
236 }
237
238 # Returns the timestamp of a revision based on the revision number
239 function getTimestamp ( $revision ) {
240 $ts = $this->rev2date[$revision] ;
241 $ts = $ts->rev_timestamp ;
242 return $ts ;
243 }
244
245 # Returns the revision number of a revision based on the timestamp
246 function getRevisionNumber ( $ts ) {
247 $revision = $this->date2rev[$ts] ;
248 $revision = $revision->rev_id ;
249 return $revision ;
250 }
251
252
253 # HTML generation functions from this point on
254
255 # Returns the metadata string for a revision
256 function getMetadata ( $idx ) {
257 $metadata = "" ;
258 $x = $this->rev2date[$idx] ;
259 $metadata .= wfTimestamp ( TS_DB , $x->rev_timestamp ) ;
260 $metadata .= " by " ;
261 if ( $x->rev_user == 0 ) {
262 $metadata .= $x->rev_user_text ;
263 } else {
264 $u = new User ;
265 $u->setId ( $x->rev_user ) ;
266 $u->setName ( $x->rev_user_text ) ;
267 $nt = $u->getUserPage() ;
268 $url = "<a href='" . $nt->getLocalUrl () . "'>" . $nt->getText() . "</a>" ;
269 $metadata .= $url ;
270 }
271 $metadata .= " : <small>\"" . htmlspecialchars ( $x->rev_comment ) . "\"</small>" ;
272 return $metadata ;
273 }
274
275 # Generates a form for a single revision
276 function getRevisionForm ( &$article , $idx , &$data , $focus = false ) {
277 # Fill data with blank values
278 $ts = $idx ;
279 $revision = $this->getRevisionNumber ( $ts ) ;
280 foreach ( $this->topicList AS $x => $y ) {
281 if ( !isset ( $data[$x] ) ) {
282 $data[$x]->value = 0 ;
283 $data[$x]->comment = "" ;
284 }
285 }
286 ksort ( $data ) ;
287
288 # Generate form
289 $ret = "<form method='post'>" ;
290 $ret .= "<table border='1' cellspacing='0' cellpadding='2'" ;
291 if ( $focus ) $ret .= " style='background-color:#00BBFF'" ;
292 $ret .= ">\n" ;
293 $head = "Revision #" . $revision ;
294 $link = " " . $this->getVersionLink ( $article , $revision ) ;
295 $metadata = $this->getMetadata ( $revision ) ;
296 $ret .= "<tr><th align='left' colspan='3'>" . $head . " ({$link}) {$metadata}</th></tr>\n" ;
297 $line = 0 ;
298 foreach ( $data AS $x => $y ) {
299 $line = 1 - $line ;
300 $col = $line == 1 ? "#DDDDDD" : "#EEEEEE" ;
301 $idx = "_{$revision}[{$x}]" ;
302 $ret .= "<tr bgcolor='{$col}'>\n" ;
303 $ret .= "<th nowrap>" ;
304 $ret .= $this->topicList[$x]->val_comment ;
305 $ret .= "</th>\n" ;
306
307 $tlx = $this->topicList[$x] ;
308 $vote = "" ;
309 $max = $tlx->val_value ;
310 for ( $a = 0 ; $a <= $max ; $a++ ) {
311 if ( $a == 0 ) $vote .= wfMsg ( "val_noop" ) ;
312 $vote .= "<input type='radio' name='re_v{$idx}' value='{$a}'" ;
313 if ( $a == $y->value ) $vote .= " checked" ;
314 $vote .= "/>" ;
315 if ( $max == 2 && $a == 1 ) $vote .= wfMsg ( "val_no" ) . " " ;
316 else if ( $max == 2 && $a == 2 ) $vote .= wfMsg ( "val_yes" ) ;
317 else if ( $a != 0 ) $vote .= $a . " " ;
318 if ( $a == 0 ) $vote .= " &nbsp; " ;
319 }
320 $ret .= "<td nowrap valign='center'>{$vote}</td>\n" ;
321
322 $ret .= "<td width='100%' align='center'><input size='50' style='width:98%' maxlength='250' type='text' name='re_c{$idx}' value='{$y->comment}'/>" ;
323 $ret .= "</td></tr>\n" ;
324 }
325 $checked = $focus ? " checked" : "" ;
326 $ret .= "<tr><td colspan='3' valign='center'>\n" ;
327 $ret .= "<input type='checkbox' name='re_merge_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_merge_old' ) . " \n" ;
328 $ret .= "<input type='checkbox' name='re_clear_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_clear_old' ) . " \n" ;
329 $ret .= "<input type='submit' name='re_submit[{$revision}]' value='" . htmlspecialchars( wfMsg("ok") ) . "'/>\n" ;
330 if ( $focus ) $ret .= "<br/>\n<small>" . wfMsg ( "val_form_note" ) . "</small>" ;
331 $ret .= "</td></tr>\n" ;
332 $ret .= "</table>\n</form>\n\n" ;
333 return $ret ;
334 }
335
336
337 # Generates the page from the validation tab
338 function validatePageForm ( &$article , $revision ) {
339 global $wgOut, $wgRequest, $wgUser ;
340
341 $ret = "" ;
342 $this->prepareRevisions ( $article->getID() ) ;
343 $this->topicList = $this->getTopicList() ;
344 $this->voteCache = $this->getVoteList ( $article->getID() ) ;
345
346 # Check for POST data
347 $re = $wgRequest->getArray( 're_submit' );
348 if ( isset ( $re ) )
349 {
350 $id = array_keys ( $re ) ;
351 $id = $id[0] ; # $id is now the revision number the user clicked "OK" for
352 $clearOldRev = $wgRequest->getVal( "re_clear_{$id}" , 0 );
353 $mergeOldRev = $wgRequest->getVal( "re_merge_{$id}" , 0 );
354 $this->updateRevision ( $article , $id ) ;
355 if ( $mergeOldRev ) $this->mergeOldRevisions ( $article , $id ) ;
356 if ( $clearOldRev ) $this->clearOldRevisions ( $article , $id ) ;
357 $ret .= "<p><font color='red'>" . wfMsg ( 'val_revision_changes_ok' ) . "</font></p>" ;
358 }
359
360 # Make sure the requested revision exists
361 $ts = $this->rev2date[$revision]->rev_timestamp ;
362 if ( !isset ( $this->voteCache[$ts] ) ) $this->voteCache[$ts] = array () ;
363
364 # Sort revisions list, newest first
365 krsort ( $this->voteCache ) ;
366
367 # Output
368 $title = $article->getTitle();
369 $title = $title->getPrefixedText() ;
370 $wgOut->setPageTitle ( wfMsg ( 'val_rev_for' ) . $title ) ;
371 foreach ( $this->voteCache AS $x => $y )
372 {
373 $ret .= $this->getRevisionForm ( $article , $x , $y , $x == $ts ) ;
374 $ret .= "<br/>\n" ;
375 }
376 $ret .= $this->link2statistics ( $article ) ;
377 $ret .= "<p>" . $this->link2userratings ( $wgUser->GetID() , wfMsg('val_show_my_ratings') ) . "</p>" ;
378 return $ret ;
379 }
380
381 # This function performs the "management" mode on Special:Validate
382 function manageTopics () {
383 global $wgRequest ;
384 $this->topicList = $this->getTopicList() ;
385
386 $iamsure = $wgRequest->getVal ( "iamsure" , "0" ) == 1 ;
387
388 if ( $iamsure && $wgRequest->getVal ( "m_add" , "--" ) != "--" ) {
389 $new_topic = $wgRequest->getVal ( "m_topic" ) ;
390 $new_limit = $wgRequest->getVal ( "m_limit" ) ;
391 if ( $new_topic != "" && $new_limit > 1 )
392 $this->addTopic ( $new_topic , $new_limit ) ;
393 }
394
395 $da = $wgRequest->getArray ( "m_del" ) ;
396 if ( $iamsure && isset ( $da ) && count ( $da ) > 0 ) {
397 $id = array_keys ( $da ) ;
398 $id = array_shift ( $id ) ;
399 $this->deleteTopic ( $id ) ;
400 }
401
402 $r = "<p>" . wfMsg ( 'val_warning' ) . "</p>\n" ;
403 $r .= "<form method='post'>\n" ;
404 $r .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
405 $r .= "<tr>" . wfMsg ( 'val_list_header' ) . "</tr>\n" ;
406 foreach ( $this->topicList AS $x => $y ) {
407 $r .= "<tr>\n" ;
408 $r .= "<th>" . $y->val_type . "</th>\n" ;
409 $r .= "<td>{$y->val_comment}</td>\n" ;
410 $r .= "<td>1 .. <b>{$y->val_value}</b></td>\n" ;
411 $r .= "<td><input type='submit' name='m_del[{$x}]' value='" . wfMsg ( 'val_del' ) . "'/></td>\n" ;
412 $r .= "</tr>\n" ;
413 }
414 $r .= "<tr>\n" ;
415 $r .= "<td/>\n" ;
416 $r .= "<td><input type='text' name='m_topic' value=''/></td>\n" ;
417 $r .= "<td><input type='text' name='m_limit' value=''/></td>\n" ;
418 $r .= "<td><input type='submit' name='m_add' value='" . wfMsg ( 'val_add' ) . "'/></td>\n" ;
419 $r .= "</tr>\n" ;
420 $r .= "</table>\n" ;
421 $r .= "<input type='checkbox' name='iamsure' value='1'/>" . wfMsg ( 'val_iamsure' ) . "\n" ;
422 $r .= "</form>\n" ;
423 return $r ;
424 }
425
426 function showDetails ( &$article , $revision ) {
427 global $wgDBprefix , $wgOut, $wgUser ;
428 $this->prepareRevisions ( $article->getID() ) ;
429 $this->topicList = $this->getTopicList() ;
430
431 $title = $article->getTitle() ;
432 $wgOut->setPageTitle ( str_replace ( '$1' , $title->getPrefixedText() , wfMsg ( 'val_validation_of' ) ) ) ;
433
434 # Collecting statistic data
435 $id = $article->getID() ;
436 $sql = "SELECT * FROM {$wgDBprefix}validate WHERE val_page='{$id}' AND val_revision='{$revision}'" ;
437 $res = wfQuery( $sql, DB_READ );
438 $data = array () ;
439 $users = array () ;
440 $topics = array () ;
441 while( $x = wfFetchObject( $res ) ) {
442 $data[$x->val_user][$x->val_type] = $x ;
443 $users[$x->val_user] = true ;
444 $topics[$x->val_type] = true ;
445 }
446
447 # Sorting lists of topics and users
448 ksort ( $users ) ;
449 ksort ( $topics ) ;
450
451 $ts = $this->getTimestamp ( $revision ) ;
452 $url = $this->getVersionLink ( $article , $revision , wfTimestamp ( TS_DB , $ts ) ) ;
453
454 # Table headers
455 $ret = "" ;
456 $ret .= "<p><b>" . str_replace ( '$1' , $url , wfMsg ( 'val_revision_of' ) ) . "</b></p>\n" ;
457 $ret .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
458 $ret .= "<tr><th/>" ;
459
460 foreach ( $topics AS $t => $dummy ) {
461 $ret .= "<th>" . $this->topicList[$t]->val_comment . "</th>" ;
462 }
463 $ret .= "</tr>\n" ;
464
465 # Table data
466 foreach ( $users AS $u => $dummy ) { # Every row a user
467 $ret .= "<tr>" ;
468 $ret .= "<th>" . str_replace ( "$1" , $u , wfMsg ( 'val_details_th_user') ) . "</th>" ;
469 foreach ( $topics AS $t => $dummy ) { # Every column a topic
470 if ( !isset ( $data[$u][$t] ) ) $ret .= "<td/>" ;
471 else {
472 $ret .= "<td valign='center'>" ;
473 $ret .= $data[$u][$t]->val_value ;
474 if ( $data[$u][$t]->val_comment != "" )
475 $ret .= " <small>(" . $data[$u][$t]->val_comment . ")</small>" ;
476 $ret .= "</td>" ;
477 }
478 }
479 $ret .= "</tr>" ;
480 }
481 $ret .= "</table>" ;
482 $ret .= "<p>" . $this->link2statistics ( $article ) . "</p>" ;
483 $ret .= "<p>" . $this->link2userratings ( $wgUser->GetID() , wfMsg('val_show_my_ratings') ) . "</p>" ;
484
485 return $ret ;
486 }
487
488 function showList ( &$article ) {
489 global $wgDBprefix , $wgOut;
490 $this->prepareRevisions ( $article->getID() ) ;
491 $this->topicList = $this->getTopicList() ;
492
493 $title = $article->getTitle() ;
494 $wgOut->setPageTitle ( str_replace ( '$1' , $title->getPrefixedText() , wfMsg ( 'val_validation_of' ) ) ) ;
495
496 # Collecting statistic data
497 $id = $article->getID() ;
498 $sql = "SELECT * FROM {$wgDBprefix}validate WHERE val_page='{$id}'" ;
499 $res = wfQuery( $sql, DB_READ );
500 $data = array () ;
501 while( $x = wfFetchObject( $res ) ) {
502 $idx = $this->getTimestamp ( $x->val_revision ) ;
503 if ( !isset ( $data[$idx] ) )
504 $data[$idx] = array () ;
505 if ( !isset ( $data[$idx][$x->val_type] ) ) {
506 $data[$idx][$x->val_type]->count = 0 ;
507 $data[$idx][$x->val_type]->sum = 0 ;
508 }
509 $data[$idx][$x->val_type]->count++ ;
510 $data[$idx][$x->val_type]->sum += $x->val_value ;
511 }
512
513 krsort ( $data ) ;
514
515 $ret = "" ;
516 $ret .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
517 $ret .= "<tr><th>" . wfMsg("val_revision") . "</th>" ;
518 foreach ( $this->topicList AS $x => $y )
519 $ret .= "<th>{$y->val_comment}</th>" ;
520 $ret .= "</tr>\n" ;
521 foreach ( $data AS $ts => $y ) {
522 $revision = $this->getRevisionNumber ( $ts ) ;
523 $url = $this->getVersionLink ( $article , $revision , wfTimestamp ( TS_DB , $ts ) ) ;
524 $detailsurl = $this->link2revisionstatistics ( $article , $revision ) ;
525 $ret .= "<tr><td>{$url} {$detailsurl}</td>" ;
526 foreach ( $this->topicList AS $topicID => $dummy ) {
527 if ( isset ( $y[$topicID] ) ) {
528 $z = $y[$topicID] ;
529 if ( $z->count == 0 ) $a = 0 ;
530 else $a = $z->sum / $z->count ;
531 $ret .= sprintf ( "<td><b>%1.1f</b> (%d)</td>" , $a , $z->count ) ;
532 } else $ret .= "<td/>" ;
533 }
534 $ret .= "</tr>\n" ;
535 }
536 $ret .= "</table>\n" ;
537 $ret .= "<p>" . $this->link2userratings ( $wgUser->GetID() , wfMsg('val_show_my_ratings') ) . "</p>" ;
538 return $ret ;
539 }
540
541 function getRatingText ( $value , $max ) {
542 if ( $max == 2 && $value == 1 ) $ret = wfMsg ( "val_no" ) . " " ;
543 else if ( $max == 2 && $value == 2 ) $ret = wfMsg ( "val_yes" ) ;
544 else if ( $value != 0 ) $ret = wfMsg ( "val_of" , $value , $max ) . " " ;
545 else $ret = "" ;
546 return $ret ;
547 }
548
549 function showUserStats ( $user ) {
550 global $wgDBprefix , $wgOut, $wgUser ;
551 $this->topicList = $this->getTopicList() ;
552 $data = $this->getAllVoteLists ( $user ) ;
553
554 if ( $user == $wgUser->getID() ) $wgOut->setPageTitle ( wfMsg ( 'val_my_stats_title' ) ) ;
555 else $wgOut->setPageTitle ( wfMsg ( 'val_user_stats_title' , $user ) ) ;
556
557 $ret = "" ;
558 $ret .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
559
560 foreach ( $data AS $articleid => $revisions ) {
561 $title = Title::newFromID ( $articleid ) ;
562 $ret .= "<tr><th align='left' colspan='4'><a href=\"" . $title->getLocalURL() . "\">" . $title->getPrefixedText() . "</a></th></tr>" ;
563 krsort ( $revisions ) ;
564 foreach ( $revisions AS $revid => $revision ) {
565 $url = $title->getLocalURL ( "oldid={$revid}" ) ;
566 $ret .= "<tr><th align='left'><a href=\"{$url}\">" . wfMsg ( 'val_revision_number' , $revid ) . "</a></th>" ;
567 ksort ( $revision ) ;
568 $initial = true ;
569 foreach ( $revision AS $topic => $rating ) {
570 if ( !$initial ) $ret .= "<tr><td/>" ;
571 $initial = false ;
572 $ret .= "<td>" . $this->topicList[$topic]->val_comment . "</td>" ;
573 $ret .= "<td>" . $this->getRatingText ( $rating->val_value , $this->topicList[$topic]->val_value ) . "</td>" ;
574 $ret .= "<td>" . htmlentities ( $rating->val_comment ) . "</td>" ;
575 $ret .= "</tr>" ;
576 }
577 }
578 $ret .= "</tr>" ;
579 }
580 $ret .= "</table>" ;
581
582 return $ret ;
583 }
584
585 }
586
587 /**
588 * constructor
589 */
590 function wfSpecialValidate( $page = '' ) {
591 global $wgOut, $wgRequest, $wgUseValidation, $wgUser, $wgContLang;
592
593 if( !$wgUseValidation ) {
594 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
595 return;
596 }
597
598 /*
599 # Can do?
600 if ( ! $wgUser->isAllowed('change_validation') ) {
601 $wgOut->sysopRequired();
602 return;
603 }
604 */
605
606 $mode = $wgRequest->getVal ( "mode" ) ;
607 $skin = $wgUser->getSkin() ;
608
609
610 if ( $mode == "manage" ) {
611 $v = new Validation ;
612 $html = $v->manageTopics () ;
613 } else if ( $mode == "userstats" ) {
614 $v = new Validation ;
615 $user = $wgUser->GetID() ;
616 #$user = $wgRequest->getVal ( "user" ) ; # Uncomment this to allow all user statistics to be public
617 $html = $v->showUserStats ( $user ) ;
618 } else {
619 $html = "$mode" ;
620 $html .= "<ul>\n" ;
621
622 $t = Title::newFromText ( "Special:Validate" ) ;
623 $url = $t->getLocalURL ( "mode=manage" ) ;
624 $html .= "<li><a href=\"" . $url . "\">Manage</a></li>\n" ;
625
626 $html .= "</ul>\n" ;
627 }
628
629 $wgOut->addHTML( $html );
630 }
631
632 ?>