* No longer passing the name of the language class by reference
[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 /**
21 *
22 * @package MediaWiki
23 * @subpackage SpecialPage
24 */
25
26 /**
27 * constructor
28 */
29 function wfSpecialLog( $par = '' ) {
30 global $wgRequest;
31 $logReader =& new LogReader( $wgRequest );
32 if( '' == $wgRequest->getVal( 'type' ) && !empty( $par ) ) {
33 $logReader->limitType( $par );
34 }
35 $logViewer =& new LogViewer( $logReader );
36 $logViewer->show();
37 }
38
39 /**
40 *
41 * @package MediaWiki
42 * @subpackage SpecialPage
43 */
44 class LogReader {
45 var $db, $joinClauses, $whereClauses;
46 var $type = '', $user = '', $title = null;
47
48 /**
49 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
50 */
51 function LogReader( $request ) {
52 $this->db =& wfGetDB( DB_SLAVE );
53 $this->setupQuery( $request );
54 }
55
56 /**
57 * Basic setup and applies the limiting factors from the WebRequest object.
58 * @param WebRequest $request
59 * @private
60 */
61 function setupQuery( $request ) {
62 $page = $this->db->tableName( 'page' );
63 $user = $this->db->tableName( 'user' );
64 $this->joinClauses = array( "LEFT OUTER JOIN $page ON log_namespace=page_namespace AND log_title=page_title" );
65 $this->whereClauses = array( 'user_id=log_user' );
66
67 $this->limitType( $request->getVal( 'type' ) );
68 $this->limitUser( $request->getText( 'user' ) );
69 $this->limitTitle( $request->getText( 'page' ) );
70 $this->limitTime( $request->getVal( 'from' ), '>=' );
71 $this->limitTime( $request->getVal( 'until' ), '<=' );
72
73 list( $this->limit, $this->offset ) = $request->getLimitOffset();
74 }
75
76 /**
77 * Set the log reader to return only entries of the given type.
78 * @param string $type A log type ('upload', 'delete', etc)
79 * @private
80 */
81 function limitType( $type ) {
82 if( empty( $type ) ) {
83 return false;
84 }
85 $this->type = $type;
86 $safetype = $this->db->strencode( $type );
87 $this->whereClauses[] = "log_type='$safetype'";
88 }
89
90 /**
91 * Set the log reader to return only entries by the given user.
92 * @param string $name Valid user name
93 * @private
94 */
95 function limitUser( $name ) {
96 $title = Title::makeTitle( NS_USER, $name );
97 if( empty( $name ) || is_null( $title ) ) {
98 return false;
99 }
100 $this->user = str_replace( '_', ' ', $title->getDBkey() );
101 $safename = $this->db->strencode( $this->user );
102 $user = $this->db->tableName( 'user' );
103 $this->whereClauses[] = "user_name='$safename'";
104 }
105
106 /**
107 * Set the log reader to return only entries affecting the given page.
108 * (For the block and rights logs, this is a user page.)
109 * @param string $page Title name as text
110 * @private
111 */
112 function limitTitle( $page ) {
113 $title = Title::newFromText( $page );
114 if( empty( $page ) || is_null( $title ) ) {
115 return false;
116 }
117 $this->title =& $title;
118 $safetitle = $this->db->strencode( $title->getDBkey() );
119 $ns = $title->getNamespace();
120 $this->whereClauses[] = "log_namespace=$ns AND log_title='$safetitle'";
121 }
122
123 /**
124 * Set the log reader to return only entries in a given time range.
125 * @param string $time Timestamp of one endpoint
126 * @param string $direction either ">=" or "<=" operators
127 * @private
128 */
129 function limitTime( $time, $direction ) {
130 # Direction should be a comparison operator
131 if( empty( $time ) ) {
132 return false;
133 }
134 $safetime = $this->db->strencode( wfTimestamp( TS_MW, $time ) );
135 $this->whereClauses[] = "log_timestamp $direction '$safetime'";
136 }
137
138 /**
139 * Build an SQL query from all the set parameters.
140 * @return string the SQL query
141 * @private
142 */
143 function getQuery() {
144 $logging = $this->db->tableName( "logging" );
145 $user = $this->db->tableName( 'user' );
146 $sql = "SELECT log_type, log_action, log_timestamp,
147 log_user, user_name,
148 log_namespace, log_title, page_id,
149 log_comment, log_params FROM $user, $logging ";
150 if( !empty( $this->joinClauses ) ) {
151 $sql .= implode( ',', $this->joinClauses );
152 }
153 if( !empty( $this->whereClauses ) ) {
154 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses );
155 }
156 $sql .= " ORDER BY log_timestamp DESC ";
157 $sql = $this->db->limitResult($sql, $this->limit, $this->offset );
158 return $sql;
159 }
160
161 /**
162 * Execute the query and start returning results.
163 * @return ResultWrapper result object to return the relevant rows
164 */
165 function getRows() {
166 $res = $this->db->query( $this->getQuery() );
167 return $this->db->resultObject( $res );
168 }
169
170 /**
171 * @return string The query type that this LogReader has been limited to.
172 */
173 function queryType() {
174 return $this->type;
175 }
176
177 /**
178 * @return string The username type that this LogReader has been limited to, if any.
179 */
180 function queryUser() {
181 return $this->user;
182 }
183
184 /**
185 * @return string The text of the title that this LogReader has been limited to.
186 */
187 function queryTitle() {
188 if( is_null( $this->title ) ) {
189 return '';
190 } else {
191 return $this->title->getPrefixedText();
192 }
193 }
194 }
195
196 /**
197 *
198 * @package MediaWiki
199 * @subpackage SpecialPage
200 */
201 class LogViewer {
202 /**
203 * @var LogReader $reader
204 */
205 var $reader;
206 var $numResults = 0;
207
208 /**
209 * @param LogReader &$reader where to get our data from
210 */
211 function LogViewer( &$reader ) {
212 global $wgUser;
213 $this->skin =& $wgUser->getSkin();
214 $this->reader =& $reader;
215 }
216
217 /**
218 * Take over the whole output page in $wgOut with the log display.
219 */
220 function show() {
221 global $wgOut;
222 $this->showHeader( $wgOut );
223 $this->showOptions( $wgOut );
224 $result = $this->getLogRows();
225 $this->showPrevNext( $wgOut );
226 $this->doShowList( $wgOut, $result );
227 $this->showPrevNext( $wgOut );
228 }
229
230 /**
231 * Load the data from the linked LogReader
232 * Preload the link cache
233 * Initialise numResults
234 *
235 * Must be called before calling showPrevNext
236 *
237 * @return object database result set
238 */
239 function getLogRows() {
240 global $wgLinkCache;
241 $result = $this->reader->getRows();
242 $this->numResults = 0;
243
244 // Fetch results and form a batch link existence query
245 $batch = new LinkBatch;
246 while ( $s = $result->fetchObject() ) {
247 // User link
248 $title = Title::makeTitleSafe( NS_USER, $s->user_name );
249 $batch->addObj( $title );
250
251 // Move destination link
252 if ( $s->log_type == 'move' ) {
253 $paramArray = LogPage::extractParams( $s->log_params );
254 $title = Title::newFromText( $paramArray[0] );
255 $batch->addObj( $title );
256 }
257 $this->numResults++;
258 }
259 $batch->execute( $wgLinkCache );
260
261 return $result;
262 }
263
264
265 /**
266 * Output just the list of entries given by the linked LogReader,
267 * with extraneous UI elements. Use for displaying log fragments in
268 * another page (eg at Special:Undelete)
269 * @param OutputPage $out where to send output
270 */
271 function showList( &$out ) {
272 $this->doShowList( $out, $this->getLogRows() );
273 }
274
275 function doShowList( &$out, $result ) {
276 // Rewind result pointer and go through it again, making the HTML
277 $html='';
278 if ($this->numResults > 0) {
279 $html = "\n<ul>\n";
280 $result->seek( 0 );
281 while( $s = $result->fetchObject() ) {
282 $html .= $this->logLine( $s );
283 }
284 $html .= "\n</ul>\n";
285 }
286 $result->free();
287 $out->addHTML( $html );
288 }
289
290 /**
291 * @param Object $s a single row from the result set
292 * @return string Formatted HTML list item
293 * @private
294 */
295 function logLine( $s ) {
296 global $wgLang, $wgLinkCache;
297 $title = Title::makeTitle( $s->log_namespace, $s->log_title );
298 $user = Title::makeTitleSafe( NS_USER, $s->user_name );
299 $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $s->log_timestamp), true );
300
301 // Enter the existence or non-existence of this page into the link cache,
302 // for faster makeLinkObj() in LogPage::actionText()
303 if( $s->page_id ) {
304 $wgLinkCache->addGoodLinkObj( $s->page_id, $title );
305 } else {
306 $wgLinkCache->addBadLinkObj( $title );
307 }
308
309 $userLink = $this->skin->makeLinkObj( $user, htmlspecialchars( $s->user_name ) );
310 $comment = $this->skin->commentBlock( $s->log_comment );
311 $paramArray = LogPage::extractParams( $s->log_params );
312 $revert = '';
313 if ( $s->log_type == 'move' && isset( $paramArray[0] ) ) {
314 $specialTitle = Title::makeTitle( NS_SPECIAL, 'Movepage' );
315 $destTitle = Title::newFromText( $paramArray[0] );
316 if ( $destTitle ) {
317 $revert = '(' . $this->skin->makeKnownLinkObj( $specialTitle, wfMsg( 'revertmove' ),
318 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
319 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
320 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
321 '&wpMovetalk=0' ) . ')';
322 }
323 }
324
325 $action = LogPage::actionText( $s->log_type, $s->log_action, $title, $this->skin, $paramArray, true );
326 $out = "<li>$time $userLink $action $comment $revert</li>\n";
327 return $out;
328 }
329
330 /**
331 * @param OutputPage &$out where to send output
332 * @private
333 */
334 function showHeader( &$out ) {
335 $type = $this->reader->queryType();
336 if( LogPage::isLogType( $type ) ) {
337 $out->setPageTitle( LogPage::logName( $type ) );
338 $out->addWikiText( LogPage::logHeader( $type ) );
339 }
340 }
341
342 /**
343 * @param OutputPage &$out where to send output
344 * @private
345 */
346 function showOptions( &$out ) {
347 global $wgScript;
348 $action = htmlspecialchars( $wgScript );
349 $title = Title::makeTitle( NS_SPECIAL, 'Log' );
350 $special = htmlspecialchars( $title->getPrefixedDBkey() );
351 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
352 "<input type='hidden' name='title' value=\"$special\" />\n" .
353 $this->getTypeMenu() .
354 $this->getUserInput() .
355 $this->getTitleInput() .
356 "<input type='submit' value=\"" . wfMsg( 'allpagessubmit' ) . "\" />" .
357 "</form>" );
358 }
359
360 /**
361 * @return string Formatted HTML
362 * @private
363 */
364 function getTypeMenu() {
365 $out = "<select name='type'>\n";
366 foreach( LogPage::validTypes() as $type ) {
367 $text = htmlspecialchars( LogPage::logName( $type ) );
368 $selected = ($type == $this->reader->queryType()) ? ' selected="selected"' : '';
369 $out .= "<option value=\"$type\"$selected>$text</option>\n";
370 }
371 $out .= "</select>\n";
372 return $out;
373 }
374
375 /**
376 * @return string Formatted HTML
377 * @private
378 */
379 function getUserInput() {
380 $user = htmlspecialchars( $this->reader->queryUser() );
381 return wfMsg('specialloguserlabel') . "<input type='text' name='user' size='12' value=\"$user\" />\n";
382 }
383
384 /**
385 * @return string Formatted HTML
386 * @private
387 */
388 function getTitleInput() {
389 $title = htmlspecialchars( $this->reader->queryTitle() );
390 return wfMsg('speciallogtitlelabel') . "<input type='text' name='page' size='20' value=\"$title\" />\n";
391 }
392
393 /**
394 * @param OutputPage &$out where to send output
395 * @private
396 */
397 function showPrevNext( &$out ) {
398 global $wgContLang,$wgRequest;
399 $pieces = array();
400 $pieces[] = 'type=' . urlencode( $this->reader->queryType() );
401 $pieces[] = 'user=' . urlencode( $this->reader->queryUser() );
402 $pieces[] = 'page=' . urlencode( $this->reader->queryTitle() );
403 $bits = implode( '&', $pieces );
404 list( $limit, $offset ) = $wgRequest->getLimitOffset();
405
406 # TODO: use timestamps instead of offsets to make it more natural
407 # to go huge distances in time
408 $html = wfViewPrevNext( $offset, $limit,
409 $wgContLang->specialpage( 'Log' ),
410 $bits,
411 $this->numResults < $limit);
412 $out->addHTML( '<p>' . $html . '</p>' );
413 }
414 }
415
416
417 ?>