caf32e07240fba6bbb5ac52df6b6643070e093a8
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
33 # Returns a HTML link to the specified article revision
34 function getVersionLink( &$article , $revision , $text = "" ) {
35 $t = $article->getTitle() ;
36 if ( $text == "" ) $text = wfMsg("val_view_version");
37 $ret = "<a href=\"" . $t->getLocalURL ( "oldid={$revision}" ) . "\">" . $text . "</a>" ;
41 # Returns an array containing all topics you can vote on
42 function getTopicList () {
44 $sql = "SELECT * FROM validate WHERE val_user=0" ;
45 $res = wfQuery( $sql, DB_READ
);
46 while( $x = wfFetchObject( $res ) ) {
47 $ret[$x->val_type
] = $x ;
53 # Generates a form for a single revision
54 function getRevisionForm ( &$article , $revision , &$data , $focus = false ) {
55 # Fill data with blank values
56 foreach ( $this->topicList
AS $x => $y ) {
57 if ( !isset ( $data[$x] ) ) {
58 $data[$x]->value
= 0 ;
59 $data[$x]->comment
= "" ;
65 $ret = "<form method='post'>" ;
66 $ret .= "<table border='1' cellspacing='0' cellpadding='2'" ;
67 if ( $focus ) $ret .= " style='background-color:#00BBFF'" ;
69 $head = "Revision #" . $revision ;
70 # if ( $focus ) $head = "<font color='red'>{$head}</font>" ;
71 $link = " " . $this->getVersionLink ( $article , $revision ) ;
72 $ret .= "<tr><th align='left' colspan='3'>" . $head . " ({$link})</th></tr>\n" ;
74 foreach ( $data AS $x => $y ) {
76 $col = $line == 1 ?
"#DDDDDD" : "#EEEEEE" ;
77 $idx = "_{$revision}[{$x}]" ;
78 $ret .= "<tr bgcolor='{$col}'>\n" ;
79 $ret .= "<th nowrap>" ;
80 $ret .= $this->topicList
[$x]->val_comment
;
83 $tlx = $this->topicList
[$x] ;
85 $max = $tlx->val_value
;
86 for ( $a = 0 ; $a <= $max ; $a++
) {
87 if ( $a == 0 ) $vote .= wfMsg ( "val_noop" ) ;
88 $vote .= "<input type='radio' name='re_v{$idx}' value='{$a}'" ;
89 if ( $a == $y->value
) $vote .= " checked" ;
91 if ( $max == 2 && $a == 1 ) $vote .= wfMsg ( "val_no" ) . " " ;
92 else if ( $max == 2 && $a == 2 ) $vote .= wfMsg ( "val_yes" ) ;
93 else if ( $a != 0 ) $vote .= $a . " " ;
94 if ( $a == 0 ) $vote .= " " ;
96 $ret .= "<td nowrap valign='center'>{$vote}</td>\n" ;
98 $ret .= "<td width='100%' align='center'><input size='50' style='width:98%' maxlength='250' type='text' name='re_c{$idx}' value='{$y->comment}'/>" ;
99 $ret .= "</td></tr>\n" ;
101 $checked = $focus ?
" checked" : "" ;
102 $ret .= "<tr><td colspan='3' valign='center'>\n" ;
103 $ret .= "<input type='checkbox' name='re_merge_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_merge_old' ) . " \n" ;
104 $ret .= "<input type='checkbox' name='re_clear_{$revision}' value='1'{$checked}/>" . wfMsg( 'val_clear_old' ) . " \n" ;
105 $ret .= "<input type='submit' name='re_submit[{$revision}]' value='" . htmlspecialchars( wfMsg("ok") ) . "'/>\n" ;
106 if ( $focus ) $ret .= "<br/>\n<small>" . wfMsg ( "val_form_note" ) . "</small>" ;
107 $ret .= "</td></tr>\n" ;
108 $ret .= "</table>\n</form>\n\n" ;
112 # Merges one dataset into another
113 function mergeInto ( &$source , &$dest ) {
115 foreach ( $source AS $x => $y ) {
117 if ( !isset ( $dest[$x] ) ) $doit = true ;
118 else if ( $dest[$x]->value
== 0 ) $doit = true ;
124 if ( $ret ) ksort ( $dest ) ;
128 # Merges all votes prior to the given revision into it
129 function mergeOldRevisions ( &$article , $revision ) {
130 $tmp = $this->voteCache
;
133 $data = $this->voteCache
[$revision] ;
134 foreach ( $tmp AS $x => $y ) {
135 if ( $x < $revision ) {
136 if ( $this->mergeInto ( $y , $data ) ) $update = true ;
139 if ( $update ) $this->setRevision ( $article , $revision , $data ) ;
142 # Clears all votes prior to the given revision
143 function clearOldRevisions ( &$article , $revision ) {
144 $tmp = $this->voteCache
;
145 foreach ( $tmp AS $x => $y ) {
146 if ( $x < $revision ) $this->deleteRevision ( $article , $x ) ;
150 # Updates the votes for the given revision from the FORM data
151 function updateRevision ( &$article , $revision ) {
152 global $wgUser, $wgRequest ;
154 if ( isset ( $this->voteCache
[$revision] ) ) $data = $this->voteCache
[$revision] ;
155 else $data = array () ;
156 $nv = $wgRequest->getArray ( "re_v_{$revision}" , array() ) ;
157 $nc = $wgRequest->getArray ( "re_c_{$revision}" , array() ) ;
159 foreach ( $nv AS $x => $y ) {
161 $data[$x]->value
= $y ;
162 $data[$x]->comment
= $nc[$x] ;
167 $this->setRevision ( $article , $revision , $data ) ;
170 # Sets a specific revision to both cache and database
171 function setRevision ( &$article , $revision , &$data ) {
173 $this->deleteRevision ( $article , $revision ) ;
174 $this->voteCache
[$revision] = $data ;
175 foreach ( $data AS $x => $y ) {
176 if ( $y->value
> 0 ) {
177 $sql = "INSERT INTO validate (val_user,val_page,val_revision,val_type,val_value,val_comment) VALUES ('" ;
178 $sql .= $wgUser->getID() . "','" ;
179 $sql .= $article->getID() . "','" ;
180 $sql .= $revision . "','" ;
182 $sql .= $y->value
. "','" ;
183 $sql .= Database
::strencode ( $y->comment
) . "')" ;
184 $res = wfQuery( $sql, DB_WRITE
);
189 # Deletes a specific vote set in both cache and database
190 function deleteRevision ( &$article , $revision ) {
192 if ( !isset ( $this->voteCache
[$revision] ) ) return ; # Nothing to do
193 $sql = "DELETE FROM validate WHERE val_user='" . $wgUser->GetID() . "' AND " ;
194 $sql .= " val_page='" . $article->getID() . "' AND val_revision='{$revision}'" ;
195 $res = wfQuery( $sql, DB_WRITE
);
196 unset ( $this->voteCache
[$revision] ) ;
199 # Reads the entire vote list for this user for the given article
200 function getVoteList ( $id ) {
202 $r = array () ; # Revisions
203 $sql = "SELECT * FROM validate WHERE val_page=" . $id . " AND val_user=" . $wgUser->getID() ;
204 $res = wfQuery( $sql, DB_READ
);
205 while( $x = wfFetchObject( $res ) ) {
206 if ( !isset($r[$x->val_revision
]) ) $r[$x->val_revision
] = array () ;
207 $r[$x->val_revision
][$x->val_type
]->value
= $x->val_value
;
208 $r[$x->val_revision
][$x->val_type
]->comment
= $x->val_comment
;
213 # This functions adds a topic to the database
214 function addTopic ( $topic , $limit ) {
216 while ( isset ( $this->topicList
[$a] ) ) $a++
;
217 $sql = "INSERT INTO validate (val_user,val_page,val_revision,val_type,val_value,val_comment) VALUES (" ;
218 $sql .= "'0','0','0','{$a}','{$limit}','" ;
219 $sql .= Database
::strencode ( $topic ) . "')" ;
220 $res = wfQuery( $sql, DB_WRITE
);
221 $x->val_user
= $x->val_page
= $x->val_revision
= 0 ;
223 $x->val_value
= $limit ;
224 $x->val_comment
= $topic ;
225 $this->topicList
[$a] = $x ;
226 ksort ( $this->topicList
) ;
229 # This functions adds a topic to the database
230 function deleteTopic ( $id ) {
231 $sql = "DELETE FROM validate WHERE val_type='{$id}'" ;
232 $res = wfQuery( $sql, DB_WRITE
);
233 unset ( $this->topicList
[$id] ) ;
236 # This function returns a link text to the page validation statistics
237 function link2statistics ( &$article ) {
238 $ret = wfMsg ( 'val_rev_stats_link' ) ;
239 $nt = $article->getTitle() ;
240 $ret = str_replace ( "$1" , $nt->getPrefixedText() , $ret ) ;
242 # $t = Title::newFromText ( "Special:Validate" ) ;
243 # $url = $t->getLocalURL ( "mode=list&id=" . $article->getID() ) ;
244 $url = $nt->getLocalURL ( "action=validate&mode=list" ) ;
245 $ret = str_replace ( "$2" , $url , $ret ) ;
252 # HTML generation functions from this point on
255 # Generates the page from the validation tab
256 function validatePageForm ( &$article , $revision ) {
257 global $wgOut, $wgRequest ;
259 $this->topicList
= $this->getTopicList() ;
260 $this->voteCache
= $this->getVoteList ( $article->getID() ) ;
262 # Check for POST data
263 $re = $wgRequest->getArray( 're_submit' );
266 $id = array_keys ( $re ) ;
267 $id = $id[0] ; # $id is now the revision number the user clicked "OK" for
268 $clearOldRev = $wgRequest->getVal( "re_clear_{$id}" , 0 );
269 $mergeOldRev = $wgRequest->getVal( "re_merge_{$id}" , 0 );
270 $this->updateRevision ( $article , $id ) ;
271 if ( $mergeOldRev ) $this->mergeOldRevisions ( $article , $id ) ;
272 if ( $clearOldRev ) $this->clearOldRevisions ( $article , $id ) ;
275 # Make sure the requested revision exists
276 if ( !isset ( $this->voteCache
[$revision] ) ) $this->voteCache
[$revision] = array () ;
278 # Sort revisions list, newest first
279 krsort ( $this->voteCache
) ;
283 $title = $article->getTitle();
284 $title = $title->getPrefixedText() ;
285 $wgOut->setPageTitle ( wfMsg ( 'val_rev_for' ) . $title ) ;
286 foreach ( $this->voteCache
AS $x => $y )
288 $ret .= $this->getRevisionForm ( $article , $x , $y , $x == $revision ) ;
291 $ret .= $this->link2statistics ( $article ) ;
295 # This function performs the "management" mode on Special:Validate
296 function manageTopics () {
298 $this->topicList
= $this->getTopicList() ;
300 $iamsure = $wgRequest->getVal ( "iamsure" , "0" ) == 1 ;
302 if ( $iamsure && $wgRequest->getVal ( "m_add" , "--" ) != "--" ) {
303 $new_topic = $wgRequest->getVal ( "m_topic" ) ;
304 $new_limit = $wgRequest->getVal ( "m_limit" ) ;
305 if ( $new_topic != "" && $new_limit > 1 )
306 $this->addTopic ( $new_topic , $new_limit ) ;
309 $da = $wgRequest->getArray ( "m_del" ) ;
310 if ( $iamsure && isset ( $da ) && count ( $da ) > 0 ) {
311 $id = array_keys ( $da ) ;
312 $id = array_shift ( $id ) ;
313 $this->deleteTopic ( $id ) ;
316 $r = "<p>" . wfMsg ( 'val_warning' ) . "</p>\n" ;
317 $r .= "<form method='post'>\n" ;
318 $r .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
319 $r .= "<tr>" . wfMsg ( 'val_list_header' ) . "</tr>\n" ;
320 foreach ( $this->topicList
AS $x => $y ) {
322 $r .= "<th>" . $y->val_type
. "</th>\n" ;
323 $r .= "<td>{$y->val_comment}</td>\n" ;
324 $r .= "<td>1 .. <b>{$y->val_value}</b></td>\n" ;
325 $r .= "<td><input type='submit' name='m_del[{$x}]' value='" . wfMsg ( 'val_del' ) . "'/></td>\n" ;
330 $r .= "<td><input type='text' name='m_topic' value=''/></td>\n" ;
331 $r .= "<td><input type='text' name='m_limit' value=''/></td>\n" ;
332 $r .= "<td><input type='submit' name='m_add' value='" . wfMsg ( 'val_add' ) . "'/></td>\n" ;
335 $r .= "<input type='checkbox' name='iamsure' value='1'/>" . wfMsg ( 'val_iamsure' ) . "\n" ;
340 function showList ( &$article ) {
341 $this->topicList
= $this->getTopicList() ;
343 # Collecting statistic data
344 $id = $article->getID() ;
345 $sql = "SELECT * FROM validate WHERE val_page='{$id}'" ;
346 $res = wfQuery( $sql, DB_READ
);
348 while( $x = wfFetchObject( $res ) ) {
349 if ( !isset ( $data[$x->val_revision
] ) )
350 $data[$x->val_revision
] = array () ;
351 if ( !isset ( $data[$x->val_revision
][$x->val_type
] ) ) {
352 $data[$x->val_revision
][$x->val_type
]->count
= 0 ;
353 $data[$x->val_revision
][$x->val_type
]->sum
= 0 ;
355 $data[$x->val_revision
][$x->val_type
]->count++
;
356 $data[$x->val_revision
][$x->val_type
]->sum +
= $x->val_value
;
360 $ret .= "<table border='1' cellspacing='0' cellpadding='2'>\n" ;
361 $ret .= "<tr><th>Revision</th>" ;
362 foreach ( $this->topicList
AS $x => $y )
363 $ret .= "<th>{$y->val_comment}</th>" ;
365 foreach ( $data AS $revision => $y ) {
366 $url = $this->getVersionLink ( $article , $revision , $revision ) ;
367 $ret .= "<tr><th>{$url}</th>" ;
368 foreach ( $this->topicList
AS $topicID => $dummy ) {
369 if ( isset ( $y[$topicID] ) ) {
371 if ( $z->count
== 0 ) $a = 0 ;
372 else $a = $z->sum
/ $z->count
;
373 $ret .= sprintf ( "<td><b>%1.1f</b> (%d)</td>" , $a , $z->count
) ;
374 } else $ret .= "<td/>" ;
378 $ret .= "</table>\n" ;
387 function wfSpecialValidate( $page = '' ) {
388 global $wgOut, $wgRequest, $wgUseValidation, $wgUser, $wgContLang;
390 if( !$wgUseValidation ) {
391 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
397 if ( ! $wgUser->isAllowed('change_validation') ) {
398 $wgOut->sysopRequired();
403 $mode = $wgRequest->getVal ( "mode" ) ;
404 $skin = $wgUser->getSkin() ;
407 if ( $mode == "manage" ) {
408 $v = new Validation
;
409 $html = $v->manageTopics () ;
410 # } else if ( $mode == "list" ) {
411 # $v = new Validation ;
412 # $html = $v->showList ( $wgRequest->getVal ( "id" ) ) ;
417 $t = Title
::newFromText ( "Special:Validate" ) ;
418 $url = $t->getLocalURL ( "mode=manage" ) ;
419 $html .= "<li><a href=\"" . $url . "\">Manage</a></li>\n" ;
424 $wgOut->addHTML( $html );