* (bug 11690) Show revert link for page moves in Special:Log to allowed users only
[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 // XXX This all needs to use Pager, ugly hack for now.
76 global $wgMiserMode;
77 if( $wgMiserMode )
78 $this->offset = min( $this->offset, 10000 );
79 }
80
81 /**
82 * Set the log reader to return only entries of the given type.
83 * @param string $type A log type ('upload', 'delete', etc)
84 * @private
85 */
86 function limitType( $type ) {
87 if( empty( $type ) ) {
88 return false;
89 }
90 $this->type = $type;
91 $safetype = $this->db->strencode( $type );
92 $this->whereClauses[] = "log_type='$safetype'";
93 }
94
95 /**
96 * Set the log reader to return only entries by the given user.
97 * @param string $name (In)valid user name
98 * @private
99 */
100 function limitUser( $name ) {
101 if ( $name == '' )
102 return false;
103 $usertitle = Title::makeTitleSafe( NS_USER, $name );
104 if ( is_null( $usertitle ) )
105 return false;
106 $this->user = $usertitle->getText();
107
108 /* Fetch userid at first, if known, provides awesome query plan afterwards */
109 $userid = $this->db->selectField('user','user_id',array('user_name'=>$this->user));
110 if (!$userid)
111 /* It should be nicer to abort query at all,
112 but for now it won't pass anywhere behind the optimizer */
113 $this->whereClauses[] = "NULL";
114 else
115 $this->whereClauses[] = "log_user=$userid";
116 }
117
118 /**
119 * Set the log reader to return only entries affecting the given page.
120 * (For the block and rights logs, this is a user page.)
121 * @param string $page Title name as text
122 * @private
123 */
124 function limitTitle( $page , $pattern ) {
125 global $wgMiserMode;
126 $title = Title::newFromText( $page );
127
128 if( strlen( $page ) == 0 || !$title instanceof Title )
129 return false;
130
131 $this->title =& $title;
132 $this->pattern = $pattern;
133 $ns = $title->getNamespace();
134 if ( $pattern && !$wgMiserMode ) {
135 $safetitle = $this->db->escapeLike( $title->getDBkey() ); // use escapeLike to avoid expensive search patterns like 't%st%'
136 $this->whereClauses[] = "log_namespace=$ns AND log_title LIKE '$safetitle%'";
137 } else {
138 $safetitle = $this->db->strencode( $title->getDBkey() );
139 $this->whereClauses[] = "log_namespace=$ns AND log_title = '$safetitle'";
140 }
141 }
142
143 /**
144 * Set the log reader to return only entries in a given time range.
145 * @param string $time Timestamp of one endpoint
146 * @param string $direction either ">=" or "<=" operators
147 * @private
148 */
149 function limitTime( $time, $direction ) {
150 # Direction should be a comparison operator
151 if( empty( $time ) ) {
152 return false;
153 }
154 $safetime = $this->db->strencode( wfTimestamp( TS_MW, $time ) );
155 $this->whereClauses[] = "log_timestamp $direction '$safetime'";
156 }
157
158 /**
159 * Build an SQL query from all the set parameters.
160 * @return string the SQL query
161 * @private
162 */
163 function getQuery() {
164 $logging = $this->db->tableName( "logging" );
165 $sql = "SELECT /*! STRAIGHT_JOIN */ log_type, log_action, log_timestamp,
166 log_user, user_name,
167 log_namespace, log_title, page_id,
168 log_comment, log_params FROM $logging ";
169 if( !empty( $this->joinClauses ) ) {
170 $sql .= implode( ' ', $this->joinClauses );
171 }
172 if( !empty( $this->whereClauses ) ) {
173 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses );
174 }
175 $sql .= " ORDER BY log_timestamp DESC ";
176 $sql = $this->db->limitResult($sql, $this->limit, $this->offset );
177 return $sql;
178 }
179
180 /**
181 * Execute the query and start returning results.
182 * @return ResultWrapper result object to return the relevant rows
183 */
184 function getRows() {
185 $res = $this->db->query( $this->getQuery(), 'LogReader::getRows' );
186 return $this->db->resultObject( $res );
187 }
188
189 /**
190 * @return string The query type that this LogReader has been limited to.
191 */
192 function queryType() {
193 return $this->type;
194 }
195
196 /**
197 * @return string The username type that this LogReader has been limited to, if any.
198 */
199 function queryUser() {
200 return $this->user;
201 }
202
203 /**
204 * @return boolean The checkbox, if titles should be searched by a pattern too
205 */
206 function queryPattern() {
207 return $this->pattern;
208 }
209
210 /**
211 * @return string The text of the title that this LogReader has been limited to.
212 */
213 function queryTitle() {
214 if( is_null( $this->title ) ) {
215 return '';
216 } else {
217 return $this->title->getPrefixedText();
218 }
219 }
220
221 /**
222 * Is there at least one row?
223 *
224 * @return bool
225 */
226 public function hasRows() {
227 # Little hack...
228 $limit = $this->limit;
229 $this->limit = 1;
230 $res = $this->db->query( $this->getQuery() );
231 $this->limit = $limit;
232 $ret = $this->db->numRows( $res ) > 0;
233 $this->db->freeResult( $res );
234 return $ret;
235 }
236
237 }
238
239 /**
240 *
241 * @addtogroup SpecialPage
242 */
243 class LogViewer {
244 const NO_ACTION_LINK = 1;
245
246 /**
247 * @var LogReader $reader
248 */
249 var $reader;
250 var $numResults = 0;
251 var $flags = 0;
252
253 /**
254 * @param LogReader &$reader where to get our data from
255 * @param integer $flags Bitwise combination of flags:
256 * self::NO_ACTION_LINK Don't show restore/unblock/block links
257 */
258 function LogViewer( &$reader, $flags = 0 ) {
259 global $wgUser;
260 $this->skin = $wgUser->getSkin();
261 $this->reader =& $reader;
262 $this->flags = $flags;
263 }
264
265 /**
266 * Take over the whole output page in $wgOut with the log display.
267 */
268 function show() {
269 global $wgOut;
270 $this->showHeader( $wgOut );
271 $this->showOptions( $wgOut );
272 $result = $this->getLogRows();
273 if ( $this->numResults > 0 ) {
274 $this->showPrevNext( $wgOut );
275 $this->doShowList( $wgOut, $result );
276 $this->showPrevNext( $wgOut );
277 } else {
278 $this->showError( $wgOut );
279 }
280 }
281
282 /**
283 * Load the data from the linked LogReader
284 * Preload the link cache
285 * Initialise numResults
286 *
287 * Must be called before calling showPrevNext
288 *
289 * @return object database result set
290 */
291 function getLogRows() {
292 $result = $this->reader->getRows();
293 $this->numResults = 0;
294
295 // Fetch results and form a batch link existence query
296 $batch = new LinkBatch;
297 while ( $s = $result->fetchObject() ) {
298 // User link
299 $batch->addObj( Title::makeTitleSafe( NS_USER, $s->user_name ) );
300 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $s->user_name ) );
301
302 // Move destination link
303 if ( $s->log_type == 'move' ) {
304 $paramArray = LogPage::extractParams( $s->log_params );
305 $title = Title::newFromText( $paramArray[0] );
306 $batch->addObj( $title );
307 }
308 ++$this->numResults;
309 }
310 $batch->execute();
311
312 return $result;
313 }
314
315
316 /**
317 * Output just the list of entries given by the linked LogReader,
318 * with extraneous UI elements. Use for displaying log fragments in
319 * another page (eg at Special:Undelete)
320 * @param OutputPage $out where to send output
321 */
322 function showList( &$out ) {
323 $result = $this->getLogRows();
324 if ( $this->numResults > 0 ) {
325 $this->doShowList( $out, $result );
326 } else {
327 $this->showError( $out );
328 }
329 }
330
331 function doShowList( &$out, $result ) {
332 // Rewind result pointer and go through it again, making the HTML
333 $html = "\n<ul>\n";
334 $result->seek( 0 );
335 while( $s = $result->fetchObject() ) {
336 $html .= $this->logLine( $s );
337 }
338 $html .= "\n</ul>\n";
339 $out->addHTML( $html );
340 $result->free();
341 }
342
343 function showError( &$out ) {
344 $out->addWikiText( wfMsg( 'logempty' ) );
345 }
346
347 /**
348 * @param Object $s a single row from the result set
349 * @return string Formatted HTML list item
350 * @private
351 */
352 function logLine( $s ) {
353 global $wgLang, $wgUser, $wgContLang;
354 $skin = $wgUser->getSkin();
355 $title = Title::makeTitle( $s->log_namespace, $s->log_title );
356 $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $s->log_timestamp), true );
357
358 // Enter the existence or non-existence of this page into the link cache,
359 // for faster makeLinkObj() in LogPage::actionText()
360 $linkCache =& LinkCache::singleton();
361 if( $s->page_id ) {
362 $linkCache->addGoodLinkObj( $s->page_id, $title );
363 } else {
364 $linkCache->addBadLinkObj( $title );
365 }
366
367 $userLink = $this->skin->userLink( $s->log_user, $s->user_name ) . $this->skin->userToolLinksRedContribs( $s->log_user, $s->user_name );
368 $comment = $wgContLang->getDirMark() . $this->skin->commentBlock( $s->log_comment );
369 $paramArray = LogPage::extractParams( $s->log_params );
370 $revert = '';
371 // show revertmove link
372 if ( !( $this->flags & self::NO_ACTION_LINK ) ) {
373 if ( $s->log_type == 'move' && isset( $paramArray[0] ) && $wgUser->isAllowed( 'move' ) ) {
374 $destTitle = Title::newFromText( $paramArray[0] );
375 if ( $destTitle ) {
376 $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Movepage' ),
377 wfMsg( 'revertmove' ),
378 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
379 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
380 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
381 '&wpMovetalk=0' ) . ')';
382 }
383 // show undelete link
384 } elseif ( $s->log_action == 'delete' && $wgUser->isAllowed( 'delete' ) ) {
385 $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Undelete' ),
386 wfMsg( 'undeletebtn' ) ,
387 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
388 // show unblock link
389 } elseif ( $s->log_action == 'block' && $wgUser->isAllowed( 'block' ) ) {
390 $revert = '(' . $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Ipblocklist' ),
391 wfMsg( 'unblocklink' ),
392 'action=unblock&ip=' . urlencode( $s->log_title ) ) . ')';
393 // show change protection link
394 } elseif ( ( $s->log_action == 'protect' || $s->log_action == 'modify' ) && $wgUser->isAllowed( 'protect' ) ) {
395 $revert = '(' . $skin->makeKnownLinkObj( $title, wfMsg( 'protect_change' ), 'action=unprotect' ) . ')';
396 // show user tool links for self created users
397 // TODO: The extension should be handling this, get it out of core!
398 } elseif ( $s->log_action == 'create2' ) {
399 if( isset( $paramArray[0] ) ) {
400 $revert = $this->skin->userToolLinks( $paramArray[0], $s->log_title, true );
401 } else {
402 # Fall back to a blue contributions link
403 $revert = $this->skin->userToolLinks( 1, $s->log_title );
404 }
405 # Suppress $comment from old entries, not needed and can contain incorrect links
406 $comment = '';
407 }
408 }
409
410 $action = LogPage::actionText( $s->log_type, $s->log_action, $title, $this->skin, $paramArray, true );
411 $out = "<li>$time $userLink $action $comment $revert</li>\n";
412 return $out;
413 }
414
415 /**
416 * @param OutputPage &$out where to send output
417 * @private
418 */
419 function showHeader( &$out ) {
420 $type = $this->reader->queryType();
421 if( LogPage::isLogType( $type ) ) {
422 $out->setPageTitle( LogPage::logName( $type ) );
423 $out->addWikiText( LogPage::logHeader( $type ) );
424 }
425 }
426
427 /**
428 * @param OutputPage &$out where to send output
429 * @private
430 */
431 function showOptions( &$out ) {
432 global $wgScript, $wgMiserMode;
433 $action = htmlspecialchars( $wgScript );
434 $title = SpecialPage::getTitleFor( 'Log' );
435 $special = htmlspecialchars( $title->getPrefixedDBkey() );
436 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
437 '<fieldset>' .
438 Xml::element( 'legend', array(), wfMsg( 'log' ) ) .
439 Xml::hidden( 'title', $special ) . "\n" .
440 $this->getTypeMenu() . "\n" .
441 $this->getUserInput() . "\n" .
442 $this->getTitleInput() . "\n" .
443 (!$wgMiserMode?($this->getTitlePattern()."\n"):"") .
444 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
445 "</fieldset></form>" );
446 }
447
448 /**
449 * @return string Formatted HTML
450 * @private
451 */
452 function getTypeMenu() {
453 $out = "<select name='type'>\n";
454
455 $validTypes = LogPage::validTypes();
456 $m = array(); // Temporary array
457
458 // First pass to load the log names
459 foreach( $validTypes as $type ) {
460 $text = LogPage::logName( $type );
461 $m[$text] = $type;
462 }
463
464 // Second pass to sort by name
465 ksort($m);
466
467 // Third pass generates sorted XHTML content
468 foreach( $m as $text => $type ) {
469 $selected = ($type == $this->reader->queryType());
470 $out .= Xml::option( $text, $type, $selected ) . "\n";
471 }
472
473 $out .= '</select>';
474 return $out;
475 }
476
477 /**
478 * @return string Formatted HTML
479 * @private
480 */
481 function getUserInput() {
482 $user = $this->reader->queryUser();
483 return Xml::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 12, $user );
484 }
485
486 /**
487 * @return string Formatted HTML
488 * @private
489 */
490 function getTitleInput() {
491 $title = $this->reader->queryTitle();
492 return Xml::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
493 }
494
495 /**
496 * @return boolean Checkbox
497 * @private
498 */
499 function getTitlePattern() {
500 $pattern = $this->reader->queryPattern();
501 return Xml::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern );
502 }
503
504 /**
505 * @param OutputPage &$out where to send output
506 * @private
507 */
508 function showPrevNext( &$out ) {
509 global $wgContLang,$wgRequest;
510 $pieces = array();
511 $pieces[] = 'type=' . urlencode( $this->reader->queryType() );
512 $pieces[] = 'user=' . urlencode( $this->reader->queryUser() );
513 $pieces[] = 'page=' . urlencode( $this->reader->queryTitle() );
514 $pieces[] = 'pattern=' . urlencode( $this->reader->queryPattern() );
515 $bits = implode( '&', $pieces );
516 list( $limit, $offset ) = $wgRequest->getLimitOffset();
517
518 # TODO: use timestamps instead of offsets to make it more natural
519 # to go huge distances in time
520 $html = wfViewPrevNext( $offset, $limit,
521 $wgContLang->specialpage( 'Log' ),
522 $bits,
523 $this->numResults < $limit);
524 $out->addHTML( '<p>' . $html . '</p>' );
525 }
526 }