Article validation code (bug fixes)
[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 class Validation
21 {
22
23 function find_this_version ( $article_title , &$article_time , &$id , &$tab )
24 {
25 $id = "" ;
26 $tab = "" ;
27 $sql = "SELECT cur_id,cur_timestamp FROM cur WHERE cur_namespace=0 AND cur_title='{$article_title}'" ;
28 $res = wfQuery( $sql, DB_READ );
29 if( $s = wfFetchObject( $res ) )
30 {
31 if ( $article_time == "" ) $article_time = $s->cur_timestamp ; # No timestamp = current version
32 if ( $article_time == $s->cur_timestamp ) # Means current version
33 {
34 $tab = "cur" ;
35 $id = $s->cur_id ;
36 }
37 }
38
39 if ( $id == "" )
40 {
41 $sql = "SELECT old_id FROM old WHERE old_namespace=0 AND old_title='{$article_title}' AND old_timestamp='{$article_time}'" ;
42 $res = wfQuery( $sql, DB_READ );
43 if( $s = wfFetchObject( $res ) )
44 {
45 $tab = "old" ;
46 $id = $s->old_id ;
47 }
48 }
49 }
50
51 function get_prev_data ( $user_id , $article_title , $article_timestamp = "" )
52 {
53 $ret = array () ;
54 $sql = "SELECT * FROM validate WHERE val_user='{$user_id}' AND val_title='{$article_title}'" ;
55 if ( $article_timestamp != "" ) $sql .= " AND val_timestamp='{$article_timestamp}'" ;
56 $res = wfQuery( $sql, DB_READ );
57 while( $s = wfFetchObject( $res ) ) $ret[$s->val_timestamp][$s->val_type] = $s ;
58 return $ret ;
59 }
60
61 function validate_form ( $article_title = "" )
62 {
63 global $wgOut, $wgLang, $wgUser, $wgArticle ;
64
65 if ( $wgUser->getID() == 0 ) # Anon
66 {
67 $wgOut->addHTML ( wfMsg ( 'val_no_anon_validation' ) . $this->getPageStatistics ( $article_title ) ) ;
68 return ;
69 }
70
71 $validationtypes = $wgLang->getValidationTypes() ;
72 if ( $article_title == "" )
73 {
74 $article_title = $_GET['article_title'] ;
75 $heading = "<h1>" . $article_title . "</h1>\n" ;
76 }
77 else $heading = "" ;
78 $article_time = "" ;
79 if ( isset ( $_GET['timestamp'] ) ) $article_time = $_GET['timestamp'] ;
80 else $article_time = "" ;
81 $article = Title::newFromText ( $article_title ) ;
82
83 # Now we get all the "votes" for the different versions of this article for this user
84 $val = $this->get_prev_data ( $wgUser->getID() , $article_title ) ;
85
86 # No votes for this version, initial data
87 if ( !isset ( $val[$article_time] ) )
88 {
89 if ( $article_time == "" )
90 {
91 $res = wfQuery( "select cur_timestamp FROM cur WHERE cur_title=\"{$article_title}\" AND cur_namespace=0", DB_READ );
92 if ( $s = wfFetchObject( $res ) ) $article_time = $s->cur_timestamp ;
93 }
94 $val[$article_time] = array () ;
95 }
96
97 krsort ( $val ) ; # Newest versions first
98
99 # User has clicked "Doit" before, so evaluate form
100 if ( isset ( $_POST['doit'] ) )
101 {
102 $oldtime = $_POST['oldtime'] ;
103 if ( !isset ( $val["{$oldtime}"] ) ) $val["{$oldtime}"] = array () ;
104
105 # Reading postdata
106 $postrad = array () ;
107 $poscomment = array () ;
108 for ( $idx = 0 ; $idx < count ( $validationtypes) ; $idx++ )
109 {
110 $postrad[$idx] = $_POST["rad{$idx}"] ;
111 $postcomment[$idx] = $_POST["comment{$idx}"] ;
112 }
113
114 # Merge others into this one
115 if ( isset ( $_POST['merge_other'] ) && $_POST['merge_other'] == 1 )
116 {
117 foreach ( $val AS $time => $stuff )
118 {
119 if ( $time <> $article_time )
120 {
121 for ( $idx = 0 ; $idx < count ( $validationtypes) ; $idx++ )
122 {
123 $rad = $postrad[$idx] ;
124 if ( isset ( $stuff[$idx] ) AND $stuff[$idx]->val_value != -1 AND $rad == -1 )
125 {
126 $postrad[$idx] = $stuff[$idx]->val_value ;
127 $postcomment[$idx] = $stuff[$idx]->val_comment ;
128 }
129 }
130 }
131 }
132 }
133
134 # Clear all others
135 if ( isset ( $_POST['clear_other'] ) && $_POST['clear_other'] == 1 )
136 {
137 $sql = "DELETE FROM validate WHERE val_title='{$article_title}' AND val_timestamp<>'{$oldtime}' AND val_user='" ;
138 $sql .= $wgUser->getID() . "'" ;
139 wfQuery( $sql, DB_WRITE );
140 $val2 = $val["{$oldtime}"] ; # Only version left
141 $val = array () ; # So clear others
142 $val["{$oldtime}"] = $val2 ;
143 }
144
145 # Delete old "votes" for this version
146 $sql = "DELETE FROM validate WHERE val_title='{$article_title}' AND val_timestamp='{$oldtime}' AND val_user='" ;
147 $sql .= $wgUser->getID() . "'" ;
148 wfQuery( $sql, DB_WRITE );
149
150 # Incorporate changes
151 for ( $idx = 0 ; $idx < count ( $validationtypes) ; $idx++ ) # Changes
152 {
153 $comment = $postcomment[$idx] ;
154 $comment_sql = str_replace ( "'" , "\'" , $comment ) ;
155 $rad = $postrad[$idx] ;
156 if ( !isset ( $val["{$oldtime}"][$idx] ) ) $val["{$oldtime}"][$idx] = "" ;
157 $val["{$oldtime}"][$idx]->val_value = $rad ;
158 $val["{$oldtime}"][$idx]->val_comment = $comment ;
159 if ( $rad != -1 )
160 {
161 # Store it in the database
162 $sql = "INSERT INTO validate (val_user,val_title,val_timestamp,val_type,val_value,val_comment) " .
163 "VALUES ( '" . $wgUser->getID() . "','{$article_title}','{$oldtime}','{$idx}','{$rad}','{$comment_sql}')" ;
164 if ( $rad != -1 ) wfQuery( $sql, DB_WRITE );
165 }
166 }
167 $wgArticle->showArticle( "Juhuu", wfMsg( 'val_validated' ) );
168 return ; # Show article instead of validation page
169 }
170
171 # Generating HTML
172 $html = "" ;
173
174 $skin = $wgUser->getSkin() ;
175 $staturl = $skin->makeSpecialURL ( "validate" , "mode=stat_page&article_title={$article_title}" ) ;
176 $listurl = $skin->makeSpecialURL ( "validate" , "mode=list_page" ) ;
177 $html .= "<a href=\"{$staturl}\">" . wfMsg('val_stat_link_text') . "</a> \n" ;
178 $html .= "<a href=\"{$listurl}\">" . wfMsg('val_article_lists') . "</a><br>\n" ;
179 $html .= "<small>" . wfMsg('val_form_note') . "</small><br>\n" ;
180
181 # Generating data tables
182 $tabsep = "<td width=0px style='border-left:2px solid black;'></td>" ;
183 $topstyle = "style='border-top:2px solid black'" ;
184 foreach ( $val AS $time => $stuff )
185 {
186 $tablestyle = "cellspacing=0 cellpadding=2" ;
187 if ( $article_time == $time ) $tablestyle .=" style='border: 2px solid red'" ;
188 $html .= "<h2>" . wfMsg( 'val_version_of', gmdate( "F d, Y H:i:s", wfTimestamp2Unix( $time ) ) ) ;
189 $this->find_this_version ( $article_title , $time , $table_id , $table_name ) ;
190 if ( $table_name == "cur" ) $html .= " (" . wfMsg ( 'val_this_is_current_version' ) . ")" ;
191 $html .= "</h2>\n" ;
192 $html .= "<form method=post>\n" ;
193 $html .= "<input type=hidden name=oldtime value='{$time}'>" ;
194 $html .= "<table {$tablestyle}>\n" ;
195 $html .= wfMsg( 'val_table_header', $tabsep ) ;
196 for ( $idx = 0 ; $idx < count ( $validationtypes) ; $idx++ )
197 {
198 $x = explode ( "|" , $validationtypes[$idx] , 4 ) ;
199 if ( isset ( $stuff[$idx] ) ) $choice = $stuff[$idx]->val_value ;
200 else $choice = -1 ;
201 if ( isset ( $stuff[$idx] ) ) $comment = $stuff[$idx]->val_comment ;
202 else $comment = "" ;
203 $html .= "<tr><th align=left>{$x[0]}</th>{$tabsep}<td align=right>{$x[1]}</td><td align=center>" ;
204 for ( $cnt = 0 ; $cnt < $x[3] ; $cnt++)
205 {
206 $html .= "<input type=radio name='rad{$idx}' value='{$cnt}'" ;
207 if ( $choice == $cnt ) $html .= " checked" ;
208 $html .= "> " ;
209 }
210 $html .= "</td><td>{$x[2]}</td>" ;
211 $html .= "<td><input type=radio name='rad{$idx}' value='-1'" ;
212 if ( $choice == -1 ) $html .= " checked" ;
213 $html .= "> " . wfMsg ( "val_noop" ) . "</td>{$tabsep}" ;
214 $html .= "<td><input type=text name='comment{$idx}' value='{$comment}'></td>" ;
215 $html .= "</tr>\n" ;
216 }
217 $html .= "<tr><td {$topstyle} colspan=2>" ;
218
219 # link to version
220 $title = Title::newFromDBkey ( $article_title ) ;
221 if ( $table_name == "cur" ) $link_version = $title->getLocalURL( "" ) ;
222 else $link_version = $title->getLocalURL( "oldid={$table_id}" ) ;
223 $link_version = "<a href=\"{$link_version}\">" . wfMsg ( 'val_view_version' ) . "</a>" ;
224 $html .= $link_version ;
225 $html .= "</td><td {$topstyle} colspan=5>" ;
226 $html .= "<input type=checkbox name=merge_other value=1 checked>" ;
227 $html .= wfMsg ( 'val_merge_old' );
228 $html .= "<br><input type=checkbox name=clear_other value=1 checked>" ;
229 $html .= wfMsg ( 'val_clear_old', $skin->makeKnownLinkObj( $article ) );
230 $html .= "</td><td {$topstyle} align=right valign=center><input type=submit name=doit value='" . wfMsg("ok") . "'></td>" ;
231 $html .= "</tr></table></form>\n" ;
232 }
233
234 $html .= "<h2>" . wfMsg ( 'preview' ) . "</h2>" ;
235 $wgOut->addHTML ( $html ) ;
236 $wgOut->addWikiText ( $wgArticle->getContent( true ) ) ;
237 }
238
239 function getData ( $user = -1 , $title = "" , $type = -1 )
240 {
241 $ret = array () ;
242 $sql = array () ;
243 if ( $user != -1 ) $sql[] = "val_user='{$user}'" ;
244 if ( $type != -1 ) $sql[] = "val_type='{$type}'" ;
245 if ( $title != "" ) $sql[] = "val_title='{$title}'" ;
246 $sql = implode ( " AND " , $sql ) ;
247 if ( $sql != "" ) $sql = " WHERE " . $sql ;
248 $sql = "SELECT * FROM validate" . $sql ;
249 $res = wfQuery( $sql, DB_READ );
250 while( $s = wfFetchObject( $res ) ) $ret["{$s->val_title}"]["{$s->val_timestamp}"]["{$s->val_type}"][] = $s ;
251 return $ret ;
252 }
253
254 # Show statistics for the different versions of a single article
255 function getPageStatistics ( $article_title = "" )
256 {
257 global $wgLang, $wgUser ;
258 $validationtypes = $wgLang->getValidationTypes() ;
259 if ( $article_title == "" ) $article_title = $_GET['article_title'] ;
260 $d = $this->getData ( -1 , $article_title , -1 ) ;
261 if ( count ( $d ) ) $d = array_shift ( $d ) ;
262 else $d = array () ;
263 krsort ( $d ) ;
264
265 # Getting table data (cur_id, old_id etc.) for each version
266 $table_id = array() ;
267 $table_name = array() ;
268 foreach ( $d AS $version => $data )
269 {
270 $this->find_this_version ( $article_title , $version , $table_id[$version] , $table_name[$version] ) ;
271 }
272
273 # Generating HTML
274 $html = "<h1>Page validation statistics</h1>\n" ;
275
276 $skin = $wgUser->getSkin() ;
277 $listurl = $skin->makeSpecialURL ( "validate" , "mode=list_page" ) ;
278 $html .= "<a href=\"{$listurl}\">" . wfMsg('val_article_lists') . "</a><br><br>\n" ;
279
280 $html .= "<table border=1 cellpadding=2 style='font-size:8pt;'>\n" ;
281 $html .= "<tr><th>" . wfMsg('val_version') . "</th>" ;
282 foreach ( $validationtypes AS $idx => $title )
283 {
284 $title = explode ( "|" , $title ) ;
285 $html .= "<th>{$title[0]}</th>" ;
286 }
287 $html .= "<th>" . wfMsg('val_total') . "</th>" ;
288 $html .= "</tr>\n" ;
289 foreach ( $d AS $version => $data )
290 {
291 # Preamble for this version
292 $title = Title::newFromDBkey ( $article_title ) ;
293 $version_date = gmdate("F d, Y H:i:s",wfTimestamp2Unix($version)) ;
294 $version_validate_link = $title->getLocalURL( "action=validate&timestamp={$version}" ) ;
295 $version_validate_link = "<a class=intern href=\"{$version_validate_link}\">" . wfMsg('val_validate_version') . "</a>" ;
296 if ( $table_name[$version] == 'cur' ) $version_view_link = $title->getLocalURL( "" ) ;
297 else $version_view_link = $title->getLocalURL( "oldid={$table_id[$version]}" ) ;
298 $version_view_link = "<a href=\"{$version_view_link}\">" . wfMsg('val_view_version') . "</a>" ;
299 $html .= "<tr>" ;
300 $html .= "<td align=center valign=top nowrap><b>{$version_date}</b><br>{$version_view_link}<br>{$version_validate_link}</td>" ;
301
302 # Individual data
303 $vmax = array() ;
304 $vcur = array() ;
305 $users = array() ;
306 foreach ( $data AS $type => $x2 )
307 {
308 if ( !isset ( $vcur[$type] ) ) $vcur[$type] = 0 ;
309 if ( !isset ( $vmax[$type] ) ) $vmax[$type] = 0 ;
310 if ( !isset ( $users[$type] ) ) $users[$type] = 0 ;
311 foreach ( $x2 AS $user => $x )
312 {
313 $vcur[$type] += $x->val_value ;
314 $temp = explode ( "|" , $validationtypes[$type]) ;
315 $vmax[$type] += $temp[3] - 1 ;
316 $users[$type] += 1 ;
317 }
318 }
319
320 $total_count = 0 ;
321 $total_percent = 0 ;
322 foreach ( $validationtypes AS $idx => $title )
323 {
324 if ( isset ( $vcur[$idx] ) )
325 {
326 $average = 100 * $vcur[$idx] / $vmax[$idx] ;
327 $total_count += 1 ;
328 $total_percent += $average ;
329 if ( $users[$idx] > 1 ) $msgid = "val_percent" ;
330 else $msgid = "val_percent_single" ;
331 $html .= "<td align=center valign=top>" .
332 wfMsg ( $msgid, number_format ( $average , 2 ) ,
333 $vcur[$idx] , $vmax[$idx] , $users[$idx] ) ;
334 }
335 else
336 {
337 $html .= "<td align=center valign=center>" ;
338 $html .= "(" . wfMsg ( "val_noop" ) . ")" ;
339 }
340 $html .= "</td>" ;
341 }
342
343 if ( $total_count > 0 )
344 {
345 $total = $total_percent / $total_count ;
346 $total = number_format ( $total , 2 ) . " %" ;
347 }
348 else $total = "" ;
349 $html .= "<td align=center valign=top nowrap><b>{$total}</b></td>" ;
350
351 $html .= "</tr>" ;
352 }
353 $html .= "</table>\n" ;
354 return $html ;
355 }
356
357 function countUserValidations ( $userid )
358 {
359 $sql = "SELECT count(DISTINCT val_title) AS num FROM validate WHERE val_user={$userid}" ;
360 $res = wfQuery( $sql, DB_READ );
361 if ( $s = wfFetchObject( $res ) ) $num = $s->num ;
362 else $num = 0 ;
363 return $num ;
364 }
365
366 function getArticleList ()
367 {
368 }
369
370 }
371
372 function wfSpecialValidate( $page = "" )
373 {
374 global $wgOut ;
375 if ( isset ( $_GET['mode'] ) ) $mode = $_GET['mode'] ;
376 else $mode = "form" ;
377 $v = new Validation ;
378 $html = "" ;
379 /* if ( $mode == "form" )
380 {
381 $html = $v->validate_form () ;
382 }
383 else */
384 if ( $mode == "stat_page" )
385 {
386 $html = $v->getPageStatistics () ;
387 }
388 else if ( $mode == "list_page" )
389 {
390 $html = $v->getArticleList () ;
391 }
392
393 $wgOut->addHTML( $html ) ;
394 }
395
396 ?>