]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/SpecialValidate.php
MediaWiki 1.5.8 (initial commit)
[autoinstallsdev/mediawiki.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 class Validation {
27         var $topicList;
28         var $voteCache;
29         var $page_id;
30
31         function getRevisionFromId( $rev_id ) {
32                 if( isset( $this->id2rev[$rev_id] ) ) return $this->id2rev[$rev_id];
33
34                 $db =& wfGetDB( DB_SLAVE );
35                 $fname = 'SpecialValidate::getRevisionFromId';
36                 $res = $db->select( 'revision', '*', array( 'rev_id' => $rev_id ), $fname, array( 'LIMIT' => 1 ) );
37                 $rev = $db->fetchObject($res);
38                 $db->freeResult($res);
39
40                 $this->id2rev[$rev->rev_id] = $rev;
41                 $this->ts2rev[$rev->rev_timestamp] = $rev;
42
43                 return $rev;
44         }
45
46         function getRevisionFromTimestamp( $timestamp ) {
47                 if( isset( $this->ts2rev[$timestamp] ) ) return $this->ts2rev[$timestamp];
48
49                 $db =& wfGetDB( DB_SLAVE );
50                 $fname = 'SpecialValidate::getRevisionFromTimestamp';
51                 $res = $db->select( 'revision', '*',
52                         array( 'rev_page' => $this->page_id, 'rev_timestamp' => $timestamp ),
53                         $fname, array( 'LIMIT' => 1 )
54                 );
55                 $rev = $db->fetchObject($res);
56                 $db->freeResult($res);
57
58                 $this->id2rev[$rev->rev_id] = $rev;
59                 $this->ts2rev[$rev->rev_timestamp] = $rev;
60
61                 return $rev;
62         }
63
64         # Returns a HTML link to the specified article revision
65         function getRevisionLink( &$article, $revision, $text = "" ) {
66                 global $wgUser;
67                 $sk = $wgUser->getSkin();
68                 $t = $article->getTitle();
69                 if( $text == "" ) $text = wfMsg("val_view_version");
70                 return $sk->makeKnownLinkObj( $t, $this->getParsedWiki($text), 'oldid='.urlencode($revision) );
71         }
72
73         # Returns an array containing all topics you can vote on
74         function getTopicList() {
75                 $db =& wfGetDB( DB_SLAVE );
76
77                 $topics = array();
78                 $res = $db->select( 'validate', '*', array( 'val_page' => 0 ), 'SpecialValidate::getTopicList' );
79                 while( $topic = $db->fetchObject($res) ) {
80                         $topics[$topic->val_type] = $topic;
81                 }
82                 $db->freeResult($res);
83
84                 ksort( $topics );
85                 return $topics;
86         }
87         
88         # Merges one dataset into another
89         function mergeInto( &$source, &$dest ) {
90                 $ret = false;
91                 foreach( $source as $x => $y ) {
92                         $doit = false;
93                         if( !isset( $dest[$x] ) ) {
94                                 $doit = true;
95                         } elseif( $dest[$x]->value == 0 ) {
96                                 $doit = true;
97                         }
98                         if( $doit ) {
99                                 $dest[$x] = $y;
100                                 $ret = true;
101                         }
102                 }
103                 if( $ret ) {
104                         ksort ( $dest );
105                 }
106                 return $ret;
107         }
108
109         # Merges all votes prior to the given revision into it
110         function mergeOldRevisions( &$article, $revision ) {
111                 $tmp = $this->voteCache;
112                 krsort( $tmp );
113                 $update = false;
114                 $ts = $this->getRevisionTimestamp( $revision );
115                 $data = $this->voteCache[$ts];
116                 foreach( $tmp as $x => $y ) {
117                         if( $x < $ts ) {
118                                 if( $this->mergeInto( $y, $data ) ) {
119                                         $update = true;
120                                 }
121                         }
122                 }
123                 if( $update ) {
124                         $this->setRevision( $article, $revision, $data );
125                 }
126         }
127         
128         # Clears all votes prior to the given revision
129         function clearOldRevisions( &$article, $revision ) {
130                 $tmp = $this->voteCache;
131                 $ts = $this->getRevisionTimestamp( $revision );
132                 foreach( $tmp as $x => $y ) {
133                         if( $x < $ts ) {
134                                 $this->deleteRevisionVote ( $article, $this->getRevisionId( $x ) );
135                         }
136                 }
137         }
138         
139         # Updates the votes for the given revision from the FORM data
140         function updateRevision( &$article, $revision ) {
141                 global $wgRequest;
142                 
143                 if( isset( $this->voteCache[$this->getRevisionTimestamp( $revision )] ) ) {
144                         $data = $this->voteCache[$this->getRevisionTimestamp( $revision )];
145                 } else {
146                         $data = array();
147                 }
148                 $nv = $wgRequest->getArray( "re_v_{$revision}", array() );
149                 $nc = $wgRequest->getArray( "re_c_{$revision}", array() );
150                 
151                 foreach( $nv as $x => $y ) {
152                         $data[$x]->value = $y;
153                         $data[$x]->comment = $nc[$x];
154                 }
155                 krsort( $data );
156                 
157                 $this->setRevision( $article, $revision, $data );
158         }
159         
160         # Sets a specific revision to both cache and database
161         function setRevision( &$article, $revision, &$data ) {
162                 global $wgUser;
163                 $this->deleteRevisionVote( $article, $revision );
164                 $this->voteCache[ $this->getRevisionTimestamp($revision) ] = $data;
165                 foreach( $data as $x => $y ) {
166                         if( $y->value > 0 ) {
167                                 $ip = $wgUser->isAnon() ? $wgUser->getName() : '';
168                                 $dbw =& wfGetDB( DB_MASTER );
169                                 $dbw->insert( 'validate',
170                                         array(
171                                                 'val_user'     => $wgUser->getId(),
172                                                 'val_page'     => $article->getId(),
173                                                 'val_revision' => $revision,
174                                                 'val_type'     => $x,
175                                                 'val_value'    => $y->value,
176                                                 'val_comment'  => $y->comment,
177                                                 'val_ip'       => $ip ),
178                                         'SpecialValidate::setRevision'
179                                 );
180                         }
181                 }
182         }
183         
184         # Returns a map identifying the current user
185         function identifyUser( $user = "" ) {
186                 global $wgUser;
187                 if( $user == "" ) $user = $wgUser->getID();
188                 return User::isIP($user)
189                         ? array( 'val_user' => 0, 'val_ip' => $user )
190                         : array( 'val_user' => $user );
191         }
192         
193         # Deletes a specific vote set in both cache and database
194         function deleteRevisionVote( &$article, $revision ) {
195                 $ts = $this->getRevisionTimestamp( $revision );
196                 if( !isset ( $this->voteCache[$ts] ) ) return;
197
198                 $db =& wfGetDB( DB_MASTER );
199                 $db->delete(
200                         'validate',
201                         array_merge(
202                                 $this->identifyUser(),
203                                 array(
204                                         'val_page' => $article->getID(),
205                                         'val_revision' => $revision
206                                 )
207                         ),
208                         'SpecialValidate::deleteRevisionVote'
209                 );
210
211                 unset( $this->voteCache[$ts] );
212         }
213         
214         # Reads the entire vote list for this user for the given article
215         function getVoteList( $id, $user = "" ) {
216                 $db =& wfGetDB( DB_SLAVE );
217                 $res = $db->select( 'validate', '*', array_merge( array( 'val_page' => $id ), $this->identifyUser($user) ) );
218
219                 $revisions = array();
220                 while( $vote = $db->fetchObject($res) ) {
221                         $ts = $this->getRevisionTimestamp( $vote->val_revision );
222                         if( ! isset( $revisions[$ts] ) ) {
223                                 $revisions[$ts] = array();
224                         }
225                         $revisions[$ts][$vote->val_type]->value = $vote->val_value;
226                         $revisions[$ts][$vote->val_type]->comment = $vote->val_comment;
227                 }
228                 $db->freeResult($res);
229
230                 return $revisions;
231         }
232         
233         # Reads the entire vote list for this user for all articles
234         # XXX Should be paged
235         function getAllVoteLists( $user ) {
236                 $db =& wfGetDB( DB_SLAVE );
237                 $res = $db->select( 'validate', '*', $this->identifyUser($user) );
238
239                 $votes = array();
240                 while( $vote = $db->fetchObject($res) ) {
241                         $votes[$vote->val_page][$vote->val_revision][$vote->val_type] = $vote;
242                 }
243                 $db->freeResult($res);
244
245                 return $votes;
246         }
247         
248         # This functions adds a topic to the database
249         function addTopic( $topic, $limit ) {
250                 $db =& wfGetDB( DB_MASTER );
251
252                 $next_idx = 1;
253                 while( isset( $this->topicList[$next_idx] ) ) {
254                        $next_idx++;
255                 }
256
257                 $db->insert(
258                         'validate',
259                         array(
260                                 'val_user' => 0,
261                                 'val_page' => 0,
262                                 'val_revision' => 0,
263                                 'val_type' => $next_idx,
264                                 'val_value' => $limit,
265                                 'val_comment' => $topic,
266                                 'val_ip' => ''
267                         ),
268                         'SpecialValidate::addTopic'
269                 );
270
271                 $t->val_user = $t->val_page = $t->val_revision = 0;
272                 $t->val_type = $next_idx;
273                 $t->val_value = $limit;
274                 $t->val_comment = $topic;
275                 $t->val_ip = "";
276                 $this->topicList[$next_idx] = $t;
277
278                 ksort( $this->topicList );
279         }
280
281         function deleteTopic( $id ) {
282                 $db =& wfGetDB( DB_MASTER );
283                 $db->delete( 'validate', array( 'val_type' => $id ), 'SpecialValidate::deleteTopic' );
284                 unset( $this->topicList[$id] );
285         }
286         
287         # This function returns a link text to the page validation statistics
288         function getStatisticsLink( &$article ) {
289                 global $wgUser;
290                 $sk = $wgUser->getSkin();
291                 $nt = $article->getTitle();
292                 return $sk->makeKnownLinkObj( $nt, wfMsg( 'val_rev_stats', $nt->getPrefixedText() ), 'action=validate&mode=list' );
293         }
294
295         # This function returns a link text to the page validation statistics of a single revision
296         function getRevisionStatsLink( &$article, $revision ) {
297                 global $wgUser;
298                 $sk = $wgUser->getSkin();
299                 $nt = $article->getTitle();
300                 $text = $this->getParsedWiki( wfMsg('val_revision_stats_link') );
301                 $query = "action=validate&mode=details&revision={$revision}";
302                 return '(' . $sk->makeKnownLinkObj( $nt, $text, $query ) . ')';
303         }
304
305         # This function returns a link text to the user rating statistics page
306         function getUserRatingsLink( $user, $text ) {
307                 global $wgUser;
308                 $sk = $wgUser->getSkin();
309                 if( $user == 0 ) $user = $wgUser->getName();
310                 $nt = Title::newFromText( 'Special:Validate' );
311                 return $sk->makeKnownLinkObj( $nt, $text, 'mode=userstats&user='.urlencode($user) );
312         }
313
314         # Returns the timestamp of a revision based on the revision number
315         function getRevisionTimestamp( $rev_id ) {
316                 $rev = $this->getRevisionFromId( $rev_id );
317                 return $rev->rev_timestamp;
318         }
319
320         # Returns the revision number of a revision based on the timestamp
321         function getRevisionId( $ts ) {
322                 $rev = $this->getRevisionFromTimestamp( $ts );
323                 return $rev->rev_id;
324         }
325
326
327         # HTML generation functions from this point on
328         
329         # Returns the metadata string for a revision
330         function getMetadata( $rev_id, &$article ) {
331                 global $wgUser;
332                 $sk = $wgUser->getSkin();
333
334                 $metadata = "";
335                 $x = $this->getRevisionFromId($rev_id);
336                 $metadata .= wfTimestamp( TS_DB, $x->rev_timestamp );
337                 $metadata .= " by ";
338                 if( $x->rev_user == 0 ) {
339                         $metadata .= $x->rev_user_text;
340                 } else {
341                         $u = new User;
342                         $u->setId( $x->rev_user );
343                         $u->setName( $x->rev_user_text );
344                         $nt = $u->getUserPage();
345                         $metadata .= $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
346                 }
347                 $metadata .= ': '. $sk->commentBlock( $x->rev_comment, $article->getTitle() );
348                 return $metadata;
349         }
350         
351         # Generates a link to the topic description
352         function getTopicLink($s) {
353                 $t = Title::newFromText ( wfMsg ( 'val_topic_desc_page' ) ) ;
354                 # FIXME: Why doesn't this use standard linking code?
355                 $r = "<a href=\"" ;
356                 $r .= $t->escapeLocalURL () ;
357                 $r .= "#" . urlencode ( $s ) ;
358                 $r .= "\">{$s}</a>" ;
359                 return $r ;
360         }
361                 
362         # Generates HTML from a wiki text, e.g., a wfMsg
363         function getParsedWiki ( $text ) {
364                 global $wgOut, $wgTitle, $wgParser ;
365                 $parserOutput = $wgParser->parse( $text , $wgTitle, $wgOut->mParserOptions,false);
366                 return $parserOutput->getText() ;
367         }
368
369         # Generates a form for a single revision
370         function getRevisionForm( &$article, $idx, &$data, $focus = false ) {
371                 # Fill data with blank values
372                 $ts = $idx;
373                 $revision = $this->getRevisionId( $ts );
374                 foreach( $this->topicList as $x => $y ) {
375                         if( !isset( $data[$x] ) ) {
376                                 $data[$x]->value = 0;
377                                 $data[$x]->comment = "";
378                         }
379                 }
380                 ksort( $data ) ;                
381         
382                 # Generate form
383                 $table_class = $focus ? 'revisionform_focus' : 'revisionform_default';
384                 $ret = "<form method='post'><table class='{$table_class}'>\n";
385                 $head = "Revision #" . $revision;
386                 $link = $this->getRevisionLink( $article, $revision );
387                 $metadata = $this->getMetadata( $revision, $article );
388                 $ret .= "<tr><th colspan='3'>" . $head . " ({$link}) {$metadata}</th></tr>\n";
389                 $line = 0;
390                 foreach( $data as $x => $y ) {
391                         $line = 1 - $line;
392                         $trclass = $line == 1 ? "revision_tr_first" : "revision_tr_default";
393                         $idx = "_{$revision}[{$x}]";
394                         $ret .= "<tr class='{$trclass}'>\n";
395                         $ret .= "<th>\n";
396                         $ret .= $this->getTopicLink ( $this->topicList[$x]->val_comment ) ;
397                         $ret .= "</th>\n";
398                         
399                         $tlx = $this->topicList[$x];
400                         $vote = "";
401                         $max = $tlx->val_value;
402                         for( $a = 0 ; $a <= $max ; $a++ ) {
403                                 if( $a == 0 ) {
404                                         $vote .= wfMsg ( "val_noop" );
405                                 }
406                                 $vote .= "<input type='radio' name='re_v{$idx}' value='{$a}'";
407                                 if( $a == $y->value ) {
408                                         $vote .= " checked='checked'";
409                                 }
410                                 $vote .= " />";
411                                 if( $max == 2 && $a == 1 ) {
412                                         $vote .= wfMsg( "val_no" ) . " ";
413                                 } elseif( $max == 2 && $a == 2 ) {
414                                         $vote .= wfMsg( "val_yes" );
415                                 } elseif( $a != 0 ) {
416                                         $vote .= $a . " ";
417                                 }
418                                 if ( $a == 0 ) {
419                                         $vote .= " &nbsp; ";
420                                 }
421                         }                       
422                         $ret .= "<td>{$vote}</td>\n";
423                         
424                         $ret .= "<td><input size='50' style='width:98%' maxlength='250' type='text' name='re_c{$idx}' value='{$y->comment}'/>";
425                         $ret .= "</td></tr>\n";
426                 }
427                 $checked = $focus ? " checked='checked'" : "";
428                 $ret .= "<tr><td colspan='3'>\n";
429                 $ret .= "<input type='checkbox' name='re_merge_{$revision}' value='1'{$checked} />" . $this->getParsedWiki( wfMsg( 'val_merge_old' ) ) . " \n";
430                 $ret .= "<input type='checkbox' name='re_clear_{$revision}' value='1'{$checked} />" . $this->getParsedWiki( wfMsg( 'val_clear_old' ) ) . " \n";
431                 $ret .= "<input type='submit' name='re_submit[{$revision}]' value=\"" . wfMsgHtml( "ok" ) . "\" />\n";
432                 
433                 if( $focus ) $ret .= "<br/>\n<small>" . $this->getParsedWiki ( wfMsg( "val_form_note" ) ) . "</small>";
434                 $ret .= "</td></tr>\n";
435                 $ret .= "</table></form>\n\n";
436                 return $ret;
437         }
438         
439
440         # Generates the page from the validation tab
441         function validatePageForm( &$article, $revision ) {
442                 global $wgOut, $wgRequest, $wgUser;
443                 
444                 $ret = "";
445                 $this->page_id = $article->getID();
446                 $this->topicList = $this->getTopicList();
447                 $this->voteCache = $this->getVoteList( $article->getID() );
448                 
449                 # Check for POST data
450                 $re = $wgRequest->getArray( 're_submit' );
451                 if ( isset( $re ) ) {
452                         $id = array_keys( $re );
453                         $id = $id[0] ; # $id is now the revision number the user clicked "OK" for
454                         $clearOldRev = $wgRequest->getVal( "re_clear_{$id}", 0 );
455                         $mergeOldRev = $wgRequest->getVal( "re_merge_{$id}", 0 );
456                         $this->updateRevision( $article, $id );
457                         if( $mergeOldRev ) {
458                                 $this->mergeOldRevisions( $article, $id );
459                         }
460                         if( $clearOldRev ) {
461                                 $this->clearOldRevisions( $article, $id );
462                         }
463                         $ret .= '<p class="revision_saved">' . $this->getParsedWiki( wfMsg( 'val_revision_changes_ok' ) ) . "</p>";
464                 } else {
465                         $ret .= $this->getParsedWiki( wfMsg ('val_votepage_intro') );
466                 }
467                 
468                 # Make sure the requested revision exists
469                 $rev = $this->getRevisionFromId($revision);
470                 $ts = $rev->rev_timestamp;
471                 if( !isset( $this->voteCache[$ts] ) ) {
472                         $this->voteCache[$ts] = array();
473                 }
474                 
475                 # Sort revisions list, newest first
476                 krsort( $this->voteCache );
477                 
478                 # Output
479                 $title = $article->getTitle();
480                 $title = $title->getPrefixedText();
481                 $wgOut->setPageTitle( wfMsg( 'val_rev_for', $title ) );
482                 foreach( $this->voteCache as $x => $y ) {
483                         $ret .= $this->getRevisionForm( $article, $x, $y, $x == $ts );
484                         $ret .= "<br/>\n";
485                 }
486                 $ret .= $this->getStatisticsLink( $article );
487                 $ret .= "<p>" . $this->getUserRatingsLink( $wgUser->getID(), wfMsg( 'val_show_my_ratings' ) ) . "</p>";
488                 return $ret ;   
489         }
490         
491         # This function performs the "management" mode on Special:Validate
492         function manageTopics() {
493                 global $wgRequest;
494                 $this->topicList = $this->getTopicList();
495                 
496                 $iamsure = $wgRequest->getVal( "iamsure", "0" ) == 1;
497                 
498                 if( $iamsure && $wgRequest->getVal( "m_add", "--" ) != "--" ) {
499                         $new_topic = $wgRequest->getVal( "m_topic" );
500                         $new_limit = $wgRequest->getVal( "m_limit" );
501                         if( $new_topic != "" && $new_limit > 1 ) {
502                                 $this->addTopic( $new_topic, $new_limit );
503                         }
504                 }
505
506                 $da = $wgRequest->getArray( "m_del" );
507                 if( $iamsure && isset( $da ) && count( $da ) > 0 ) {
508                         $id = array_keys( $da );
509                         $id = array_shift( $id );
510                         $this->deleteTopic( $id );
511                 }
512                 
513                 # FIXME: Wikitext this
514                 $r = "<p>" . $this->getParsedWiki( wfMsg( 'val_warning' ) ) . "</p>\n";
515                 $r .= "<form method='post'>\n";
516                 $r .= "<table>\n";
517                 $r .= "<tr>" . wfMsg( 'val_list_header' ) . "</tr>\n";
518                 foreach( $this->topicList as $x => $y ) {
519                         $r .= "<tr>\n";
520                         $r .= "<th>{$y->val_type}</th>\n";
521                         $r .= "<td>" . $this->getTopicLink ( $y->val_comment ) . "</td>\n";
522                         $r .= "<td>1 .. <b>" . intval( $y->val_value ) . "</b></td>\n";
523                         $r .= "<td><input type='submit' name='m_del[" . intval( $x ) . "]' value='" . htmlspecialchars( wfMsg( 'val_del' ) ) . "'/></td>\n";
524                         $r .= "</tr>\n";
525                 }
526                 $r .= "<tr>\n";
527                 $r .= "<td></td>\n";
528                 $r .= '<td><input type="text" name="m_topic" value=""/></td>' . "\n";
529                 $r .= '<td>1 .. <input type="text" name="m_limit" value="" size="4"/></td>' . "\n";
530                 $r .= '<td><input type="submit" name="m_add" value="' . htmlspecialchars( wfMsg( 'val_add' ) ) . '"/></td>' . "\n";
531                 $r .= "</tr></table>\n";
532                 $r .= '<p><input type="checkbox" name="iamsure" id="iamsure" value="1"/>';
533                 $r .= '<label for="iamsure">' . $this->getParsedWiki( wfMsg( 'val_iamsure' ) ) . "</label></p>\n";
534                 $r .= "</form>\n";
535                 return $r;
536         }
537         
538         # Generates an ID for both logged-in users and anons; $res is an object from an SQL query
539         function make_user_id( &$res ) {
540                 return $res->val_user == 0 ? $res->val_ip : $res->val_user;
541         }
542
543         function showDetails( &$article, $revision ) {
544                 global $wgOut, $wgUser;
545                 $this->page_id = $article->getID();
546                 $this->topicList = $this->getTopicList();
547
548                 $sk = $wgUser->getSkin();
549                 $title = $article->getTitle();
550                 $wgOut->setPageTitle( str_replace( '$1', $title->getPrefixedText(), wfMsg( 'val_validation_of' ) ) );
551                 
552                 $data = array();
553                 $users = array();
554                 $topics = array();
555
556                 # Collecting statistic data
557                 $db =& wfGetDB( DB_SLAVE );
558                 $res = $db->select( 'validate', '*', array( 'val_page' => $this->page_id, 'val_revision' => $revision ), 'SpecialValidate::showDetails' );
559                 while( $x = $db->fetchObject($res) ) {
560                         $data[$this->make_user_id($x)][$x->val_type] = $x;
561                         $users[$this->make_user_id($x)] = true;
562                         $topics[$x->val_type] = true;
563                 }
564                 $db->freeResult($res);
565                 
566                 # Sorting lists of topics and users
567                 ksort( $users );
568                 ksort( $topics );
569                 
570                 $ts = $this->getRevisionTimestamp( $revision );
571                 $url = $this->getRevisionLink( $article, $revision, wfTimestamp( TS_DB, $ts ) );
572
573                 # Table headers
574                 $ret = "" ;                     
575                 $ret .= "<p><b>" . str_replace( '$1', $url, wfMsg( 'val_revision_of' ) ) . "</b></p>\n";
576                 $ret .= "<table>\n";
577                 $ret .= "<tr><th>" . $this->getParsedWiki ( wfMsg('val_details_th') ) . "</th>" ;
578                 
579                 foreach( $topics as $t => $dummy ) {
580                         $ret .= '<th>' . $sk->commentBlock( $this->topicList[$t]->val_comment, $article->getTitle() ) . '</th>';
581                 }
582                 $ret .= "</tr>\n";
583
584                 # Table data
585                 foreach( $users as $u => $dummy ) { # Every row a user
586                         $ret .= "<tr>";
587                         $ret .= "<th>";
588                         if( !User::IsIP( $u ) ) { # Logged-in user rating
589                                 $ret .= $this->getUserRatingsLink( $u, User::whoIs( $u ) );
590                         } else { # Anon rating
591                                 $ret .= $this->getUserRatingsLink( $u, $u );
592                         }
593                         $ret .= "</th>";
594                         foreach( $topics as $t => $dummy ) { # Every column a topic
595                                 if( !isset( $data[$u][$t] ) ) {
596                                         $ret .= "<td/>";
597                                 } else {
598                                         $ret .= "<td>";
599                                         $ret .= $data[$u][$t]->val_value;
600                                         if( $data[$u][$t]->val_comment != "" ) {
601                                                 $ret .= ' ' . $sk->commentBlock( $data[$u][$t]->val_comment, $article->getTitle() );
602                                         }
603                                         $ret .= "</td>";
604                                 }
605                         }
606                         $ret .= "</tr>";
607                 }
608                 $ret .= "</table>";
609                 $ret .= "<p>" . $this->getStatisticsLink( $article ) . "</p>";
610                 $ret .= "<p>" . $this->getUserRatingsLink( $wgUser->getID(), wfMsg( 'val_show_my_ratings' ) ) . "</p>";
611                 
612                 return $ret;
613         }
614         
615         # XXX This should be paged
616         function showList( &$article ) {
617                 global $wgOut, $wgUser;
618                 $this->page_id = $article->getID();
619                 $this->topicList = $this->getTopicList();
620
621                 $title = $article->getTitle();
622                 $wgOut->setPageTitle( str_replace( '$1', $title->getPrefixedText(), wfMsg( 'val_validation_of' ) ) );
623                 
624                 # Collecting statistic data
625                 $db =& wfGetDB( DB_SLAVE );
626                 $res = $db->select( 'validate', '*', array( "val_page" => $this->page_id ), 'SpecialValidate::showList' );
627
628                 $statistics = array();
629                 while( $vote = $db->fetchObject($res) ) {
630                         $ts = $this->getRevisionTimestamp($vote->val_revision);
631                         if ( !isset ( $statistics[$ts] ) ) $statistics[$ts] = array () ;
632                         if ( !isset ( $statistics[$ts][$vote->val_type]->count ) ) $statistics[$ts][$vote->val_type]->count = 0 ;
633                         if ( !isset ( $statistics[$ts][$vote->val_type]->sum ) ) $statistics[$ts][$vote->val_type]->sum = 0 ;
634                         $statistics[$ts][$vote->val_type]->count++;
635                         $statistics[$ts][$vote->val_type]->sum += $vote->val_value;
636                 }
637                 $db->freeResult($res);
638
639                 krsort( $statistics );
640                 
641                 $ret = "<table><tr>\n";
642                 $ret .= "<th>" . $this->getParsedWiki( wfMsg( "val_revision" ) ) . "</th>\n";
643                 foreach( $this->topicList as $topic ) {
644                         $ret .= "<th>" . $this->getTopicLink($topic->val_comment) . "</th>";
645                 }
646                 $ret .= "</tr>\n";
647
648                 foreach( $statistics as $ts => $data ) {
649                         $rev_id = $this->getRevisionId( $ts );
650                         $revision_link = $this->getRevisionLink( $article, $rev_id, wfTimestamp( TS_DB, $ts ) );
651                         $details_link = $this->getRevisionStatsLink( $article, $rev_id );
652                         $ret .= "<tr><td>{$revision_link} {$details_link}</td>";
653                         foreach( $this->topicList as $topicType => $topic ) {
654                                 if( isset( $data[$topicType] ) ) {
655                                         $stats = $data[$topicType];
656                                         $average = $stats->count == 0 ? 0 : $stats->sum / $stats->count;
657                                         $ret .= sprintf( "<td><b>%1.1f</b> (%d)</td>", $average, $stats->count );
658                                 } else {
659                                         $ret .= "<td></td>";
660                                 }
661                         }
662                         $ret .= "</tr>\n";
663                 }
664                 $ret .= "</table>\n";
665                 $ret .= "<p>" . $this->getUserRatingsLink( $wgUser->getID(), wfMsg( 'val_show_my_ratings' ) ) . "</p>";
666                 return $ret;
667         }
668         
669         function getRatingText( $value, $max ) {
670                 if( $max == 2 && $value == 1 ) {
671                         $ret = wfMsg ( "val_no" ) . " ";
672                 } elseif( $max == 2 && $value == 2 ) {
673                         $ret = wfMsg( "val_yes" );
674                 } elseif( $value != 0 ) {
675                         $ret = wfMsg( "val_of", $value, $max ) . " ";
676                 } else {
677                         $ret = "";
678                 }
679                 return $ret;
680         }
681
682         # XXX This should be paged
683         function showUserStats( $user ) {
684                 global $wgOut, $wgUser;
685                 $this->topicList = $this->getTopicList();
686                 $data = $this->getAllVoteLists( $user );
687                 $sk = $wgUser->getSkin();
688                 
689                 if( $user == $wgUser->getID() ) {
690                         $wgOut->setPageTitle ( wfMsg ( 'val_my_stats_title' ) );
691                 } elseif( !User::IsIP( $user ) ) {
692                         $wgOut->setPageTitle( wfMsg( 'val_user_stats_title', User::whoIs( $user ) ) );
693                 } else {
694                         $wgOut->setPageTitle( wfMsg( 'val_user_stats_title', $user ) );
695                 }
696                 
697                 $ret = "<table>\n";
698                 foreach( $data as $articleid => $revisions ) {
699                         $title = Title::newFromID( $articleid );
700                         $ret .= "<tr><th colspan='4'>";
701                         $ret .= $sk->makeKnownLinkObj( $title, $title->getEscapedText() );
702                         $ret .= "</th></tr>";
703                         krsort( $revisions );
704                         foreach( $revisions as $revid => $revision ) {
705                                 $url = $title->getLocalURL( "oldid={$revid}" );
706                                 $ret .= "<tr><th>";
707                                 $ret .= $sk->makeKnownLinkObj( $title, wfMsg('val_revision_number', $revid ), "oldid={$revid}" );
708                                 $ret .= "</th>";
709                                 ksort( $revision );
710                                 $initial = true;
711                                 foreach( $revision as $topic => $rating ) {
712                                         if( !$initial ) {
713                                                 $ret .= "<tr><td/>";
714                                         }
715                                         $initial = false;
716                                         $ret .= "<td>" . $this->getTopicLink ( $this->topicList[$topic]->val_comment ) . "</td>";
717                                         $ret .= "<td>" . $this->getRatingText( $rating->val_value, $this->topicList[$topic]->val_value ) . "</td>";
718                                         $ret .= "<td>" . $sk->commentBlock( $rating->val_comment ) . "</td>";
719                                         $ret .= "</tr>";
720                                 }
721                         }
722                         $ret .= "</tr>";
723                 }
724                 $ret .= "</table>";
725                 
726                 return $ret;
727         }
728
729 }
730
731 /**
732  * constructor
733  */
734 function wfSpecialValidate( $page = '' ) {
735         global $wgOut, $wgRequest, $wgUseValidation, $wgUser, $wgContLang;
736         
737         if( !$wgUseValidation ) {
738                 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
739                 return;
740         }
741
742 /*
743         # Can do?
744         if( ! $wgUser->isAllowed('change_validation') ) {
745                 $wgOut->sysopRequired();
746                 return;
747         }
748 */      
749
750         $mode = $wgRequest->getVal( "mode" );
751         $skin = $wgUser->getSkin();
752
753         
754         if( $mode == "manage" ) {
755                 $v = new Validation();
756                 $html = $v->manageTopics();
757         } elseif( $mode == "userstats" ) {
758                 $v = new Validation();
759                 $user = $wgRequest->getVal( "user" );
760                 $html = $v->showUserStats( $user );
761         } else {
762                 $html = "$mode";
763                 $html .= "<ul>\n";
764
765                 $t = Title::newFromText( "Special:Validate" );
766                 $url = $t->escapeLocalURL( "mode=manage" );
767                 $html .= "<li><a href=\"" . $url . "\">Manage</a></li>\n";
768
769                 $html .= "</ul>\n";
770         }
771
772         $wgOut->addHTML( $html );
773 }
774
775 ?>