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