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