Use the result wrapper object.
[lhc/web/wiklou.git] / includes / SpecialLog.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
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 function wfSpecialLog( $par = '' ) {
21 global $wgRequest;
22 $logReader =& new LogReader( $wgRequest );
23 if( '' == $wgRequest->getVal( 'type' ) && !empty( $par ) ) {
24 $logReader->limitType( $par );
25 }
26 $logViewer =& new LogViewer( $logReader );
27 $logViewer->show();
28 }
29
30 class LogReader {
31 var $db, $joinClauses, $whereClauses;
32 var $type = '', $user = '', $title = null;
33
34 function LogReader( $request ) {
35 $this->db =& wfGetDB( DB_SLAVE );
36 $this->setupQuery( $request );
37 }
38
39 function setupQuery( $request ) {
40 $cur = $this->db->tableName( 'cur' );
41 $user = $this->db->tableName( 'user' );
42 $this->joinClauses = array( "LEFT OUTER JOIN $cur ON log_namespace=cur_namespace AND log_title=cur_title" );
43 $this->whereClauses = array( 'user_id=log_user' );
44
45 $this->limitType( $request->getVal( 'type' ) );
46 $this->limitUser( $request->getText( 'user' ) );
47 $this->limitTitle( $request->getText( 'page' ) );
48 $this->limitTime( $request->getVal( 'from' ), '>=' );
49 $this->limitTime( $request->getVal( 'until' ), '<=' );
50
51 list( $this->limit, $this->offset ) = $request->getLimitOffset();
52 }
53
54 function limitType( $type ) {
55 if( empty( $type ) ) {
56 return false;
57 }
58 $this->type = $type;
59 $safetype = $this->db->strencode( $type );
60 $this->whereClauses[] = "log_type='$safetype'";
61 }
62
63 function limitUser( $name ) {
64 $title = Title::makeTitleSafe( NS_USER, $name );
65 if( empty( $name ) || is_null( $title ) ) {
66 return false;
67 }
68 $this->user = str_replace( '_', ' ', $title->getDBkey() );
69 $safename = $this->db->strencode( $this->user );
70 $user = $this->db->tableName( 'user' );
71 $this->whereClauses[] = "user_name='$safename'";
72 }
73
74 function limitTitle( $page ) {
75 $title = Title::newFromText( $page );
76 if( empty( $page ) || is_null( $title ) ) {
77 return false;
78 }
79 $this->title =& $title;
80 $safetitle = $this->db->strencode( $title->getDBkey() );
81 $ns = $title->getNamespace();
82 $this->whereClauses[] = "log_namespace=$ns AND log_title='$safetitle'";
83 }
84
85 function limitTime( $time, $direction ) {
86 # Direction should be a comparison operator
87 if( empty( $time ) ) {
88 return false;
89 }
90 $safetime = $this->db->strencode( wfTimestamp( TS_MW, $time ) );
91 $this->whereClauses[] = "log_timestamp $direction '$safetime'";
92 }
93
94 function getQuery() {
95 $logging = $this->db->tableName( "logging" );
96 $user = $this->db->tableName( 'user' );
97 $sql = "SELECT log_type, log_action, log_timestamp,
98 log_user, user_name,
99 log_namespace, log_title, cur_id,
100 log_comment FROM $logging, $user ";
101 if( !empty( $this->joinClauses ) ) {
102 $sql .= implode( ',', $this->joinClauses );
103 }
104 if( !empty( $this->whereClauses ) ) {
105 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses );
106 }
107 $sql .= " ORDER BY log_timestamp DESC ";
108 $sql .= $this->db->limitResult( $this->limit, $this->offset );
109 return $sql;
110 }
111
112 function getRows() {
113 return $this->db->resultObject( $this->db->query( $this->getQuery() ) );
114 }
115
116 function queryType() {
117 return $this->type;
118 }
119
120 function queryUser() {
121 return $this->user;
122 }
123
124 function queryTitle() {
125 if( is_null( $this->title ) ) {
126 return '';
127 } else {
128 return $this->title->getPrefixedText();
129 }
130 }
131 }
132
133 class LogViewer {
134 var $reader, $skin;
135
136 function LogViewer( &$reader ) {
137 global $wgUser;
138 $this->skin =& $wgUser->getSkin();
139 $this->reader =& $reader;
140 }
141
142 function show() {
143 global $wgOut;
144 $this->showHeader( $wgOut );
145 $this->showOptions( $wgOut );
146 $this->showPrevNext( $wgOut );
147 $this->showList( $wgOut );
148 $this->showPrevNext( $wgOut );
149 }
150
151 function showList( &$out ) {
152 $html = "";
153 $result = $this->reader->getRows();
154 while( $s = $result->fetchObject() ) {
155 $html .= $this->logLine( $s );
156 }
157 $result->free();
158 $out->addHTML( $html );
159 }
160
161 # wfMsg( unprotectedarticle, $text )
162 # wfMsg( 'protectedarticle', $text )
163 # wfMsg( 'deletedarticle', $text )
164 # wfMsg( 'uploadedimage', $text )
165 # wfMsg( "blocklogentry", $this->BlockAddress, $this->BlockExpiry );
166 # wfMsg( "bureaucratlogentry", $this->mUser, implode( " ", $rightsNotation ) );
167 # wfMsg( "undeletedarticle", $this->mTarget ),
168 function logLine( $s ) {
169 global $wgLang;
170 $title = Title::makeTitle( $s->log_namespace, $s->log_title );
171 $user = Title::makeTitleSafe( NS_USER, $s->user_name );
172 $time = $wgLang->timeanddate( $s->log_timestamp );
173 if( $s->cur_id ) {
174 $titleLink = $this->skin->makeKnownLinkObj( $title );
175 } else {
176 $titleLink = $this->skin->makeBrokenLinkObj( $title );
177 }
178 $userLink = $this->skin->makeLinkObj( $user, htmlspecialchars( $s->user_name ) );
179 if( '' === $s->log_comment ) {
180 $comment = '';
181 } else {
182 $comment = '(<em>' . $this->skin->formatComment( $s->log_comment ) . '</em>)';
183 }
184
185 $action = LogPage::actionText( $s->log_type, $s->log_action, $titleLink );
186 $out = "<li>$time $userLink $action $comment</li>\n";
187 return $out;
188 }
189
190 function showHeader( &$out ) {
191 $type = $this->reader->queryType();
192 if( LogPage::isLogType( $type ) ) {
193 $out->setPageTitle( LogPage::logName( $type ) );
194 $out->addWikiText( LogPage::logHeader( $type ) );
195 }
196 }
197
198 function showOptions( &$out ) {
199 global $wgScript;
200 $action = htmlspecialchars( $wgScript );
201 $title = Title::makeTitle( NS_SPECIAL, 'Log' );
202 $special = htmlspecialchars( $title->getPrefixedDBkey() );
203 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
204 "<input type='hidden' name='title' value=\"$special\" />\n" .
205 $this->getTypeMenu() .
206 $this->getUserInput() .
207 $this->getTitleInput() .
208 "<input type='submit' />" .
209 "</form>" );
210 }
211
212 function getTypeMenu() {
213 $out = "<select name='type'>\n";
214 foreach( LogPage::validTypes() as $type ) {
215 $text = htmlspecialchars( LogPage::logName( $type ) );
216 $selected = ($type == $this->reader->queryType()) ? ' selected="selected"' : '';
217 $out .= "<option value=\"$type\"$selected>$text</option>\n";
218 }
219 $out .= "</select>\n";
220 return $out;
221 }
222
223 function getUserInput() {
224 $user = htmlspecialchars( $this->reader->queryUser() );
225 return "User: <input type='text' name='user' size='12' value=\"$user\" />\n";
226 }
227
228 function getTitleInput() {
229 $title = htmlspecialchars( $this->reader->queryTitle() );
230 return "Title: <input type='text' name='page' size='20' value=\"$title\" />\n";
231 }
232
233 function showPrevNext( &$out ) {
234 global $wgLang;
235 $pieces = array();
236 $pieces[] = 'type=' . htmlspecialchars( $this->reader->queryType() );
237 $pieces[] = 'user=' . htmlspecialchars( $this->reader->queryUser() );
238 $pieces[] = 'page=' . htmlspecialchars( $this->reader->queryTitle() );
239 $bits = implode( '&', $pieces );
240 $offset = 0; $limit = 50;
241
242 # TODO: use timestamps instead of offsets to make it more natural
243 # to go huge distances in time
244 $html = wfViewPrevNext( $offset, $limit,
245 $wgLang->specialpage( 'Log' ),
246 $bits,
247 false);
248 $out->addHTML( '<p>' . $html . '</p>' );
249 }
250 }
251
252
253 ?>