I had a fix for this bug sitting in my working copy for over a week without getting...
[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
33 function find_this_version( $article_title , &$article_time , &$id , &$tab ) {
34 $id = "";
35 $tab = "";
36 $sql = "SELECT cur_id,cur_timestamp FROM cur WHERE cur_namespace=0 AND cur_title='" . wfStrencode( $article_title ) . "'";
37 $res = wfQuery( $sql, DB_READ );
38 if( $s = wfFetchObject( $res ) ) {
39 if ( $article_time == "" ) {
40 # No timestamp = current version
41 $article_time = $s->cur_timestamp;
42 } elseif ( $article_time == $s->cur_timestamp ) {
43 # Means current version
44 $tab = "cur";
45 $id = $s->cur_id;
46 }
47 }
48
49 if ( $id == "" ) {
50 $sql = "SELECT old_id FROM old WHERE old_namespace=0 AND old_title='" . wfStrencode( $article_title ) .
51 "' AND old_timestamp='" . wfStrencode( $article_time ) . "'";
52 $res = wfQuery( $sql, DB_READ );
53 if( $s = wfFetchObject( $res ) ) {
54 $tab = "old";
55 $id = $s->old_id;
56 }
57 }
58 }
59
60 function get_prev_data( $user_id , $article_title , $article_timestamp = "" ) {
61 $ret = array ();
62 $sql = "SELECT * FROM validate WHERE val_user='" . wfStrencode( $user_id ) .
63 "' AND val_title='" . wfStrencode( $article_title ) . "'";
64 if ( $article_timestamp != "" ) {
65 $sql .= " AND val_timestamp='" . wfStrencode( $article_timestamp ) . "'";
66 }
67 $res = wfQuery( $sql, DB_READ );
68 while( $s = wfFetchObject( $res ) ) {
69 $ret[$s->val_timestamp][$s->val_type] = $s;
70 }
71 return $ret;
72 }
73
74 function validate_form( $article_title = "" ) {
75 global $wgOut, $wgLang, $wgUser, $wgArticle, $wgRequest;
76
77 if ( $wgUser->getID() == 0 ) {
78 # Anon
79 $wgOut->addHTML( htmlspecialchars( wfMsg( 'val_no_anon_validation' ) ) .
80 $this->getPageStatistics ( $article_title ) ) ;
81 return;
82 }
83
84 $validationtypes = $wgLang->getValidationTypes();
85 if ( $article_title == "" ) {
86 $article_title = $wgRequest->getVal( 'article_title' );
87 $heading = "<h1>" . htmlspecialchars( $article_title ) . "</h1>\n";
88 } else {
89 $heading = "";
90 }
91 $article_time = "";
92 $article_time = $wgRequest->getVal( 'timestamp' );
93 $article = Title::newFromText( $article_title );
94 if( is_null( $article ) ) {
95 $wgOut->errorpage( "badtitle", "badtitletext" );
96 return;
97 }
98
99 # Now we get all the "votes" for the different versions of this article for this user
100 $val = $this->get_prev_data( $wgUser->getID() , $article_title );
101
102 # No votes for this version, initial data
103 if( !isset( $val[$article_time] ) ) {
104 if( $article_time == "" ) {
105 $res = wfQuery( "select cur_timestamp FROM cur WHERE cur_title='" .
106 wfStrencode( $article_title ) . "' AND cur_namespace=0", DB_READ );
107 if( $s = wfFetchObject( $res ) ) {
108 $article_time = $s->cur_timestamp;
109 }
110 }
111 $val[$article_time] = array();
112 }
113
114 # Newest versions first
115 krsort( $val );
116
117 # User has clicked "Doit" before, so evaluate form
118 if( $wgRequest->wasPosted() ) {
119 $oldtime = StrVal( $wgRequest->getVal( 'oldtime' ) );
120 if( !isset ( $val[$oldtime] ) ) {
121 $val[$oldtime] = array();
122 }
123
124 # Reading postdata
125 $postrad = array();
126 $poscomment = array();
127 for( $idx = 0 ; $idx < count( $validationtypes) ; $idx++ ) {
128 $postrad[$idx] = $wgRequest->getVal( "rad{$idx}" );
129 $postcomment[$idx] = $wgRequest->getText( "comment{$idx}" );
130 }
131
132 # Merge others into this one
133 if( $wgRequest->getCheck( 'merge_other' ) ) {
134 foreach( $val as $time => $stuff ) {
135 if( $time != $article_time ) {
136 for( $idx = 0; $idx < count( $validationtypes ); $idx++ ) {
137 $rad = $postrad[$idx];
138 if( isset ( $stuff[$idx] ) && $stuff[$idx]->val_value != -1 && $rad == -1 ) {
139 $postrad[$idx] = $stuff[$idx]->val_value;
140 $postcomment[$idx] = $stuff[$idx]->val_comment;
141 }
142 }
143 }
144 }
145 }
146
147 # Clear all others
148 if( $wgRequest->getCheck( 'clear_other' ) ) {
149 $sql = "DELETE FROM validate WHERE val_title='" . wfStrencode( $article_title ) .
150 "' AND val_timestamp<>'" . wfStrencode( $oldtime ) . "' AND val_user='";
151 $sql .= wfStrencode( $wgUser->getID() ) . "'";
152 wfQuery( $sql, DB_WRITE );
153 $val2 = $val[$oldtime]; # Only version left
154 $val = array(); # So clear others
155 $val[$oldtime] = $val2;
156 }
157
158 # Delete old "votes" for this version
159 $sql = "DELETE FROM validate WHERE val_title='" . wfStrencode( $article_title ) .
160 "' AND val_timestamp='" . wfStrencode( $oldtime ) . "' AND val_user='";
161 $sql .= wfStrencode( $wgUser->getID() ) . "'";
162 wfQuery( $sql, DB_WRITE );
163
164 # Incorporate changes
165 for( $idx = 0; $idx < count( $validationtypes ); $idx++ ) {
166 $comment = $postcomment[$idx] ;
167 $rad = $postrad[$idx] ;
168 if ( !isset( $val[$oldtime][$idx] ) ) {
169 $val[$oldtime][$idx] = "";
170 }
171 $val[$oldtime][$idx]->val_value = $rad;
172 $val[$oldtime][$idx]->val_comment = $comment;
173 if( $rad != -1 ) {
174 # Store it in the database
175 $sql = "INSERT INTO validate (val_user,val_title,val_timestamp,val_type,val_value,val_comment) " .
176 "VALUES ( '" . wfStrencode( $wgUser->getID() ) . "','" .
177 wfStrencode( $article_title ) . "','" .
178 wfStrencode( $oldtime ) . "','" .
179 wfStrencode( $idx ) . "','" .
180 wfStrencode( $rad ) . "','" .
181 wfStrencode( $comment ) . "')";
182 wfQuery( $sql, DB_WRITE );
183 }
184 }
185 $wgArticle->showArticle( "Juhuu", wfMsg( 'val_validated' ) );
186 return; # Show article instead of validation page
187 }
188
189 # Generating HTML
190 $html = "";
191
192 $skin = $wgUser->getSkin();
193 $staturl = $skin->makeSpecialURL( "validate" , "mode=stat_page&article_title=" . urlencode( $article_title ) );
194 $listurl = $skin->makeSpecialURL( "validate" , "mode=list_page" );
195 $html .= "<a href=\"" . htmlspecialchars( $staturl ) . "\">" . wfMsg('val_stat_link_text') . "</a> \n";
196 $html .= "<a href=\"" . htmlspecialchars( $listurl ) . "\">" . wfMsg('val_article_lists') . "</a><br />\n";
197 $html .= "<small>" . wfMsg('val_form_note') . "</small><br />\n";
198
199 # Generating data tables
200 $tabsep = "<td width='0' style='border-left:2px solid black;'></td>";
201 $topstyle = "style='border-top:2px solid black'";
202 foreach( $val as $time => $stuff ) {
203 $tablestyle = "cellspacing='0' cellpadding='2'";
204 if ( $article_time == $time ) {
205 $tablestyle .=" style='border: 2px solid red'";
206 }
207 $html .= "<h2>" . wfMsg( 'val_version_of', gmdate( "F d, Y H:i:s", wfTimestamp2Unix( $time ) ) );
208 $this->find_this_version ( $article_title , $time , $table_id , $table_name );
209 if( $table_name == "cur" ) {
210 $html .= " (" . wfMsg( 'val_this_is_current_version' ) . ")";
211 }
212 $html .= "</h2>\n" ;
213 $html .= "<form method='post'>\n" ;
214 $html .= "<input type='hidden' name='oldtime' value=\"" . htmlspecialchars( $time ) . "\" />" ;
215 $html .= "<table {$tablestyle}>\n" ;
216 $html .= wfMsg( 'val_table_header', $tabsep );
217 for( $idx = 0; $idx < count( $validationtypes ); $idx++ ) {
218 $x = explode( "|" , $validationtypes[$idx] , 4 );
219 if( isset ( $stuff[$idx] ) ) {
220 $choice = $stuff[$idx]->val_value;
221 } else {
222 $choice = -1;
223 }
224 if( isset( $stuff[$idx] ) ) {
225 $comment = $stuff[$idx]->val_comment;
226 } else {
227 $comment = "";
228 }
229 $html .= "<tr><th align='left'>{$x[0]}</th>{$tabsep}<td align='right'>{$x[1]}</td>"
230 . "<td align='center'><span style='white-space: nowrap;'>" ;
231 for( $cnt = 0 ; $cnt < $x[3] ; $cnt++) {
232 $html .= "<input type='radio' name='rad{$idx}' value='{$cnt}'";
233 if ( $choice == $cnt ) $html .= " checked='checked'";
234 $html .= " /> ";
235 }
236 $html .= "</span></td><td>{$x[2]}</td>";
237 $html .= "<td><input type='radio' name='rad{$idx}' value='-1'";
238 if( $choice == -1 ) {
239 $html .= " checked='checked'";
240 }
241 $html .= " /> " . wfMsg ( "val_noop" ) . "</td>{$tabsep}";
242 $html .= "<td><input type='text' name='comment{$idx}' value=\"" . htmlspecialchars( $comment ) . "\" /></td>";
243 $html .= "</tr>\n";
244 }
245 $html .= "<tr><td {$topstyle} colspan='2'>";
246
247 # link to version
248 $title = Title::newFromDBkey( $article_title );
249 if ( $table_name == "cur" ) {
250 $link_version = $title->getLocalURL( "" );
251 } else {
252 $link_version = $title->getLocalURL( "oldid={$table_id}" );
253 }
254 $link_version = "<a href=\"" . htmlspecialchars( $link_version ) . "\">" . wfMsg ( 'val_view_version' ) . "</a>";
255 $html .= $link_version;
256 $html .= "</td><td {$topstyle} colspan='5'>";
257 $html .= "<input type='checkbox' name='merge_other' value='1' checked='checked' />";
258 $html .= wfMsg( 'val_merge_old' );
259 $html .= "<br /><input type='checkbox' name='clear_other' value='1' checked='checked' />";
260 $html .= wfMsg( 'val_clear_old', $skin->makeKnownLinkObj( $article ) );
261 $html .= "</td><td {$topstyle} align='right' valign='center'><input type='submit' name='doit' value=\"" . htmlspecialchars( wfMsg("ok") ) . "\" /></td>";
262 $html .= "</tr></table></form>\n";
263 }
264
265 $html .= "<h2>" . wfMsg( 'preview' ) . "</h2>";
266 $wgOut->addHTML( $html );
267 $wgOut->addWikiText( $wgArticle->getContent( true ) );
268 }
269
270 function getData( $user = -1 , $title = "" , $type = -1 ) {
271 $ret = array();
272 $sql = array();
273 if( $user != -1 ) {
274 $sql[] = "val_user='" . wfStrencode( $user ) . "'";
275 }
276 if( $type != -1 ) {
277 $sql[] = "val_type='" . wfStrencode( $type ) . "'";
278 }
279 if( $title != "" ) {
280 $sql[] = "val_title='" . wfStrencode( $title ) . "'";
281 }
282 $sql = implode( " AND " , $sql );
283 if( $sql != "" ) {
284 $sql = " WHERE " . $sql;
285 }
286 $sql = "SELECT * FROM validate" . $sql;
287 $res = wfQuery( $sql, DB_READ );
288 while( $s = wfFetchObject( $res ) ) {
289 $ret["{$s->val_title}"]["{$s->val_timestamp}"]["{$s->val_type}"][] = $s;
290 }
291 return $ret;
292 }
293
294 # Show statistics for the different versions of a single article
295 function getPageStatistics( $article_title = "" ) {
296 global $wgLang, $wgUser, $wgOut, $wgRequest;
297 $validationtypes = $wgLang->getValidationTypes();
298 if( $article_title == "" ) {
299 $article_title = $wgRequest->getVal( 'article_title' );
300 }
301 $d = $this->getData( -1 , $article_title , -1 );
302 if( count ( $d ) ) {
303 $d = array_shift ( $d ) ;
304 } else {
305 $d = array();
306 }
307 krsort( $d );
308
309 # Getting table data (cur_id, old_id etc.) for each version
310 $table_id = array();
311 $table_name = array();
312 foreach( $d as $version => $data ) {
313 $this->find_this_version( $article_title, $version, $table_id[$version], $table_name[$version] );
314 }
315
316 # Generating HTML
317 $title = Title::newFromDBkey( $article_title );
318 $wgOut->setPageTitle( wfMsg( 'val_page_validation_statistics' , $title->getPrefixedText() ) );
319 $html = "";
320 $skin = $wgUser->getSkin();
321 $listurl = $skin->makeSpecialURL( "validate" , "mode=list_page" );
322 $html .= "<a href=\"" . htmlspecialchars( $listurl ) . "\">" . wfMsg( 'val_article_lists' ) . "</a><br /><br />\n";
323
324 $html .= "<table border='1' cellpadding='2' style='font-size:8pt;'>\n";
325 $html .= "<tr><th>" . wfMsg('val_version') . "</th>";
326 foreach( $validationtypes as $idx => $title ) {
327 $title = explode ( "|" , $title );
328 $html .= "<th>{$title[0]}</th>";
329 }
330 $html .= "<th>" . wfMsg('val_total') . "</th>";
331 $html .= "</tr>\n";
332 foreach( $d as $version => $data ) {
333 # Preamble for this version
334 $title = Title::newFromDBkey( $article_title );
335 $version_date = $wgLang->timeanddate( $version );
336 $version_validate_link = $title->escapeLocalURL( "action=validate&timestamp={$version}" );
337 $version_validate_link = "<a href=\"{$version_validate_link}\">" . wfMsg('val_validate_version') . "</a>";
338 if( $table_name[$version] == 'cur' ) {
339 $version_view_link = $title->escapeLocalURL( "" );
340 } else {
341 $version_view_link = $title->escapeLocalURL( "oldid={$table_id[$version]}" );
342 }
343 $version_view_link = "<a href=\"{$version_view_link}\">" . wfMsg('val_view_version') . "</a>";
344 $html .= "<tr>";
345 $html .= "<td align='center' valign='top' nowrap='nowrap'><b>{$version_date}</b><br />{$version_view_link}<br />{$version_validate_link}</td>";
346
347 # Individual data
348 $vmax = array();
349 $vcur = array();
350 $users = array();
351 foreach( $data as $type => $x2 ) {
352 if ( !isset ( $vcur[$type] ) ) $vcur[$type] = 0 ;
353 if ( !isset ( $vmax[$type] ) ) $vmax[$type] = 0 ;
354 if ( !isset ( $users[$type] ) ) $users[$type] = 0 ;
355 foreach( $x2 as $user => $x ) {
356 $vcur[$type] += $x->val_value;
357 $temp = explode( "|" , $validationtypes[$type] );
358 $vmax[$type] += $temp[3] - 1;
359 $users[$type] += 1;
360 }
361 }
362
363 $total_count = 0;
364 $total_percent = 0;
365 foreach( $validationtypes as $idx => $title ) {
366 if( isset ( $vcur[$idx] ) ) {
367 $average = 100 * $vcur[$idx] / $vmax[$idx] ;
368 $total_count += 1;
369 $total_percent += $average;
370 if( $users[$idx] > 1 ) {
371 $msgid = "val_percent";
372 } else {
373 $msgid = "val_percent_single";
374 }
375 $html .= "<td align='center' valign='top'>" .
376 wfMsg( $msgid, number_format( $average, 2 ) ,
377 $vcur[$idx] , $vmax[$idx] , $users[$idx] );
378 } else {
379 $html .= "<td align='center' valign='center'>";
380 $html .= "(" . wfMsg ( "val_noop" ) . ")";
381 }
382 $html .= "</td>";
383 }
384
385 if( $total_count > 0 ) {
386 $total = $total_percent / $total_count;
387 $total = number_format( $total , 2 ) . " %";
388 } else {
389 $total = "";
390 }
391 $html .= "<td align='center' valign='top' nowrap='nowrap'><b>{$total}</b></td>";
392
393 $html .= "</tr>";
394 }
395 $html .= "</table>\n";
396 return $html ;
397 }
398
399 function countUserValidations( $userid ) {
400 $sql = "SELECT count(DISTINCT val_title) AS num FROM validate WHERE val_user=" . IntVal( $userid );
401 $res = wfQuery( $sql, DB_READ );
402 if ( $s = wfFetchObject( $res ) ) {
403 $num = $s->num;
404 } else {
405 $num = 0;
406 }
407 return $num;
408 }
409
410 function getArticleList() {
411 global $wgLang, $wgOut;
412 $validationtypes = $wgLang->getValidationTypes();
413 $wgOut->setPageTitle( wfMsg( 'val_article_lists' ) );
414 $html = "";
415
416 # Choices
417 $choice = array ();
418 $maxw = 0;
419 foreach( $validationtypes as $idx => $data ) {
420 $x = explode( "|" , $data , 4 );
421 if( $x[3] > $maxw ) {
422 $maxw = $x[3];
423 }
424 }
425 foreach( $validationtypes as $idx => $data ) {
426 $choice[$idx] = array();
427 for( $a = 0 ; $a < $maxw ; $a++ ) {
428 $var = "cb_{$idx}_{$a}";
429 if( isset ( $_POST[$var] ) ) $choice[$idx][$a] = $_POST[$var] ; # Selected
430 else if ( !isset ( $_POST["doit"] ) ) $choice[$idx][$a] = 1 ; # First time
431 else $choice[$idx][$a] = 0 ; # De-selected
432 }
433 }
434
435 # The form
436 $html .= "<form method='post'>\n";
437 $html .= "<table border='1' cellspacing='0' cellpadding='2'>" ;
438 foreach( $validationtypes as $idx => $data ) {
439 $x = explode ( "|" , $data , 4 );
440
441 $html .= "<tr>";
442 $html .= "<th nowrap='nowrap'>{$x[0]}</th>";
443 $html .= "<td align='right' nowrap='nowrap'>{$x[1]}</td>";
444
445 for( $a = 0; $a < $maxw; $a++ ) {
446 if( $a < $x[3] ) {
447 $td = "<input type='checkbox' name='cb_{$idx}_{$a}' value='1'";
448 if( $choice[$idx][$a] == 1 ) {
449 $td .= " checked='checked'" ;
450 }
451 $td .= " />";
452 } else {
453 $td = '';
454 }
455 $html .= "<td>{$td}</td>";
456 }
457
458 $html .= "<td nowrap='nowrap'>{$x[2]}</td>";
459 $html .= "</tr>\n";
460 }
461 $html .= "<tr><td colspan='" . ( $maxw + 2 ) . "'></td>\n";
462 $html .= "<td align='right' valign='center'><input type='submit' name='doit' value=\"" . htmlspecialchars( wfMsg ( 'ok' ) ) . "\" /></td></tr>";
463 $html .= "</table>\n";
464 $html .= "</form>\n";
465
466 # The query
467 $articles = array();
468 $sql = "SELECT DISTINCT val_title,val_timestamp,val_type,avg(val_value) AS avg FROM validate GROUP BY val_title,val_timestamp,val_type";
469 $res = wfQuery( $sql, DB_READ );
470 while( $s = wfFetchObject( $res ) ) {
471 $articles[$s->val_title][$s->val_timestamp][$s->val_type] = $s;
472 }
473
474 # The list
475 $html .= "<ul>\n";
476 foreach( $articles as $dbkey => $timedata ) {
477 $title = Title::newFromDBkey( $dbkey );
478 $out = array();
479 krsort( $timedata );
480
481 foreach( $timedata as $timestamp => $typedata ) {
482 $showit = true;
483 foreach( $typedata as $type => $data ) {
484 $avg = intval ( $data->avg + 0.5 );
485 if ( $choice[$type][$avg] == 0 ) $showit = false;
486 }
487 if( $showit ) {
488 $out[] = "<li>" . $this->getVersionLink ( $title , $timestamp ) . "</li>\n";
489 }
490 }
491
492 if( count( $out ) > 0 ) {
493 $html .= "<li>\n";
494 $html .= htmlspecialchars( $title->getText() ) . "\n";
495 $html .= "<ul>\n";
496 $html .= implode( "\n" , $out );
497 $html .= "</ul>\n</li>\n";
498 }
499 }
500 $html .= "</ul>\n";
501 return $html;
502 }
503
504 function getVersionLink( &$title , $timestamp ) {
505 global $wgLang;
506 $dbkey = $title->getDBkey();
507 $this->find_this_version( $dbkey, $timestamp, $table_id, $table_name );
508 if( $table_name == 'cur' ) {
509 $link = $title->getLocalURL( "" );
510 } else {
511 $link = $title->getLocalURL( "action=validate&timestamp={$table_id}" );
512 }
513 $linktitle = wfMsg( 'val_version_of', $wgLang->timeanddate( $timestamp ) );
514 $link = "<a href=\"" . htmlspecialchars( $link ) . "\">" . $linktitle . "</a>";
515 if( $table_name == 'cur' ) {
516 $link .= " (" . wfMsg ( 'val_this_is_current_version' ) . ")";
517 }
518
519 $vlink = wfMsg( 'val_tab' );
520 $vlink = "[<a href=\"" . $title->escapeLocalURL( "action=validate&timestamp={$timestamp}" ) . "\">{$vlink}</a>] " . $link;
521 return $vlink ;
522 }
523
524 }
525
526 /**
527 * constructor
528 */
529 function wfSpecialValidate( $page = '' ) {
530 global $wgOut, $wgRequest, $wgUseValidation;
531
532 if( !$wgUseValidation ) {
533 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
534 return;
535 }
536
537 $mode = $wgRequest->getVal( 'mode', 'form' );
538 $v = new Validation;
539 $html = "" ;
540 /* if( $mode == "form" ) {
541 $html = $v->validate_form () ;
542 } else */
543 if( $mode == "stat_page" ) {
544 $html = $v->getPageStatistics();
545 } else if( $mode == "list_page" ) {
546 $html = $v->getArticleList();
547 }
548
549 $wgOut->addHTML( $html );
550 }
551
552 ?>