Second part of bug 4083: Special:Validation doesn't check wpEditToken
[mediawiki.git] / includes / SpecialValidate.php
blobf58facdd3d85d670c7a765ebcd75b727207752f1
1 <?php
2 # Copyright (C) 2004 Magnus Manske <magnus.manske@web.de>
3 # http://www.mediawiki.org/
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.
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.
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
20 /**
22 * @package MediaWiki
23 * @subpackage SpecialPage
25 class Validation {
26 var $topicList;
27 var $voteCache;
28 var $page_id;
29 var $rev_fields = "rev_id,rev_page,rev_timestamp,rev_user_text,rev_user,rev_comment" ;
31 function getRevisionFromId( $rev_id ) {
32 if( isset( $this->id2rev[$rev_id] ) ) return $this->id2rev[$rev_id];
34 $db =& wfGetDB( DB_SLAVE );
35 $fname = 'SpecialValidate::getRevisionFromId';
36 $res = $db->select( 'revision', $this->rev_fields, array( 'rev_id' => $rev_id ), $fname, array( 'LIMIT' => 1 ) );
37 $rev = $db->fetchObject($res);
38 $db->freeResult($res);
40 $this->id2rev[$rev->rev_id] = $rev;
41 $this->ts2rev[$rev->rev_timestamp] = $rev;
43 return $rev;
46 function getRevisionFromTimestamp( $timestamp ) {
47 if( isset( $this->ts2rev[$timestamp] ) ) return $this->ts2rev[$timestamp];
49 $db =& wfGetDB( DB_SLAVE );
50 $fname = 'SpecialValidate::getRevisionFromTimestamp';
51 $res = $db->select( 'revision', $this->rev_fields,
52 array( 'rev_page' => $this->page_id, 'rev_timestamp' => $timestamp ),
53 $fname, array( 'LIMIT' => 1 )
55 $rev = $db->fetchObject($res);
56 $db->freeResult($res);
58 $this->id2rev[$rev->rev_id] = $rev;
59 $this->ts2rev[$rev->rev_timestamp] = $rev;
61 return $rev;
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) );
73 # Returns an array containing all topics you can vote on
74 function getTopicList() {
75 $db =& wfGetDB( DB_SLAVE );
77 $topics = array();
79 # NOTE : This query returns only the topics to vote on
80 $res = $db->select( 'validate', '*', array( 'val_page' => 0 ), 'SpecialValidate::getTopicList' );
81 while( $topic = $db->fetchObject($res) ) {
82 $topics[$topic->val_type] = $topic;
84 $db->freeResult($res);
86 ksort( $topics );
87 return $topics;
90 # Merges one dataset into another
91 function mergeInto( &$source, &$dest ) {
92 $ret = false;
93 foreach( $source as $x => $y ) {
94 $doit = false;
95 if( !isset( $dest[$x] ) ) {
96 $doit = true;
97 } elseif( $dest[$x]->value == 0 ) {
98 $doit = true;
100 if( $doit ) {
101 $dest[$x] = $y;
102 $ret = true;
105 if( $ret ) {
106 ksort ( $dest );
108 return $ret;
111 # Merges all votes prior to the given revision into it
112 function mergeOldRevisions( &$article, $revision ) {
113 $tmp = $this->voteCache;
114 krsort( $tmp );
115 $update = false;
116 $ts = $this->getRevisionTimestamp( $revision );
117 $data = $this->voteCache[$ts];
118 foreach( $tmp as $x => $y ) {
119 if( $x < $ts ) {
120 if( $this->mergeInto( $y, $data ) ) {
121 $update = true;
125 if( $update ) {
126 $this->setRevision( $article, $revision, $data );
130 # Clears all votes prior to the given revision
131 function clearOldRevisions( &$article, $revision ) {
132 $tmp = $this->voteCache;
133 $ts = $this->getRevisionTimestamp( $revision );
134 foreach( $tmp as $x => $y ) {
135 if( $x < $ts ) {
136 $this->deleteRevisionVote ( $article, $this->getRevisionId( $x ) );
141 # Updates the votes for the given revision from the FORM data
142 function updateRevision( &$article, $revision ) {
143 global $wgRequest;
145 if( isset( $this->voteCache[$this->getRevisionTimestamp( $revision )] ) ) {
146 $data = $this->voteCache[$this->getRevisionTimestamp( $revision )];
147 } else {
148 $data = array();
150 $nv = $wgRequest->getArray( "re_v_{$revision}", array() );
151 $nc = $wgRequest->getArray( "re_c_{$revision}", array() );
153 foreach( $nv as $x => $y ) {
154 $data[$x]->value = $y;
155 $data[$x]->comment = $nc[$x];
157 krsort( $data );
159 $this->setRevision( $article, $revision, $data );
162 # Sets a specific revision to both cache and database
163 function setRevision( &$article, $revision, &$data ) {
164 global $wgUser;
165 $this->deleteRevisionVote( $article, $revision );
166 $this->voteCache[ $this->getRevisionTimestamp($revision) ] = $data;
167 foreach( $data as $x => $y ) {
168 if( $y->value > 0 ) {
169 $ip = $wgUser->isAnon() ? $wgUser->getName() : '';
170 $dbw =& wfGetDB( DB_MASTER );
171 $dbw->insert( 'validate',
172 array(
173 'val_user' => $wgUser->getId(),
174 'val_page' => $article->getId(),
175 'val_revision' => $revision,
176 'val_type' => $x,
177 'val_value' => $y->value,
178 'val_comment' => $y->comment,
179 'val_ip' => $ip ),
180 'SpecialValidate::setRevision'
186 # Returns a map identifying the current user
187 function identifyUser( $user = "" ) {
188 global $wgUser;
189 if( $user == "" ) $user = $wgUser->getID();
190 return User::isIP($user)
191 ? array( 'val_user' => 0, 'val_ip' => $user )
192 : array( 'val_user' => $user );
195 # Deletes a specific vote set in both cache and database
196 function deleteRevisionVote( &$article, $revision ) {
197 $ts = $this->getRevisionTimestamp( $revision );
198 if( !isset ( $this->voteCache[$ts] ) ) return;
200 $db =& wfGetDB( DB_MASTER );
201 $db->delete(
202 'validate',
203 array_merge(
204 $this->identifyUser(),
205 array(
206 'val_page' => $article->getID(),
207 'val_revision' => $revision
210 'SpecialValidate::deleteRevisionVote'
213 unset( $this->voteCache[$ts] );
216 # Reads the entire vote list for this user for the given article
217 function getVoteList( $id, $user = "" ) {
218 $db =& wfGetDB( DB_SLAVE );
220 # NOTE : This query gets the votes for a single user on a single page.
221 # Assuming most people will use the "merge" feature,
222 # this will be only a single entry.
223 $res = $db->select( 'validate', '*', array_merge( array( 'val_page' => $id ), $this->identifyUser($user) ) );
225 $revisions = array();
226 while( $vote = $db->fetchObject($res) ) {
227 $ts = $this->getRevisionTimestamp( $vote->val_revision );
228 if( ! isset( $revisions[$ts] ) ) {
229 $revisions[$ts] = array();
231 $revisions[$ts][$vote->val_type]->value = $vote->val_value;
232 $revisions[$ts][$vote->val_type]->comment = $vote->val_comment;
234 $db->freeResult($res);
236 return $revisions;
239 # Reads a partial vote list for this user for all articles
240 function getAllVoteLists( $user , $offset , $limit ) {
241 $db =& wfGetDB( DB_SLAVE );
242 $a = $this->identifyUser($user) ;
243 $b = array ( "ORDER BY" => "val_page,val_revision" , "OFFSET" => $offset , "LIMIT" => $limit ) ;
244 $res = $db->select( 'validate', '*', $a , 'getAllVotesList' , $b );
246 $votes = array();
247 while( $vote = $db->fetchObject($res) ) {
248 $votes[$vote->val_page][$vote->val_revision][$vote->val_type] = $vote;
250 $db->freeResult($res);
252 return $votes ;
255 # This functions adds a topic to the database
256 function addTopic( $topic, $limit ) {
257 $db =& wfGetDB( DB_MASTER );
259 $next_idx = 1;
260 while( isset( $this->topicList[$next_idx] ) ) {
261 $next_idx++;
264 $db->insert(
265 'validate',
266 array(
267 'val_user' => 0,
268 'val_page' => 0,
269 'val_revision' => 0,
270 'val_type' => $next_idx,
271 'val_value' => $limit,
272 'val_comment' => $topic,
273 'val_ip' => ''
275 'SpecialValidate::addTopic'
278 $t->val_user = $t->val_page = $t->val_revision = 0;
279 $t->val_type = $next_idx;
280 $t->val_value = $limit;
281 $t->val_comment = $topic;
282 $t->val_ip = "";
283 $this->topicList[$next_idx] = $t;
285 ksort( $this->topicList );
288 # This function deletes a topic and all votes for it. CAREFUL!
289 function deleteTopic( $id ) {
290 $db =& wfGetDB( DB_MASTER );
291 $db->delete( 'validate', array( 'val_type' => $id ), 'SpecialValidate::deleteTopic' );
292 unset( $this->topicList[$id] );
295 # This function returns a link text to the page validation statistics
296 function getStatisticsLink( &$article ) {
297 global $wgUser;
298 $sk = $wgUser->getSkin();
299 $nt = $article->getTitle();
300 return $sk->makeKnownLinkObj( $nt, wfMsg( 'val_rev_stats', $nt->getPrefixedText() ), 'action=validate&mode=list' );
303 # This function returns a link text to the page validation statistics of a single revision
304 function getRevisionStatsLink( &$article, $revision ) {
305 global $wgUser;
306 $sk = $wgUser->getSkin();
307 $nt = $article->getTitle();
308 $text = $this->getParsedWiki( wfMsg('val_revision_stats_link') );
309 $query = "action=validate&mode=details&revision={$revision}";
310 return '(' . $sk->makeKnownLinkObj( $nt, $text, $query ) . ')';
313 # This function returns a link text to the user rating statistics page
314 function getUserRatingsLink( $user, $text ) {
315 global $wgUser;
316 $sk = $wgUser->getSkin();
317 if( $user == 0 ) $user = $wgUser->getName();
318 $nt = Title::newFromText( 'Special:Validate' );
319 return $sk->makeKnownLinkObj( $nt, $text, 'mode=userstats&user='.urlencode($user) );
322 # Returns the timestamp of a revision based on the revision number
323 function getRevisionTimestamp( $rev_id ) {
324 $rev = $this->getRevisionFromId( $rev_id );
325 return $rev->rev_timestamp;
328 # Returns the revision number of a revision based on the timestamp
329 function getRevisionId( $ts ) {
330 $rev = $this->getRevisionFromTimestamp( $ts );
331 return $rev->rev_id;
335 # HTML generation functions from this point on
337 # Returns the metadata string for a revision
338 function getMetadata( $rev_id, &$article ) {
339 global $wgUser;
340 $sk = $wgUser->getSkin();
342 $metadata = "";
343 $x = $this->getRevisionFromId($rev_id);
344 $metadata .= wfTimestamp( TS_DB, $x->rev_timestamp );
345 $metadata .= " by ";
346 if( $x->rev_user == 0 ) {
347 $metadata .= $x->rev_user_text;
348 } else {
349 $u = new User;
350 $u->setId( $x->rev_user );
351 $u->setName( $x->rev_user_text );
352 $nt = $u->getUserPage();
353 $metadata .= $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
355 $metadata .= ': '. $sk->commentBlock( $x->rev_comment, $article->getTitle() );
356 return $metadata;
359 # Generates a link to the topic description
360 function getTopicLink($s) {
361 $t = Title::newFromText ( wfMsg ( 'val_topic_desc_page' ) ) ;
362 # FIXME: Why doesn't this use standard linking code?
363 $r = "<a href=\"" ;
364 $r .= $t->escapeLocalURL () ;
365 $r .= "#" . urlencode ( $s ) ;
366 $r .= "\">{$s}</a>" ;
367 return $r ;
370 # Generates HTML from a wiki text, e.g., a wfMsg
371 function getParsedWiki ( $text ) {
372 global $wgOut, $wgTitle, $wgParser ;
373 $parserOutput = $wgParser->parse( $text , $wgTitle, $wgOut->mParserOptions,false);
374 return $parserOutput->getText() ;
377 # Generates a form for a single revision
378 function getRevisionForm( &$article, $idx, &$data, $focus = false ) {
379 # Fill data with blank values
380 $ts = $idx;
381 $revision = $this->getRevisionId( $ts );
382 foreach( $this->topicList as $x => $y ) {
383 if( !isset( $data[$x] ) ) {
384 $data[$x]->value = 0;
385 $data[$x]->comment = "";
388 ksort( $data ) ;
389 $token = htmlspecialchars( $wgUser->editToken() );
391 # Generate form
392 $table_class = $focus ? 'revisionform_focus' : 'revisionform_default';
393 $ret = "<form method='post'><table class='{$table_class}'>\n"
394 . '<input type="hidden" name="wpEditToken" value="' . $token .'" />';
395 $head = "Revision #" . $revision;
396 $link = $this->getRevisionLink( $article, $revision );
397 $metadata = $this->getMetadata( $revision, $article );
398 $ret .= "<tr><th colspan='3'>" . $head . " ({$link}) {$metadata}</th></tr>\n";
399 $line = 0;
400 foreach( $data as $x => $y ) {
401 $line = 1 - $line;
402 $trclass = $line == 1 ? "revision_tr_first" : "revision_tr_default";
403 $idx = "_{$revision}[{$x}]";
404 $ret .= "<tr class='{$trclass}'>\n";
405 $ret .= "<th>\n";
406 $ret .= $this->getTopicLink ( $this->topicList[$x]->val_comment ) ;
407 $ret .= "</th>\n";
409 $tlx = $this->topicList[$x];
410 $vote = "";
411 $max = $tlx->val_value;
412 for( $a = 0 ; $a <= $max ; $a++ ) {
413 if( $a == 0 ) {
414 $vote .= wfMsg ( "val_noop" );
416 $vote .= "<input type='radio' name='re_v{$idx}' value='{$a}'";
417 if( $a == $y->value ) {
418 $vote .= " checked='checked'";
420 $vote .= " />";
421 if( $max == 2 && $a == 1 ) {
422 $vote .= wfMsg( "val_no" ) . " ";
423 } elseif( $max == 2 && $a == 2 ) {
424 $vote .= wfMsg( "val_yes" );
425 } elseif( $a != 0 ) {
426 $vote .= $a . " ";
428 if ( $a == 0 ) {
429 $vote .= " &nbsp; ";
432 $ret .= "<td nowrap>{$vote}</td>\n";
434 $ret .= "<td width='100%'><input size='50' style='width:98%' maxlength='250' type='text' name='re_c{$idx}' value='{$y->comment}'/>";
435 $ret .= "</td></tr>\n";
437 $checked = $focus ? " checked='checked'" : "";
438 $ret .= "<tr><td colspan='3'>\n";
439 $ret .= "<input type='checkbox' name='re_merge_{$revision}' value='1'{$checked} />" . $this->getParsedWiki( wfMsg( 'val_merge_old' ) ) . " \n";
440 $ret .= "<input type='checkbox' name='re_clear_{$revision}' value='1'{$checked} />" . $this->getParsedWiki( wfMsg( 'val_clear_old' ) ) . " \n";
441 $ret .= "<input type='submit' name='re_submit[{$revision}]' value=\"" . wfMsgHtml( "ok" ) . "\" />\n";
443 if( $focus ) $ret .= "<br/>\n<small>" . $this->getParsedWiki ( wfMsg( "val_form_note" ) ) . "</small>";
444 $ret .= "</td></tr>\n";
445 $ret .= "</table></form>\n\n";
446 return $ret;
450 # Generates the page from the validation tab
451 function validatePageForm( &$article, $revision ) {
452 global $wgOut, $wgRequest, $wgUser;
454 $ret = "";
455 $this->page_id = $article->getID();
456 $this->topicList = $this->getTopicList();
457 if ( $this->getNoTopicsWarning() ) return "" ;
458 $this->voteCache = $this->getVoteList( $article->getID() );
460 # Check for POST data
461 $re = $wgRequest->getArray( 're_submit' );
462 if ( isset( $re ) ) {
463 $id = array_keys( $re );
464 $id = $id[0] ; # $id is now the revision number the user clicked "OK" for
465 $clearOldRev = $wgRequest->getVal( "re_clear_{$id}", 0 );
466 $mergeOldRev = $wgRequest->getVal( "re_merge_{$id}", 0 );
467 $this->updateRevision( $article, $id );
468 if( $mergeOldRev ) {
469 $this->mergeOldRevisions( $article, $id );
471 if( $clearOldRev ) {
472 $this->clearOldRevisions( $article, $id );
474 $ret .= '<p class="revision_saved">' . $this->getParsedWiki( wfMsg( 'val_revision_changes_ok' ) ) . "</p>";
475 } else {
476 $ret .= $this->getParsedWiki( wfMsg ('val_votepage_intro') );
479 # Make sure the requested revision exists
480 $rev = $this->getRevisionFromId($revision);
481 $ts = $rev->rev_timestamp;
482 if( !isset( $this->voteCache[$ts] ) ) {
483 $this->voteCache[$ts] = array();
486 # Sort revisions list, newest first
487 krsort( $this->voteCache );
489 # Output
490 $title = $article->getTitle();
491 $title = $title->getPrefixedText();
492 $wgOut->setPageTitle( wfMsg( 'val_rev_for', $title ) );
493 foreach( $this->voteCache as $x => $y ) {
494 $ret .= $this->getRevisionForm( $article, $x, $y, $x == $ts );
495 $ret .= "<br/>\n";
497 $ret .= $this->getStatisticsLink( $article );
498 $ret .= "<p>" . $this->getUserRatingsLink( $wgUser->getID(), wfMsg( 'val_show_my_ratings' ) ) . "</p>";
499 return $ret ;
502 # This function performs the "management" mode on Special:Validate
503 function manageTopics() {
504 global $wgRequest, $wgValidationMaxTopics;
505 $this->topicList = $this->getTopicList();
507 $r = "" ; # Return value
508 $iamsure = true ; # Sure by default, see checkbox below # $wgRequest->getVal( "iamsure", "0" ) == 1;
510 if( $iamsure && $wgRequest->getVal( "m_add", "--" ) != "--" ) {
511 if ( count ( $this->topicList ) >= $wgValidationMaxTopics ) { # Catching this in case someone tries a manually edited URL...
512 $ret .= "<p><b>" . wfMsg ( 'val_max_topics' , $wgValidationMaxTopics ) . "</b></p>" ;
513 } else {
514 $new_topic = $wgRequest->getVal( "m_topic" );
515 $new_limit = $wgRequest->getVal( "m_limit" );
516 if( $new_topic != "" && $new_limit > 1 ) {
517 $this->addTopic( $new_topic, $new_limit );
522 $da = $wgRequest->getArray( "m_del" );
523 if( $iamsure && isset( $da ) && count( $da ) > 0 ) {
524 $id = array_keys( $da );
525 $id = array_shift( $id );
526 $this->deleteTopic( $id );
529 $token = htmlspecialchars( $wgUser->editToken() );
531 # FIXME: Wikitext this
532 $r .= "<p>" . $this->getParsedWiki( wfMsg( 'val_warning' ) ) . "</p>\n";
533 $r .= "<form method='post'>\n";
534 $r .= '<input type="hidden" name="wpEditToken" value="' . $token .'" />';
535 $r .= "<table>\n";
536 $r .= "<tr>" . wfMsg( 'val_list_header' ) . "</tr>\n";
537 foreach( $this->topicList as $x => $y ) {
538 $r .= "<tr>\n";
539 $r .= "<th>{$y->val_type}</th>\n";
540 $r .= "<td>" . $this->getTopicLink ( $y->val_comment ) . "</td>\n";
541 $r .= "<td>1 .. <b>" . intval( $y->val_value ) . "</b></td>\n";
542 $r .= "<td><input type='submit' name='m_del[" . intval( $x ) . "]' value='" . htmlspecialchars( wfMsg( 'val_del' ) ) . "'/></td>\n";
543 $r .= "</tr>\n";
546 # Add topic, or too-many-topics warning
547 if ( count ( $this->topicList ) >= $wgValidationMaxTopics ) {
548 $r .= "<tr><td colspan='4' align='center'>" ;
549 $r .= wfMsg ( 'val_max_topics' , $wgValidationMaxTopics ) ;
550 $r .= "</td></tr>" ;
551 } else {
552 $r .= "<tr>\n";
553 $r .= "<td></td>\n";
554 $r .= '<td><input type="text" name="m_topic" value=""/></td>' . "\n";
555 $r .= '<td>1 .. <input type="text" name="m_limit" value="" size="4"/></td>' . "\n";
556 $r .= '<td>' ;
557 $r .= '<input type="submit" name="m_add" value="' . htmlspecialchars( wfMsg( 'val_add' ) ) . '"/>' ;
558 $r .= '</td>' . "\n";
559 $r .= "</tr>" ;
562 $r .= "</table>\n";
564 # This is fot the checkbox "I am sure" for actions, which was found to be unituitive
565 # $r .= '<p><input type="checkbox" name="iamsure" id="iamsure" value="1"/>';
566 # $r .= '<label for="iamsure">' . $this->getParsedWiki( wfMsg( 'val_iamsure' ) ) . "</label></p>\n";
568 $r .= "</form>\n";
569 return $r;
572 # Generates an ID for both logged-in users and anons; $res is an object from an SQL query
573 function make_user_id( &$res ) {
574 return $res->val_user == 0 ? $res->val_ip : $res->val_user;
577 function showDetails( &$article, $revision ) {
578 global $wgOut, $wgUser;
579 $this->page_id = $article->getID();
580 $this->topicList = $this->getTopicList();
581 if ( $this->getNoTopicsWarning() ) return "" ;
583 $sk = $wgUser->getSkin();
584 $title = $article->getTitle();
585 $wgOut->setPageTitle( str_replace( '$1', $title->getPrefixedText(), wfMsg( 'val_validation_of' ) ) );
587 $data = array();
588 $users = array();
589 $topics = array();
591 # Collecting statistic data
592 $db =& wfGetDB( DB_SLAVE );
593 $res = $db->select( 'validate', '*', array( 'val_page' => $this->page_id, 'val_revision' => $revision ), 'SpecialValidate::showDetails' );
594 while( $x = $db->fetchObject($res) ) {
595 $data[$this->make_user_id($x)][$x->val_type] = $x;
596 $users[$this->make_user_id($x)] = true;
597 $topics[$x->val_type] = true;
599 $db->freeResult($res);
601 # Sorting lists of topics and users
602 ksort( $users );
603 ksort( $topics );
605 $ts = $this->getRevisionTimestamp( $revision );
606 $url = $this->getRevisionLink( $article, $revision, wfTimestamp( TS_DB, $ts ) );
608 # Table headers
609 $ret = "" ;
610 $ret .= "<p><b>" . str_replace( '$1', $url, wfMsg( 'val_revision_of' ) ) . "</b></p>\n";
611 $ret .= "<table>\n";
612 $ret .= "<tr><th>" . $this->getParsedWiki ( wfMsg('val_details_th') ) . "</th>" ;
614 foreach( $topics as $t => $dummy ) {
615 $ret .= '<th>' . $sk->commentBlock( $this->topicList[$t]->val_comment, $article->getTitle() ) . '</th>';
617 $ret .= "</tr>\n";
619 # Table data
620 foreach( $users as $u => $dummy ) { # Every row a user
621 $ret .= "<tr>";
622 $ret .= "<th>";
623 if( !User::IsIP( $u ) ) { # Logged-in user rating
624 $ret .= $this->getUserRatingsLink( $u, User::whoIs( $u ) );
625 } else { # Anon rating
626 $ret .= $this->getUserRatingsLink( $u, $u );
628 $ret .= "</th>";
629 foreach( $topics as $t => $dummy ) { # Every column a topic
630 if( !isset( $data[$u][$t] ) ) {
631 $ret .= "<td/>";
632 } else {
633 $ret .= "<td>";
634 $ret .= $data[$u][$t]->val_value;
635 if( $data[$u][$t]->val_comment != "" ) {
636 $ret .= ' ' . $sk->commentBlock( $data[$u][$t]->val_comment, $article->getTitle() );
638 $ret .= "</td>";
641 $ret .= "</tr>";
643 $ret .= "</table>";
644 $ret .= "<p>" . $this->getStatisticsLink( $article ) . "</p>";
645 $ret .= "<p>" . $this->getUserRatingsLink( $wgUser->getID(), wfMsg( 'val_show_my_ratings' ) ) . "</p>";
647 return $ret;
650 function showList( &$article ) {
651 global $wgOut, $wgUser , $wgRequest;
652 $this->page_id = $article->getID();
653 $this->topicList = $this->getTopicList();
654 if ( $this->getNoTopicsWarning() ) return "" ;
656 $title = $article->getTitle();
657 $wgOut->setPageTitle( wfMsg( 'val_validation_of', $title->getPrefixedText() ) );
659 $offset = $wgRequest->getVal ( "offset" , 0 ) ;
660 $limit = $wgRequest->getVal ( "limit" , 25 ) ;
662 # Collecting statistic data
663 # Unfortunately, it has to read all the data, though it will only display a part
664 $db =& wfGetDB( DB_SLAVE );
665 $res = $db->select( 'validate', 'val_revision,val_type,val_value', array( "val_page" => $this->page_id ), 'SpecialValidate::showList' );#, $b );
667 $statistics = array();
668 while( $vote = $db->fetchObject($res) ) {
669 $ts = $this->getRevisionTimestamp($vote->val_revision);
670 if ( !isset ( $statistics[$ts] ) ) $statistics[$ts] = array () ;
671 if ( !isset ( $statistics[$ts][$vote->val_type]->count ) ) $statistics[$ts][$vote->val_type]->count = 0 ;
672 if ( !isset ( $statistics[$ts][$vote->val_type]->sum ) ) $statistics[$ts][$vote->val_type]->sum = 0 ;
673 $statistics[$ts][$vote->val_type]->count++;
674 $statistics[$ts][$vote->val_type]->sum += $vote->val_value;
676 $db->freeResult($res);
678 krsort( $statistics );
680 $ret = "<table><tr>\n";
681 $ret .= "<th>" . $this->getParsedWiki( wfMsg( "val_revision" ) ) . "</th>\n";
682 foreach( $this->topicList as $topic ) {
683 $ret .= "<th>" . $this->getTopicLink($topic->val_comment) . "</th>";
685 $ret .= "</tr>\n";
687 # Paging
688 $statistics = array_slice ( $statistics , $offset , $limit ) ;
690 foreach( $statistics as $ts => $data ) {
691 $rev_id = $this->getRevisionId( $ts );
692 $revision_link = $this->getRevisionLink( $article, $rev_id, wfTimestamp( TS_DB, $ts ) );
693 $details_link = $this->getRevisionStatsLink( $article, $rev_id );
694 $ret .= "<tr><td>{$revision_link} {$details_link}</td>";
695 foreach( $this->topicList as $topicType => $topic ) {
696 if( isset( $data[$topicType] ) ) {
697 $stats = $data[$topicType];
698 $average = $stats->count == 0 ? 0 : $stats->sum / $stats->count;
699 $ret .= sprintf( "<td><b>%1.1f</b> (%d)</td>", $average, $stats->count );
700 } else {
701 $ret .= "<td></td>";
704 $ret .= "</tr>\n";
706 $ret .= "</table>\n";
708 # Navbar
709 $s = $this->navBar ( $offset , $limit , count ( $statistics ) , "list" ) ;
710 $ret = $s . $ret . $s . "<p/>" ;
712 $ret .= "<p>" . $this->getUserRatingsLink( $wgUser->getID(), wfMsg( 'val_show_my_ratings' ) ) . "</p>";
714 return $ret;
717 function getRatingText( $value, $max ) {
718 if( $max == 2 && $value == 1 ) {
719 $ret = wfMsg ( "val_no" ) . " ";
720 } elseif( $max == 2 && $value == 2 ) {
721 $ret = wfMsg( "val_yes" );
722 } elseif( $value != 0 ) {
723 $ret = wfMsg( "val_of", $value, $max ) . " ";
724 } else {
725 $ret = "";
727 return $ret;
730 function navBar ( $offset , $limit , $lastcount , $mode = "userstats" ) {
731 global $wgRequest , $wgUser , $wgTitle ;
732 $sk = $wgUser->getSkin();
733 $r = array () ;
734 $user = $wgRequest->getVal( "user" );
736 if ( $mode == "userstats" ) {
737 $nt = Title::newFromText( 'Special:Validate' );
738 } else {
739 $nt = $wgTitle ;
742 $base = "action=validate&mode={$mode}&" ;
743 if ( $user != "" ) $base .= "user={$user}&" ;
744 $base .= "limit={$limit}&offset=" ;
746 if ( $offset > 0 ) {
747 $o = $offset - $limit ;
748 $t = $offset-$limit+1 ;
749 $r[] = $sk->makeKnownLinkObj( $nt, "{$t} <<" , $base.$o );
752 $s1 = $offset + 1 ;
753 $s2 = $s1 + $lastcount - 1 ;
754 $r[] = $s1 . " - " . $s2 ;
756 if ( $lastcount == $limit ) {
757 $o = $offset + $limit ;
758 $t = $offset+$limit+1 ;
759 $r[] = $sk->makeKnownLinkObj( $nt, ">> {$t}" , $base.$o );
762 $r = implode ( " | " , $r ) ;
763 return $r ;
766 function showUserStats( $user ) {
767 global $wgOut, $wgUser, $wgRequest;
768 $this->topicList = $this->getTopicList();
769 if ( $this->getNoTopicsWarning() ) return "" ;
770 $sk = $wgUser->getSkin();
772 $offset = $wgRequest->getVal( "offset" , 0 );
773 $limit = $wgRequest->getVal( "limit" , 25 );
774 $data = $this->getAllVoteLists( $user , $offset , $limit ) ;
776 if( $user == $wgUser->getID() ) {
777 $wgOut->setPageTitle ( wfMsg ( 'val_my_stats_title' ) );
778 } elseif( !User::IsIP( $user ) ) {
779 $wgOut->setPageTitle( wfMsg( 'val_user_stats_title', User::whoIs( $user ) ) );
780 } else {
781 $wgOut->setPageTitle( wfMsg( 'val_user_stats_title', $user ) );
784 $ret = "" ;
785 $ret = "<table>\n";
787 $linecount = 0 ;
788 $lastpage = -1 ;
789 $lastrevision = -1 ;
790 $initial = false ;
791 foreach ( $data AS $articleid => $revisions ) {
792 $title = Title::newFromID( $articleid );
793 if ( $lastpage != $articleid ) {
794 $ret .= "<tr><th colspan='4'>";
795 $ret .= $sk->makeKnownLinkObj( $title, $title->getEscapedText() );
796 $ret .= "</th></tr>";
797 $lastpage = $articleid ;
798 $lastrevision = -1 ;
800 krsort( $revisions );
801 foreach( $revisions as $revid => $revision ) {
802 $url = $title->getLocalURL( "oldid={$revid}" );
803 if ( $lastrevision != $revid ) {
804 $initial = true ;
805 $lastrevision = $revid ;
807 $ret .= "<tr>" ;
808 if ( $initial ) {
809 $ret .= "<th>";
810 $ret .= $sk->makeKnownLinkObj( $title, wfMsg('val_revision_number', $revid ), "oldid={$revid}" );
811 $ret .= "</th>";
813 ksort( $revision );
814 #$initial = true;
815 foreach( $revision as $topic => $rating ) {
816 if( !$initial ) {
817 $ret .= "<tr><td/>";
819 $initial = false;
820 $ret .= "<td>" . $this->getTopicLink ( $this->topicList[$topic]->val_comment ) . "</td>";
821 $ret .= "<td>" . $this->getRatingText( $rating->val_value, $this->topicList[$topic]->val_value ) . "</td>";
822 $ret .= "<td>" . $sk->commentBlock( $rating->val_comment ) . "</td>";
823 $ret .= "</tr>";
824 $linecount++ ;
827 $ret .= "</tr>";
829 $ret .= "</table>";
832 $s = $this->navBar ( $offset , $limit , $linecount ) ;
833 if ( $s != "" ) $ret = $s . "<br/>" . $ret . "<br/>" . $s ;
835 return $ret;
838 function getNoTopicsWarning () {
839 global $wgOut ;
840 if ( !isset ( $this->topicList ) ) # Just making sure, shouldn't be necessary...
841 $this->topicList = $this->getTopicList();
842 if ( count ( $this->topicList ) > 0 ) return false ; # Topics set, all right
843 $wgOut->errorpage( "val_no_topics_defined", "val_no_topics_defined_text" );
844 return true ;
850 * constructor
852 function wfSpecialValidate( $page = '' ) {
853 global $wgOut, $wgRequest, $wgUseValidation, $wgUser;
855 if( !$wgUseValidation ) {
856 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
857 return;
860 # Can do?
861 if( ! $wgUser->isBureaucrat() ) {#isAllowed('change_validation') ) {
862 $wgOut->sysopRequired();
863 return;
866 $mode = $wgRequest->getVal( "mode" );
867 $skin = $wgUser->getSkin();
869 $token = $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) );
871 if( $token ) {
872 if( $mode == "manage" ) {
873 $v = new Validation();
874 $html = $v->manageTopics();
875 } elseif( $mode == "userstats" ) {
876 $v = new Validation();
877 $user = $wgRequest->getVal( "user" );
878 $html = $v->showUserStats( $user );
880 } else {
881 $html = htmlspecialchars( $mode );
882 $html .= "<ul>\n";
884 $t = Title::newFromText( "Special:Validate" );
885 $url = $t->escapeLocalURL( "mode=manage" );
886 $html .= "<li><a href=\"" . $url . "\">Manage</a></li>\n";
888 $html .= "</ul>\n";
891 $wgOut->addHTML( $html );