validation improvements
[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=".NS_MAIN." 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=".NS_MAIN." 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=".NS_MAIN, 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", wfTimestamp( TW_UNIX, $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 # Returns a HTML link to the specified article revision
505 function getVersionLink( &$article , $revision , $text = "" ) {
506 $t = $article->getTitle() ;
507 if ( $text == "" ) $text = wfMsg("val_view_version");
508 $ret = "<a href=\"" . $t->getLocalURL ( "oldid={$revision}" ) . "\">" . $text . "</a>" ;
509 return $ret ;
510 }
511
512 # Returns an array containing all topics you can vote on
513 function getTopicList () {
514 $ret = array () ;
515 $sql = "SELECT * FROM validate WHERE val_user=0" ;
516 $res = wfQuery( $sql, DB_READ );
517 while( $x = wfFetchObject( $res ) ) {
518 $ret[$x->val_type] = $x ;
519 }
520 ksort ( $ret ) ;
521 return $ret ;
522 }
523
524 # Generates a form for a single revision
525 function getRevisionForm ( &$article , $revision , &$data , $focus = false ) {
526 # Fill data with blank values
527 foreach ( $this->topicList AS $x => $y ) {
528 if ( !isset ( $data[$x] ) ) {
529 $data[$x]->value = 0 ;
530 $data[$x]->comment = "" ;
531 }
532 }
533 ksort ( $data ) ;
534
535 # Generate form
536 $ret = "<form method='post'>" ;
537 $ret .= "<table border='1' cellspacing='0' cellpadding='2'" ;
538 if ( $focus ) $ret .= " style='background-color:#00BBFF'" ;
539 $ret .= ">\n" ;
540 $head = "Revision #" . $revision ;
541 # if ( $focus ) $head = "<font color='red'>{$head}</font>" ;
542 $link = " " . $this->getVersionLink ( $article , $revision ) ;
543 $ret .= "<tr><th align='left' colspan='3'>" . $head . " ({$link})</th></tr>\n" ;
544 $line = 0 ;
545 foreach ( $data AS $x => $y ) {
546 $line = 1 - $line ;
547 $col = $line == 1 ? "#DDDDDD" : "#EEEEEE" ;
548 $idx = "_{$revision}[{$x}]" ;
549 $ret .= "<tr bgcolor='{$col}'>\n" ;
550 $ret .= "<th nowrap>" ;
551 $ret .= $this->topicList[$x]->val_comment ;
552 $ret .= "</th>\n" ;
553
554 $tlx = $this->topicList[$x] ;
555 $vote = "" ;
556 $max = $tlx->val_value ;
557 for ( $a = 0 ; $a <= $max ; $a++ ) {
558 if ( $a == 0 ) $vote .= wfMsg ( "val_noop" ) ;
559 $vote .= "<input type='radio' name='re_v{$idx}' value='{$a}'" ;
560 if ( $a == $y->value ) $vote .= " checked" ;
561 $vote .= "/>" ;
562 if ( $max == 2 && $a == 1 ) $vote .= wfMsg ( "val_no" ) . " " ;
563 else if ( $max == 2 && $a == 2 ) $vote .= wfMsg ( "val_yes" ) ;
564 else if ( $a != 0 ) $vote .= $a . " " ;
565 if ( $a == 0 ) $vote .= " &nbsp; " ;
566 }
567 $ret .= "<td nowrap valign='center'>{$vote}</td>\n" ;
568
569 $ret .= "<td width='100%' align='center'><input size='50' style='width:98%' maxlength='250' type='text' name='re_c{$idx}' value='{$y->comment}'/>" ;
570 $ret .= "</td></tr>\n" ;
571 }
572 $checked = $focus ? " checked" : "" ;
573 $ret .= "<tr><td colspan='3' valign='center'>\n" ;
574 $ret .= "<input type='checkbox' name='re_merge_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_merge_old' ) . " \n" ;
575 $ret .= "<input type='checkbox' name='re_clear_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_clear_old' ) . " \n" ;
576 $ret .= "<input type='submit' name='re_submit[{$revision}]' value='" . htmlspecialchars( wfMsg("ok") ) . "'/>\n" ;
577 if ( $focus ) $ret .= "<br/>\n<small>" . wfMsg ( "val_form_note" ) . "</small>" ;
578 $ret .= "</td></tr>\n" ;
579 $ret .= "</table>\n</form>\n\n" ;
580 return $ret ;
581 }
582
583 # Merges one dataset into another
584 function mergeInto ( &$source , &$dest ) {
585 $ret = false ;
586 foreach ( $source AS $x => $y ) {
587 $doit = false ;
588 if ( !isset ( $dest[$x] ) ) $doit = true ;
589 else if ( $dest[$x]->value == 0 ) $doit = true ;
590 if ( $doit ) {
591 $dest[$x] = $y ;
592 $ret = true ;
593 }
594 }
595 if ( $ret ) ksort ( $dest ) ;
596 return $ret ;
597 }
598
599 # Merges all votes prior to the given revision into it
600 function mergeOldRevisions ( &$article , $revision ) {
601 $tmp = $this->voteCache ;
602 krsort ( $tmp ) ;
603 $update = false ;
604 $data = $this->voteCache[$revision] ;
605 foreach ( $tmp AS $x => $y ) {
606 if ( $x < $revision ) {
607 if ( $this->mergeInto ( $y , $data ) ) $update = true ;
608 }
609 }
610 if ( $update ) $this->setRevision ( $article , $revision , $data ) ;
611 }
612
613 # Clears all votes prior to the given revision
614 function clearOldRevisions ( &$article , $revision ) {
615 $tmp = $this->voteCache ;
616 foreach ( $tmp AS $x => $y ) {
617 if ( $x < $revision ) $this->deleteRevision ( $article , $x ) ;
618 }
619 }
620
621 # Updates the votes for the given revision from the FORM data
622 function updateRevision ( &$article , $revision ) {
623 global $wgUser, $wgRequest ;
624
625 if ( isset ( $this->voteCache[$revision] ) ) $data = $this->voteCache[$revision] ;
626 else $data = array () ;
627 $nv = $wgRequest->getArray ( "re_v_{$revision}" , array() ) ;
628 $nc = $wgRequest->getArray ( "re_c_{$revision}" , array() ) ;
629
630 foreach ( $nv AS $x => $y ) {
631 # if ( $y > 0 ) {
632 $data[$x]->value = $y ;
633 $data[$x]->comment = $nc[$x] ;
634 # }
635 }
636 krsort ( $data ) ;
637
638 $this->setRevision ( $article , $revision , $data ) ;
639 }
640
641 # Sets a specific revision to both cache and database
642 function setRevision ( &$article , $revision , &$data ) {
643 global $wgUser ;
644 $this->deleteRevision ( $article , $revision ) ;
645 $this->voteCache[$revision] = $data ;
646 foreach ( $data AS $x => $y ) {
647 if ( $y->value > 0 ) {
648 $sql = "INSERT INTO validate (val_user,val_page,val_revision,val_type,val_value,val_comment) VALUES ('" ;
649 $sql .= $wgUser->getID() . "','" ;
650 $sql .= $article->getID() . "','" ;
651 $sql .= $revision . "','" ;
652 $sql .= $x . "','" ;
653 $sql .= $y->value . "','" ;
654 $sql .= Database::strencode ( $y->comment ) . "')" ;
655 $res = wfQuery( $sql, DB_WRITE );
656 }
657 }
658 }
659
660 # Deletes a specific vote set in both cache and database
661 function deleteRevision ( &$article , $revision ) {
662 global $wgUser ;
663 if ( !isset ( $this->voteCache[$revision] ) ) return ; # Nothing to do
664 $sql = "DELETE FROM validate WHERE val_user='" . $wgUser->GetID() . "' AND " ;
665 $sql .= " val_page='" . $article->getID() . "' AND val_revision='{$revision}'" ;
666 $res = wfQuery( $sql, DB_WRITE );
667 unset ( $this->voteCache[$revision] ) ;
668 }
669
670 # Reads the entire vote list for this user for the given article
671 function getVoteList ( $id ) {
672 global $wgUser ;
673 $r = array () ; # Revisions
674 $sql = "SELECT * FROM validate WHERE val_page=" . $id . " AND val_user=" . $wgUser->getID() ;
675 $res = wfQuery( $sql, DB_READ );
676 while( $x = wfFetchObject( $res ) ) {
677 if ( !isset($r[$x->val_revision]) ) $r[$x->val_revision] = array () ;
678 $r[$x->val_revision][$x->val_type]->value = $x->val_value ;
679 $r[$x->val_revision][$x->val_type]->comment = $x->val_comment ;
680 }
681 return $r ;
682 }
683
684 # Generates the page from the validation tab
685 function validatePageForm ( &$article , $revision ) {
686 global $wgOut, $wgRequest ;
687
688 $this->topicList = $this->getTopicList() ;
689 $this->voteCache = $this->getVoteList ( $article->getID() ) ;
690
691 # Check for POST data
692 $re = $wgRequest->getArray( 're_submit' );
693 if ( isset ( $re ) )
694 {
695 $id = array_keys ( $re ) ;
696 $id = $id[0] ; # $id is now the revision number the user clicked "OK" for
697 $clearOldRev = $wgRequest->getVal( "re_clear_{$id}" , 0 );
698 $mergeOldRev = $wgRequest->getVal( "re_merge_{$id}" , 0 );
699 $this->updateRevision ( $article , $id ) ;
700 if ( $mergeOldRev ) $this->mergeOldRevisions ( $article , $id ) ;
701 if ( $clearOldRev ) $this->clearOldRevisions ( $article , $id ) ;
702 }
703
704 # Make sure the requested revision exists
705 if ( !isset ( $this->voteCache[$revision] ) ) $this->voteCache[$revision] = array () ;
706
707 # Sort revisions list, newest first
708 krsort ( $this->voteCache ) ;
709
710 # Output
711 $ret = "" ;
712 $title = $article->getTitle();
713 $title = $title->getPrefixedText() ;
714 $wgOut->setPageTitle ( wfMsg ( 'val_rev_for' ) . $title ) ;
715 foreach ( $this->voteCache AS $x => $y )
716 {
717 $ret .= $this->getRevisionForm ( $article , $x , $y , $x == $revision ) ;
718 $ret .= "<br/>\n" ;
719 }
720 return $ret ;
721 }
722
723 # This functions adds a topic to the database
724 function addTopic ( $topic , $limit ) {
725 $a = 1 ;
726 while ( isset ( $this->topicList[$a] ) ) $a++ ;
727 $sql = "INSERT INTO validate (val_user,val_page,val_revision,val_type,val_value,val_comment) VALUES (" ;
728 $sql .= "'0','0','0','{$a}','{$limit}','" ;
729 $sql .= Database::strencode ( $topic ) . "')" ;
730 $res = wfQuery( $sql, DB_WRITE );
731 $x->val_user = $x->val_page = $x->val_revision = 0 ;
732 $x->val_type = $a ;
733 $x->val_value = $limit ;
734 $x->val_comment = $topic ;
735 $this->topicList[$a] = $x ;
736 ksort ( $this->topicList ) ;
737 }
738
739 # This functions adds a topic to the database
740 function deleteTopic ( $id ) {
741 $sql = "DELETE FROM validate WHERE val_type='{$id}'" ;
742 $res = wfQuery( $sql, DB_WRITE );
743 unset ( $this->topicList[$id] ) ;
744 }
745
746 # This function performs the "management" mode on Special:Validate
747 function manageTopics () {
748 global $wgRequest ;
749 $this->topicList = $this->getTopicList() ;
750
751 $iamsure = $wgRequest->getVal ( "iamsure" , "0" ) == 1 ;
752
753 if ( $iamsure && $wgRequest->getVal ( "m_add" , "--" ) != "--" ) {
754 $new_topic = $wgRequest->getVal ( "m_topic" ) ;
755 $new_limit = $wgRequest->getVal ( "m_limit" ) ;
756 if ( $new_topic != "" && $new_limit > 1 )
757 $this->addTopic ( $new_topic , $new_limit ) ;
758 }
759
760 $da = $wgRequest->getArray ( "m_del" ) ;
761 if ( $iamsure && isset ( $da ) && count ( $da ) > 0 ) {
762 $id = array_keys ( $da ) ;
763 $id = array_shift ( $id ) ;
764 $this->deleteTopic ( $id ) ;
765 }
766
767 $r = "<p>" . wfMsg ( 'val_warning' ) . "</p>\n" ;
768 $r .= "<form method='post'>\n" ;
769 $r .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
770 $r .= "<tr>" . wfMsg ( 'val_list_header' ) . "</tr>\n" ;
771 foreach ( $this->topicList AS $x => $y ) {
772 $r .= "<tr>\n" ;
773 $r .= "<th>" . $y->val_type . "</th>\n" ;
774 $r .= "<td>{$y->val_comment}</td>\n" ;
775 $r .= "<td>1 .. <b>{$y->val_value}</b></td>\n" ;
776 $r .= "<td><input type='submit' name='m_del[{$x}]' value='" . wfMsg ( 'val_del' ) . "'/></td>\n" ;
777 $r .= "</tr>\n" ;
778 }
779 $r .= "<tr>\n" ;
780 $r .= "<td/>\n" ;
781 $r .= "<td><input type='text' name='m_topic' value=''/></td>\n" ;
782 $r .= "<td><input type='text' name='m_limit' value=''/></td>\n" ;
783 $r .= "<td><input type='submit' name='m_add' value='" . wfMsg ( 'val_add' ) . "'/></td>\n" ;
784 $r .= "</tr>\n" ;
785 $r .= "</table>\n" ;
786 $r .= "<input type='checkbox' name='iamsure' value='1'/>" . wfMsg ( 'val_iamsure' ) . "\n" ;
787 $r .= "</form>\n" ;
788 return $r ;
789 }
790
791 }
792
793 /**
794 * constructor
795 */
796 function wfSpecialValidate( $page = '' ) {
797 global $wgOut, $wgRequest, $wgUseValidation, $wgUser, $wgContLang;
798
799 if( !$wgUseValidation ) {
800 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
801 return;
802 }
803
804 $mode = $wgRequest->getVal ( "mode" ) ;
805 $skin = $wgUser->getSkin() ;
806
807
808 if ( $mode == "manage" ) {
809 $v = new Validation ;
810 $html = $v->manageTopics () ;
811 } else {
812 $t = Title::newFromText ( "Special:Validate" ) ;
813 $url = $t->getLocalURL ( "mode=manage" ) ;
814 $html = "$mode" ;
815 $html .= "<ul>\n" ;
816 $html .= "<li><a href=\"" . $url . "\">Manage</a></li>\n" ;
817 $html .= "<li></li>\n" ;
818 $html .= "<li></li>\n" ;
819 $html .= "</ul>\n" ;
820 }
821
822 $wgOut->addHTML( $html );
823 }
824
825 ?>