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 ) {
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 ;
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>" ;
57 # Returns an array containing all topics you can vote on
58 function getTopicList () {
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 ;
69 # Merges one dataset into another
70 function mergeInto ( &$source , &$dest ) {
72 foreach ( $source AS $x => $y ) {
74 if ( !isset ( $dest[$x] ) ) $doit = true ;
75 else if ( $dest[$x]->value
== 0 ) $doit = true ;
81 if ( $ret ) ksort ( $dest ) ;
85 # Merges all votes prior to the given revision into it
86 function mergeOldRevisions ( &$article , $revision ) {
87 $tmp = $this->voteCache
;
90 $ts = $this->getTimestamp($revision) ;
91 $data = $this->voteCache
[$ts] ;
92 foreach ( $tmp AS $x => $y ) {
94 if ( $this->mergeInto ( $y , $data ) ) $update = true ;
97 if ( $update ) $this->setRevision ( $article , $revision , $data ) ;
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) ) ;
109 # Updates the votes for the given revision from the FORM data
110 function updateRevision ( &$article , $revision ) {
111 global $wgUser, $wgRequest ;
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() ) ;
118 foreach ( $nv AS $x => $y ) {
119 $data[$x]->value
= $y ;
120 $data[$x]->comment
= $nc[$x] ;
124 $this->setRevision ( $article , $revision , $data ) ;
127 # Sets a specific revision to both cache and database
128 function setRevision ( &$article , $revision , &$data ) {
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 . "','" ;
139 $sql .= $y->value
. "','" ;
140 $sql .= Database
::strencode ( $y->comment
) . "')" ;
141 $res = wfQuery( $sql, DB_WRITE
);
146 # Deletes a specific vote set in both cache and database
147 function deleteRevision ( &$article , $revision ) {
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] ) ;
157 # Reads the entire vote list for this user for the given article
158 function getVoteList ( $id ) {
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
;
174 # This functions adds a topic to the database
175 function addTopic ( $topic , $limit ) {
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 ;
184 $x->val_value
= $limit ;
185 $x->val_comment
= $topic ;
186 $this->topicList
[$a] = $x ;
187 ksort ( $this->topicList
) ;
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] ) ;
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 ) ;
203 $url = $nt->getLocalURL ( "action=validate&mode=list" ) ;
204 $ret = str_replace ( "$2" , $url , $ret ) ;
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
;
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
;
224 # HTML generation functions from this point on
226 # Returns the metadata string for a revision
227 function getMetadata ( $idx ) {
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
;
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>" ;
242 $metadata .= " : <small>\"" . htmlspecialchars ( $x->rev_comment
) . "\"</small>" ;
246 # Generates a form for a single revision
247 function getRevisionForm ( &$article , $idx , &$data , $focus = false ) {
248 # Fill data with blank values
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
= "" ;
260 $ret = "<form method='post'>" ;
261 $ret .= "<table border='1' cellspacing='0' cellpadding='2'" ;
262 if ( $focus ) $ret .= " style='background-color:#00BBFF'" ;
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" ;
269 foreach ( $data AS $x => $y ) {
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
;
278 $tlx = $this->topicList
[$x] ;
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" ;
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 .= " " ;
291 $ret .= "<td nowrap valign='center'>{$vote}</td>\n" ;
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" ;
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" ;
308 # Generates the page from the validation tab
309 function validatePageForm ( &$article , $revision ) {
310 global $wgOut, $wgRequest ;
312 $this->prepareRevisions ( $article->getID() ) ;
313 $this->topicList
= $this->getTopicList() ;
314 $this->voteCache
= $this->getVoteList ( $article->getID() ) ;
316 # Check for POST data
317 $re = $wgRequest->getArray( 're_submit' );
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 ) ;
329 # Make sure the requested revision exists
330 $ts = $this->rev2date
[$revision]->rev_timestamp
;
331 if ( !isset ( $this->voteCache
[$ts] ) ) $this->voteCache
[$ts] = array () ;
333 # Sort revisions list, newest first
334 krsort ( $this->voteCache
) ;
338 $title = $article->getTitle();
339 $title = $title->getPrefixedText() ;
340 $wgOut->setPageTitle ( wfMsg ( 'val_rev_for' ) . $title ) ;
341 foreach ( $this->voteCache
AS $x => $y )
343 $ret .= $this->getRevisionForm ( $article , $x , $y , $x == $ts ) ;
346 $ret .= $this->link2statistics ( $article ) ;
350 # This function performs the "management" mode on Special:Validate
351 function manageTopics () {
353 $this->topicList
= $this->getTopicList() ;
355 $iamsure = $wgRequest->getVal ( "iamsure" , "0" ) == 1 ;
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 ) ;
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 ) ;
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 ) {
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" ;
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" ;
390 $r .= "<input type='checkbox' name='iamsure' value='1'/>" . wfMsg ( 'val_iamsure' ) . "\n" ;
395 function showList ( &$article ) {
396 $this->prepareRevisions ( $article->getID() ) ;
397 $this->topicList
= $this->getTopicList() ;
399 # Collecting statistic data
400 $id = $article->getID() ;
401 $sql = "SELECT * FROM validate WHERE val_page='{$id}'" ;
402 $res = wfQuery( $sql, DB_READ
);
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 ;
413 $data[$idx][$x->val_type
]->count++
;
414 $data[$idx][$x->val_type
]->sum +
= $x->val_value
;
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>" ;
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] ) ) {
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/>" ;
442 $ret .= "</table>\n" ;
451 function wfSpecialValidate( $page = '' ) {
452 global $wgOut, $wgRequest, $wgUseValidation, $wgUser, $wgContLang;
454 if( !$wgUseValidation ) {
455 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
461 if ( ! $wgUser->isAllowed('change_validation') ) {
462 $wgOut->sysopRequired();
467 $mode = $wgRequest->getVal ( "mode" ) ;
468 $skin = $wgUser->getSkin() ;
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" ) ) ;
481 $t = Title
::newFromText ( "Special:Validate" ) ;
482 $url = $t->getLocalURL ( "mode=manage" ) ;
483 $html .= "<li><a href=\"" . $url . "\">Manage</a></li>\n" ;
488 $wgOut->addHTML( $html );