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