Article validation code (article preview 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 $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
212 global $wgArticle ;
213 $html .= "<h2>" . wfMsg ( 'preview' ) . "</h2>" ;
214 $wgOut->addHTML ( $html ) ;
215 $wgOut->addWikiText ( $wgArticle->getContent( true ) ) ;
216 return "" ;
217
218 # $html .= $wgArticle->getContent( true ) ;
219 # $html .= $wgArticle->view () ;
220 # return $html ;
221 }
222
223 function getData ( $user = -1 , $title = "" , $type = -1 )
224 {
225 $ret = array () ;
226 $sql = array () ;
227 if ( $user != -1 ) $sql[] = "val_user='{$user}'" ;
228 if ( $type != -1 ) $sql[] = "val_type='{$type}'" ;
229 if ( $title != "" ) $sql[] = "val_title='{$title}'" ;
230 $sql = implode ( " AND " , $sql ) ;
231 if ( $sql != "" ) $sql = " WHERE " . $sql ;
232 $sql = "SELECT * FROM validate" . $sql ;
233 $res = wfQuery( $sql, DB_READ );
234 while( $s = wfFetchObject( $res ) ) $ret["{$s->val_title}"]["{$s->val_timestamp}"]["{$s->val_type}"][] = $s ;
235 return $ret ;
236 }
237
238 # Show statistics for the different versions of a single article
239 function getPageStatistics ( $article_title = "" )
240 {
241 global $wgLang, $wgUser ;
242 $validationtypes = $wgLang->getValidationTypes() ;
243 $article_title = $_GET['article_title'] ;
244 $d = $this->getData ( -1 , $article_title , -1 ) ;
245 if ( count ( $d ) ) $d = array_shift ( $d ) ;
246 else $d = array () ;
247 krsort ( $d ) ;
248
249 # Getting table data (cur_id, old_id etc.) for each version
250 $table_id = array() ;
251 $table_name = array() ;
252 foreach ( $d AS $version => $data )
253 {
254 $this->find_this_version ( $article_title , $version , $table_id[$version] , $table_name[$version] ) ;
255 }
256
257 # Generating HTML
258 $html = "<h1>Page validation statistics</h1>\n" ;
259 $html .= "<table border=1 cellpadding=2 style='font-size:8pt;'>\n" ;
260 $html .= "<tr><th>" . wfMsg('val_version') . "</th>" ;
261 foreach ( $validationtypes AS $idx => $title )
262 {
263 $title = explode ( "|" , $title ) ;
264 $html .= "<th>{$title[0]}</th>" ;
265 }
266 $html .= "<th>" . wfMsg('val_total') . "</th>" ;
267 $html .= "</tr>\n" ;
268 foreach ( $d AS $version => $data )
269 {
270 # Preamble for this version
271 $title = Title::newFromDBkey ( $article_title ) ;
272 $version_date = gmdate("F d, Y H:i:s",wfTimestamp2Unix($version)) ;
273 $version_validate_link = $title->getLocalURL( "action=validate&timestamp={$version}" ) ;
274 $version_validate_link = "<a class=intern href=\"{$version_validate_link}\">" . wfMsg('val_validate_version') . "</a>" ;
275 if ( $table_name[$version] == 'cur' ) $version_view_link = $title->getLocalURL( "" ) ;
276 else $version_view_link = $title->getLocalURL( "oldid={$table_id[$version]}" ) ;
277 $version_view_link = "<a href=\"{$version_view_link}\">" . wfMsg('val_view_version') . "</a>" ;
278 $html .= "<tr>" ;
279 $html .= "<td align=center valign=top nowrap><b>{$version_date}</b><br>{$version_view_link}<br>{$version_validate_link}</td>" ;
280
281 # Individual data
282 $vmax = array() ;
283 $vcur = array() ;
284 $users = array() ;
285 foreach ( $data AS $type => $x2 )
286 {
287 if ( !isset ( $vcur[$type] ) ) $vcur[$type] = 0 ;
288 if ( !isset ( $vmax[$type] ) ) $vmax[$type] = 0 ;
289 if ( !isset ( $users[$type] ) ) $users[$type] = 0 ;
290 foreach ( $x2 AS $user => $x )
291 {
292 $vcur[$type] += $x->val_value ;
293 $temp = explode ( "|" , $validationtypes[$type]) ;
294 $vmax[$type] += $temp[3] - 1 ;
295 $users[$type] += 1 ;
296 }
297 }
298
299 $total_count = 0 ;
300 $total_percent = 0 ;
301 foreach ( $validationtypes AS $idx => $title )
302 {
303 if ( isset ( $vcur[$idx] ) )
304 {
305 $average = 100 * $vcur[$idx] / $vmax[$idx] ;
306 $total_count += 1 ;
307 $total_percent += $average ;
308 if ( $users[$idx] > 1 ) $h = wfMsg ( "val_percent" ) ;
309 else $h = wfMsg ( "val_percent_single" ) ;
310 $h = str_replace ( "$1" , number_format ( $average , 2 ) , $h ) ;
311 $h = str_replace ( "$2" , $vcur[$idx] , $h ) ;
312 $h = str_replace ( "$3" , $vmax[$idx] , $h ) ;
313 $h = str_replace ( "$4" , $users[$idx] , $h ) ;
314 $html .= "<td align=center valign=top>" . $h ;
315 }
316 else
317 {
318 $html .= "<td align=center valign=center>" ;
319 $html .= "(" . wfMsg ( "val_noop" ) . ")" ;
320 }
321 $html .= "</td>" ;
322 }
323
324 if ( $total_count > 0 )
325 {
326 $total = $total_percent / $total_count ;
327 $total = number_format ( $total , 2 ) . " %" ;
328 }
329 else $total = "" ;
330 $html .= "<td align=center valign=top nowrap><b>{$total}</b></td>" ;
331
332 $html .= "</tr>" ;
333 }
334 $html .= "</table>\n" ;
335 return $html ;
336 }
337
338 function countUserValidations ( $userid )
339 {
340 $sql = "SELECT count(DISTINCT val_title) AS num FROM validate WHERE val_user={$userid}" ;
341 $res = wfQuery( $sql, DB_READ );
342 if ( $s = wfFetchObject( $res ) ) $num = $s->num ;
343 else $num = 0 ;
344 return $num ;
345 }
346
347 }
348
349 function wfSpecialValidate( $page = "" )
350 {
351 global $wgOut ;
352 if ( isset ( $_GET['mode'] ) ) $mode = $_GET['mode'] ;
353 else $mode = "form" ;
354 $v = new Validation ;
355 $html = "" ;
356 if ( $mode == "form" )
357 {
358 $html = $v->validate_form () ;
359 }
360 else if ( $mode == "stat_page" )
361 {
362 $html = $v->getPageStatistics () ;
363 }
364
365 $wgOut->addHTML( $html ) ;
366 }
367
368 ?>