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