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