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'] ;
68 # $heading = "<h1>" . $article->getPrefixedText() . "</h1>\n" ;
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 , $article_time ) ;
79
80 # No votes for this version, initial data
81 if ( count ( $val ) == 0 )
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 # User has clicked "Doit" before, so evaluate form
92 if ( isset ( $_POST['doit'] ) )
93 {
94 $oldtime = $_POST['oldtime'] ;
95 if ( !isset ( $val["{$oldtime}"] ) ) $val["{$oldtime}"] = array () ;
96 if ( isset ( $_POST['clear_other'] ) && $_POST['clear_other'] == 1 ) # Clear all others
97 {
98 $sql = "DELETE FROM validate WHERE val_title='{$article_title}' AND val_timestamp<>'{$oldtime}' AND val_user='" ;
99 $sql .= $wgUser->getID() . "'" ;
100 wfQuery( $sql, DB_WRITE );
101 $val2 = $val["{$oldtime}"] ; # Only version left
102 $val = array () ; # So clear others
103 $val["{$oldtime}"] = $val2 ;
104 }
105
106 # Delete old "votes" for this version
107 $sql = "DELETE FROM validate WHERE val_title='{$article_title}' AND val_timestamp='{$oldtime}' AND val_user='" ;
108 $sql .= $wgUser->getID() . "'" ;
109 wfQuery( $sql, DB_WRITE );
110
111 for ( $idx = 0 ; $idx < count ( $validationtypes) ; $idx++ ) # Changes
112 {
113 $comment = $_POST["comment{$idx}"] ;
114 $comment_sql = str_replace ( "'" , "\'" , $comment ) ;
115 $rad = $_POST["rad{$idx}"] ;
116 if ( !isset ( $val["{$oldtime}"][$idx] ) ) $val["{$oldtime}"][$idx] = "" ;
117 $val["{$oldtime}"][$idx]->val_value = $rad ;
118 $val["{$oldtime}"][$idx]->val_comment = $comment ;
119 if ( $rad != -1 )
120 {
121 # Store it in the database
122 $sql = "INSERT INTO validate (val_user,val_title,val_timestamp,val_type,val_value,val_comment) " .
123 "VALUES ( '" . $wgUser->getID() . "','{$article_title}','{$oldtime}','{$idx}','{$rad}','{$comment_sql}')" ;
124 if ( $rad != -1 ) wfQuery( $sql, DB_WRITE );
125 }
126 }
127 }
128
129 # Generating HTML
130 $html = $heading ;
131 $tabsep = "<td width=0px style='border-left:2px solid black;'></td>" ;
132 $topstyle = "style='border-top:2px solid black'" ;
133 foreach ( $val AS $time => $stuff )
134 {
135 if ( $time != $article_time ) $html .= wfMsg("val_this_version") ;
136 else $html .= str_replace ( "$1" , gmdate("F d, Y H:i:s",wfTimestamp2Unix($time)) , wfMsg("val_version_of") ) ;
137 $html .= "<form method=post>\n" ;
138 $html .= "<input type=hidden name=oldtime value='{$time}'>" ;
139 $html .= "<table cellspacing=0 cellpadding=2>\n" ;
140 $html .= str_replace ( "$1" , $tabsep , wfMsg("val_table_header") ) ;
141 for ( $idx = 0 ; $idx < count ( $validationtypes) ; $idx++ )
142 {
143 $x = explode ( "|" , $validationtypes[$idx] , 4 ) ;
144 if ( isset ( $stuff[$idx] ) ) $choice = $stuff[$idx]->val_value ;
145 else $choice = -1 ;
146 if ( isset ( $stuff[$idx] ) ) $comment = $stuff[$idx]->val_comment ;
147 else $comment = "" ;
148 $html .= "<tr><th align=left>{$x[0]}</th>{$tabsep}<td align=right>{$x[1]}</td><td align=center>" ;
149 for ( $cnt = 0 ; $cnt < $x[3] ; $cnt++)
150 {
151 $html .= "<input type=radio name='rad{$idx}' value='{$cnt}'" ;
152 if ( $choice == $cnt ) $html .= " checked" ;
153 $html .= "> " ;
154 }
155 $html .= "</td><td>{$x[2]}</td>" ;
156 $html .= "<td><input type=radio name='rad{$idx}' value='-1'" ;
157 if ( $choice == -1 ) $html .= " checked" ;
158 $html .= "> " . wfMsg ( "val_noop" ) . "</td>{$tabsep}" ;
159 $html .= "<td><input type=text name='comment{$idx}' value='{$comment}'></td>" ;
160 $html .= "</tr>\n" ;
161 }
162 $html .= "<tr><td {$topstyle} colspan=2></td><td {$topstyle} colspan=3><input type=checkbox name=clear_other value=1 checked>" ;
163 $html .= str_replace ( "$1" , $article->getPrefixedURL() , wfMsg("val_clear_old") ) ;
164 $html .= "</td><td {$topstyle} align=right><input type=submit name=doit value='" . wfMsg("ok") . "'></td>" ;
165 $html .= "<td {$topstyle} colspan=2></td></tr></table></form>\n" ;
166 }
167 return $html ;
168 }
169
170 function getData ( $user = -1 , $title = "" , $type = -1 )
171 {
172 $ret = array () ;
173 $sql = array () ;
174 if ( $user != -1 ) $sql[] = "val_user='{$user}'" ;
175 if ( $type != -1 ) $sql[] = "val_type='{$type}'" ;
176 if ( $title != "" ) $sql[] = "val_title='{$title}'" ;
177 $sql = implode ( " AND " , $sql ) ;
178 if ( $sql != "" ) $sql = " WHERE " . $sql ;
179 $sql = "SELECT * FROM validate" . $sql ;
180 $res = wfQuery( $sql, DB_READ );
181 while( $s = wfFetchObject( $res ) ) $ret["{$s->val_title}"]["{$s->val_timestamp}"]["{$s->val_type}"] = $s ;
182 return $ret ;
183 }
184
185 function getPageStatistics ( $article_title = "" )
186 {
187 $validationtypes = $wgLang->getValidationTypes() ;
188 $article_title = $_GET['article'] ;
189 $html = "<h1>Page validation statistics</h1>\n" ;
190 $d = $this->getData ( -1 , $article_title , -1 ) ;
191 if ( count ( $d ) ) $d = array_shift ( $d ) ;
192 else $d = array () ;
193 $html .= "<table border=1 cellpadding=2>\n" ;
194 $html .= "<tr><th>Version</th>" ;
195 foreach ( $validationtypes AS $idx => $title )
196 {
197 $title = explode ( "|" , $title ) ;
198 $html .= "<th>{$title[0]}</th>" ;
199 }
200 $html .= "</tr>\n" ;
201 foreach ( $d AS $version => $data )
202 {
203 $html .= "<tr>" ;
204 $html .= "<th align=left>{$version}</th>" ;
205
206 $vmax = array() ;
207 $vcur = array() ;
208 foreach ( $data AS $type => $x )
209 {
210 if ( !isset ( $vcur[$type] ) ) $vcur[$type] = 0 ;
211 if ( !isset ( $vmax[$type] ) ) $vmax[$type] = 0 ;
212 $vcur[$type] += $x->val_value ;
213 $vmax[$type] += $title[3] ;
214 }
215
216
217 foreach ( $validationtypes AS $idx => $title )
218 {
219 $html .= "<td>" ;
220 if ( isset ( $vcur[$idx] ) )
221 {
222 $average = 100 * $vcur[$idx] / $vmax[$idx] ;
223 $h = wfMsg ( "val_percent" ) ;
224 $h = str_replace ( "$1" , $average , $h ) ;
225 $h = str_replace ( "$2" , $vcur[$idx] , $h ) ;
226 $h = str_replace ( "$3" , $vmax[$idx] , $h ) ;
227 $html .= $h ;
228 }
229 else $html .= "(" . wfMsg ( "val_noop" ) . ")" ;
230 $html .= "</td>" ;
231 }
232
233 $html .= "</tr>" ;
234 }
235 $html .= "</table>\n" ;
236 return $html ;
237 }
238
239 }
240
241 function wfSpecialValidate( $page = "" )
242 {
243 global $wgOut ;
244 if ( isset ( $_GET['mode'] ) ) $mode = $_GET['mode'] ;
245 else $mode = "form" ;
246 $v = new Validation ;
247 $html = "" ;
248 if ( $mode == "form" )
249 {
250 $html = $v->validate_form () ;
251 }
252 else if ( $mode == "stat_page" )
253 {
254 $html = $v->getPageStatistics () ;
255 }
256
257 $wgOut->addHTML( $html ) ;
258 }
259
260 ?>