Since nobody commented on this, I assume all the changes are OK
[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
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 }
168
169 # Generating HTML
170 $html = "" ;
171
172 $skin = $wgUser->getSkin() ;
173 $staturl = $skin->makeSpecialURL ( "validate" , "mode=stat_page&article_title={$article_title}" ) ;
174 $html .= "<a href=\"{$staturl}\">" . wfMsg('val_stat_link_text') . "</a><br>\n" ;
175 $html .= "<small>" . wfMsg('val_form_note') . "</small><br>\n" ;
176
177 # Generating data tables
178 $tabsep = "<td width=0px style='border-left:2px solid black;'></td>" ;
179 $topstyle = "style='border-top:2px solid black'" ;
180 foreach ( $val AS $time => $stuff )
181 {
182 $tablestyle = "cellspacing=0 cellpadding=2" ;
183 if ( $article_time == $time ) $tablestyle .=" style='border: 2px solid red'" ;
184 $html .= "<h2>" . wfMsg( 'val_version_of', gmdate( "F d, Y H:i:s", wfTimestamp2Unix( $time ) ) ) ;
185 $this->find_this_version ( $article_title , $time , $table_id , $table_name ) ;
186 if ( $table_name == "cur" ) $html .= " (" . wfMsg ( 'val_this_is_current_version' ) . ")" ;
187 $html .= "</h2>\n" ;
188 $html .= "<form method=post>\n" ;
189 $html .= "<input type=hidden name=oldtime value='{$time}'>" ;
190 $html .= "<table {$tablestyle}>\n" ;
191 $html .= wfMsg( 'val_table_header', $tabsep ) ;
192 for ( $idx = 0 ; $idx < count ( $validationtypes) ; $idx++ )
193 {
194 $x = explode ( "|" , $validationtypes[$idx] , 4 ) ;
195 if ( isset ( $stuff[$idx] ) ) $choice = $stuff[$idx]->val_value ;
196 else $choice = -1 ;
197 if ( isset ( $stuff[$idx] ) ) $comment = $stuff[$idx]->val_comment ;
198 else $comment = "" ;
199 $html .= "<tr><th align=left>{$x[0]}</th>{$tabsep}<td align=right>{$x[1]}</td><td align=center>" ;
200 for ( $cnt = 0 ; $cnt < $x[3] ; $cnt++)
201 {
202 $html .= "<input type=radio name='rad{$idx}' value='{$cnt}'" ;
203 if ( $choice == $cnt ) $html .= " checked" ;
204 $html .= "> " ;
205 }
206 $html .= "</td><td>{$x[2]}</td>" ;
207 $html .= "<td><input type=radio name='rad{$idx}' value='-1'" ;
208 if ( $choice == -1 ) $html .= " checked" ;
209 $html .= "> " . wfMsg ( "val_noop" ) . "</td>{$tabsep}" ;
210 $html .= "<td><input type=text name='comment{$idx}' value='{$comment}'></td>" ;
211 $html .= "</tr>\n" ;
212 }
213 $html .= "<tr><td {$topstyle} colspan=2>" ;
214
215 # link to version
216 $title = Title::newFromDBkey ( $article_title ) ;
217 if ( $table_name == "cur" ) $link_version = $title->getLocalURL( "" ) ;
218 else $link_version = $title->getLocalURL( "oldid={$table_id}" ) ;
219 $link_version = "<a href=\"{$link_version}\">" . wfMsg ( 'val_view_version' ) . "</a>" ;
220 $html .= $link_version ;
221 $html .= "</td><td {$topstyle} colspan=5>" ;
222 $html .= "<input type=checkbox name=merge_other value=1 checked>" ;
223 $html .= wfMsg ( 'val_merge_old' );
224 $html .= "<br><input type=checkbox name=clear_other value=1 checked>" ;
225 $html .= wfMsg ( 'val_clear_old', $skin->makeKnownLinkObj( $article ) );
226 $html .= "</td><td {$topstyle} align=right valign=center><input type=submit name=doit value='" . wfMsg("ok") . "'></td>" ;
227 $html .= "</tr></table></form>\n" ;
228 }
229
230 global $wgArticle ;
231 $html .= "<h2>" . wfMsg ( 'preview' ) . "</h2>" ;
232 $wgOut->addHTML ( $html ) ;
233 $wgOut->addWikiText ( $wgArticle->getContent( true ) ) ;
234 }
235
236 function getData ( $user = -1 , $title = "" , $type = -1 )
237 {
238 $ret = array () ;
239 $sql = array () ;
240 if ( $user != -1 ) $sql[] = "val_user='{$user}'" ;
241 if ( $type != -1 ) $sql[] = "val_type='{$type}'" ;
242 if ( $title != "" ) $sql[] = "val_title='{$title}'" ;
243 $sql = implode ( " AND " , $sql ) ;
244 if ( $sql != "" ) $sql = " WHERE " . $sql ;
245 $sql = "SELECT * FROM validate" . $sql ;
246 $res = wfQuery( $sql, DB_READ );
247 while( $s = wfFetchObject( $res ) ) $ret["{$s->val_title}"]["{$s->val_timestamp}"]["{$s->val_type}"][] = $s ;
248 return $ret ;
249 }
250
251 # Show statistics for the different versions of a single article
252 function getPageStatistics ( $article_title = "" )
253 {
254 global $wgLang, $wgUser ;
255 $validationtypes = $wgLang->getValidationTypes() ;
256 if ( $article_title == "" ) $article_title = $_GET['article_title'] ;
257 $d = $this->getData ( -1 , $article_title , -1 ) ;
258 if ( count ( $d ) ) $d = array_shift ( $d ) ;
259 else $d = array () ;
260 krsort ( $d ) ;
261
262 # Getting table data (cur_id, old_id etc.) for each version
263 $table_id = array() ;
264 $table_name = array() ;
265 foreach ( $d AS $version => $data )
266 {
267 $this->find_this_version ( $article_title , $version , $table_id[$version] , $table_name[$version] ) ;
268 }
269
270 # Generating HTML
271 $html = "<h1>Page validation statistics</h1>\n" ;
272 $html .= "<table border=1 cellpadding=2 style='font-size:8pt;'>\n" ;
273 $html .= "<tr><th>" . wfMsg('val_version') . "</th>" ;
274 foreach ( $validationtypes AS $idx => $title )
275 {
276 $title = explode ( "|" , $title ) ;
277 $html .= "<th>{$title[0]}</th>" ;
278 }
279 $html .= "<th>" . wfMsg('val_total') . "</th>" ;
280 $html .= "</tr>\n" ;
281 foreach ( $d AS $version => $data )
282 {
283 # Preamble for this version
284 $title = Title::newFromDBkey ( $article_title ) ;
285 $version_date = gmdate("F d, Y H:i:s",wfTimestamp2Unix($version)) ;
286 $version_validate_link = $title->getLocalURL( "action=validate&timestamp={$version}" ) ;
287 $version_validate_link = "<a class=intern href=\"{$version_validate_link}\">" . wfMsg('val_validate_version') . "</a>" ;
288 if ( $table_name[$version] == 'cur' ) $version_view_link = $title->getLocalURL( "" ) ;
289 else $version_view_link = $title->getLocalURL( "oldid={$table_id[$version]}" ) ;
290 $version_view_link = "<a href=\"{$version_view_link}\">" . wfMsg('val_view_version') . "</a>" ;
291 $html .= "<tr>" ;
292 $html .= "<td align=center valign=top nowrap><b>{$version_date}</b><br>{$version_view_link}<br>{$version_validate_link}</td>" ;
293
294 # Individual data
295 $vmax = array() ;
296 $vcur = array() ;
297 $users = array() ;
298 foreach ( $data AS $type => $x2 )
299 {
300 if ( !isset ( $vcur[$type] ) ) $vcur[$type] = 0 ;
301 if ( !isset ( $vmax[$type] ) ) $vmax[$type] = 0 ;
302 if ( !isset ( $users[$type] ) ) $users[$type] = 0 ;
303 foreach ( $x2 AS $user => $x )
304 {
305 $vcur[$type] += $x->val_value ;
306 $temp = explode ( "|" , $validationtypes[$type]) ;
307 $vmax[$type] += $temp[3] - 1 ;
308 $users[$type] += 1 ;
309 }
310 }
311
312 $total_count = 0 ;
313 $total_percent = 0 ;
314 foreach ( $validationtypes AS $idx => $title )
315 {
316 if ( isset ( $vcur[$idx] ) )
317 {
318 $average = 100 * $vcur[$idx] / $vmax[$idx] ;
319 $total_count += 1 ;
320 $total_percent += $average ;
321 if ( $users[$idx] > 1 ) $msgid = "val_percent" ;
322 else $msgid = "val_percent_single" ;
323 $html .= "<td align=center valign=top>" .
324 wfMsg ( $msgid, number_format ( $average , 2 ) ,
325 $vcur[$idx] , $vmax[$idx] , $users[$idx] ) ;
326 }
327 else
328 {
329 $html .= "<td align=center valign=center>" ;
330 $html .= "(" . wfMsg ( "val_noop" ) . ")" ;
331 }
332 $html .= "</td>" ;
333 }
334
335 if ( $total_count > 0 )
336 {
337 $total = $total_percent / $total_count ;
338 $total = number_format ( $total , 2 ) . " %" ;
339 }
340 else $total = "" ;
341 $html .= "<td align=center valign=top nowrap><b>{$total}</b></td>" ;
342
343 $html .= "</tr>" ;
344 }
345 $html .= "</table>\n" ;
346 return $html ;
347 }
348
349 function countUserValidations ( $userid )
350 {
351 $sql = "SELECT count(DISTINCT val_title) AS num FROM validate WHERE val_user={$userid}" ;
352 $res = wfQuery( $sql, DB_READ );
353 if ( $s = wfFetchObject( $res ) ) $num = $s->num ;
354 else $num = 0 ;
355 return $num ;
356 }
357
358 }
359
360 function wfSpecialValidate( $page = "" )
361 {
362 global $wgOut ;
363 if ( isset ( $_GET['mode'] ) ) $mode = $_GET['mode'] ;
364 else $mode = "form" ;
365 $v = new Validation ;
366 $html = "" ;
367 if ( $mode == "form" )
368 {
369 $html = $v->validate_form () ;
370 }
371 else if ( $mode == "stat_page" )
372 {
373 $html = $v->getPageStatistics () ;
374 }
375
376 $wgOut->addHTML( $html ) ;
377 }
378
379 ?>