removed crippled enotif code from checkPassword(), this is roughly how it was in...
[lhc/web/wiklou.git] / includes / SpecialValidate.php
index 735de01..dd8fa13 100644 (file)
 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 # http://www.gnu.org/copyleft/gpl.html
 
-class Validation
-       {
-       
-       function find_this_version ( $article_title , &$article_time , &$id , &$tab )
-               {
-               $id = "" ;
-               $tab = "" ;
-               $sql = "SELECT cur_id,cur_timestamp FROM cur WHERE cur_namespace=0 AND cur_title='" . wfStrencode( $article_title ) . "'" ;
+/**
+ *
+ * @package MediaWiki
+ * @subpackage SpecialPage
+ */
+
+/**
+ *
+ * @package MediaWiki
+ * @subpackage SpecialPage
+ */
+class Validation {
+       var $topicList ;
+       var $voteCache ;
+       var $rev2date ;
+       var $date2ref ;
+
+       # Reads all revision information of the specified article
+       function prepareRevisions ( $id ) {
+               global $wgDBprefix ;
+               $this->rev2date = array () ;
+               $this->date2rev = array () ;
+               $sql = "SELECT * FROM {$wgDBprefix}revision WHERE rev_page='{$id}'" ;
                $res = wfQuery( $sql, DB_READ );
-               if( $s = wfFetchObject( $res ) )
-                       {
-                       if ( $article_time == "" ) $article_time = $s->cur_timestamp ; # No timestamp = current version
-                       if ( $article_time == $s->cur_timestamp ) # Means current version
-                               {
-                               $tab = "cur" ;
-                               $id = $s->cur_id ;
-                               }
-                       }
-                       
-               if ( $id == "" )
-                       {
-                       $sql = "SELECT old_id FROM old WHERE old_namespace=0 AND old_title='" . wfStrencode( $article_title ) .
-                               "' AND old_timestamp='" . wfStrencode( $article_time ) . "'" ;
-                       $res = wfQuery( $sql, DB_READ );
-                       if( $s = wfFetchObject( $res ) )
-                               {
-                               $tab = "old" ;
-                               $id = $s->old_id ;
-                               }
-                       }
+               while( $x = wfFetchObject( $res ) ) {
+                       $this->rev2date[$x->rev_id] = $x ;
+                       $this->date2rev[$x->rev_timestamp] = $x ;
                }
-               
-       function get_prev_data ( $user_id , $article_title , $article_timestamp = "" )
-               {
+               }
+
+       # Returns a HTML link to the specified article revision
+       function getVersionLink( &$article , $revision , $text = "" ) {
+               $t = $article->getTitle() ;
+               if ( $text == "" ) $text = wfMsg("val_view_version");
+               $ret = "<a href=\"" . $t->getLocalURL ( "oldid={$revision}" ) . "\">" . $text . "</a>" ;
+               return $ret ;
+       }
+
+       # Returns an array containing all topics you can vote on
+       function getTopicList () {
+               global $wgDBprefix ;
                $ret = array () ;
-               $sql = "SELECT * FROM validate WHERE val_user='" . wfStrencode( $user_id ) .
-                       "' AND val_title='" . wfStrencode( $article_title ) . "'" ;
-               if ( $article_timestamp != "" ) $sql .= " AND val_timestamp='" . wfStrencode( $article_timestamp ) . "'" ;
+               $sql = "SELECT * FROM {$wgDBprefix}validate WHERE val_page=0" ;
                $res = wfQuery( $sql, DB_READ );
-               while( $s = wfFetchObject( $res ) ) $ret[$s->val_timestamp][$s->val_type] = $s ;
-               return $ret ;
+               while( $x = wfFetchObject( $res ) ) {
+                       $ret[$x->val_type] = $x ;
                }
+               ksort ( $ret ) ;
+               return $ret ;
+       }
        
-       function validate_form ( $article_title = "" )
-               {
-               global $wgOut, $wgLang, $wgUser, $wgArticle ;
-               
-               if ( $wgUser->getID() == 0 ) # Anon
-                       {
-                       $wgOut->addHTML ( wfMsg ( 'val_no_anon_validation' ) . $this->getPageStatistics ( $article_title ) ) ;
-                       return ;
+       # Merges one dataset into another
+       function mergeInto ( &$source , &$dest ) {
+               $ret = false ;
+               foreach ( $source AS $x => $y ) {
+                       $doit = false ;
+                       if ( !isset ( $dest[$x] ) ) $doit = true ;
+                       else if ( $dest[$x]->value == 0 ) $doit = true ;
+                       if ( $doit ) {
+                               $dest[$x] = $y ;
+                               $ret = true ;
                        }
-                       
-               $validationtypes = $wgLang->getValidationTypes() ;
-               if ( $article_title == "" )
-                       {
-                       $article_title = $_GET['article_title'] ;
-                       $heading = "<h1>" . $article_title . "</h1>\n" ;
+               }
+               if ( $ret ) ksort ( $dest ) ;
+               return $ret ;
+       }
+
+       # Merges all votes prior to the given revision into it
+       function mergeOldRevisions ( &$article , $revision ) {
+               $tmp = $this->voteCache ;
+               krsort ( $tmp ) ;
+               $update = false ;
+               $ts = $this->getTimestamp($revision) ;
+               $data = $this->voteCache[$ts] ;
+               foreach ( $tmp AS $x => $y ) {
+                       if ( $x < $ts ) {
+                               if ( $this->mergeInto ( $y , $data ) ) $update = true ;
                        }
-               else $heading = "" ;
-               $article_time = "" ;
-               if ( isset ( $_GET['timestamp'] ) ) $article_time = $_GET['timestamp'] ;
-               else $article_time = "" ;
-               $article = Title::newFromText ( $article_title ) ;
+               }
+               if ( $update ) $this->setRevision ( $article , $revision , $data ) ;
+       }
+       
+       # Clears all votes prior to the given revision
+       function clearOldRevisions ( &$article , $revision ) {
+               $tmp = $this->voteCache ;
+               $ts = $this->getTimestamp($revision);
+               foreach ( $tmp AS $x => $y ) {
+                       if ( $x < $ts ) $this->deleteRevision ( $article , $this->getRevisionNumber($x) ) ;
+               }
+       }
+       
+       # Updates the votes for the given revision from the FORM data
+       function updateRevision ( &$article , $revision ) {
+               global $wgUser, $wgRequest ;
                
-               # Now we get all the "votes" for the different versions of this article for this user
-               $val = $this->get_prev_data ( $wgUser->getID() , $article_title ) ;
+               if ( isset ( $this->voteCache[$this->getTimestamp($revision)] ) ) $data = $this->voteCache[$this->getTimestamp($revision)] ;
+               else $data = array () ;
+               $nv = $wgRequest->getArray ( "re_v_{$revision}" , array() ) ;
+               $nc = $wgRequest->getArray ( "re_c_{$revision}" , array() ) ;
                
-               # No votes for this version, initial data
-               if ( !isset ( $val[$article_time] ) )
-                       {
-                       if ( $article_time == "" )
-                               {
-                               $res = wfQuery( "select cur_timestamp FROM cur WHERE cur_title='" .
-                                       wfStrencode( $article_title ) . "' AND cur_namespace=0", DB_READ );
-                               if ( $s = wfFetchObject( $res ) ) $article_time = $s->cur_timestamp ;
-                               }
-                       $val[$article_time] = array () ;
+               foreach ( $nv AS $x => $y ) {
+                       $data[$x]->value = $y ;
+                       $data[$x]->comment = $nc[$x] ;
+               }
+               krsort ( $data ) ;
+               
+               $this->setRevision ( $article , $revision , $data ) ;
+       }
+       
+       # Sets a specific revision to both cache and database
+       function setRevision ( &$article , $revision , &$data ) {
+               global $wgUser , $wgDBprefix ;
+               $this->deleteRevision ( $article , $revision ) ;
+               $this->voteCache[$this->getTimestamp($revision)] = $data ;
+               foreach ( $data AS $x => $y ) {
+                       if ( $y->value > 0 ) {
+                               $sql = "INSERT INTO {$wgDBprefix}validate (val_user,val_page,val_revision,val_type,val_value,val_comment,val_ip) VALUES ('" ;
+                               $sql .= $wgUser->getID() . "','" ;
+                               $sql .= $article->getID() . "','" ;
+                               $sql .= $revision . "','" ;
+                               $sql .= $x . "','" ;
+                               $sql .= $y->value . "','" ;
+                               $sql .= Database::strencode ( $y->comment ) . "','" ;
+                               if ( $wgUser->isAnon() ) $sql .= $wgUser->getName() ;
+                               $sql .= "')" ;
+                               $res = wfQuery( $sql, DB_WRITE );
                        }
+               }
+       }
+       
+       # This function returns a MySQL statement to identify the current user
+       function identifyMe ( $user = "" ) {
+               global $wgUser ;
+               if ( $user == "" ) $user = $wgUser->GetID() ;
+               if ( User::isIP ( $user ) ) {
+                       return "(val_user='0' AND val_ip='{$user}')" ;
+               } else {
+                       return "(val_user='{$user}')" ;
+               }
+               }
+       
+       # Deletes a specific vote set in both cache and database
+       function deleteRevision ( &$article , $revision ) {
+               global $wgUser , $wgDBprefix ;
+               $ts = $this->getTimestamp ( $revision ) ;
+               if ( !isset ( $this->voteCache[$ts] ) ) return ; # Nothing to do
+               $sql = "DELETE FROM {$wgDBprefix}validate WHERE" . $this->identifyMe() . " AND " ;
+               $sql .= " val_page='" . $article->getID() . "' AND val_revision='{$revision}'" ;
+               $res = wfQuery( $sql, DB_WRITE );
+               unset ( $this->voteCache[$ts] ) ;
+       }
+       
+       # Reads the entire vote list for this user for the given article
+       function getVoteList ( $id , $user = "" ) {
+               global $wgUser , $wgDBprefix ;
+               if ( $user == "" ) $user = $wgUser->GetID() ;
+               $r = array () ; # Revisions
+               $sql = "SELECT * FROM {$wgDBprefix}validate WHERE val_page=" . $id . " AND " . $this->identifyMe($user) ;
+               $res = wfQuery( $sql, DB_READ );
+               while( $x = wfFetchObject( $res ) ) {
+                       #$y = $x->val_revision ;
+                       $y = $this->rev2date[$x->val_revision] ;
+                       $y = $y->rev_timestamp ;
+                       if ( !isset($r[$y]) ) $r[$y] = array () ;
+                       $r[$y][$x->val_type]->value = $x->val_value ;
+                       $r[$y][$x->val_type]->comment = $x->val_comment ;
+               }               
+               return $r ;
+       }
+       
+       # Reads the entire vote list for this user for all articles
+       function getAllVoteLists ( $user ) {
+               global $wgDBprefix ;
+               $r = array () ; # Revisions
+               $sql = "SELECT * FROM {$wgDBprefix}validate WHERE " . $this->identifyMe ( $user ) ;
+               $res = wfQuery( $sql, DB_READ );
+               while( $x = wfFetchObject( $res ) ) {
+                       $a = $x->val_page ;
+                       $y = $x->val_revision ;
+                       if ( !isset ( $r[$a] ) ) $r[$a] = array () ;
+                       if ( !isset($r[$a][$y]) ) $r[$a][$y] = array () ;
+                       $r[$a][$y][$x->val_type] = $x ;
+               }               
+               return $r ;
+       }
+       
+       # This functions adds a topic to the database
+       function addTopic ( $topic , $limit ) {
+               global $wgDBprefix ;
+               $a = 1 ;
+               while ( isset ( $this->topicList[$a] ) ) $a++ ;
+               $sql = "INSERT INTO {$wgDBprefix}validate (val_user,val_page,val_revision,val_type,val_value,val_comment,val_ip) VALUES (" ;
+               $sql .= "'0','0','0','{$a}','{$limit}','" ;
+               $sql .= Database::strencode ( $topic ) . "','')" ;
+               $res = wfQuery( $sql, DB_WRITE );
+               $x->val_user = $x->val_page = $x->val_revision = 0 ;
+               $x->val_type = $a ;
+               $x->val_value = $limit ;
+               $x->val_comment = $topic ;
+               $x->val_ip = "" ;
+               $this->topicList[$a] = $x ;
+               ksort ( $this->topicList ) ;
+       }
 
-               krsort ( $val ) ; # Newest versions first
+       # This functions adds a topic to the database
+       function deleteTopic ( $id ) {
+               global $wgDBprefix ;
+               $sql = "DELETE FROM {$wgDBprefix}validate WHERE val_type='{$id}'" ;
+               $res = wfQuery( $sql, DB_WRITE );
+               unset ( $this->topicList[$id] ) ;
+       }
+       
+       # This function returns a link text to the page validation statistics
+       function link2statistics ( &$article ) {
+               $nt = $article->getTitle();
+               $url = htmlspecialchars( $nt->getLocalURL( "action=validate&mode=list" ) );
+               return wfMsg ( 'val_rev_stats_link', $nt->getPrefixedText(), $url );
+       }
 
-               # User has clicked "Doit" before, so evaluate form
-               if ( isset ( $_POST['doit'] ) )
-                       {
-                       $oldtime = $_POST['oldtime'] ;
-                       if ( !isset ( $val["{$oldtime}"] ) ) $val["{$oldtime}"] = array () ;
-                       
-                       # Reading postdata
-                       $postrad = array () ;
-                       $poscomment = array () ;
-                       for ( $idx = 0 ; $idx < count ( $validationtypes) ; $idx++ )    
-                               {
-                               $postrad[$idx] = $_POST["rad{$idx}"] ;
-                               $postcomment[$idx] = $_POST["comment{$idx}"] ;
-                               }
+       # This function returns a link text to the page validation statistics of a single revision
+       function link2revisionstatistics ( &$article , $revision ) {
+               $nt = $article->getTitle();
+               $url = htmlspecialchars( $nt->getLocalURL( "action=validate&mode=details&revision={$revision}" ) );
+               return wfMsg ( 'val_revision_stats_link', $url );
+       }
+
+       # This function returns a link text to the user rating statistics page
+       function link2userratings ( $user , $text ) {
+               global $wgUser ;
+               if ( $user == 0 ) $user = $wgUser->GetName() ;
+               $nt = Title::newFromText ( "Special:Validate" ) ;
+               $url = htmlspecialchars( $nt->getLocalURL( "mode=userstats&user={$user}" ) );
+               return "<a href=\"{$url}\">{$text}</a>" ;
+       }
+
+       # Returns the timestamp of a revision based on the revision number
+       function getTimestamp ( $revision ) {
+               $ts = $this->rev2date[$revision] ;
+               $ts = $ts->rev_timestamp ;
+               return $ts ;
+       }
+
+       # Returns the revision number of a revision based on the timestamp
+       function getRevisionNumber ( $ts ) {
+               $revision = $this->date2rev[$ts] ;
+               $revision = $revision->rev_id ;
+               return $revision ;
+       }
+
+
+       # HTML generation functions from this point on
+       
+       # Returns the metadata string for a revision
+       function getMetadata ( $idx ) {
+               $metadata = "" ;
+               $x = $this->rev2date[$idx] ;
+               $metadata .= wfTimestamp ( TS_DB , $x->rev_timestamp ) ;
+               $metadata .= " by " ;
+               if ( $x->rev_user == 0 ) {
+                       $metadata .= $x->rev_user_text ;
+               } else {
+                       $u = new User ;
+                       $u->setId ( $x->rev_user ) ;
+                       $u->setName ( $x->rev_user_text ) ;
+                       $nt = $u->getUserPage() ;
+                       $url = "<a href='" . $nt->getLocalUrl () . "'>" . $nt->getText() . "</a>" ;
+                       $metadata .= $url ;
+               }
+               $metadata .= " : <small>\"" . htmlspecialchars ( $x->rev_comment ) . "\"</small>" ;
+               return $metadata ;
+       }
+
+       # Generates a form for a single revision
+       function getRevisionForm ( &$article , $idx , &$data , $focus = false ) {
+               # Fill data with blank values
+               $ts = $idx ;
+               $revision = $this->getRevisionNumber ( $ts ) ;
+               foreach ( $this->topicList AS $x => $y ) {
+                       if ( !isset ( $data[$x] ) ) {
+                               $data[$x]->value = 0 ;
+                               $data[$x]->comment = "" ;
+                       }
+               }
+               ksort ( $data ) ;               
+       
+               # Generate form
+               $ret = "<form method='post'>" ;
+               $ret .= "<table border='1' cellspacing='0' cellpadding='2'" ;
+               if ( $focus ) $ret .= " style='background-color:#00BBFF'" ;
+               $ret .= ">\n" ;
+               $head = "Revision #" . $revision ;
+               $link = " " . $this->getVersionLink ( $article , $revision ) ;
+               $metadata = $this->getMetadata ( $revision ) ;
+               $ret .= "<tr><th align='left' colspan='3'>" . $head . " ({$link}) {$metadata}</th></tr>\n" ;
+               $line = 0 ;
+               foreach ( $data AS $x => $y ) {
+                       $line = 1 - $line ;
+                       $col = $line == 1 ? "#DDDDDD" : "#EEEEEE" ;
+                       $idx = "_{$revision}[{$x}]" ;
+                       $ret .= "<tr bgcolor='{$col}'>\n" ;
+                       $ret .= "<th nowrap>" ;
+                       $ret .= $this->topicList[$x]->val_comment ;
+                       $ret .= "</th>\n" ;
                        
-                       # Merge others into this one
-                       if ( isset ( $_POST['merge_other'] ) && $_POST['merge_other'] == 1 )
-                               {
-                               foreach ( $val AS $time => $stuff )
-                                       {
-                                       if ( $time <> $article_time )
-                                               {
-                                               for ( $idx = 0 ; $idx < count ( $validationtypes) ; $idx++ )
-                                                       {
-                                                       $rad = $postrad[$idx] ;
-                                                       if ( isset ( $stuff[$idx] ) AND $stuff[$idx]->val_value != -1 AND $rad == -1 )
-                                                               {
-                                                               $postrad[$idx] = $stuff[$idx]->val_value ;
-                                                               $postcomment[$idx] = $stuff[$idx]->val_comment ;
-                                                               }
-                                                       }
-                                               }
-                                       }
-                               }
+                       $tlx = $this->topicList[$x] ;
+                       $vote = "" ;
+                       $max = $tlx->val_value ;
+                       for ( $a = 0 ; $a <= $max ; $a++ ) {
+                               if ( $a == 0 ) $vote .= wfMsg ( "val_noop" ) ;
+                               $vote .= "<input type='radio' name='re_v{$idx}' value='{$a}'" ;
+                               if ( $a == $y->value ) $vote .= " checked" ;
+                               $vote .= "/>" ;
+                               if ( $max == 2 && $a == 1 ) $vote .= wfMsg ( "val_no" ) . " " ;
+                               else if ( $max == 2 && $a == 2 ) $vote .= wfMsg ( "val_yes" ) ;
+                               else if ( $a != 0 ) $vote .= $a . " " ;
+                               if ( $a == 0 ) $vote .= " &nbsp; " ;
+                       }                       
+                       $ret .= "<td nowrap valign='center'>{$vote}</td>\n" ;
                        
-                       # Clear all others
-                       if ( isset ( $_POST['clear_other'] ) && $_POST['clear_other'] == 1 )
-                               {
-                               $sql = "DELETE FROM validate WHERE val_title='" . wfStrencode( $article_title ) .
-                                       "' AND val_timestamp<>'" . wfStrencode( $oldtime ) . "' AND val_user='" ;
-                               $sql .= wfStrencode( $wgUser->getID() ) . "'" ;
-                               wfQuery( $sql, DB_WRITE );
-                               $val2 = $val["{$oldtime}"] ; # Only version left
-                               $val = array () ; # So clear others
-                               $val["{$oldtime}"] = $val2 ;
-                               }
-
-                       # Delete old "votes" for this version
-                       $sql = "DELETE FROM validate WHERE val_title='" . wfStrencode( $article_title ) .
-                               "' AND val_timestamp='" . wfStrencode( $oldtime ) . "' AND val_user='" ;
-                       $sql .= wfStrencode( $wgUser->getID() ) . "'" ;
-                       wfQuery( $sql, DB_WRITE );
+                       $ret .= "<td width='100%' align='center'><input size='50' style='width:98%' maxlength='250' type='text' name='re_c{$idx}' value='{$y->comment}'/>" ;
+                       $ret .= "</td></tr>\n" ;
+               }
+               $checked = $focus ? " checked" : "" ;
+               $ret .= "<tr><td colspan='3' valign='center'>\n" ;
+               $ret .= "<input type='checkbox' name='re_merge_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_merge_old' ) . " \n" ;
+               $ret .= "<input type='checkbox' name='re_clear_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_clear_old' ) . " \n" ;
+               $ret .= "<input type='submit' name='re_submit[{$revision}]' value='" . htmlspecialchars( wfMsg("ok") ) . "'/>\n" ;
+               if ( $focus ) $ret .= "<br/>\n<small>" . wfMsg ( "val_form_note" ) . "</small>" ;
+               $ret .= "</td></tr>\n" ;
+               $ret .= "</table>\n</form>\n\n" ;
+               return $ret ;
+       }
        
-                       # Incorporate changes
-                       for ( $idx = 0 ; $idx < count ( $validationtypes) ; $idx++ ) # Changes
-                               {
-                               $comment = $postcomment[$idx] ;
-                               $rad = $postrad[$idx] ;
-                               if ( !isset ( $val["{$oldtime}"][$idx] ) ) $val["{$oldtime}"][$idx] = "" ;
-                               $val["{$oldtime}"][$idx]->val_value = $rad ;
-                               $val["{$oldtime}"][$idx]->val_comment = $comment ;
-                               if ( $rad != -1 )
-                                       {
-                                       # Store it in the database
-                                       $sql = "INSERT INTO validate (val_user,val_title,val_timestamp,val_type,val_value,val_comment) " . 
-                                                "VALUES ( '" . wfStrencode( $wgUser->getID() ) . "','" .
-                                                wfStrencode( $article_title ) . "','" .
-                                                wfStrencode( $oldtime ) . "','" . 
-                                                wfStrencode( $idx ) . "','" . 
-                                                wfStrencode( $rad ) . "','" .
-                                                wfStrencode( $comment ) . "')" ;
-                                       if ( $rad != -1 ) wfQuery( $sql, DB_WRITE );
-                                       }
-                               }
-                       $wgArticle->showArticle( "Juhuu", wfMsg( 'val_validated' ) );
-                       return ; # Show article instead of validation page
+
+       # Generates the page from the validation tab
+       function validatePageForm ( &$article , $revision ) {
+               global $wgOut, $wgRequest, $wgUser ;
+               
+               $ret = "" ;
+               $this->prepareRevisions ( $article->getID() ) ;
+               $this->topicList = $this->getTopicList() ;
+               $this->voteCache = $this->getVoteList ( $article->getID() ) ;
+               
+               # Check for POST data
+               $re = $wgRequest->getArray( 're_submit' );
+               if ( isset ( $re ) )
+                       {
+                       $id = array_keys ( $re ) ;
+                       $id = $id[0] ; # $id is now the revision number the user clicked "OK" for
+                       $clearOldRev = $wgRequest->getVal( "re_clear_{$id}" , 0 );
+                       $mergeOldRev = $wgRequest->getVal( "re_merge_{$id}" , 0 );
+                       $this->updateRevision ( $article , $id ) ;
+                       if ( $mergeOldRev ) $this->mergeOldRevisions ( $article , $id ) ;
+                       if ( $clearOldRev ) $this->clearOldRevisions ( $article , $id ) ;
+                       $ret .= "<p><font color='red'>" . wfMsg ( 'val_revision_changes_ok' ) . "</font></p>" ;
                        }
                
-               # Generating HTML
-               $html = "" ;
+               # Make sure the requested revision exists
+               $ts = $this->rev2date[$revision]->rev_timestamp ;
+               if ( !isset ( $this->voteCache[$ts] ) ) $this->voteCache[$ts] = array () ;
                
-               $skin = $wgUser->getSkin() ;
-               $staturl = $skin->makeSpecialURL ( "validate" , "mode=stat_page&article_title=" . urlencode( $article_title ) ) ;
-               $listurl = $skin->makeSpecialURL ( "validate" , "mode=list_page" ) ;
-               $html .= "<a href=\"" . htmlspecialchars( $staturl ) . "\">" . wfMsg('val_stat_link_text') . "</a> \n" ;
-               $html .= "<a href=\"" . htmlspecialchars( $listurl ) . "\">" . wfMsg('val_article_lists') . "</a><br />\n" ;
-               $html .= "<small>" . wfMsg('val_form_note') . "</small><br />\n" ;
+               # Sort revisions list, newest first
+               krsort ( $this->voteCache ) ;
                
-               # Generating data tables
-               $tabsep = "<td width='0' style='border-left:2px solid black;'></td>" ;
-               $topstyle = "style='border-top:2px solid black'" ;
-               foreach ( $val AS $time => $stuff )
+               # Output
+               $title = $article->getTitle();
+               $title = $title->getPrefixedText() ;
+               $wgOut->setPageTitle ( wfMsg ( 'val_rev_for' ) . $title ) ;
+               foreach ( $this->voteCache AS $x => $y )
                        {
-                       $tablestyle = "cellspacing='0' cellpadding='2'" ;
-                       if ( $article_time == $time ) $tablestyle .=" style='border: 2px solid red'" ;
-                       $html .= "<h2>" . wfMsg( 'val_version_of', gmdate( "F d, Y H:i:s", wfTimestamp2Unix( $time ) ) ) ;
-                       $this->find_this_version ( $article_title , $time , $table_id , $table_name ) ;
-                       if ( $table_name == "cur" ) $html .= " (" . wfMsg ( 'val_this_is_current_version' ) . ")" ;
-                       $html .= "</h2>\n" ;
-                       $html .= "<form method='post'>\n" ;
-                       $html .= "<input type='hidden' name='oldtime' value=\"" . htmlspecialchars( $time ) . "\" />" ;
-                       $html .= "<table {$tablestyle}>\n" ;
-                       $html .= wfMsg( 'val_table_header', $tabsep ) ;
-                       for ( $idx = 0 ; $idx < count ( $validationtypes) ; $idx++ )
-                               {
-                               $x = explode ( "|" , $validationtypes[$idx] , 4 ) ;
-                               if ( isset ( $stuff[$idx] ) ) $choice = $stuff[$idx]->val_value ;
-                               else $choice = -1 ;
-                               if ( isset ( $stuff[$idx] ) ) $comment = $stuff[$idx]->val_comment ;
-                               else $comment = "" ;
-                               $html .= "<tr><th align='left'>{$x[0]}</th>{$tabsep}<td align='right'>{$x[1]}</td><td align='center'>" ;
-                               for ( $cnt = 0 ; $cnt < $x[3] ; $cnt++)
-                                       {
-                                       $html .= "<input type='radio' name='rad{$idx}' value='{$cnt}'" ;
-                                       if ( $choice == $cnt ) $html .= " checked='checked'" ;
-                                       $html .= " /> " ;
-                                       }
-                               $html .= "</td><td>{$x[2]}</td>" ;
-                               $html .= "<td><input type='radio' name='rad{$idx}' value='-1'" ;
-                               if ( $choice == -1 ) $html .= " checked='checked'" ;
-                               $html .= " /> " . wfMsg ( "val_noop" ) . "</td>{$tabsep}" ;
-                               $html .= "<td><input type='text' name='comment{$idx}' value=\"" . htmlspecialchars( $comment ) . "\" /></td>" ;
-                               $html .= "</tr>\n" ;
-                               }
-                       $html .= "<tr><td {$topstyle} colspan='2'>" ;
-                       
-                       # link to version
-                       $title = Title::newFromDBkey ( $article_title ) ;
-                       if ( $table_name == "cur" ) $link_version = $title->getLocalURL( "" ) ;
-                       else $link_version = $title->getLocalURL( "oldid={$table_id}" ) ;
-                       $link_version = "<a href=\"" . htmlspecialchars( $link_version ) . "\">" . wfMsg ( 'val_view_version' ) . "</a>" ;
-                       $html .= $link_version ;
-                       $html .= "</td><td {$topstyle} colspan='5'>" ;
-                       $html .= "<input type='checkbox' name='merge_other' value='1' checked='checked' />" ;
-                       $html .= wfMsg ( 'val_merge_old' );
-                       $html .= "<br /><input type='checkbox' name='clear_other' value='1' checked='checked' />" ;
-                       $html .= wfMsg ( 'val_clear_old', $skin->makeKnownLinkObj( $article ) );
-                       $html .= "</td><td {$topstyle} align='right' valign='center'><input type='submit' name='doit' value=\"" . htmlspecialchars( wfMsg("ok") ) . "\" /></td>" ;
-                       $html .= "</tr></table></form>\n" ;
+                       $ret .= $this->getRevisionForm ( $article , $x , $y , $x == $ts ) ;
+                       $ret .= "<br/>\n" ;
                        }
+               $ret .= $this->link2statistics ( $article ) ;
+               $ret .= "<p>" . $this->link2userratings ( $wgUser->GetID() , wfMsg('val_show_my_ratings') ) . "</p>" ;
+               return $ret ;   
+       }
+       
+       # This function performs the "management" mode on Special:Validate
+       function manageTopics () {
+               global $wgRequest ;
+               $this->topicList = $this->getTopicList() ;
                
-               $html .= "<h2>" . wfMsg ( 'preview' ) . "</h2>" ;
-               $wgOut->addHTML ( $html ) ;
-               $wgOut->addWikiText ( $wgArticle->getContent( true ) ) ;
+               $iamsure = $wgRequest->getVal ( "iamsure" , "0" ) == 1 ;
+               
+               if ( $iamsure && $wgRequest->getVal ( "m_add" , "--" ) != "--" ) {
+                       $new_topic = $wgRequest->getVal ( "m_topic" ) ;
+                       $new_limit = $wgRequest->getVal ( "m_limit" ) ;
+                       if ( $new_topic != "" && $new_limit > 1 )
+                               $this->addTopic ( $new_topic , $new_limit ) ;
+               }
+
+               $da = $wgRequest->getArray ( "m_del" ) ;
+               if ( $iamsure && isset ( $da ) && count ( $da ) > 0 ) {
+                       $id = array_keys ( $da ) ;
+                       $id = array_shift ( $id ) ;
+                       $this->deleteTopic ( $id ) ;
                }
                
-       function getData ( $user = -1 , $title = "" , $type = -1 )
-               {
-               $ret = array () ;
-               $sql = array () ;
-               if ( $user != -1 ) $sql[] = "val_user='" . wfStrencode( $user ) . "'" ;
-               if ( $type != -1 ) $sql[] = "val_type='" . wfStrencode( $type ) . "'" ;
-               if ( $title != "" ) $sql[] = "val_title='" . wfStrencode( $title ) . "'" ;
-               $sql = implode ( " AND " , $sql ) ;
-               if ( $sql != "" ) $sql = " WHERE " . $sql ;
-               $sql = "SELECT * FROM validate" . $sql ;
+               $r = "<p>" . wfMsg ( 'val_warning' ) . "</p>\n" ;
+               $r .= "<form method='post'>\n" ;
+               $r .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
+               $r .= "<tr>" . wfMsg ( 'val_list_header' ) . "</tr>\n" ;
+               foreach ( $this->topicList AS $x => $y ) {
+                       $r .= "<tr>\n" ;
+                       $r .= "<th>" . $y->val_type . "</th>\n" ;
+                       $r .= "<td>{$y->val_comment}</td>\n" ;
+                       $r .= "<td>1 .. <b>{$y->val_value}</b></td>\n" ;
+                       $r .= "<td><input type='submit' name='m_del[{$x}]' value='" . wfMsg ( 'val_del' ) . "'/></td>\n" ;
+                       $r .= "</tr>\n" ;
+               }
+               $r .= "<tr>\n" ;
+               $r .= "<td/>\n" ;
+               $r .= "<td><input type='text' name='m_topic' value=''/></td>\n" ;
+               $r .= "<td><input type='text' name='m_limit' value=''/></td>\n" ;
+               $r .= "<td><input type='submit' name='m_add' value='" . wfMsg ( 'val_add' ) . "'/></td>\n" ;
+               $r .= "</tr>\n" ;
+               $r .= "</table>\n" ;
+               $r .= "<input type='checkbox' name='iamsure' value='1'/>" . wfMsg ( 'val_iamsure' ) . "\n" ;
+               $r .= "</form>\n" ;
+               return $r ;
+       }
+       
+       # Generates a user ID for both logged-in users and anons; $x is an object from an SQL query
+       function make_user_id ( &$x ) {
+               if ( $x->val_user == 0 ) return $x->val_ip ;
+               else return $x->val_user ;
+               }
+
+       function showDetails ( &$article , $revision ) {
+               global $wgDBprefix , $wgOut, $wgUser ;
+               $this->prepareRevisions ( $article->getID() ) ;
+               $this->topicList = $this->getTopicList() ;
+
+               $title = $article->getTitle() ;
+               $wgOut->setPageTitle ( str_replace ( '$1' , $title->getPrefixedText() , wfMsg ( 'val_validation_of' ) ) ) ;
+               
+               # Collecting statistic data
+               $id = $article->getID() ;
+               $sql = "SELECT * FROM {$wgDBprefix}validate WHERE val_page='{$id}' AND val_revision='{$revision}'" ;
                $res = wfQuery( $sql, DB_READ );
-               while( $s = wfFetchObject( $res ) ) $ret["{$s->val_title}"]["{$s->val_timestamp}"]["{$s->val_type}"][] = $s ;
-               return $ret ;
+               $data = array () ;
+               $users = array () ;
+               $topics = array () ;
+               while( $x = wfFetchObject( $res ) ) {
+                       $data[$this->make_user_id($x)][$x->val_type] = $x ;
+                       $users[$this->make_user_id($x)] = true ;
+                       $topics[$x->val_type] = true ;
                }
                
-       # Show statistics for the different versions of a single article
-       function getPageStatistics ( $article_title = "" )
-               {
-               global $wgLang, $wgUser , $wgOut ;
-               $validationtypes = $wgLang->getValidationTypes() ;
-               if ( $article_title == "" ) $article_title = $_GET['article_title'] ;
-               $d = $this->getData ( -1 , $article_title , -1 ) ;
-               if ( count ( $d ) ) $d = array_shift ( $d ) ;
-               else $d = array () ;
-               krsort ( $d ) ;
+               # Sorting lists of topics and users
+               ksort ( $users ) ;
+               ksort ( $topics ) ;
+               
+               $ts = $this->getTimestamp ( $revision ) ;
+               $url = $this->getVersionLink ( $article , $revision , wfTimestamp ( TS_DB , $ts ) ) ;
 
-               # Getting table data (cur_id, old_id etc.) for each version
-               $table_id = array() ;
-               $table_name = array() ;
-               foreach ( $d AS $version => $data )
-                       {
-                       $this->find_this_version ( $article_title , $version , $table_id[$version] , $table_name[$version] ) ;
+               # Table headers
+               $ret = "" ;                     
+               $ret .= "<p><b>" . str_replace ( '$1' , $url , wfMsg ( 'val_revision_of' ) ) . "</b></p>\n" ;
+               $ret .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
+               $ret .= "<tr><th/>" ;
+               
+               foreach ( $topics AS $t => $dummy ) {
+                       $ret .= "<th>" . $this->topicList[$t]->val_comment . "</th>" ;
                        }
-               # Generating HTML
-               $title = Title::newFromDBkey ( $article_title ) ;
-               $wgOut->setPageTitle ( wfMsg ( 'val_page_validation_statistics' , $title->getText() ) ) ;
-#              $html = "<h1>" . wfMsg ( 'val_page_validation_statistics' , $title->getText() ) . "</h1>\n" ;
-               $html = "" ;
-               $skin = $wgUser->getSkin() ;
-               $listurl = $skin->makeSpecialURL ( "validate" , "mode=list_page" ) ;
-               $html .= "<a href=\"" . htmlspecialchars( $listurl ) . "\">" . wfMsg('val_article_lists') . "</a><br /><br />\n" ;
+               $ret .= "</tr>\n" ;
 
-               $html .= "<table border='1' cellpadding='2' style='font-size:8pt;'>\n" ;
-               $html .= "<tr><th>" . wfMsg('val_version') . "</th>" ;
-               foreach ( $validationtypes AS $idx => $title )
-                       {
-                       $title = explode ( "|" , $title ) ;
-                       $html .= "<th>{$title[0]}</th>" ;
+               # Table data
+               foreach ( $users AS $u => $dummy ) { # Every row a user
+                       $ret .= "<tr>" ;
+                       $ret .= "<th align='left'>" ;
+                       if ( !User::IsIP ( $u ) ) { # Logged-in user rating
+                               $ret .= $this->link2userratings ( $u , User::whoIs ( $u ) ) ;
+                       } else { # Anon rating
+                               $ret .= $this->link2userratings ( $u , $u ) ;
                        }
-               $html .= "<th>" . wfMsg('val_total') . "</th>" ;
-               $html .= "</tr>\n" ;
-               foreach ( $d AS $version => $data )
-                       {
-                       # Preamble for this version
-                       $title = Title::newFromDBkey ( $article_title ) ;
-                       $version_date = gmdate("F d, Y H:i:s",wfTimestamp2Unix($version)) ;
-                       $version_validate_link = $title->getLocalURL( "action=validate&timestamp={$version}" ) ;
-                       $version_validate_link = "<a class='intern' href=\"" . htmlspecialchars( $version_validate_link ) . "\">" . wfMsg('val_validate_version') . "</a>" ;
-                       if ( $table_name[$version] == 'cur' ) $version_view_link = $title->getLocalURL( "" ) ;
-                       else $version_view_link = $title->getLocalURL( "oldid={$table_id[$version]}" ) ;
-                       $version_view_link = "<a href=\"{$version_view_link}\">" . wfMsg('val_view_version') . "</a>" ;
-                       $html .= "<tr>" ;
-                       $html .= "<td align='center' valign='top' nowrap='nowrap'><b>{$version_date}</b><br />{$version_view_link}<br />{$version_validate_link}</td>" ;
-
-                       # Individual data
-                       $vmax = array() ;
-                       $vcur = array() ;
-                       $users = array() ;
-                       foreach ( $data AS $type => $x2 )
-                               {
-                               if ( !isset ( $vcur[$type] ) ) $vcur[$type] = 0 ;
-                               if ( !isset ( $vmax[$type] ) ) $vmax[$type] = 0 ;
-                               if ( !isset ( $users[$type] ) ) $users[$type] = 0 ;
-                               foreach ( $x2 AS $user => $x )
-                                       {
-                                       $vcur[$type] += $x->val_value ;
-                                       $temp = explode ( "|" , $validationtypes[$type]) ;
-                                       $vmax[$type] += $temp[3] - 1 ;
-                                       $users[$type] += 1 ;
-                                       }
-                               }
-       
-                       $total_count = 0 ;
-                       $total_percent = 0 ;
-                       foreach ( $validationtypes AS $idx => $title )
-                               {
-                               if ( isset ( $vcur[$idx] ) )
-                                       {
-                                       $average = 100 * $vcur[$idx] / $vmax[$idx] ;
-                                       $total_count += 1 ;
-                                       $total_percent += $average ;
-                                       if ( $users[$idx] > 1 ) $msgid = "val_percent" ;
-                                       else $msgid = "val_percent_single" ;
-                                       $html .= "<td align='center' valign='top'>" .
-                                                       wfMsg ( $msgid, number_format ( $average , 2 ) ,
-                                                                       $vcur[$idx] , $vmax[$idx] , $users[$idx] ) ;
-                                       }
-                               else
-                                       {
-                                       $html .= "<td align='center' valign='center'>" ;
-                                       $html .= "(" . wfMsg ( "val_noop" ) . ")" ;
-                                       }
-                               $html .= "</td>" ;
+                       $ret .= "</th>" ;
+                       foreach ( $topics AS $t => $dummy ) { # Every column a topic
+                               if ( !isset ( $data[$u][$t] ) ) $ret .= "<td/>" ;
+                               else {
+                                       $ret .= "<td valign='center'>" ;
+                                       $ret .= $data[$u][$t]->val_value ;
+                                       if ( $data[$u][$t]->val_comment != "" )
+                                               $ret .= " <small>(" . $data[$u][$t]->val_comment . ")</small>" ;
+                                       $ret .= "</td>" ;
                                }
-                       
-                       if ( $total_count > 0 )
-                               {
-                               $total = $total_percent / $total_count ;
-                               $total = number_format ( $total , 2 ) . " %" ;
-                               }
-                       else $total = "" ;
-                       $html .= "<td align='center' valign='top' nowrap='nowrap'><b>{$total}</b></td>" ;
-                       
-                       $html .= "</tr>" ;
                        }
-               $html .= "</table>\n" ;
-               return $html ;
+                       $ret .= "</tr>" ;
+                       }
+               $ret .= "</table>" ;
+               $ret .= "<p>" . $this->link2statistics ( $article ) . "</p>" ;
+               $ret .= "<p>" . $this->link2userratings ( $wgUser->GetID() , wfMsg('val_show_my_ratings') ) . "</p>" ;
+               
+               return $ret ;
                }
+       
+       function showList ( &$article ) {
+               global $wgDBprefix , $wgOut, $wgUser;
+               $this->prepareRevisions ( $article->getID() ) ;
+               $this->topicList = $this->getTopicList() ;
 
-       function countUserValidations ( $userid )
-               {
-               $sql = "SELECT count(DISTINCT val_title) AS num FROM validate WHERE val_user=" . IntVal( $userid );
+               $title = $article->getTitle() ;
+               $wgOut->setPageTitle ( str_replace ( '$1' , $title->getPrefixedText() , wfMsg ( 'val_validation_of' ) ) ) ;
+               
+               # Collecting statistic data
+               $id = $article->getID() ;
+               $sql = "SELECT * FROM {$wgDBprefix}validate WHERE val_page='{$id}'" ;
                $res = wfQuery( $sql, DB_READ );
-               if ( $s = wfFetchObject( $res ) ) $num = $s->num ;
-               else $num = 0 ;
-               return $num ;
+               $data = array () ;
+               while( $x = wfFetchObject( $res ) ) {
+                       $idx = $this->getTimestamp ( $x->val_revision ) ;
+                       if ( !isset ( $data[$idx] ) )
+                               $data[$idx] = array () ;
+                       if ( !isset ( $data[$idx][$x->val_type] ) ) {
+                               $data[$idx][$x->val_type]->count = 0 ;
+                               $data[$idx][$x->val_type]->sum = 0 ;
+                       }
+                       $data[$idx][$x->val_type]->count++ ;
+                       $data[$idx][$x->val_type]->sum += $x->val_value ;
                }
                
-       function getArticleList ()
-               {
-               global $wgLang , $wgOut ;
-               $validationtypes = $wgLang->getValidationTypes() ;
-               $wgOut->setPageTitle ( wfMsg ( 'val_article_lists' ) ) ;
-               $html = "" ;
+               krsort ( $data ) ;
                
-               # Choices
-               $choice = array () ;
-               $maxw = 0 ;
-               foreach ( $validationtypes AS $idx => $data )
-                       {
-                       $x = explode ( "|" , $data , 4 ) ;
-                       if ( $x[3] > $maxw ) $maxw = $x[3] ;
-                       }
-               foreach ( $validationtypes AS $idx => $data )
-                       {
-                       $choice[$idx] = array () ;
-                       for ( $a = 0 ; $a < $maxw ; $a++ )
-                               {
-                               $var = "cb_{$idx}_{$a}" ;
-                               if ( isset ( $_POST[$var] ) ) $choice[$idx][$a] = $_POST[$var] ; # Selected
-                               else if ( !isset ( $_POST["doit"] ) ) $choice[$idx][$a] = 1 ; # First time
-                               else $choice[$idx][$a] = 0 ; # De-selected
+               $ret = "" ;
+               $ret .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
+               $ret .= "<tr><th>" . wfMsg("val_revision") . "</th>" ;
+               foreach ( $this->topicList AS $x => $y )
+                       $ret .= "<th>{$y->val_comment}</th>" ;
+               $ret .= "</tr>\n" ;
+               foreach ( $data AS $ts => $y ) {
+                       $revision = $this->getRevisionNumber ( $ts ) ;
+                       $url = $this->getVersionLink ( $article , $revision , wfTimestamp ( TS_DB , $ts ) ) ;
+                       $detailsurl = $this->link2revisionstatistics ( $article , $revision ) ;
+                       $ret .= "<tr><td>{$url} {$detailsurl}</td>" ;
+                       foreach ( $this->topicList AS $topicID => $dummy ) {
+                               if ( isset ( $y[$topicID] ) ) {
+                                       $z = $y[$topicID] ;
+                                       if ( $z->count == 0 ) $a = 0 ;
+                                       else $a = $z->sum / $z->count ;
+                                       $ret .= sprintf ( "<td><b>%1.1f</b> (%d)</td>" , $a , $z->count ) ;
+                               } else $ret .= "<td/>" ;
                                }
-                       }
+                       $ret .= "</tr>\n" ;
+               }
+               $ret .= "</table>\n" ;
+               $ret .= "<p>" . $this->link2userratings ( $wgUser->GetID() , wfMsg('val_show_my_ratings') ) . "</p>" ;
+               return $ret ;
+       }
+       
+       function getRatingText ( $value , $max ) {
+               if ( $max == 2 && $value == 1 ) $ret = wfMsg ( "val_no" ) . " " ;
+               else if ( $max == 2 && $value == 2 ) $ret = wfMsg ( "val_yes" ) ;
+               else if ( $value != 0 ) $ret = wfMsg ( "val_of" , $value , $max ) . " " ;
+               else $ret = "" ;
+               return $ret ;
+       }
 
+       function showUserStats ( $user ) {
+               global $wgDBprefix , $wgOut, $wgUser ;
+               $this->topicList = $this->getTopicList() ;
+               $data = $this->getAllVoteLists ( $user ) ;
                
-               # The form
-               $html .= "<form method='post'>\n" ;
-               $html .= "<table border='1' cellspacing='0' cellpadding='2'>" ;
-               foreach ( $validationtypes AS $idx => $data )
-                       {
-                       $x = explode ( "|" , $data , 4 ) ;
-                       
-                       $html .= "<tr>" ;
-                       $html .= "<th nowrap='nowrap'>{$x[0]}</th>" ;
-                       $html .= "<td align='right' nowrap='nowrap'>{$x[1]}</td>" ;
-
-                       for ( $a = 0 ; $a < $maxw ; $a++ )
-                               {
-                               if ( $a < $x[3] )
-                                       {
-                                       $td = "<input type='checkbox' name='cb_{$idx}_{$a}' value='1'" ;
-                                       if ( $choice[$idx][$a] == 1 ) $td .= " checked='checked'" ;
-                                       $td .= " />" ;
-                                       }
-                               else $td = '' ;
-                               $html .= "<td>{$td}</td>" ;
-                               }
-
-                       $html .= "<td nowrap='nowrap'>{$x[2]}</td>" ;
-                       $html .= "</tr>\n" ;
-                       }
-               $html .= "<tr><td colspan='" . ( $maxw + 2 ) . "'></td>\n" ;
-               $html .= "<td align='right' valign='center'><input type='submit' name='doit' value=\"" . htmlspecialchars( wfMsg ( 'ok' ) ) . "\" /></td></tr>" ;
-               $html .= "</table>\n" ;
-               $html .= "</form>\n" ;
-
-               # The query
-               $articles = array() ;
-               $sql = "SELECT DISTINCT val_title,val_timestamp,val_type,avg(val_value) AS avg FROM validate GROUP BY val_title,val_timestamp,val_type" ;
-               $res = wfQuery( $sql, DB_READ );
-               while( $s = wfFetchObject( $res ) ) $articles[$s->val_title][$s->val_timestamp][$s->val_type] = $s ;
-
-               # The list
-               $html .= "<ul>\n" ;
-               foreach ( $articles AS $dbkey => $timedata )
-                       {
-                       $title = Title::newFromDBkey ( $dbkey ) ;
-                       $out = array () ;
-                       krsort ( $timedata ) ;
-                       
-                       foreach ( $timedata AS $timestamp => $typedata )
-                               {
-                               $showit = true ;
-                               foreach ( $typedata AS $type => $data )
-                                       {
-                                       $avg = intval ( $data->avg + 0.5 ) ;
-                                       if ( $choice[$type][$avg] == 0 ) $showit = false ;
-                                       }
-                               if ( $showit )
-                                       {
-                                       $out[] = "<li>" . $this->getVersionLink ( $title , $timestamp ) . "</li>\n" ;
-                                       }
-                               }
-                               
-                       if ( count ( $out ) > 0 )
-                               {
-                               $html .= "<li>\n" ;
-                               $html .= htmlspecialchars( $title->getText() ) . "\n" ;
-                               $html .= "<ul>\n" ;                     
-                               $html .= implode ( "\n" , $out ) ;
-                               $html .= "</ul>\n</li>\n" ;
+               if ( $user == $wgUser->getID() ) $wgOut->setPageTitle ( wfMsg ( 'val_my_stats_title' ) ) ;
+               elseif ( ! User::IsIP ( $user ) ) $wgOut->setPageTitle ( wfMsg ( 'val_user_stats_title' , User::whoIs ( $user ) ) ) ;
+               else $wgOut->setPageTitle ( wfMsg ( 'val_user_stats_title' , $user ) ) ;
+               
+               $ret = "" ;
+               $ret .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
+               
+               foreach ( $data AS $articleid => $revisions ) {
+                       $title = Title::newFromID ( $articleid ) ;
+                       $ret .= "<tr><th align='left' colspan='4'><a href=\"" . $title->getLocalURL() . "\">" . $title->getPrefixedText() . "</a></th></tr>" ;
+                       krsort ( $revisions ) ;
+                       foreach ( $revisions AS $revid => $revision ) {
+                               $url = $title->getLocalURL ( "oldid={$revid}" ) ;
+                               $ret .= "<tr><th align='left'><a href=\"{$url}\">" . wfMsg ( 'val_revision_number' , $revid ) . "</a></th>" ;
+                               ksort ( $revision ) ;
+                               $initial = true ;
+                               foreach ( $revision AS $topic => $rating ) {
+                                       if ( !$initial ) $ret .= "<tr><td/>" ;
+                                       $initial = false ;
+                                       $ret .= "<td>" . $this->topicList[$topic]->val_comment . "</td>" ;
+                                       $ret .= "<td>" . $this->getRatingText ( $rating->val_value , $this->topicList[$topic]->val_value ) . "</td>" ;
+                                       $ret .= "<td>" . htmlentities ( $rating->val_comment ) . "</td>" ;
+                                       $ret .= "</tr>" ;
                                }
                        }
-               $html .= "</ul>\n" ;
-               return $html ;
+                       $ret .= "</tr>" ;
                }
-
-       function getVersionLink ( &$title , $timestamp )
-               {
-               $dbkey = $title->getDBkey () ;
-               $this->find_this_version ( $dbkey , $timestamp , $table_id , $table_name ) ;
-               if ( $table_name == 'cur' ) $link = $title->getLocalURL( "" ) ;
-               else $link = $title->getLocalURL( "action=validate&timestamp={$table_id}" ) ;
-               $linktitle = wfMsg( 'val_version_of', gmdate( "F d, Y H:i:s", wfTimestamp2Unix( $timestamp ) ) ) ;
-               $link = "<a href=\"" . htmlspecialchars( $link ) . "\">" . $linktitle . "</a>" ;
-               if ( $table_name == 'cur' ) $link .= " (" . wfMsg ( 'val_this_is_current_version' ) . ")" ;
+               $ret .= "</table>" ;
                
-               $vlink = wfMsg ( 'val_tab' ) ;
-               $vlink = "[<a href=\"" . $title->escapeLocalURL( "action=validate&timestamp={$timestamp}" ) . "\">{$vlink}</a>] " . $link ;
-               return $vlink ;
+               return $ret ;
                }
 
+}
+
+/**
+ * constructor
+ */
+function wfSpecialValidate( $page = '' ) {
+       global $wgOut, $wgRequest, $wgUseValidation, $wgUser, $wgContLang;
+       
+       if( !$wgUseValidation ) {
+               $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
+               return;
        }
 
-function wfSpecialValidate( $page = "" )
-       {
-       global $wgOut ;
-       if ( isset ( $_GET['mode'] ) ) $mode = $_GET['mode'] ;
-       else $mode = "form" ;
-       $v = new Validation ;
-       $html = "" ;
-/*     if ( $mode == "form" )
-               {
-               $html = $v->validate_form () ;
-               }
-       else */
-       if ( $mode == "stat_page" )
-               {
-               $html = $v->getPageStatistics () ;
-               }
-       else if ( $mode == "list_page" )
-               {
-               $html = $v->getArticleList () ;
-               }
+/*
+       # Can do?
+       if ( ! $wgUser->isAllowed('change_validation') ) {
+               $wgOut->sysopRequired();
+               return;
+       }
+*/     
+
+       $mode = $wgRequest->getVal ( "mode" ) ;
+       $skin = $wgUser->getSkin() ;
+
        
-       $wgOut->addHTML( $html ) ;
+       if ( $mode == "manage" ) {
+               $v = new Validation ;
+               $html = $v->manageTopics () ;
+       } else if ( $mode == "userstats" ) {
+               $v = new Validation ;
+               $user = $wgRequest->getVal ( "user" ) ;
+               $html = $v->showUserStats ( $user ) ;
+       } else {
+               $html = "$mode" ;
+               $html .= "<ul>\n" ;
+
+               $t = Title::newFromText ( "Special:Validate" ) ;
+               $url = $t->getLocalURL ( "mode=manage" ) ;
+               $html .= "<li><a href=\"" . $url . "\">Manage</a></li>\n" ;
+
+               $html .= "</ul>\n" ;
        }
 
+       $wgOut->addHTML( $html );
+}
+
 ?>