2 # Copyright (C) 2004 Magnus Manske <magnus.manske@web.de>
3 # http://www.mediawiki.org/
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.
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.
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
23 * @subpackage SpecialPage
29 * @subpackage SpecialPage
33 function find_this_version( $article_title , &$article_time , &$id , &$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
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 ) ) {
60 function get_prev_data( $user_id , $article_title , $article_timestamp = "" ) {
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 ) . "'";
67 $res = wfQuery( $sql, DB_READ
);
68 while( $s = wfFetchObject( $res ) ) {
69 $ret[$s->val_timestamp
][$s->val_type
] = $s;
74 function validate_form( $article_title = "" ) {
75 global $wgOut, $wgLang, $wgUser, $wgArticle, $wgRequest;
77 if ( $wgUser->getID() == 0 ) {
79 $wgOut->addHTML( htmlspecialchars( wfMsg( 'val_no_anon_validation' ) ) .
80 $this->getPageStatistics ( $article_title ) ) ;
84 $validationtypes = $wgLang->getValidationTypes();
85 if ( $article_title == "" ) {
86 $article_title = $wgRequest->getVal( 'article_title' );
87 $heading = "<h1>" . htmlspecialchars( $article_title ) . "</h1>\n";
92 $article_time = $wgRequest->getVal( 'timestamp' );
93 $article = Title
::newFromText( $article_title );
94 if( is_null( $article ) ) {
95 $wgOut->errorpage( "badtitle", "badtitletext" );
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 );
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
;
111 $val[$article_time] = array();
114 # Newest versions first
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();
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}" );
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
;
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;
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
);
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] = "";
171 $val[$oldtime][$idx]->val_value
= $rad;
172 $val[$oldtime][$idx]->val_comment
= $comment;
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
);
185 $wgArticle->showArticle( "Juhuu", wfMsg( 'val_validated' ) );
186 return; # Show article instead of validation page
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";
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'";
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' ) . ")";
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
;
224 if( isset( $stuff[$idx] ) ) {
225 $comment = $stuff[$idx]->val_comment
;
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'";
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'";
241 $html .= " /> " . wfMsg ( "val_noop" ) . "</td>{$tabsep}";
242 $html .= "<td><input type='text' name='comment{$idx}' value=\"" . htmlspecialchars( $comment ) . "\" /></td>";
245 $html .= "<tr><td {$topstyle} colspan='2'>";
248 $title = Title
::newFromDBkey( $article_title );
249 if ( $table_name == "cur" ) {
250 $link_version = $title->getLocalURL( "" );
252 $link_version = $title->getLocalURL( "oldid={$table_id}" );
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";
265 $html .= "<h2>" . wfMsg( 'preview' ) . "</h2>";
266 $wgOut->addHTML( $html );
267 $wgOut->addWikiText( $wgArticle->getContent( true ) );
270 function getData( $user = -1 , $title = "" , $type = -1 ) {
274 $sql[] = "val_user='" . wfStrencode( $user ) . "'";
277 $sql[] = "val_type='" . wfStrencode( $type ) . "'";
280 $sql[] = "val_title='" . wfStrencode( $title ) . "'";
282 $sql = implode( " AND " , $sql );
284 $sql = " WHERE " . $sql;
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;
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' );
301 $d = $this->getData( -1 , $article_title , -1 );
303 $d = array_shift ( $d ) ;
309 # Getting table data (cur_id, old_id etc.) for each version
311 $table_name = array();
312 foreach( $d as $version => $data ) {
313 $this->find_this_version( $article_title, $version, $table_id[$version], $table_name[$version] );
317 $title = Title
::newFromDBkey( $article_title );
318 $wgOut->setPageTitle( wfMsg( 'val_page_validation_statistics' , $title->getPrefixedText() ) );
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";
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>";
330 $html .= "<th>" . wfMsg('val_total') . "</th>";
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×tamp={$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( "" );
341 $version_view_link = $title->escapeLocalURL( "oldid={$table_id[$version]}" );
343 $version_view_link = "<a href=\"{$version_view_link}\">" . wfMsg('val_view_version') . "</a>";
345 $html .= "<td align='center' valign='top' nowrap='nowrap'><b>{$version_date}</b><br />{$version_view_link}<br />{$version_validate_link}</td>";
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;
365 foreach( $validationtypes as $idx => $title ) {
366 if( isset ( $vcur[$idx] ) ) {
367 $average = 100 * $vcur[$idx] / $vmax[$idx] ;
369 $total_percent +
= $average;
370 if( $users[$idx] > 1 ) {
371 $msgid = "val_percent";
373 $msgid = "val_percent_single";
375 $html .= "<td align='center' valign='top'>" .
376 wfMsg( $msgid, number_format( $average, 2 ) ,
377 $vcur[$idx] , $vmax[$idx] , $users[$idx] );
379 $html .= "<td align='center' valign='center'>";
380 $html .= "(" . wfMsg ( "val_noop" ) . ")";
385 if( $total_count > 0 ) {
386 $total = $total_percent / $total_count;
387 $total = number_format( $total , 2 ) . " %";
391 $html .= "<td align='center' valign='top' nowrap='nowrap'><b>{$total}</b></td>";
395 $html .= "</table>\n";
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 ) ) {
410 function getArticleList() {
411 global $wgLang, $wgOut;
412 $validationtypes = $wgLang->getValidationTypes();
413 $wgOut->setPageTitle( wfMsg( 'val_article_lists' ) );
419 foreach( $validationtypes as $idx => $data ) {
420 $x = explode( "|" , $data , 4 );
421 if( $x[3] > $maxw ) {
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
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 );
442 $html .= "<th nowrap='nowrap'>{$x[0]}</th>";
443 $html .= "<td align='right' nowrap='nowrap'>{$x[1]}</td>";
445 for( $a = 0; $a < $maxw; $a++
) {
447 $td = "<input type='checkbox' name='cb_{$idx}_{$a}' value='1'";
448 if( $choice[$idx][$a] == 1 ) {
449 $td .= " checked='checked'" ;
455 $html .= "<td>{$td}</td>";
458 $html .= "<td nowrap='nowrap'>{$x[2]}</td>";
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";
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;
476 foreach( $articles as $dbkey => $timedata ) {
477 $title = Title
::newFromDBkey( $dbkey );
481 foreach( $timedata as $timestamp => $typedata ) {
483 foreach( $typedata as $type => $data ) {
484 $avg = intval ( $data->avg +
0.5 );
485 if ( $choice[$type][$avg] == 0 ) $showit = false;
488 $out[] = "<li>" . $this->getVersionLink ( $title , $timestamp ) . "</li>\n";
492 if( count( $out ) > 0 ) {
494 $html .= htmlspecialchars( $title->getText() ) . "\n";
496 $html .= implode( "\n" , $out );
497 $html .= "</ul>\n</li>\n";
504 function getVersionLink( &$title , $timestamp ) {
506 $dbkey = $title->getDBkey();
507 $this->find_this_version( $dbkey, $timestamp, $table_id, $table_name );
508 if( $table_name == 'cur' ) {
509 $link = $title->getLocalURL( "" );
511 $link = $title->getLocalURL( "action=validate×tamp={$table_id}" );
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' ) . ")";
519 $vlink = wfMsg( 'val_tab' );
520 $vlink = "[<a href=\"" . $title->escapeLocalURL( "action=validate×tamp={$timestamp}" ) . "\">{$vlink}</a>] " . $link;
529 function wfSpecialValidate( $page = '' ) {
530 global $wgOut, $wgRequest, $wgUseValidation;
532 if( !$wgUseValidation ) {
533 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
537 $mode = $wgRequest->getVal( 'mode', 'form' );
540 /* if( $mode == "form" ) {
541 $html = $v->validate_form () ;
543 if( $mode == "stat_page" ) {
544 $html = $v->getPageStatistics();
545 } else if( $mode == "list_page" ) {
546 $html = $v->getArticleList();
549 $wgOut->addHTML( $html );