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 $nt = $article->getTitle();
204 $url = htmlspecialchars( $nt->getLocalURL( 'action=validate&mode=list' ) );
205 return wfMsg ( 'val_rev_stats_link', $nt->getPrefixedText(), $url );
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
;
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
;
223 # HTML generation functions from this point on
225 # Returns the metadata string for a revision
226 function getMetadata ( $idx ) {
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
;
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>" ;
241 $metadata .= " : <small>\"" . htmlspecialchars ( $x->rev_comment
) . "\"</small>" ;
245 # Generates a form for a single revision
246 function getRevisionForm ( &$article , $idx , &$data , $focus = false ) {
247 # Fill data with blank values
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
= "" ;
259 $ret = "<form method='post'>" ;
260 $ret .= "<table border='1' cellspacing='0' cellpadding='2'" ;
261 if ( $focus ) $ret .= " style='background-color:#00BBFF'" ;
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" ;
268 foreach ( $data AS $x => $y ) {
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
;
277 $tlx = $this->topicList
[$x] ;
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" ;
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 .= " " ;
290 $ret .= "<td nowrap valign='center'>{$vote}</td>\n" ;
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" ;
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" ;
307 # Generates the page from the validation tab
308 function validatePageForm ( &$article , $revision ) {
309 global $wgOut, $wgRequest ;
311 $this->prepareRevisions ( $article->getID() ) ;
312 $this->topicList
= $this->getTopicList() ;
313 $this->voteCache
= $this->getVoteList ( $article->getID() ) ;
315 # Check for POST data
316 $re = $wgRequest->getArray( 're_submit' );
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 ) ;
328 # Make sure the requested revision exists
329 $ts = $this->rev2date
[$revision]->rev_timestamp
;
330 if ( !isset ( $this->voteCache
[$ts] ) ) $this->voteCache
[$ts] = array () ;
332 # Sort revisions list, newest first
333 krsort ( $this->voteCache
) ;
337 $title = $article->getTitle();
338 $title = $title->getPrefixedText() ;
339 $wgOut->setPageTitle ( wfMsg ( 'val_rev_for' ) . $title ) ;
340 foreach ( $this->voteCache
AS $x => $y )
342 $ret .= $this->getRevisionForm ( $article , $x , $y , $x == $ts ) ;
345 $ret .= $this->link2statistics ( $article ) ;
349 # This function performs the "management" mode on Special:Validate
350 function manageTopics () {
352 $this->topicList
= $this->getTopicList() ;
354 $iamsure = $wgRequest->getVal ( "iamsure" , "0" ) == 1 ;
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 ) ;
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 ) ;
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 ) {
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" ;
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" ;
389 $r .= "<input type='checkbox' name='iamsure' value='1'/>" . wfMsg ( 'val_iamsure' ) . "\n" ;
394 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 {$wgDBprefix}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 );