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