Article validation code
[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 if ( isset ( $_POST['clear_other'] ) && $_POST['clear_other'] == 1 ) # Clear all others
98 {
99 $sql = "DELETE FROM validate WHERE val_title='{$article_title}' AND val_timestamp<>'{$oldtime}' AND val_user='" ;
100 $sql .= $wgUser->getID() . "'" ;
101 wfQuery( $sql, DB_WRITE );
102 $val2 = $val["{$oldtime}"] ; # Only version left
103 $val = array () ; # So clear others
104 $val["{$oldtime}"] = $val2 ;
105 }
106
107 # Delete old "votes" for this version
108 $sql = "DELETE FROM validate WHERE val_title='{$article_title}' AND val_timestamp='{$oldtime}' AND val_user='" ;
109 $sql .= $wgUser->getID() . "'" ;
110 wfQuery( $sql, DB_WRITE );
111
112 for ( $idx = 0 ; $idx < count ( $validationtypes) ; $idx++ ) # Changes
113 {
114 $comment = $_POST["comment{$idx}"] ;
115 $comment_sql = str_replace ( "'" , "\'" , $comment ) ;
116 $rad = $_POST["rad{$idx}"] ;
117 if ( !isset ( $val["{$oldtime}"][$idx] ) ) $val["{$oldtime}"][$idx] = "" ;
118 $val["{$oldtime}"][$idx]->val_value = $rad ;
119 $val["{$oldtime}"][$idx]->val_comment = $comment ;
120 if ( $rad != -1 )
121 {
122 # Store it in the database
123 $sql = "INSERT INTO validate (val_user,val_title,val_timestamp,val_type,val_value,val_comment) " .
124 "VALUES ( '" . $wgUser->getID() . "','{$article_title}','{$oldtime}','{$idx}','{$rad}','{$comment_sql}')" ;
125 if ( $rad != -1 ) wfQuery( $sql, DB_WRITE );
126 }
127 }
128 }
129
130 # Generating HTML
131 $html = "" ;
132
133 $skin = $wgUser->getSkin() ;
134 $staturl = $skin->makeSpecialURL ( "validate" , "mode=stat_page&article_title={$article_title}" ) ;
135 $html .= "<a href=\"{$staturl}\">" . wfMsg('val_stat_link_text') . "</a>" ;
136
137 $tabsep = "<td width=0px style='border-left:2px solid black;'></td>" ;
138 $topstyle = "style='border-top:2px solid black'" ;
139 foreach ( $val AS $time => $stuff )
140 {
141 $tablestyle = "cellspacing=0 cellpadding=2" ;
142 if ( $article_time == $time ) $tablestyle .=" style='border: 2px solid red'" ;
143 $html .= str_replace ( "$1" , gmdate("F d, Y H:i:s",wfTimestamp2Unix($time)) , wfMsg("val_version_of") ) ;
144 $html .= "<form method=post>\n" ;
145 $html .= "<input type=hidden name=oldtime value='{$time}'>" ;
146 $html .= "<table {$tablestyle}>\n" ;
147 $html .= str_replace ( "$1" , $tabsep , wfMsg("val_table_header") ) ;
148 for ( $idx = 0 ; $idx < count ( $validationtypes) ; $idx++ )
149 {
150 $x = explode ( "|" , $validationtypes[$idx] , 4 ) ;
151 if ( isset ( $stuff[$idx] ) ) $choice = $stuff[$idx]->val_value ;
152 else $choice = -1 ;
153 if ( isset ( $stuff[$idx] ) ) $comment = $stuff[$idx]->val_comment ;
154 else $comment = "" ;
155 $html .= "<tr><th align=left>{$x[0]}</th>{$tabsep}<td align=right>{$x[1]}</td><td align=center>" ;
156 for ( $cnt = 0 ; $cnt < $x[3] ; $cnt++)
157 {
158 $html .= "<input type=radio name='rad{$idx}' value='{$cnt}'" ;
159 if ( $choice == $cnt ) $html .= " checked" ;
160 $html .= "> " ;
161 }
162 $html .= "</td><td>{$x[2]}</td>" ;
163 $html .= "<td><input type=radio name='rad{$idx}' value='-1'" ;
164 if ( $choice == -1 ) $html .= " checked" ;
165 $html .= "> " . wfMsg ( "val_noop" ) . "</td>{$tabsep}" ;
166 $html .= "<td><input type=text name='comment{$idx}' value='{$comment}'></td>" ;
167 $html .= "</tr>\n" ;
168 }
169 $html .= "<tr><td {$topstyle} colspan=2></td><td {$topstyle} colspan=3><input type=checkbox name=clear_other value=1 checked>" ;
170 $html .= str_replace ( "$1" , $article->getLocalURL() , wfMsg("val_clear_old") ) ;
171 $html .= "</td><td {$topstyle} align=right><input type=submit name=doit value='" . wfMsg("ok") . "'></td>" ;
172 $html .= "<td {$topstyle} colspan=2></td></tr></table></form>\n" ;
173 }
174 return $html ;
175 }
176
177 function getData ( $user = -1 , $title = "" , $type = -1 )
178 {
179 $ret = array () ;
180 $sql = array () ;
181 if ( $user != -1 ) $sql[] = "val_user='{$user}'" ;
182 if ( $type != -1 ) $sql[] = "val_type='{$type}'" ;
183 if ( $title != "" ) $sql[] = "val_title='{$title}'" ;
184 $sql = implode ( " AND " , $sql ) ;
185 if ( $sql != "" ) $sql = " WHERE " . $sql ;
186 $sql = "SELECT * FROM validate" . $sql ;
187 $res = wfQuery( $sql, DB_READ );
188 while( $s = wfFetchObject( $res ) ) $ret["{$s->val_title}"]["{$s->val_timestamp}"]["{$s->val_type}"][] = $s ;
189 return $ret ;
190 }
191
192 function getPageStatistics ( $article_title = "" )
193 {
194 global $wgLang, $wgUser ;
195 $validationtypes = $wgLang->getValidationTypes() ;
196 $article_title = $_GET['article_title'] ;
197 $html = "<h1>Page validation statistics</h1>\n" ;
198 $d = $this->getData ( -1 , $article_title , -1 ) ;
199 if ( count ( $d ) ) $d = array_shift ( $d ) ;
200 else $d = array () ;
201 krsort ( $d ) ;
202 $html .= "<table border=1 cellpadding=2 style='font-size:8pt;'>\n" ;
203 $html .= "<tr><th>" . wfMsg('val_version') . "</th>" ;
204 foreach ( $validationtypes AS $idx => $title )
205 {
206 $title = explode ( "|" , $title ) ;
207 $html .= "<th>{$title[0]}</th>" ;
208 }
209 $html .= "<th>" . wfMsg('val_total') . "</th>" ;
210 $html .= "</tr>\n" ;
211 foreach ( $d AS $version => $data )
212 {
213 $title = Title::newFromDBkey ( $article_title ) ;
214 $version_link = $title->getLocalURL( "action=validate&timestamp={$version}" ) ;
215 $version_link = "<a class=intern href=\"{$version_link}\">" . gmdate("F d, Y H:i:s",wfTimestamp2Unix($version)) . "</a>" ;
216 $html .= "<tr>" ;
217 $html .= "<th align=left>{$version_link}</th>" ;
218
219 $vmax = array() ;
220 $vcur = array() ;
221 $users = array() ;
222 foreach ( $data AS $type => $x2 )
223 {
224 if ( !isset ( $vcur[$type] ) ) $vcur[$type] = 0 ;
225 if ( !isset ( $vmax[$type] ) ) $vmax[$type] = 0 ;
226 if ( !isset ( $users[$type] ) ) $users[$type] = 0 ;
227 foreach ( $x2 AS $user => $x )
228 {
229 $vcur[$type] += $x->val_value ;
230 $temp = explode ( "|" , $validationtypes[$type]) ;
231 $vmax[$type] += $temp[3] - 1 ;
232 $users[$type] += 1 ;
233 }
234 }
235
236
237 $total_count = 0 ;
238 $total_percent = 0 ;
239 foreach ( $validationtypes AS $idx => $title )
240 {
241 $html .= "<td align=center valign=center>" ;
242 if ( isset ( $vcur[$idx] ) )
243 {
244 $average = 100 * $vcur[$idx] / $vmax[$idx] ;
245 $total_count += 1 ;
246 $total_percent += $average ;
247 if ( $users[$idx] > 1 ) $h = wfMsg ( "val_percent" ) ;
248 else $h = wfMsg ( "val_percent_single" ) ;
249 $h = str_replace ( "$1" , $average , $h ) ;
250 $h = str_replace ( "$2" , $vcur[$idx] , $h ) ;
251 $h = str_replace ( "$3" , $vmax[$idx] , $h ) ;
252 $h = str_replace ( "$4" , $users[$idx] , $h ) ;
253 $html .= $h ;
254 }
255 else $html .= "(" . wfMsg ( "val_noop" ) . ")" ;
256 $html .= "</td>" ;
257 }
258
259 if ( $total_count > 0 )
260 {
261 $total = $total_percent / $total_count ;
262 $total = "{$total} %" ;
263 }
264 else $total = "" ;
265 $html .= "<td align=center valign=center>{$total}</td>" ;
266
267 $html .= "</tr>" ;
268 }
269 $html .= "</table>\n" ;
270 return $html ;
271 }
272
273 }
274
275 function wfSpecialValidate( $page = "" )
276 {
277 global $wgOut ;
278 if ( isset ( $_GET['mode'] ) ) $mode = $_GET['mode'] ;
279 else $mode = "form" ;
280 $v = new Validation ;
281 $html = "" ;
282 if ( $mode == "form" )
283 {
284 $html = $v->validate_form () ;
285 }
286 else if ( $mode == "stat_page" )
287 {
288 $html = $v->getPageStatistics () ;
289 }
290
291 $wgOut->addHTML( $html ) ;
292 }
293
294 ?>