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