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