5754f6ec5a01277c3f2220750bc2f0b0abdf8bf8
[lhc/web/wiklou.git] / includes / SpecialValidate.php
1 <?php
2 # Copyright (C) 2004 Magnus Manske <magnus.manske@web.de>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 *
22 * @package MediaWiki
23 * @subpackage SpecialPage
24 */
25
26
27 class Validation {
28 var $topicList;
29 var $voteCache;
30 var $page_id;
31 var $rev_fields = "rev_id,rev_page,rev_timestamp,rev_user_text,rev_user,rev_comment" ;
32
33 function getRevisionFromId( $rev_id ) {
34 if( isset( $this->id2rev[$rev_id] ) ) return $this->id2rev[$rev_id];
35
36 $db =& wfGetDB( DB_SLAVE );
37 $fname = 'SpecialValidate::getRevisionFromId';
38 $res = $db->select( 'revision', $this->rev_fields, array( 'rev_id' => $rev_id ), $fname, array( 'LIMIT' => 1 ) );
39 $rev = $db->fetchObject($res);
40 $db->freeResult($res);
41
42 $this->id2rev[$rev->rev_id] = $rev;
43 $this->ts2rev[$rev->rev_timestamp] = $rev;
44
45 return $rev;
46 }
47
48 function getRevisionFromTimestamp( $timestamp ) {
49 if( isset( $this->ts2rev[$timestamp] ) ) return $this->ts2rev[$timestamp];
50
51 $db =& wfGetDB( DB_SLAVE );
52 $fname = 'SpecialValidate::getRevisionFromTimestamp';
53 $res = $db->select( 'revision', $this->rev_fields,
54 array( 'rev_page' => $this->page_id, 'rev_timestamp' => $timestamp ),
55 $fname, array( 'LIMIT' => 1 )
56 );
57 $rev = $db->fetchObject($res);
58 $db->freeResult($res);
59
60 $this->id2rev[$rev->rev_id] = $rev;
61 $this->ts2rev[$rev->rev_timestamp] = $rev;
62
63 return $rev;
64 }
65
66 # Returns a HTML link to the specified article revision
67 function getRevisionLink( &$article, $revision, $text = "" ) {
68 global $wgUser;
69 $sk = $wgUser->getSkin();
70 $t = $article->getTitle();
71 if( $text == "" ) $text = wfMsg("val_view_version");
72 return $sk->makeKnownLinkObj( $t, $this->getParsedWiki($text), 'oldid='.urlencode($revision) );
73 }
74
75 # Returns an array containing all topics you can vote on
76 function getTopicList() {
77 $db =& wfGetDB( DB_SLAVE );
78
79 $topics = array();
80
81 # NOTE : This query returns only the topics to vote on
82 $res = $db->select( 'validate', '*', array( 'val_page' => 0 ), 'SpecialValidate::getTopicList' );
83 while( $topic = $db->fetchObject($res) ) {
84 $topics[$topic->val_type] = $topic;
85 }
86 $db->freeResult($res);
87
88 ksort( $topics );
89 return $topics;
90 }
91
92 # Merges one dataset into another
93 function mergeInto( &$source, &$dest ) {
94 $ret = false;
95 foreach( $source as $x => $y ) {
96 $doit = false;
97 if( !isset( $dest[$x] ) ) {
98 $doit = true;
99 } elseif( $dest[$x]->value == 0 ) {
100 $doit = true;
101 }
102 if( $doit ) {
103 $dest[$x] = $y;
104 $ret = true;
105 }
106 }
107 if( $ret ) {
108 ksort ( $dest );
109 }
110 return $ret;
111 }
112
113 # Merges all votes prior to the given revision into it
114 function mergeOldRevisions( &$article, $revision ) {
115 $tmp = $this->voteCache;
116 krsort( $tmp );
117 $update = false;
118 $ts = $this->getRevisionTimestamp( $revision );
119 $data = $this->voteCache[$ts];
120 foreach( $tmp as $x => $y ) {
121 if( $x < $ts ) {
122 if( $this->mergeInto( $y, $data ) ) {
123 $update = true;
124 }
125 }
126 }
127 if( $update ) {
128 $this->setRevision( $article, $revision, $data );
129 }
130 }
131
132 # Clears all votes prior to the given revision
133 function clearOldRevisions( &$article, $revision ) {
134 $tmp = $this->voteCache;
135 $ts = $this->getRevisionTimestamp( $revision );
136 foreach( $tmp as $x => $y ) {
137 if( $x < $ts ) {
138 $this->deleteRevisionVote ( $article, $this->getRevisionId( $x ) );
139 }
140 }
141 }
142
143 # Updates the votes for the given revision from the FORM data
144 function updateRevision( &$article, $revision ) {
145 global $wgRequest;
146
147 if( isset( $this->voteCache[$this->getRevisionTimestamp( $revision )] ) ) {
148 $data = $this->voteCache[$this->getRevisionTimestamp( $revision )];
149 } else {
150 $data = array();
151 }
152 $nv = $wgRequest->getArray( "re_v_{$revision}", array() );
153 $nc = $wgRequest->getArray( "re_c_{$revision}", array() );
154
155 foreach( $nv as $x => $y ) {
156 $data[$x]->value = $y;
157 $data[$x]->comment = $nc[$x];
158 }
159 krsort( $data );
160
161 $this->setRevision( $article, $revision, $data );
162 }
163
164 # Sets a specific revision to both cache and database
165 function setRevision( &$article, $revision, &$data ) {
166 global $wgUser;
167 $this->deleteRevisionVote( $article, $revision );
168 $this->voteCache[ $this->getRevisionTimestamp($revision) ] = $data;
169 foreach( $data as $x => $y ) {
170 if( $y->value > 0 ) {
171 $ip = $wgUser->isAnon() ? $wgUser->getName() : '';
172 $dbw =& wfGetDB( DB_MASTER );
173 $dbw->insert( 'validate',
174 array(
175 'val_user' => $wgUser->getId(),
176 'val_page' => $article->getId(),
177 'val_revision' => $revision,
178 'val_type' => $x,
179 'val_value' => $y->value,
180 'val_comment' => $y->comment,
181 'val_ip' => $ip ),
182 'SpecialValidate::setRevision'
183 );
184 }
185 }
186 }
187
188 # Returns a map identifying the current user
189 function identifyUser( $user = "" ) {
190 global $wgUser;
191 if( $user == "" ) $user = $wgUser->getID();
192 return User::isIP($user)
193 ? array( 'val_user' => 0, 'val_ip' => $user )
194 : array( 'val_user' => $user );
195 }
196
197 # Deletes a specific vote set in both cache and database
198 function deleteRevisionVote( &$article, $revision ) {
199 $ts = $this->getRevisionTimestamp( $revision );
200 if( !isset ( $this->voteCache[$ts] ) ) return;
201
202 $db =& wfGetDB( DB_MASTER );
203 $db->delete(
204 'validate',
205 array_merge(
206 $this->identifyUser(),
207 array(
208 'val_page' => $article->getID(),
209 'val_revision' => $revision
210 )
211 ),
212 'SpecialValidate::deleteRevisionVote'
213 );
214
215 unset( $this->voteCache[$ts] );
216 }
217
218 # Reads the entire vote list for this user for the given article
219 function getVoteList( $id, $user = "" ) {
220 $db =& wfGetDB( DB_SLAVE );
221
222 # NOTE : This query gets the votes for a single user on a single page.
223 # Assuming most people will use the "merge" feature,
224 # this will be only a single entry.
225 $res = $db->select( 'validate', '*', array_merge( array( 'val_page' => $id ), $this->identifyUser($user) ) );
226
227 $revisions = array();
228 while( $vote = $db->fetchObject($res) ) {
229 $ts = $this->getRevisionTimestamp( $vote->val_revision );
230 if( ! isset( $revisions[$ts] ) ) {
231 $revisions[$ts] = array();
232 }
233 $revisions[$ts][$vote->val_type]->value = $vote->val_value;
234 $revisions[$ts][$vote->val_type]->comment = $vote->val_comment;
235 }
236 $db->freeResult($res);
237
238 return $revisions;
239 }
240
241 # Reads a partial vote list for this user for all articles
242 function getAllVoteLists( $user , $offset , $limit ) {
243 $db =& wfGetDB( DB_SLAVE );
244 $a = $this->identifyUser($user) ;
245 $b = array ( "ORDER BY" => "val_page,val_revision" , "OFFSET" => $offset , "LIMIT" => $limit ) ;
246 $res = $db->select( 'validate', '*', $a , 'getAllVotesList' , $b );
247
248 $votes = array();
249 while( $vote = $db->fetchObject($res) ) {
250 $votes[$vote->val_page][$vote->val_revision][$vote->val_type] = $vote;
251 }
252 $db->freeResult($res);
253
254 return $votes ;
255 }
256
257 # This functions adds a topic to the database
258 function addTopic( $topic, $limit ) {
259 $db =& wfGetDB( DB_MASTER );
260
261 $next_idx = 1;
262 while( isset( $this->topicList[$next_idx] ) ) {
263 $next_idx++;
264 }
265
266 $db->insert(
267 'validate',
268 array(
269 'val_user' => 0,
270 'val_page' => 0,
271 'val_revision' => 0,
272 'val_type' => $next_idx,
273 'val_value' => $limit,
274 'val_comment' => $topic,
275 'val_ip' => ''
276 ),
277 'SpecialValidate::addTopic'
278 );
279
280 $t->val_user = $t->val_page = $t->val_revision = 0;
281 $t->val_type = $next_idx;
282 $t->val_value = $limit;
283 $t->val_comment = $topic;
284 $t->val_ip = "";
285 $this->topicList[$next_idx] = $t;
286
287 ksort( $this->topicList );
288 }
289
290 # This function deletes a topic and all votes for it. CAREFUL!
291 function deleteTopic( $id ) {
292 $db =& wfGetDB( DB_MASTER );
293 $db->delete( 'validate', array( 'val_type' => $id ), 'SpecialValidate::deleteTopic' );
294 unset( $this->topicList[$id] );
295 }
296
297 # This function returns a link text to the page validation statistics
298 function getStatisticsLink( &$article ) {
299 global $wgUser;
300 $sk = $wgUser->getSkin();
301 $nt = $article->getTitle();
302 return $sk->makeKnownLinkObj( $nt, wfMsg( 'val_rev_stats', $nt->getPrefixedText() ), 'action=validate&mode=list' );
303 }
304
305 # This function returns a link text to the page validation statistics of a single revision
306 function getRevisionStatsLink( &$article, $revision ) {
307 global $wgUser;
308 $sk = $wgUser->getSkin();
309 $nt = $article->getTitle();
310 $text = $this->getParsedWiki( wfMsg('val_revision_stats_link') );
311 $query = "action=validate&mode=details&revision={$revision}";
312 return '(' . $sk->makeKnownLinkObj( $nt, $text, $query ) . ')';
313 }
314
315 # This function returns a link text to the user rating statistics page
316 function getUserRatingsLink( $user, $text ) {
317 global $wgUser;
318 $sk = $wgUser->getSkin();
319 if( $user == 0 ) $user = $wgUser->getName();
320 $nt = Title::newFromText( 'Special:Validate' );
321 return $sk->makeKnownLinkObj( $nt, $text, 'mode=userstats&user='.urlencode($user) );
322 }
323
324 # Returns the timestamp of a revision based on the revision number
325 function getRevisionTimestamp( $rev_id ) {
326 $rev = $this->getRevisionFromId( $rev_id );
327 return $rev->rev_timestamp;
328 }
329
330 # Returns the revision number of a revision based on the timestamp
331 function getRevisionId( $ts ) {
332 $rev = $this->getRevisionFromTimestamp( $ts );
333 return $rev->rev_id;
334 }
335
336
337 # HTML generation functions from this point on
338
339 # Returns the metadata string for a revision
340 function getMetadata( $rev_id, &$article ) {
341 global $wgUser;
342 $sk = $wgUser->getSkin();
343
344 $metadata = "";
345 $x = $this->getRevisionFromId($rev_id);
346 $metadata .= wfTimestamp( TS_DB, $x->rev_timestamp );
347 $metadata .= " by ";
348 if( $x->rev_user == 0 ) {
349 $metadata .= $x->rev_user_text;
350 } else {
351 $u = new User;
352 $u->setId( $x->rev_user );
353 $u->setName( $x->rev_user_text );
354 $nt = $u->getUserPage();
355 $metadata .= $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
356 }
357 $metadata .= ': '. $sk->commentBlock( $x->rev_comment, $article->getTitle() );
358 return $metadata;
359 }
360
361 # Generates a link to the topic description
362 function getTopicLink($s) {
363 $t = Title::newFromText ( wfMsg ( 'val_topic_desc_page' ) ) ;
364 # FIXME: Why doesn't this use standard linking code?
365 $r = "<a href=\"" ;
366 $r .= $t->escapeLocalURL () ;
367 $r .= "#" . urlencode ( $s ) ;
368 $r .= "\">{$s}</a>" ;
369 return $r ;
370 }
371
372 # Generates HTML from a wiki text, e.g., a wfMsg
373 function getParsedWiki ( $text ) {
374 global $wgOut, $wgTitle, $wgParser ;
375 $parserOutput = $wgParser->parse( $text , $wgTitle, $wgOut->mParserOptions,false);
376 return $parserOutput->getText() ;
377 }
378
379 # Generates a form for a single revision
380 function getRevisionForm( &$article, $idx, &$data, $focus = false ) {
381 # Fill data with blank values
382 $ts = $idx;
383 $revision = $this->getRevisionId( $ts );
384 foreach( $this->topicList as $x => $y ) {
385 if( !isset( $data[$x] ) ) {
386 $data[$x]->value = 0;
387 $data[$x]->comment = "";
388 }
389 }
390 ksort( $data ) ;
391
392 # Generate form
393 $table_class = $focus ? 'revisionform_focus' : 'revisionform_default';
394 $ret = "<form method='post'><table class='{$table_class}'>\n";
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";
408
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" );
415 }
416 $vote .= "<input type='radio' name='re_v{$idx}' value='{$a}'";
417 if( $a == $y->value ) {
418 $vote .= " checked='checked'";
419 }
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 . " ";
427 }
428 if ( $a == 0 ) {
429 $vote .= " &nbsp; ";
430 }
431 }
432 $ret .= "<td>{$vote}</td>\n";
433
434 $ret .= "<td><input size='50' style='width:98%' maxlength='250' type='text' name='re_c{$idx}' value='{$y->comment}'/>";
435 $ret .= "</td></tr>\n";
436 }
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";
442
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;
447 }
448
449
450 # Generates the page from the validation tab
451 function validatePageForm( &$article, $revision ) {
452 global $wgOut, $wgRequest, $wgUser;
453
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() );
459
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 );
470 }
471 if( $clearOldRev ) {
472 $this->clearOldRevisions( $article, $id );
473 }
474 $ret .= '<p class="revision_saved">' . $this->getParsedWiki( wfMsg( 'val_revision_changes_ok' ) ) . "</p>";
475 } else {
476 $ret .= $this->getParsedWiki( wfMsg ('val_votepage_intro') );
477 }
478
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();
484 }
485
486 # Sort revisions list, newest first
487 krsort( $this->voteCache );
488
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";
496 }
497 $ret .= $this->getStatisticsLink( $article );
498 $ret .= "<p>" . $this->getUserRatingsLink( $wgUser->getID(), wfMsg( 'val_show_my_ratings' ) ) . "</p>";
499 return $ret ;
500 }
501
502 # This function performs the "management" mode on Special:Validate
503 function manageTopics() {
504 global $wgRequest, $wgValidationMaxTopics;
505 $this->topicList = $this->getTopicList();
506
507 $r = "" ; # Return value
508 $iamsure = true ; # Sure by default, see checkbox below # $wgRequest->getVal( "iamsure", "0" ) == 1;
509
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 );
518 }
519 }
520 }
521
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 );
527 }
528
529 # FIXME: Wikitext this
530 $r .= "<p>" . $this->getParsedWiki( wfMsg( 'val_warning' ) ) . "</p>\n";
531 $r .= "<form method='post'>\n";
532 $r .= "<table>\n";
533 $r .= "<tr>" . wfMsg( 'val_list_header' ) . "</tr>\n";
534 foreach( $this->topicList as $x => $y ) {
535 $r .= "<tr>\n";
536 $r .= "<th>{$y->val_type}</th>\n";
537 $r .= "<td>" . $this->getTopicLink ( $y->val_comment ) . "</td>\n";
538 $r .= "<td>1 .. <b>" . intval( $y->val_value ) . "</b></td>\n";
539 $r .= "<td><input type='submit' name='m_del[" . intval( $x ) . "]' value='" . htmlspecialchars( wfMsg( 'val_del' ) ) . "'/></td>\n";
540 $r .= "</tr>\n";
541 }
542
543 # Add topic, or too-many-topics warning
544 if ( count ( $this->topicList ) >= $wgValidationMaxTopics ) {
545 $r .= "<tr><td colspan='4' align='center'>" ;
546 $r .= wfMsg ( 'val_max_topics' , $wgValidationMaxTopics ) ;
547 $r .= "</td></tr>" ;
548 } else {
549 $r .= "<tr>\n";
550 $r .= "<td></td>\n";
551 $r .= '<td><input type="text" name="m_topic" value=""/></td>' . "\n";
552 $r .= '<td>1 .. <input type="text" name="m_limit" value="" size="4"/></td>' . "\n";
553 $r .= '<td>' ;
554 $r .= '<input type="submit" name="m_add" value="' . htmlspecialchars( wfMsg( 'val_add' ) ) . '"/>' ;
555 $r .= '</td>' . "\n";
556 $r .= "</tr>" ;
557 }
558
559 $r .= "</table>\n";
560
561 # This is fot the checkbox "I am sure" for actions, which was found to be unituitive
562 # $r .= '<p><input type="checkbox" name="iamsure" id="iamsure" value="1"/>';
563 # $r .= '<label for="iamsure">' . $this->getParsedWiki( wfMsg( 'val_iamsure' ) ) . "</label></p>\n";
564
565 $r .= "</form>\n";
566 return $r;
567 }
568
569 # Generates an ID for both logged-in users and anons; $res is an object from an SQL query
570 function make_user_id( &$res ) {
571 return $res->val_user == 0 ? $res->val_ip : $res->val_user;
572 }
573
574 function showDetails( &$article, $revision ) {
575 global $wgOut, $wgUser;
576 $this->page_id = $article->getID();
577 $this->topicList = $this->getTopicList();
578 if ( $this->getNoTopicsWarning() ) return "" ;
579
580 $sk = $wgUser->getSkin();
581 $title = $article->getTitle();
582 $wgOut->setPageTitle( str_replace( '$1', $title->getPrefixedText(), wfMsg( 'val_validation_of' ) ) );
583
584 $data = array();
585 $users = array();
586 $topics = array();
587
588 # Collecting statistic data
589 $db =& wfGetDB( DB_SLAVE );
590 $res = $db->select( 'validate', '*', array( 'val_page' => $this->page_id, 'val_revision' => $revision ), 'SpecialValidate::showDetails' );
591 while( $x = $db->fetchObject($res) ) {
592 $data[$this->make_user_id($x)][$x->val_type] = $x;
593 $users[$this->make_user_id($x)] = true;
594 $topics[$x->val_type] = true;
595 }
596 $db->freeResult($res);
597
598 # Sorting lists of topics and users
599 ksort( $users );
600 ksort( $topics );
601
602 $ts = $this->getRevisionTimestamp( $revision );
603 $url = $this->getRevisionLink( $article, $revision, wfTimestamp( TS_DB, $ts ) );
604
605 # Table headers
606 $ret = "" ;
607 $ret .= "<p><b>" . str_replace( '$1', $url, wfMsg( 'val_revision_of' ) ) . "</b></p>\n";
608 $ret .= "<table>\n";
609 $ret .= "<tr><th>" . $this->getParsedWiki ( wfMsg('val_details_th') ) . "</th>" ;
610
611 foreach( $topics as $t => $dummy ) {
612 $ret .= '<th>' . $sk->commentBlock( $this->topicList[$t]->val_comment, $article->getTitle() ) . '</th>';
613 }
614 $ret .= "</tr>\n";
615
616 # Table data
617 foreach( $users as $u => $dummy ) { # Every row a user
618 $ret .= "<tr>";
619 $ret .= "<th>";
620 if( !User::IsIP( $u ) ) { # Logged-in user rating
621 $ret .= $this->getUserRatingsLink( $u, User::whoIs( $u ) );
622 } else { # Anon rating
623 $ret .= $this->getUserRatingsLink( $u, $u );
624 }
625 $ret .= "</th>";
626 foreach( $topics as $t => $dummy ) { # Every column a topic
627 if( !isset( $data[$u][$t] ) ) {
628 $ret .= "<td/>";
629 } else {
630 $ret .= "<td>";
631 $ret .= $data[$u][$t]->val_value;
632 if( $data[$u][$t]->val_comment != "" ) {
633 $ret .= ' ' . $sk->commentBlock( $data[$u][$t]->val_comment, $article->getTitle() );
634 }
635 $ret .= "</td>";
636 }
637 }
638 $ret .= "</tr>";
639 }
640 $ret .= "</table>";
641 $ret .= "<p>" . $this->getStatisticsLink( $article ) . "</p>";
642 $ret .= "<p>" . $this->getUserRatingsLink( $wgUser->getID(), wfMsg( 'val_show_my_ratings' ) ) . "</p>";
643
644 return $ret;
645 }
646
647 function showList( &$article ) {
648 global $wgOut, $wgUser , $wgRequest;
649 $this->page_id = $article->getID();
650 $this->topicList = $this->getTopicList();
651 if ( $this->getNoTopicsWarning() ) return "" ;
652
653 $title = $article->getTitle();
654 $wgOut->setPageTitle( wfMsg( 'val_validation_of', $title->getPrefixedText() ) );
655
656 $offset = $wgRequest->getVal ( "offset" , 0 ) ;
657 $limit = $wgRequest->getVal ( "limit" , 25 ) ;
658
659 # Collecting statistic data
660 # Unfortunately, it has to read all the data, though it will only display a part
661 $db =& wfGetDB( DB_SLAVE );
662 $res = $db->select( 'validate', 'val_revision,val_type,val_value', array( "val_page" => $this->page_id ), 'SpecialValidate::showList' );#, $b );
663
664 $statistics = array();
665 while( $vote = $db->fetchObject($res) ) {
666 $ts = $this->getRevisionTimestamp($vote->val_revision);
667 if ( !isset ( $statistics[$ts] ) ) $statistics[$ts] = array () ;
668 if ( !isset ( $statistics[$ts][$vote->val_type]->count ) ) $statistics[$ts][$vote->val_type]->count = 0 ;
669 if ( !isset ( $statistics[$ts][$vote->val_type]->sum ) ) $statistics[$ts][$vote->val_type]->sum = 0 ;
670 $statistics[$ts][$vote->val_type]->count++;
671 $statistics[$ts][$vote->val_type]->sum += $vote->val_value;
672 }
673 $db->freeResult($res);
674
675 krsort( $statistics );
676
677 $ret = "<table><tr>\n";
678 $ret .= "<th>" . $this->getParsedWiki( wfMsg( "val_revision" ) ) . "</th>\n";
679 foreach( $this->topicList as $topic ) {
680 $ret .= "<th>" . $this->getTopicLink($topic->val_comment) . "</th>";
681 }
682 $ret .= "</tr>\n";
683
684 # Paging
685 $statistics = array_slice ( $statistics , $offset , $limit ) ;
686
687 foreach( $statistics as $ts => $data ) {
688 $rev_id = $this->getRevisionId( $ts );
689 $revision_link = $this->getRevisionLink( $article, $rev_id, wfTimestamp( TS_DB, $ts ) );
690 $details_link = $this->getRevisionStatsLink( $article, $rev_id );
691 $ret .= "<tr><td>{$revision_link} {$details_link}</td>";
692 foreach( $this->topicList as $topicType => $topic ) {
693 if( isset( $data[$topicType] ) ) {
694 $stats = $data[$topicType];
695 $average = $stats->count == 0 ? 0 : $stats->sum / $stats->count;
696 $ret .= sprintf( "<td><b>%1.1f</b> (%d)</td>", $average, $stats->count );
697 } else {
698 $ret .= "<td></td>";
699 }
700 }
701 $ret .= "</tr>\n";
702 }
703 $ret .= "</table>\n";
704
705 # Navbar
706 $s = $this->navBar ( $offset , $limit , count ( $statistics ) , "list" ) ;
707 $ret = $s . $ret . $s . "<p/>" ;
708
709 $ret .= "<p>" . $this->getUserRatingsLink( $wgUser->getID(), wfMsg( 'val_show_my_ratings' ) ) . "</p>";
710
711 return $ret;
712 }
713
714 function getRatingText( $value, $max ) {
715 if( $max == 2 && $value == 1 ) {
716 $ret = wfMsg ( "val_no" ) . " ";
717 } elseif( $max == 2 && $value == 2 ) {
718 $ret = wfMsg( "val_yes" );
719 } elseif( $value != 0 ) {
720 $ret = wfMsg( "val_of", $value, $max ) . " ";
721 } else {
722 $ret = "";
723 }
724 return $ret;
725 }
726
727 function navBar ( $offset , $limit , $lastcount , $mode = "userstats" ) {
728 global $wgRequest , $wgUser , $wgTitle ;
729 $sk = $wgUser->getSkin();
730 $r = array () ;
731 $user = $wgRequest->getVal( "user" );
732
733 if ( $mode == "userstats" ) {
734 $nt = Title::newFromText( 'Special:Validate' );
735 } else {
736 $nt = $wgTitle ;
737 }
738
739 $base = "action=validate&mode={$mode}&" ;
740 if ( $user != "" ) $base .= "user={$user}&" ;
741 $base .= "limit={$limit}&offset=" ;
742
743 if ( $offset > 0 ) {
744 $o = $offset - $limit ;
745 $t = $offset-$limit+1 ;
746 $r[] = $sk->makeKnownLinkObj( $nt, "{$t} <<" , $base.$o );
747 }
748
749 $s1 = $offset + 1 ;
750 $s2 = $s1 + $lastcount - 1 ;
751 $r[] = $s1 . " - " . $s2 ;
752
753 if ( $lastcount == $limit ) {
754 $o = $offset + $limit ;
755 $t = $offset+$limit+1 ;
756 $r[] = $sk->makeKnownLinkObj( $nt, ">> {$t}" , $base.$o );
757 }
758
759 $r = implode ( " | " , $r ) ;
760 return $r ;
761 }
762
763 function showUserStats( $user ) {
764 global $wgOut, $wgUser, $wgRequest;
765 $this->topicList = $this->getTopicList();
766 if ( $this->getNoTopicsWarning() ) return "" ;
767 $sk = $wgUser->getSkin();
768
769 $offset = $wgRequest->getVal( "offset" , 0 );
770 $limit = $wgRequest->getVal( "limit" , 25 );
771 $data = $this->getAllVoteLists( $user , $offset , $limit ) ;
772
773 if( $user == $wgUser->getID() ) {
774 $wgOut->setPageTitle ( wfMsg ( 'val_my_stats_title' ) );
775 } elseif( !User::IsIP( $user ) ) {
776 $wgOut->setPageTitle( wfMsg( 'val_user_stats_title', User::whoIs( $user ) ) );
777 } else {
778 $wgOut->setPageTitle( wfMsg( 'val_user_stats_title', $user ) );
779 }
780
781 $ret = "" ;
782 $ret = "<table>\n";
783
784 $linecount = 0 ;
785 $lastpage = -1 ;
786 $lastrevision = -1 ;
787 $initial = false ;
788 foreach ( $data AS $articleid => $revisions ) {
789 $title = Title::newFromID( $articleid );
790 if ( $lastpage != $articleid ) {
791 $ret .= "<tr><th colspan='4'>";
792 $ret .= $sk->makeKnownLinkObj( $title, $title->getEscapedText() );
793 $ret .= "</th></tr>";
794 $lastpage = $articleid ;
795 $lastrevision = -1 ;
796 }
797 krsort( $revisions );
798 foreach( $revisions as $revid => $revision ) {
799 $url = $title->getLocalURL( "oldid={$revid}" );
800 if ( $lastrevision != $revid ) {
801 $initial = true ;
802 $lastrevision = $revid ;
803 }
804 $ret .= "<tr>" ;
805 if ( $initial ) {
806 $ret .= "<th>";
807 $ret .= $sk->makeKnownLinkObj( $title, wfMsg('val_revision_number', $revid ), "oldid={$revid}" );
808 $ret .= "</th>";
809 }
810 ksort( $revision );
811 #$initial = true;
812 foreach( $revision as $topic => $rating ) {
813 if( !$initial ) {
814 $ret .= "<tr><td/>";
815 }
816 $initial = false;
817 $ret .= "<td>" . $this->getTopicLink ( $this->topicList[$topic]->val_comment ) . "</td>";
818 $ret .= "<td>" . $this->getRatingText( $rating->val_value, $this->topicList[$topic]->val_value ) . "</td>";
819 $ret .= "<td>" . $sk->commentBlock( $rating->val_comment ) . "</td>";
820 $ret .= "</tr>";
821 $linecount++ ;
822 }
823 }
824 $ret .= "</tr>";
825 }
826 $ret .= "</table>";
827
828
829 $s = $this->navBar ( $offset , $limit , $linecount ) ;
830 if ( $s != "" ) $ret = $s . "<br/>" . $ret . "<br/>" . $s ;
831
832 return $ret;
833 }
834
835 function getNoTopicsWarning () {
836 global $wgOut ;
837 if ( !isset ( $this->topicList ) ) # Just making sure, shouldn't be necessary...
838 $this->topicList = $this->getTopicList();
839 if ( count ( $this->topicList ) > 0 ) return false ; # Topics set, all right
840 $wgOut->errorpage( "val_no_topics_defined", "val_no_topics_defined_text" );
841 return true ;
842 }
843
844 }
845
846 /**
847 * constructor
848 */
849 function wfSpecialValidate( $page = '' ) {
850 global $wgOut, $wgRequest, $wgUseValidation, $wgUser, $wgContLang;
851
852 if( !$wgUseValidation ) {
853 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
854 return;
855 }
856
857
858 # Can do?
859 if( ! $wgUser->isBureaucrat() ) {#isAllowed('change_validation') ) {
860 $wgOut->sysopRequired();
861 return;
862 }
863
864
865 $mode = $wgRequest->getVal( "mode" );
866 $skin = $wgUser->getSkin();
867
868
869 if( $mode == "manage" ) {
870 $v = new Validation();
871 $html = $v->manageTopics();
872 } elseif( $mode == "userstats" ) {
873 $v = new Validation();
874 $user = $wgRequest->getVal( "user" );
875 $html = $v->showUserStats( $user );
876 } else {
877 $html = "$mode";
878 $html .= "<ul>\n";
879
880 $t = Title::newFromText( "Special:Validate" );
881 $url = $t->escapeLocalURL( "mode=manage" );
882 $html .= "<li><a href=\"" . $url . "\">Manage</a></li>\n";
883
884 $html .= "</ul>\n";
885 }
886
887 $wgOut->addHTML( $html );
888 }
889
890 ?>