fix for r20409. Thanks to VoA.
[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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 *
22 * @addtogroup SpecialPage
23 */
24
25 /**
26 * constructor
27 */
28 function wfSpecialLog( $par = '' ) {
29 global $wgRequest;
30 $logReader = new LogReader( $wgRequest );
31 if( $wgRequest->getVal( 'type' ) == '' && $par != '' ) {
32 $logReader->limitType( $par );
33 }
34 $logViewer = new LogViewer( $logReader );
35 $logViewer->show();
36 }
37
38 /**
39 *
40 * @addtogroup SpecialPage
41 */
42 class LogReader {
43 var $db, $joinClauses, $whereClauses;
44 var $type = '', $user = '', $title = null, $pattern = false;
45
46 /**
47 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
48 */
49 function LogReader( $request ) {
50 $this->db = wfGetDB( DB_SLAVE );
51 $this->setupQuery( $request );
52 }
53
54 /**
55 * Basic setup and applies the limiting factors from the WebRequest object.
56 * @param WebRequest $request
57 * @private
58 */
59 function setupQuery( $request ) {
60 $page = $this->db->tableName( 'page' );
61 $user = $this->db->tableName( 'user' );
62 $this->joinClauses = array(
63 "LEFT OUTER JOIN $page ON log_namespace=page_namespace AND log_title=page_title",
64 "INNER JOIN $user ON user_id=log_user" );
65 $this->whereClauses = array();
66
67 $this->limitType( $request->getVal( 'type' ) );
68 $this->limitUser( $request->getText( 'user' ) );
69 $this->limitTitle( $request->getText( 'page' ) , $request->getBool( 'pattern' ) );
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 (In)valid user name
93 * @private
94 */
95 function limitUser( $name ) {
96 if ( $name == '' )
97 return false;
98 $usertitle = Title::makeTitleSafe( NS_USER, $name );
99 if ( is_null( $usertitle ) )
100 return false;
101 $this->user = $usertitle->getText();
102
103 /* Fetch userid at first, if known, provides awesome query plan afterwards */
104 $userid = $this->db->selectField('user','user_id',array('user_name'=>$this->user));
105 if (!$userid)
106 /* It should be nicer to abort query at all,
107 but for now it won't pass anywhere behind the optimizer */
108 $this->whereClauses[] = "NULL";
109 else
110 $this->whereClauses[] = "log_user=$userid";
111 }
112
113 /**
114 * Set the log reader to return only entries affecting the given page.
115 * (For the block and rights logs, this is a user page.)
116 * @param string $page Title name as text
117 * @private
118 */
119 function limitTitle( $page , $pattern ) {
120 $title = Title::newFromText( $page );
121 if( empty( $page ) || is_null( $title ) ) {
122 return false;
123 }
124 $this->title =& $title;
125 $this->pattern = $pattern;
126 $ns = $title->getNamespace();
127 if ( $pattern ) {
128 $safetitle = $this->db->escapeLike( $title->getDBkey() ); // use escapeLike to avoid expensive search patterns like 't%st%'
129 $this->whereClauses[] = "log_namespace=$ns AND log_title LIKE '$safetitle%'";
130 } else {
131 $safetitle = $this->db->strencode( $title->getDBkey() );
132 $this->whereClauses[] = "log_namespace=$ns AND log_title = '$safetitle'";
133 }
134 }
135
136 /**
137 * Set the log reader to return only entries in a given time range.
138 * @param string $time Timestamp of one endpoint
139 * @param string $direction either ">=" or "<=" operators
140 * @private
141 */
142 function limitTime( $time, $direction ) {
143 # Direction should be a comparison operator
144 if( empty( $time ) ) {
145 return false;
146 }
147 $safetime = $this->db->strencode( wfTimestamp( TS_MW, $time ) );
148 $this->whereClauses[] = "log_timestamp $direction '$safetime'";
149 }
150
151 /**
152 * Build an SQL query from all the set parameters.
153 * @return string the SQL query
154 * @private
155 */
156 function getQuery() {
157 $logging = $this->db->tableName( "logging" );
158 $sql = "SELECT /*! STRAIGHT_JOIN */ log_type, log_action, log_timestamp,
159 log_user, user_name,
160 log_namespace, log_title, page_id,
161 log_comment, log_params FROM $logging ";
162 if( !empty( $this->joinClauses ) ) {
163 $sql .= implode( ' ', $this->joinClauses );
164 }
165 if( !empty( $this->whereClauses ) ) {
166 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses );
167 }
168 $sql .= " ORDER BY log_timestamp DESC ";
169 $sql = $this->db->limitResult($sql, $this->limit, $this->offset );
170 return $sql;
171 }
172
173 /**
174 * Execute the query and start returning results.
175 * @return ResultWrapper result object to return the relevant rows
176 */
177 function getRows() {
178 $res = $this->db->query( $this->getQuery(), 'LogReader::getRows' );
179 return $this->db->resultObject( $res );
180 }
181
182 /**
183 * @return string The query type that this LogReader has been limited to.
184 */
185 function queryType() {
186 return $this->type;
187 }
188
189 /**
190 * @return string The username type that this LogReader has been limited to, if any.
191 */
192 function queryUser() {
193 return $this->user;
194 }
195
196 /**
197 * @return boolean The checkbox, if titles should be searched by a pattern too
198 */
199 function queryPattern() {
200 return $this->pattern;
201 }
202
203 /**
204 * @return string The text of the title that this LogReader has been limited to.
205 */
206 function queryTitle() {
207 if( is_null( $this->title ) ) {
208 return '';
209 } else {
210 return $this->title->getPrefixedText();
211 }
212 }
213 }
214
215 /**
216 *
217 * @addtogroup SpecialPage
218 */
219 class LogViewer {
220 /**
221 * @var LogReader $reader
222 */
223 var $reader;
224 var $numResults = 0;
225
226 /**
227 * @param LogReader &$reader where to get our data from
228 */
229 function LogViewer( &$reader ) {
230 global $wgUser;
231 $this->skin = $wgUser->getSkin();
232 $this->reader =& $reader;
233 }
234
235 /**
236 * Take over the whole output page in $wgOut with the log display.
237 */
238 function show() {
239 global $wgOut;
240 $this->showHeader( $wgOut );
241 $this->showOptions( $wgOut );
242 $result = $this->getLogRows();
243 if ( $this->numResults > 0 ) {
244 $this->showPrevNext( $wgOut );
245 $this->doShowList( $wgOut, $result );
246 $this->showPrevNext( $wgOut );
247 } else {
248 $this->showError( $wgOut );
249 }
250 }
251
252 /**
253 * Load the data from the linked LogReader
254 * Preload the link cache
255 * Initialise numResults
256 *
257 * Must be called before calling showPrevNext
258 *
259 * @return object database result set
260 */
261 function getLogRows() {
262 $result = $this->reader->getRows();
263 $this->numResults = 0;
264
265 // Fetch results and form a batch link existence query
266 $batch = new LinkBatch;
267 while ( $s = $result->fetchObject() ) {
268 // User link
269 $batch->addObj( Title::makeTitleSafe( NS_USER, $s->user_name ) );
270 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $s->user_name ) );
271
272 // Move destination link
273 if ( $s->log_type == 'move' ) {
274 $paramArray = LogPage::extractParams( $s->log_params );
275 $title = Title::newFromText( $paramArray[0] );
276 $batch->addObj( $title );
277 }
278 ++$this->numResults;
279 }
280 $batch->execute();
281
282 return $result;
283 }
284
285
286 /**
287 * Output just the list of entries given by the linked LogReader,
288 * with extraneous UI elements. Use for displaying log fragments in
289 * another page (eg at Special:Undelete)
290 * @param OutputPage $out where to send output
291 */
292 function showList( &$out ) {
293 $this->doShowList( $out, $this->getLogRows() );
294 }
295
296 function doShowList( &$out, $result ) {
297 // Rewind result pointer and go through it again, making the HTML
298 $html = "\n<ul>\n";
299 $result->seek( 0 );
300 while( $s = $result->fetchObject() ) {
301 $html .= $this->logLine( $s );
302 }
303 $html .= "\n</ul>\n";
304 $out->addHTML( $html );
305 $result->free();
306 }
307
308 function showError( &$out ) {
309 $out->addWikiText( wfMsg( 'logempty' ) );
310 }
311
312 /**
313 * @param Object $s a single row from the result set
314 * @return string Formatted HTML list item
315 * @private
316 */
317 function logLine( $s ) {
318 global $wgLang, $wgUser;;
319 $skin = $wgUser->getSkin();
320 $title = Title::makeTitle( $s->log_namespace, $s->log_title );
321 $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $s->log_timestamp), true );
322
323 // Enter the existence or non-existence of this page into the link cache,
324 // for faster makeLinkObj() in LogPage::actionText()
325 $linkCache =& LinkCache::singleton();
326 if( $s->page_id ) {
327 $linkCache->addGoodLinkObj( $s->page_id, $title );
328 } else {
329 $linkCache->addBadLinkObj( $title );
330 }
331
332 $userLink = $this->skin->userLink( $s->log_user, $s->user_name ) . $this->skin->userToolLinksRedContribs( $s->log_user, $s->user_name );
333 $comment = $this->skin->commentBlock( $s->log_comment );
334 $paramArray = LogPage::extractParams( $s->log_params );
335 $revert = '';
336 // show revertmove link
337 if ( $s->log_type == 'move' && isset( $paramArray[0] ) ) {
338 $destTitle = Title::newFromText( $paramArray[0] );
339 if ( $destTitle ) {
340 $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Movepage' ),
341 wfMsg( 'revertmove' ),
342 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
343 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
344 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
345 '&wpMovetalk=0' ) . ')';
346 }
347 // show undelete link
348 } elseif ( $s->log_action == 'delete' && $wgUser->isAllowed( 'delete' ) ) {
349 $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Undelete' ),
350 wfMsg( 'undeletebtn' ) ,
351 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
352
353 // show unblock link
354 } elseif ( $s->log_action == 'block' && $wgUser->isAllowed( 'block' ) ) {
355 $revert = '(' . $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Ipblocklist' ),
356 wfMsg( 'unblocklink' ),
357 'action=unblock&ip=' . urlencode( $s->log_title ) ) . ')';
358 // show change protection link
359 } elseif ( $s->log_action == 'protect' && $wgUser->isAllowed( 'protect' ) ) {
360 $revert = '(' . $skin->makeKnownLink( $title->getPrefixedDBkey() ,
361 wfMsg( 'protect_change' ),
362 'action=unprotect' ) . ')';
363 }
364
365 $action = LogPage::actionText( $s->log_type, $s->log_action, $title, $this->skin, $paramArray, true, true );
366 $out = "<li>$time $userLink $action $comment $revert</li>\n";
367 return $out;
368 }
369
370 /**
371 * @param OutputPage &$out where to send output
372 * @private
373 */
374 function showHeader( &$out ) {
375 $type = $this->reader->queryType();
376 if( LogPage::isLogType( $type ) ) {
377 $out->setPageTitle( LogPage::logName( $type ) );
378 $out->addWikiText( LogPage::logHeader( $type ) );
379 }
380 }
381
382 /**
383 * @param OutputPage &$out where to send output
384 * @private
385 */
386 function showOptions( &$out ) {
387 global $wgScript;
388 $action = htmlspecialchars( $wgScript );
389 $title = SpecialPage::getTitleFor( 'Log' );
390 $special = htmlspecialchars( $title->getPrefixedDBkey() );
391 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
392 '<fieldset>' .
393 Xml::element( 'legend', array(), wfMsg( 'log' ) ) .
394 Xml::hidden( 'title', $special ) . "\n" .
395 $this->getTypeMenu() . "\n" .
396 $this->getUserInput() . "\n" .
397 $this->getTitleInput() . "\n" .
398 $this->getTitlePattern() . "\n" .
399 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
400 "</fieldset></form>" );
401 }
402
403 /**
404 * @return string Formatted HTML
405 * @private
406 */
407 function getTypeMenu() {
408 $out = "<select name='type'>\n";
409
410 $validTypes = LogPage::validTypes();
411 $m = array(); // Temporary array
412
413 // First pass to load the log names
414 foreach( $validTypes as $type ) {
415 $text = LogPage::logName( $type );
416 $m[$text] = $type;
417 }
418
419 // Second pass to sort by name
420 ksort($m);
421
422 // Third pass generates sorted XHTML content
423 foreach( $m as $text => $type ) {
424 $selected = ($type == $this->reader->queryType());
425 $out .= Xml::option( $text, $type, $selected ) . "\n";
426 }
427
428 $out .= '</select>';
429 return $out;
430 }
431
432 /**
433 * @return string Formatted HTML
434 * @private
435 */
436 function getUserInput() {
437 $user = $this->reader->queryUser();
438 return Xml::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 12, $user );
439 }
440
441 /**
442 * @return string Formatted HTML
443 * @private
444 */
445 function getTitleInput() {
446 $title = $this->reader->queryTitle();
447 return Xml::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
448 }
449
450 /**
451 * @return boolean Checkbox
452 * @private
453 */
454 function getTitlePattern() {
455 $pattern = $this->reader->queryPattern();
456 return Xml::checkLabel( wfMsg( 'title-pattern' ), 'pattern', 'pattern', $pattern );
457 }
458
459 /**
460 * @param OutputPage &$out where to send output
461 * @private
462 */
463 function showPrevNext( &$out ) {
464 global $wgContLang,$wgRequest;
465 $pieces = array();
466 $pieces[] = 'type=' . urlencode( $this->reader->queryType() );
467 $pieces[] = 'user=' . urlencode( $this->reader->queryUser() );
468 $pieces[] = 'page=' . urlencode( $this->reader->queryTitle() );
469 $pieces[] = 'pattern=' . urlencode( $this->reader->queryPattern() );
470 $bits = implode( '&', $pieces );
471 list( $limit, $offset ) = $wgRequest->getLimitOffset();
472
473 # TODO: use timestamps instead of offsets to make it more natural
474 # to go huge distances in time
475 $html = wfViewPrevNext( $offset, $limit,
476 $wgContLang->specialpage( 'Log' ),
477 $bits,
478 $this->numResults < $limit);
479 $out->addHTML( '<p>' . $html . '</p>' );
480 }
481 }
482
483
484 ?>