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
37 # Reads all revision information of the specified article
38 function prepareRevisions ( $id ) {
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 ;
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>" ;
58 # Returns an array containing all topics you can vote on
59 function getTopicList () {
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 ;
71 # Merges one dataset into another
72 function mergeInto ( &$source , &$dest ) {
74 foreach ( $source AS $x => $y ) {
76 if ( !isset ( $dest[$x] ) ) $doit = true ;
77 else if ( $dest[$x]->value
== 0 ) $doit = true ;
83 if ( $ret ) ksort ( $dest ) ;
87 # Merges all votes prior to the given revision into it
88 function mergeOldRevisions ( &$article , $revision ) {
89 $tmp = $this->voteCache
;
92 $ts = $this->getTimestamp($revision) ;
93 $data = $this->voteCache
[$ts] ;
94 foreach ( $tmp AS $x => $y ) {
96 if ( $this->mergeInto ( $y , $data ) ) $update = true ;
99 if ( $update ) $this->setRevision ( $article , $revision , $data ) ;
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) ) ;
111 # Updates the votes for the given revision from the FORM data
112 function updateRevision ( &$article , $revision ) {
113 global $wgUser, $wgRequest ;
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() ) ;
120 foreach ( $nv AS $x => $y ) {
121 $data[$x]->value
= $y ;
122 $data[$x]->comment
= $nc[$x] ;
126 $this->setRevision ( $article , $revision , $data ) ;
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 . "','" ;
141 $sql .= $y->value
. "','" ;
142 $sql .= Database
::strencode ( $y->comment
) . "')" ;
143 $res = wfQuery( $sql, DB_WRITE
);
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] ) ;
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
;
176 # This functions adds a topic to the database
177 function addTopic ( $topic , $limit ) {
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 ;
187 $x->val_value
= $limit ;
188 $x->val_comment
= $topic ;
189 $this->topicList
[$a] = $x ;
190 ksort ( $this->topicList
) ;
193 # This functions adds a topic to the database
194 function deleteTopic ( $id ) {
196 $sql = "DELETE FROM {$wgDBprefix}validate WHERE val_type='{$id}'" ;
197 $res = wfQuery( $sql, DB_WRITE
);
198 unset ( $this->topicList
[$id] ) ;
201 # This function returns a link text to the page validation statistics
202 function link2statistics ( &$article ) {
203 $ret = wfMsg ( 'val_rev_stats_link' ) ;
204 $nt = $article->getTitle() ;
205 $ret = str_replace ( "$1" , $nt->getPrefixedText() , $ret ) ;
207 $url = $nt->getLocalURL ( "action=validate&mode=list" ) ;
208 $ret = str_replace ( "$2" , $url , $ret ) ;
213 # Returns the timestamp of a revision based on the revision number
214 function getTimestamp ( $revision ) {
215 $ts = $this->rev2date
[$revision] ;
216 $ts = $ts->rev_timestamp
;
220 # Returns the revision number of a revision based on the timestamp
221 function getRevisionNumber ( $ts ) {
222 $revision = $this->date2rev
[$ts] ;
223 $revision = $revision->rev_id
;
228 # HTML generation functions from this point on
230 # Returns the metadata string for a revision
231 function getMetadata ( $idx ) {
233 $x = $this->rev2date
[$idx] ;
234 $metadata .= wfTimestamp ( TS_DB
, $x->rev_timestamp
) ;
235 $metadata .= " by " ;
236 if ( $x->rev_user
== 0 ) {
237 $metadata .= $x->rev_user_text
;
240 $u->setId ( $x->rev_user
) ;
241 $u->setName ( $x->rev_user_text
) ;
242 $nt = $u->getUserPage() ;
243 $url = "<a href='" . $nt->getLocalUrl () . "'>" . $nt->getText() . "</a>" ;
246 $metadata .= " : <small>\"" . htmlspecialchars ( $x->rev_comment
) . "\"</small>" ;
250 # Generates a form for a single revision
251 function getRevisionForm ( &$article , $idx , &$data , $focus = false ) {
252 # Fill data with blank values
254 $revision = $this->getRevisionNumber ( $ts ) ;
255 foreach ( $this->topicList
AS $x => $y ) {
256 if ( !isset ( $data[$x] ) ) {
257 $data[$x]->value
= 0 ;
258 $data[$x]->comment
= "" ;
264 $ret = "<form method='post'>" ;
265 $ret .= "<table border='1' cellspacing='0' cellpadding='2'" ;
266 if ( $focus ) $ret .= " style='background-color:#00BBFF'" ;
268 $head = "Revision #" . $revision ;
269 $link = " " . $this->getVersionLink ( $article , $revision ) ;
270 $metadata = $this->getMetadata ( $revision ) ;
271 $ret .= "<tr><th align='left' colspan='3'>" . $head . " ({$link}) {$metadata}</th></tr>\n" ;
273 foreach ( $data AS $x => $y ) {
275 $col = $line == 1 ?
"#DDDDDD" : "#EEEEEE" ;
276 $idx = "_{$revision}[{$x}]" ;
277 $ret .= "<tr bgcolor='{$col}'>\n" ;
278 $ret .= "<th nowrap>" ;
279 $ret .= $this->topicList
[$x]->val_comment
;
282 $tlx = $this->topicList
[$x] ;
284 $max = $tlx->val_value
;
285 for ( $a = 0 ; $a <= $max ; $a++
) {
286 if ( $a == 0 ) $vote .= wfMsg ( "val_noop" ) ;
287 $vote .= "<input type='radio' name='re_v{$idx}' value='{$a}'" ;
288 if ( $a == $y->value
) $vote .= " checked" ;
290 if ( $max == 2 && $a == 1 ) $vote .= wfMsg ( "val_no" ) . " " ;
291 else if ( $max == 2 && $a == 2 ) $vote .= wfMsg ( "val_yes" ) ;
292 else if ( $a != 0 ) $vote .= $a . " " ;
293 if ( $a == 0 ) $vote .= " " ;
295 $ret .= "<td nowrap valign='center'>{$vote}</td>\n" ;
297 $ret .= "<td width='100%' align='center'><input size='50' style='width:98%' maxlength='250' type='text' name='re_c{$idx}' value='{$y->comment}'/>" ;
298 $ret .= "</td></tr>\n" ;
300 $checked = $focus ?
" checked" : "" ;
301 $ret .= "<tr><td colspan='3' valign='center'>\n" ;
302 $ret .= "<input type='checkbox' name='re_merge_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_merge_old' ) . " \n" ;
303 $ret .= "<input type='checkbox' name='re_clear_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_clear_old' ) . " \n" ;
304 $ret .= "<input type='submit' name='re_submit[{$revision}]' value='" . htmlspecialchars( wfMsg("ok") ) . "'/>\n" ;
305 if ( $focus ) $ret .= "<br/>\n<small>" . wfMsg ( "val_form_note" ) . "</small>" ;
306 $ret .= "</td></tr>\n" ;
307 $ret .= "</table>\n</form>\n\n" ;
312 # Generates the page from the validation tab
313 function validatePageForm ( &$article , $revision ) {
314 global $wgOut, $wgRequest ;
316 $this->prepareRevisions ( $article->getID() ) ;
317 $this->topicList
= $this->getTopicList() ;
318 $this->voteCache
= $this->getVoteList ( $article->getID() ) ;
320 # Check for POST data
321 $re = $wgRequest->getArray( 're_submit' );
324 $id = array_keys ( $re ) ;
325 $id = $id[0] ; # $id is now the revision number the user clicked "OK" for
326 $clearOldRev = $wgRequest->getVal( "re_clear_{$id}" , 0 );
327 $mergeOldRev = $wgRequest->getVal( "re_merge_{$id}" , 0 );
328 $this->updateRevision ( $article , $id ) ;
329 if ( $mergeOldRev ) $this->mergeOldRevisions ( $article , $id ) ;
330 if ( $clearOldRev ) $this->clearOldRevisions ( $article , $id ) ;
333 # Make sure the requested revision exists
334 $ts = $this->rev2date
[$revision]->rev_timestamp
;
335 if ( !isset ( $this->voteCache
[$ts] ) ) $this->voteCache
[$ts] = array () ;
337 # Sort revisions list, newest first
338 krsort ( $this->voteCache
) ;
342 $title = $article->getTitle();
343 $title = $title->getPrefixedText() ;
344 $wgOut->setPageTitle ( wfMsg ( 'val_rev_for' ) . $title ) ;
345 foreach ( $this->voteCache
AS $x => $y )
347 $ret .= $this->getRevisionForm ( $article , $x , $y , $x == $ts ) ;
350 $ret .= $this->link2statistics ( $article ) ;
354 # This function performs the "management" mode on Special:Validate
355 function manageTopics () {
357 $this->topicList
= $this->getTopicList() ;
359 $iamsure = $wgRequest->getVal ( "iamsure" , "0" ) == 1 ;
361 if ( $iamsure && $wgRequest->getVal ( "m_add" , "--" ) != "--" ) {
362 $new_topic = $wgRequest->getVal ( "m_topic" ) ;
363 $new_limit = $wgRequest->getVal ( "m_limit" ) ;
364 if ( $new_topic != "" && $new_limit > 1 )
365 $this->addTopic ( $new_topic , $new_limit ) ;
368 $da = $wgRequest->getArray ( "m_del" ) ;
369 if ( $iamsure && isset ( $da ) && count ( $da ) > 0 ) {
370 $id = array_keys ( $da ) ;
371 $id = array_shift ( $id ) ;
372 $this->deleteTopic ( $id ) ;
375 $r = "<p>" . wfMsg ( 'val_warning' ) . "</p>\n" ;
376 $r .= "<form method='post'>\n" ;
377 $r .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
378 $r .= "<tr>" . wfMsg ( 'val_list_header' ) . "</tr>\n" ;
379 foreach ( $this->topicList
AS $x => $y ) {
381 $r .= "<th>" . $y->val_type
. "</th>\n" ;
382 $r .= "<td>{$y->val_comment}</td>\n" ;
383 $r .= "<td>1 .. <b>{$y->val_value}</b></td>\n" ;
384 $r .= "<td><input type='submit' name='m_del[{$x}]' value='" . wfMsg ( 'val_del' ) . "'/></td>\n" ;
389 $r .= "<td><input type='text' name='m_topic' value=''/></td>\n" ;
390 $r .= "<td><input type='text' name='m_limit' value=''/></td>\n" ;
391 $r .= "<td><input type='submit' name='m_add' value='" . wfMsg ( 'val_add' ) . "'/></td>\n" ;
394 $r .= "<input type='checkbox' name='iamsure' value='1'/>" . wfMsg ( 'val_iamsure' ) . "\n" ;
399 function showList ( &$article ) {
401 $this->prepareRevisions ( $article->getID() ) ;
402 $this->topicList
= $this->getTopicList() ;
404 # Collecting statistic data
405 $id = $article->getID() ;
406 $sql = "SELECT * FROM {$wgDBprefix}validate WHERE val_page='{$id}'" ;
407 $res = wfQuery( $sql, DB_READ
);
409 while( $x = wfFetchObject( $res ) ) {
410 #$idx = $x->val_revision ;
411 $idx = $this->getTimestamp ( $x->val_revision
) ;
412 if ( !isset ( $data[$idx] ) )
413 $data[$idx] = array () ;
414 if ( !isset ( $data[$idx][$x->val_type
] ) ) {
415 $data[$idx][$x->val_type
]->count
= 0 ;
416 $data[$idx][$x->val_type
]->sum
= 0 ;
418 $data[$idx][$x->val_type
]->count++
;
419 $data[$idx][$x->val_type
]->sum +
= $x->val_value
;
425 $ret .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
426 $ret .= "<tr><th>" . wfMsg("val_revision") . "</th>" ;
427 # $ret .= "<th>" . wfMsg("val_time") . "</th>" ;
428 foreach ( $this->topicList
AS $x => $y )
429 $ret .= "<th>{$y->val_comment}</th>" ;
431 foreach ( $data AS $ts => $y ) {
432 $revision = $this->getRevisionNumber ( $ts ) ;
433 # $url = $this->getVersionLink ( $article , $revision , $revision ) ;
434 $url = $this->getVersionLink ( $article , $revision , wfTimestamp ( TS_DB
, $ts ) ) ;
435 $ret .= "<tr><th>{$url}</th>" ;
436 # $ret .= "<td nowrap>" . wfTimestamp ( TS_DB , $ts ) . "</td>" ;
437 foreach ( $this->topicList
AS $topicID => $dummy ) {
438 if ( isset ( $y[$topicID] ) ) {
440 if ( $z->count
== 0 ) $a = 0 ;
441 else $a = $z->sum
/ $z->count
;
442 $ret .= sprintf ( "<td><b>%1.1f</b> (%d)</td>" , $a , $z->count
) ;
443 } else $ret .= "<td/>" ;
447 $ret .= "</table>\n" ;
456 function wfSpecialValidate( $page = '' ) {
457 global $wgOut, $wgRequest, $wgUseValidation, $wgUser, $wgContLang;
459 if( !$wgUseValidation ) {
460 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
466 if ( ! $wgUser->isAllowed('change_validation') ) {
467 $wgOut->sysopRequired();
472 $mode = $wgRequest->getVal ( "mode" ) ;
473 $skin = $wgUser->getSkin() ;
476 if ( $mode == "manage" ) {
477 $v = new Validation
;
478 $html = $v->manageTopics () ;
479 # } else if ( $mode == "list" ) {
480 # $v = new Validation ;
481 # $html = $v->showList ( $wgRequest->getVal ( "id" ) ) ;
486 $t = Title
::newFromText ( "Special:Validate" ) ;
487 $url = $t->getLocalURL ( "mode=manage" ) ;
488 $html .= "<li><a href=\"" . $url . "\">Manage</a></li>\n" ;
493 $wgOut->addHTML( $html );