f5b432ee93f2249fa7ef6798ab9e9713c9e0960f
[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 # Things that should be in the language files
23 var $types = array (
24 "0" => "Style|Awful|Awesome|5",
25 "1" => "Legal|Illegal|Legal|5",
26 "2" => "Completeness|Stub|Extensive|5",
27 "3" => "Facts|Wild guesses|Concrete|5"
28 ) ;
29
30 function find_this_version ( $article_title , &$article_time , &$id , &$tab )
31 {
32 $id = "" ;
33 $sql = "SELECT cur_id,cur_timestamp FROM cur WHERE cur_namespace=0 AND cur_title='{$article_title}'" ;
34 $res = wfQuery( $sql, DB_READ );
35 if( $s = wfFetchObject( $res ) )
36 {
37 if ( $article_time == "" ) $article_time = $s->cur_timestamp ; # No timestamp = current version
38 if ( $article_time == $s->cur_timestamp ) # Means current version
39 {
40 $tab = "cur" ;
41 $id = $s->cur_id ;
42 }
43 }
44
45 if ( $id == "" )
46 {
47 $sql = "SELECT old_id FROM old WHERE old_namespace=0 AND old_title='{$article_title}' AND old_timestamp='{$article_time}'" ;
48 $res = wfQuery( $sql, DB_READ );
49 if( $s = wfFetchObject( $res ) )
50 {
51 $tab = "old" ;
52 $id = $s->old_id ;
53 }
54 }
55 }
56
57 function get_prev_data ( $user_id , $article_title , $article_timestamp = "" )
58 {
59 $ret = array () ;
60 $sql = "SELECT * FROM validate WHERE val_user='{$user_id}' AND val_title='{$article_title}'" ;
61 if ( $article_timestamp != "" ) $sql .= " AND val_timestamp='{$article_timestamp}'" ;
62 $res = wfQuery( $sql, DB_READ );
63 while( $s = wfFetchObject( $res ) ) $ret[$s->val_timestamp][$s->val_type] = $s ;
64 return $ret ;
65 }
66
67 function validate_form ( $article_title = "" )
68 {
69 global $wgOut, $wgLang, $wgUser;
70 if ( $wgUser->getID() == 0 ) return ; # Anon
71 if ( $article_title == "" ) $article_title = $_GET['article'] ;
72 $article_time = "" ;
73 if ( isset ( $_GET['timestamp'] ) ) $article_time = $_GET['timestamp'] ;
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 # User has clicked "Doit" before, so evaluate form
80 if ( isset ( $_POST['doit'] ) )
81 {
82 $oldtime = $_POST['oldtime'] ;
83 if ( !isset ( $val["{$oldtime}"] ) ) $val["{$oldtime}"] = array () ;
84 if ( isset ( $_POST['clear_other'] ) && $_POST['clear_other'] == 1 ) # Clear all others
85 {
86 $sql = "DELETE FROM validate WHERE val_title='{$article_title}' AND val_timestamp<>'{$oldtime}' AND val_user='" ;
87 $sql .= $wgUser->getID() . "'" ;
88 wfQuery( $sql, DB_WRITE );
89 $val2 = $val["{$oldtime}"] ; # Only version left
90 $val = array () ; # So clear others
91 $val["{$oldtime}"] = $val2 ;
92 }
93
94 # Delete old "votes" for this version
95 $sql = "DELETE FROM validate WHERE val_title='{$article_title}' AND val_timestamp='{$oldtime}' AND val_user='" ;
96 $sql .= $wgUser->getID() . "'" ;
97 wfQuery( $sql, DB_WRITE );
98
99 for ( $idx = 0 ; $idx < count ( $this->types) ; $idx++ ) # Changes
100 {
101 $comment = $_POST["comment{$idx}"] ;
102 $comment_sql = str_replace ( "'" , "\'" , $comment ) ;
103 $rad = $_POST["rad{$idx}"] ;
104 if ( !isset ( $val["{$oldtime}"][$idx] ) ) $val["{$oldtime}"][$idx] = "" ;
105 $val["{$oldtime}"][$idx]->val_value = $rad ;
106 $val["{$oldtime}"][$idx]->val_comment = $comment ;
107 if ( $rad != -1 )
108 {
109 # Store it in the database
110 $sql = "INSERT INTO validate (val_user,val_title,val_timestamp,val_type,val_value,val_comment) " .
111 "VALUES ( '" . $wgUser->getID() . "','{$article_title}','{$oldtime}','{$idx}','{$rad}','{$comment_sql}')" ;
112 if ( $rad != -1 ) wfQuery( $sql, DB_WRITE );
113 }
114 }
115 }
116
117 # Generating HTML
118 $html = "<h1>" . $article->getPrefixedText() . "</h1>\n" ;
119 foreach ( $val AS $time => $stuff )
120 {
121 if ( $time == $article_time ) $html .= "<h2>This version</h2>\n" ;
122 else $html .= "<h2>Version of {$time}</h2>\n" ;
123 $html .= "<form method=post>\n" ;
124 $html .= "<input type=hidden name=oldtime value='{$time}'>" ;
125 $html .= "<table>\n" ;
126 $html .= "<tr><th>Class</th><th colspan=4>Opinion</th><th>Comment</th></tr>\n" ;
127 for ( $idx = 0 ; $idx < count ( $this->types) ; $idx++ )
128 {
129 $x = explode ( "|" , $this->types[$idx] , 4 ) ;
130 if ( isset ( $stuff[$idx] ) ) $choice = $stuff[$idx]->val_value ;
131 else $choice = -1 ;
132 if ( isset ( $stuff[$idx] ) ) $comment = $stuff[$idx]->val_comment ;
133 else $comment = "" ;
134 $html .= "<tr><th align=left>{$x[0]}</th><td align=right>{$x[1]}</td><td align=center>" ;
135 for ( $cnt = 0 ; $cnt < $x[3] ; $cnt++)
136 {
137 $html .= "<input type=radio name='rad{$idx}' value='{$cnt}'" ;
138 if ( $choice == $cnt ) $html .= " checked" ;
139 $html .= "> " ;
140 }
141 $html .= "</td><td>{$x[2]}</td>" ;
142 $html .= "<td><input type=radio name='rad{$idx}' value='-1'" ;
143 if ( $choice == -1 ) $html .= " checked" ;
144 $html .= "> " . wfMsg ( "val_noop" ) . "</td>" ;
145 $html .= "<td><input type=text name='comment{$idx}' value='{$comment}'></td>" ;
146 $html .= "</tr>\n" ;
147 }
148 $html .= "<tr><td></td><td colspan=3><input type=checkbox name=clear_other value=1 checked>" ;
149 $html .= str_replace ( "$1" , $article->getPrefixedURL() , wfMsg("clear_old") ) ;
150 $html .= "</td><td align=right><input type=submit name=doit value='Do it'></td>" ;
151 $html .= "</tr></table></form>\n" ;
152 }
153 return $html ;
154 }
155
156 function getData ( $user = -1 , $title = "" , $type = -1 )
157 {
158 $ret = array () ;
159 $sql = array () ;
160 if ( $user != -1 ) $sql[] = "val_user='{$user}'" ;
161 if ( $type != -1 ) $sql[] = "val_type='{$type}'" ;
162 if ( $title != "" ) $sql[] = "val_title='{$title}'" ;
163 $sql = implode ( " AND " , $sql ) ;
164 if ( $sql != "" ) $sql = " WHERE " . $sql ;
165 $sql = "SELECT * FROM validate" . $sql ;
166 $res = wfQuery( $sql, DB_READ );
167 while( $s = wfFetchObject( $res ) ) $ret["{$s->val_title}"]["{$s->val_timestamp}"]["{$s->val_type}"] = $s ;
168 return $ret ;
169 }
170
171 function getPageStatistics ( $article_title = "" )
172 {
173 $article_title = $_GET['article'] ;
174 $html = "<h1>Page validation statistics</h1>\n" ;
175 $d = $this->getData ( -1 , $article_title , -1 ) ;
176 if ( count ( $d ) ) $d = array_shift ( $d ) ;
177 else $d = array () ;
178 $html .= "<table border=1 cellpadding=2>\n" ;
179 $html .= "<tr><th>Version</th>" ;
180 foreach ( $this->types AS $idx => $title )
181 {
182 $title = explode ( "|" , $title ) ;
183 $html .= "<th>{$title[0]}</th>" ;
184 }
185 $html .= "</tr>\n" ;
186 foreach ( $d AS $version => $data )
187 {
188 $html .= "<tr>" ;
189 $html .= "<th align=left>{$version}</th>" ;
190
191 $vmax = array() ;
192 $vcur = array() ;
193 foreach ( $data AS $type => $x )
194 {
195 if ( !isset ( $vcur[$type] ) ) $vcur[$type] = 0 ;
196 if ( !isset ( $vmax[$type] ) ) $vmax[$type] = 0 ;
197 $vcur[$type] += $x->val_value ;
198 $vmax[$type] += $title[3] ;
199 }
200
201
202 foreach ( $this->types AS $idx => $title )
203 {
204 $html .= "<td>" ;
205 if ( isset ( $vcur[$idx] ) )
206 {
207 $average = 100 * $vcur[$idx] / $vmax[$idx] ;
208 $h = wfMsg ( "val_percent" ) ;
209 $h = str_replace ( "$1" , $average , $h ) ;
210 $h = str_replace ( "$2" , $vcur[$idx] , $h ) ;
211 $h = str_replace ( "$3" , $vmax[$idx] , $h ) ;
212 $html .= $h ;
213 }
214 else $html .= "(" . wfMsg ( "val_noop" ) . ")" ;
215 $html .= "</td>" ;
216 }
217
218 $html .= "</tr>" ;
219 }
220 $html .= "</table>\n" ;
221 return $html ;
222 }
223
224 }
225
226 function wfSpecialValidate( $page = "" )
227 {
228 global $wgOut ;
229 if ( isset ( $_GET['mode'] ) ) $mode = $_GET['mode'] ;
230 else $mode = "form" ;
231 $v = new Validation ;
232 $html = "" ;
233 if ( $mode == "form" )
234 {
235 $html = $v->validate_form () ;
236 }
237 else if ( $mode == "stat_page" )
238 {
239 $html = $v->getPageStatistics () ;
240 }
241
242 $wgOut->addHTML( $html ) ;
243 }
244
245 ?>