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