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