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