* Moving hardcoded styles into CSS.
[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 var $topicList ;
33 var $voteCache ;
34 var $rev2date ;
35 var $date2ref ;
36
37 # Reads all revision information of the specified article
38 function prepareRevisions ( $id ) {
39 $this->rev2date = array () ;
40 $this->date2rev = array () ;
41 $sql = "SELECT * FROM revision WHERE rev_page='{$id}'" ;
42 $res = wfQuery( $sql, DB_READ );
43 while( $x = wfFetchObject( $res ) ) {
44 $this->rev2date[$x->rev_id] = $x ;
45 $this->date2rev[$x->rev_timestamp] = $x ;
46 }
47 }
48
49 # Returns a HTML link to the specified article revision
50 function getVersionLink( &$article , $revision , $text = "" ) {
51 $t = $article->getTitle() ;
52 if ( $text == "" ) $text = wfMsg("val_view_version");
53 $ret = "<a href=\"" . $t->getLocalURL ( "oldid={$revision}" ) . "\">" . $text . "</a>" ;
54 return $ret ;
55 }
56
57 # Returns an array containing all topics you can vote on
58 function getTopicList () {
59 $ret = array () ;
60 $sql = "SELECT * FROM validate WHERE val_user=0" ;
61 $res = wfQuery( $sql, DB_READ );
62 while( $x = wfFetchObject( $res ) ) {
63 $ret[$x->val_type] = $x ;
64 }
65 ksort ( $ret ) ;
66 return $ret ;
67 }
68
69 # Merges one dataset into another
70 function mergeInto ( &$source , &$dest ) {
71 $ret = false ;
72 foreach ( $source AS $x => $y ) {
73 $doit = false ;
74 if ( !isset ( $dest[$x] ) ) $doit = true ;
75 else if ( $dest[$x]->value == 0 ) $doit = true ;
76 if ( $doit ) {
77 $dest[$x] = $y ;
78 $ret = true ;
79 }
80 }
81 if ( $ret ) ksort ( $dest ) ;
82 return $ret ;
83 }
84
85 # Merges all votes prior to the given revision into it
86 function mergeOldRevisions ( &$article , $revision ) {
87 $tmp = $this->voteCache ;
88 krsort ( $tmp ) ;
89 $update = false ;
90 $ts = $this->getTimestamp($revision) ;
91 $data = $this->voteCache[$ts] ;
92 foreach ( $tmp AS $x => $y ) {
93 if ( $x < $ts ) {
94 if ( $this->mergeInto ( $y , $data ) ) $update = true ;
95 }
96 }
97 if ( $update ) $this->setRevision ( $article , $revision , $data ) ;
98 }
99
100 # Clears all votes prior to the given revision
101 function clearOldRevisions ( &$article , $revision ) {
102 $tmp = $this->voteCache ;
103 $ts = $this->getTimestamp($revision);
104 foreach ( $tmp AS $x => $y ) {
105 if ( $x < $ts ) $this->deleteRevision ( $article , $this->getRevisionNumber($x) ) ;
106 }
107 }
108
109 # Updates the votes for the given revision from the FORM data
110 function updateRevision ( &$article , $revision ) {
111 global $wgUser, $wgRequest ;
112
113 if ( isset ( $this->voteCache[$this->getTimestamp($revision)] ) ) $data = $this->voteCache[$this->getTimestamp($revision)] ;
114 else $data = array () ;
115 $nv = $wgRequest->getArray ( "re_v_{$revision}" , array() ) ;
116 $nc = $wgRequest->getArray ( "re_c_{$revision}" , array() ) ;
117
118 foreach ( $nv AS $x => $y ) {
119 $data[$x]->value = $y ;
120 $data[$x]->comment = $nc[$x] ;
121 }
122 krsort ( $data ) ;
123
124 $this->setRevision ( $article , $revision , $data ) ;
125 }
126
127 # Sets a specific revision to both cache and database
128 function setRevision ( &$article , $revision , &$data ) {
129 global $wgUser ;
130 $this->deleteRevision ( $article , $revision ) ;
131 $this->voteCache[$this->getTimestamp($revision)] = $data ;
132 foreach ( $data AS $x => $y ) {
133 if ( $y->value > 0 ) {
134 $sql = "INSERT INTO validate (val_user,val_page,val_revision,val_type,val_value,val_comment) VALUES ('" ;
135 $sql .= $wgUser->getID() . "','" ;
136 $sql .= $article->getID() . "','" ;
137 $sql .= $revision . "','" ;
138 $sql .= $x . "','" ;
139 $sql .= $y->value . "','" ;
140 $sql .= Database::strencode ( $y->comment ) . "')" ;
141 $res = wfQuery( $sql, DB_WRITE );
142 }
143 }
144 }
145
146 # Deletes a specific vote set in both cache and database
147 function deleteRevision ( &$article , $revision ) {
148 global $wgUser ;
149 $ts = $this->getTimestamp ( $revision ) ;
150 if ( !isset ( $this->voteCache[$ts] ) ) return ; # Nothing to do
151 $sql = "DELETE FROM validate WHERE val_user='" . $wgUser->GetID() . "' AND " ;
152 $sql .= " val_page='" . $article->getID() . "' AND val_revision='{$revision}'" ;
153 $res = wfQuery( $sql, DB_WRITE );
154 unset ( $this->voteCache[$ts] ) ;
155 }
156
157 # Reads the entire vote list for this user for the given article
158 function getVoteList ( $id ) {
159 global $wgUser ;
160 $r = array () ; # Revisions
161 $sql = "SELECT * FROM validate WHERE val_page=" . $id . " AND val_user=" . $wgUser->getID() ;
162 $res = wfQuery( $sql, DB_READ );
163 while( $x = wfFetchObject( $res ) ) {
164 #$y = $x->val_revision ;
165 $y = $this->rev2date[$x->val_revision] ;
166 $y = $y->rev_timestamp ;
167 if ( !isset($r[$y]) ) $r[$y] = array () ;
168 $r[$y][$x->val_type]->value = $x->val_value ;
169 $r[$y][$x->val_type]->comment = $x->val_comment ;
170 }
171 return $r ;
172 }
173
174 # This functions adds a topic to the database
175 function addTopic ( $topic , $limit ) {
176 $a = 1 ;
177 while ( isset ( $this->topicList[$a] ) ) $a++ ;
178 $sql = "INSERT INTO validate (val_user,val_page,val_revision,val_type,val_value,val_comment) VALUES (" ;
179 $sql .= "'0','0','0','{$a}','{$limit}','" ;
180 $sql .= Database::strencode ( $topic ) . "')" ;
181 $res = wfQuery( $sql, DB_WRITE );
182 $x->val_user = $x->val_page = $x->val_revision = 0 ;
183 $x->val_type = $a ;
184 $x->val_value = $limit ;
185 $x->val_comment = $topic ;
186 $this->topicList[$a] = $x ;
187 ksort ( $this->topicList ) ;
188 }
189
190 # This functions adds a topic to the database
191 function deleteTopic ( $id ) {
192 $sql = "DELETE FROM validate WHERE val_type='{$id}'" ;
193 $res = wfQuery( $sql, DB_WRITE );
194 unset ( $this->topicList[$id] ) ;
195 }
196
197 # This function returns a link text to the page validation statistics
198 function link2statistics ( &$article ) {
199 $ret = wfMsg ( 'val_rev_stats_link' ) ;
200 $nt = $article->getTitle() ;
201 $ret = str_replace ( "$1" , $nt->getPrefixedText() , $ret ) ;
202
203 $url = $nt->getLocalURL ( "action=validate&mode=list" ) ;
204 $ret = str_replace ( "$2" , $url , $ret ) ;
205
206 return $ret ;
207 }
208
209 # Returns the timestamp of a revision based on the revision number
210 function getTimestamp ( $revision ) {
211 $ts = $this->rev2date[$revision] ;
212 $ts = $ts->rev_timestamp ;
213 return $ts ;
214 }
215
216 # Returns the revision number of a revision based on the timestamp
217 function getRevisionNumber ( $ts ) {
218 $revision = $this->date2rev[$ts] ;
219 $revision = $revision->rev_id ;
220 return $revision ;
221 }
222
223
224 # HTML generation functions from this point on
225
226 # Returns the metadata string for a revision
227 function getMetadata ( $idx ) {
228 $metadata = "" ;
229 $x = $this->rev2date[$idx] ;
230 $metadata .= wfTimestamp ( TS_DB , $x->rev_timestamp ) ;
231 $metadata .= " by " ;
232 if ( $x->rev_user == 0 ) {
233 $metadata .= $x->rev_user_text ;
234 } else {
235 $u = new User ;
236 $u->setId ( $x->rev_user ) ;
237 $u->setName ( $x->rev_user_text ) ;
238 $nt = $u->getUserPage() ;
239 $url = "<a href='" . $nt->getLocalUrl () . "'>" . $nt->getText() . "</a>" ;
240 $metadata .= $url ;
241 }
242 $metadata .= " : <small>\"" . htmlspecialchars ( $x->rev_comment ) . "\"</small>" ;
243 return $metadata ;
244 }
245
246 # Generates a form for a single revision
247 function getRevisionForm ( &$article , $idx , &$data , $focus = false ) {
248 # Fill data with blank values
249 $ts = $idx ;
250 $revision = $this->getRevisionNumber ( $ts ) ;
251 foreach ( $this->topicList AS $x => $y ) {
252 if ( !isset ( $data[$x] ) ) {
253 $data[$x]->value = 0 ;
254 $data[$x]->comment = "" ;
255 }
256 }
257 ksort ( $data ) ;
258
259 # Generate form
260 $ret = "<form method='post'>" ;
261 $ret .= "<table border='1' cellspacing='0' cellpadding='2'" ;
262 if ( $focus ) $ret .= " style='background-color:#00BBFF'" ;
263 $ret .= ">\n" ;
264 $head = "Revision #" . $revision ;
265 $link = " " . $this->getVersionLink ( $article , $revision ) ;
266 $metadata = $this->getMetadata ( $revision ) ;
267 $ret .= "<tr><th align='left' colspan='3'>" . $head . " ({$link}) {$metadata}</th></tr>\n" ;
268 $line = 0 ;
269 foreach ( $data AS $x => $y ) {
270 $line = 1 - $line ;
271 $col = $line == 1 ? "#DDDDDD" : "#EEEEEE" ;
272 $idx = "_{$revision}[{$x}]" ;
273 $ret .= "<tr bgcolor='{$col}'>\n" ;
274 $ret .= "<th nowrap>" ;
275 $ret .= $this->topicList[$x]->val_comment ;
276 $ret .= "</th>\n" ;
277
278 $tlx = $this->topicList[$x] ;
279 $vote = "" ;
280 $max = $tlx->val_value ;
281 for ( $a = 0 ; $a <= $max ; $a++ ) {
282 if ( $a == 0 ) $vote .= wfMsg ( "val_noop" ) ;
283 $vote .= "<input type='radio' name='re_v{$idx}' value='{$a}'" ;
284 if ( $a == $y->value ) $vote .= " checked" ;
285 $vote .= "/>" ;
286 if ( $max == 2 && $a == 1 ) $vote .= wfMsg ( "val_no" ) . " " ;
287 else if ( $max == 2 && $a == 2 ) $vote .= wfMsg ( "val_yes" ) ;
288 else if ( $a != 0 ) $vote .= $a . " " ;
289 if ( $a == 0 ) $vote .= " &nbsp; " ;
290 }
291 $ret .= "<td nowrap valign='center'>{$vote}</td>\n" ;
292
293 $ret .= "<td width='100%' align='center'><input size='50' style='width:98%' maxlength='250' type='text' name='re_c{$idx}' value='{$y->comment}'/>" ;
294 $ret .= "</td></tr>\n" ;
295 }
296 $checked = $focus ? " checked" : "" ;
297 $ret .= "<tr><td colspan='3' valign='center'>\n" ;
298 $ret .= "<input type='checkbox' name='re_merge_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_merge_old' ) . " \n" ;
299 $ret .= "<input type='checkbox' name='re_clear_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_clear_old' ) . " \n" ;
300 $ret .= "<input type='submit' name='re_submit[{$revision}]' value='" . htmlspecialchars( wfMsg("ok") ) . "'/>\n" ;
301 if ( $focus ) $ret .= "<br/>\n<small>" . wfMsg ( "val_form_note" ) . "</small>" ;
302 $ret .= "</td></tr>\n" ;
303 $ret .= "</table>\n</form>\n\n" ;
304 return $ret ;
305 }
306
307
308 # Generates the page from the validation tab
309 function validatePageForm ( &$article , $revision ) {
310 global $wgOut, $wgRequest ;
311
312 $this->prepareRevisions ( $article->getID() ) ;
313 $this->topicList = $this->getTopicList() ;
314 $this->voteCache = $this->getVoteList ( $article->getID() ) ;
315
316 # Check for POST data
317 $re = $wgRequest->getArray( 're_submit' );
318 if ( isset ( $re ) )
319 {
320 $id = array_keys ( $re ) ;
321 $id = $id[0] ; # $id is now the revision number the user clicked "OK" for
322 $clearOldRev = $wgRequest->getVal( "re_clear_{$id}" , 0 );
323 $mergeOldRev = $wgRequest->getVal( "re_merge_{$id}" , 0 );
324 $this->updateRevision ( $article , $id ) ;
325 if ( $mergeOldRev ) $this->mergeOldRevisions ( $article , $id ) ;
326 if ( $clearOldRev ) $this->clearOldRevisions ( $article , $id ) ;
327 }
328
329 # Make sure the requested revision exists
330 $ts = $this->rev2date[$revision]->rev_timestamp ;
331 if ( !isset ( $this->voteCache[$ts] ) ) $this->voteCache[$ts] = array () ;
332
333 # Sort revisions list, newest first
334 krsort ( $this->voteCache ) ;
335
336 # Output
337 $ret = "" ;
338 $title = $article->getTitle();
339 $title = $title->getPrefixedText() ;
340 $wgOut->setPageTitle ( wfMsg ( 'val_rev_for' ) . $title ) ;
341 foreach ( $this->voteCache AS $x => $y )
342 {
343 $ret .= $this->getRevisionForm ( $article , $x , $y , $x == $ts ) ;
344 $ret .= "<br/>\n" ;
345 }
346 $ret .= $this->link2statistics ( $article ) ;
347 return $ret ;
348 }
349
350 # This function performs the "management" mode on Special:Validate
351 function manageTopics () {
352 global $wgRequest ;
353 $this->topicList = $this->getTopicList() ;
354
355 $iamsure = $wgRequest->getVal ( "iamsure" , "0" ) == 1 ;
356
357 if ( $iamsure && $wgRequest->getVal ( "m_add" , "--" ) != "--" ) {
358 $new_topic = $wgRequest->getVal ( "m_topic" ) ;
359 $new_limit = $wgRequest->getVal ( "m_limit" ) ;
360 if ( $new_topic != "" && $new_limit > 1 )
361 $this->addTopic ( $new_topic , $new_limit ) ;
362 }
363
364 $da = $wgRequest->getArray ( "m_del" ) ;
365 if ( $iamsure && isset ( $da ) && count ( $da ) > 0 ) {
366 $id = array_keys ( $da ) ;
367 $id = array_shift ( $id ) ;
368 $this->deleteTopic ( $id ) ;
369 }
370
371 $r = "<p>" . wfMsg ( 'val_warning' ) . "</p>\n" ;
372 $r .= "<form method='post'>\n" ;
373 $r .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
374 $r .= "<tr>" . wfMsg ( 'val_list_header' ) . "</tr>\n" ;
375 foreach ( $this->topicList AS $x => $y ) {
376 $r .= "<tr>\n" ;
377 $r .= "<th>" . $y->val_type . "</th>\n" ;
378 $r .= "<td>{$y->val_comment}</td>\n" ;
379 $r .= "<td>1 .. <b>{$y->val_value}</b></td>\n" ;
380 $r .= "<td><input type='submit' name='m_del[{$x}]' value='" . wfMsg ( 'val_del' ) . "'/></td>\n" ;
381 $r .= "</tr>\n" ;
382 }
383 $r .= "<tr>\n" ;
384 $r .= "<td/>\n" ;
385 $r .= "<td><input type='text' name='m_topic' value=''/></td>\n" ;
386 $r .= "<td><input type='text' name='m_limit' value=''/></td>\n" ;
387 $r .= "<td><input type='submit' name='m_add' value='" . wfMsg ( 'val_add' ) . "'/></td>\n" ;
388 $r .= "</tr>\n" ;
389 $r .= "</table>\n" ;
390 $r .= "<input type='checkbox' name='iamsure' value='1'/>" . wfMsg ( 'val_iamsure' ) . "\n" ;
391 $r .= "</form>\n" ;
392 return $r ;
393 }
394
395 function showList ( &$article ) {
396 $this->prepareRevisions ( $article->getID() ) ;
397 $this->topicList = $this->getTopicList() ;
398
399 # Collecting statistic data
400 $id = $article->getID() ;
401 $sql = "SELECT * FROM validate WHERE val_page='{$id}'" ;
402 $res = wfQuery( $sql, DB_READ );
403 $data = array () ;
404 while( $x = wfFetchObject( $res ) ) {
405 #$idx = $x->val_revision ;
406 $idx = $this->getTimestamp ( $x->val_revision ) ;
407 if ( !isset ( $data[$idx] ) )
408 $data[$idx] = array () ;
409 if ( !isset ( $data[$idx][$x->val_type] ) ) {
410 $data[$idx][$x->val_type]->count = 0 ;
411 $data[$idx][$x->val_type]->sum = 0 ;
412 }
413 $data[$idx][$x->val_type]->count++ ;
414 $data[$idx][$x->val_type]->sum += $x->val_value ;
415 }
416
417 krsort ( $data ) ;
418
419 $ret = "" ;
420 $ret .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
421 $ret .= "<tr><th>" . wfMsg("val_revision") . "</th>" ;
422 # $ret .= "<th>" . wfMsg("val_time") . "</th>" ;
423 foreach ( $this->topicList AS $x => $y )
424 $ret .= "<th>{$y->val_comment}</th>" ;
425 $ret .= "</tr>\n" ;
426 foreach ( $data AS $ts => $y ) {
427 $revision = $this->getRevisionNumber ( $ts ) ;
428 # $url = $this->getVersionLink ( $article , $revision , $revision ) ;
429 $url = $this->getVersionLink ( $article , $revision , wfTimestamp ( TS_DB , $ts ) ) ;
430 $ret .= "<tr><th>{$url}</th>" ;
431 # $ret .= "<td nowrap>" . wfTimestamp ( TS_DB , $ts ) . "</td>" ;
432 foreach ( $this->topicList AS $topicID => $dummy ) {
433 if ( isset ( $y[$topicID] ) ) {
434 $z = $y[$topicID] ;
435 if ( $z->count == 0 ) $a = 0 ;
436 else $a = $z->sum / $z->count ;
437 $ret .= sprintf ( "<td><b>%1.1f</b> (%d)</td>" , $a , $z->count ) ;
438 } else $ret .= "<td/>" ;
439 }
440 $ret .= "</tr>\n" ;
441 }
442 $ret .= "</table>\n" ;
443 return $ret ;
444 }
445
446 }
447
448 /**
449 * constructor
450 */
451 function wfSpecialValidate( $page = '' ) {
452 global $wgOut, $wgRequest, $wgUseValidation, $wgUser, $wgContLang;
453
454 if( !$wgUseValidation ) {
455 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
456 return;
457 }
458
459 /*
460 # Can do?
461 if ( ! $wgUser->isAllowed('change_validation') ) {
462 $wgOut->sysopRequired();
463 return;
464 }
465 */
466
467 $mode = $wgRequest->getVal ( "mode" ) ;
468 $skin = $wgUser->getSkin() ;
469
470
471 if ( $mode == "manage" ) {
472 $v = new Validation ;
473 $html = $v->manageTopics () ;
474 # } else if ( $mode == "list" ) {
475 # $v = new Validation ;
476 # $html = $v->showList ( $wgRequest->getVal ( "id" ) ) ;
477 } else {
478 $html = "$mode" ;
479 $html .= "<ul>\n" ;
480
481 $t = Title::newFromText ( "Special:Validate" ) ;
482 $url = $t->getLocalURL ( "mode=manage" ) ;
483 $html .= "<li><a href=\"" . $url . "\">Manage</a></li>\n" ;
484
485 $html .= "</ul>\n" ;
486 }
487
488 $wgOut->addHTML( $html );
489 }
490
491 ?>