2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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.
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.
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
22 * @addtogroup SpecialPage
28 function wfSpecialLog( $par = '' ) {
30 $logReader = new LogReader( $wgRequest );
31 if( $wgRequest->getVal( 'type' ) == '' && $par != '' ) {
32 $logReader->limitType( $par );
34 $logViewer = new LogViewer( $logReader );
40 * @addtogroup SpecialPage
43 var $db, $joinClauses, $whereClauses;
44 var $type = '', $user = '', $title = null, $pattern = false;
47 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
49 function LogReader( $request ) {
50 $this->db
= wfGetDB( DB_SLAVE
);
51 $this->setupQuery( $request );
55 * Basic setup and applies the limiting factors from the WebRequest object.
56 * @param WebRequest $request
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();
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' ), '<=' );
73 list( $this->limit
, $this->offset
) = $request->getLimitOffset();
75 // XXX This all needs to use Pager, ugly hack for now.
78 $this->offset
= min( $this->offset
, 10000 );
82 * Set the log reader to return only entries of the given type.
83 * @param string $type A log type ('upload', 'delete', etc)
86 function limitType( $type ) {
87 if( empty( $type ) ) {
91 $safetype = $this->db
->strencode( $type );
92 $this->whereClauses
[] = "log_type='$safetype'";
96 * Set the log reader to return only entries by the given user.
97 * @param string $name (In)valid user name
100 function limitUser( $name ) {
103 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
104 if ( is_null( $usertitle ) )
106 $this->user
= $usertitle->getText();
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
));
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";
115 $this->whereClauses
[] = "log_user=$userid";
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
124 function limitTitle( $page , $pattern ) {
126 $title = Title
::newFromText( $page );
128 if( strlen( $page ) == 0 ||
!$title instanceof Title
)
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%'";
138 $safetitle = $this->db
->strencode( $title->getDBkey() );
139 $this->whereClauses
[] = "log_namespace=$ns AND log_title = '$safetitle'";
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
149 function limitTime( $time, $direction ) {
150 # Direction should be a comparison operator
151 if( empty( $time ) ) {
154 $safetime = $this->db
->strencode( wfTimestamp( TS_MW
, $time ) );
155 $this->whereClauses
[] = "log_timestamp $direction '$safetime'";
159 * Build an SQL query from all the set parameters.
160 * @return string the SQL query
163 function getQuery() {
164 $logging = $this->db
->tableName( "logging" );
165 $sql = "SELECT /*! STRAIGHT_JOIN */ log_type, log_action, log_timestamp,
167 log_namespace, log_title, page_id,
168 log_comment, log_params FROM $logging ";
169 if( !empty( $this->joinClauses
) ) {
170 $sql .= implode( ' ', $this->joinClauses
);
172 if( !empty( $this->whereClauses
) ) {
173 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses
);
175 $sql .= " ORDER BY log_timestamp DESC ";
176 $sql = $this->db
->limitResult($sql, $this->limit
, $this->offset
);
181 * Execute the query and start returning results.
182 * @return ResultWrapper result object to return the relevant rows
185 $res = $this->db
->query( $this->getQuery(), 'LogReader::getRows' );
186 return $this->db
->resultObject( $res );
190 * @return string The query type that this LogReader has been limited to.
192 function queryType() {
197 * @return string The username type that this LogReader has been limited to, if any.
199 function queryUser() {
204 * @return boolean The checkbox, if titles should be searched by a pattern too
206 function queryPattern() {
207 return $this->pattern
;
211 * @return string The text of the title that this LogReader has been limited to.
213 function queryTitle() {
214 if( is_null( $this->title
) ) {
217 return $this->title
->getPrefixedText();
222 * Is there at least one row?
226 public function hasRows() {
228 $limit = $this->limit
;
230 $res = $this->db
->query( $this->getQuery() );
231 $this->limit
= $limit;
232 $ret = $this->db
->numRows( $res ) > 0;
233 $this->db
->freeResult( $res );
241 * @addtogroup SpecialPage
245 * @var LogReader $reader
251 * @param LogReader &$reader where to get our data from
253 function LogViewer( &$reader ) {
255 $this->skin
= $wgUser->getSkin();
256 $this->reader
=& $reader;
260 * Take over the whole output page in $wgOut with the log display.
264 $this->showHeader( $wgOut );
265 $this->showOptions( $wgOut );
266 $result = $this->getLogRows();
267 if ( $this->numResults
> 0 ) {
268 $this->showPrevNext( $wgOut );
269 $this->doShowList( $wgOut, $result );
270 $this->showPrevNext( $wgOut );
272 $this->showError( $wgOut );
277 * Load the data from the linked LogReader
278 * Preload the link cache
279 * Initialise numResults
281 * Must be called before calling showPrevNext
283 * @return object database result set
285 function getLogRows() {
286 $result = $this->reader
->getRows();
287 $this->numResults
= 0;
289 // Fetch results and form a batch link existence query
290 $batch = new LinkBatch
;
291 while ( $s = $result->fetchObject() ) {
293 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $s->user_name
) );
294 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $s->user_name
) );
296 // Move destination link
297 if ( $s->log_type
== 'move' ) {
298 $paramArray = LogPage
::extractParams( $s->log_params
);
299 $title = Title
::newFromText( $paramArray[0] );
300 $batch->addObj( $title );
311 * Output just the list of entries given by the linked LogReader,
312 * with extraneous UI elements. Use for displaying log fragments in
313 * another page (eg at Special:Undelete)
314 * @param OutputPage $out where to send output
316 function showList( &$out ) {
317 $result = $this->getLogRows();
318 if ( $this->numResults
> 0 ) {
319 $this->doShowList( $out, $result );
321 $this->showError( $out );
325 function doShowList( &$out, $result ) {
326 // Rewind result pointer and go through it again, making the HTML
329 while( $s = $result->fetchObject() ) {
330 $html .= $this->logLine( $s );
332 $html .= "\n</ul>\n";
333 $out->addHTML( $html );
337 function showError( &$out ) {
338 $out->addWikiText( wfMsg( 'logempty' ) );
342 * @param Object $s a single row from the result set
343 * @return string Formatted HTML list item
346 function logLine( $s ) {
347 global $wgLang, $wgUser;;
348 $skin = $wgUser->getSkin();
349 $title = Title
::makeTitle( $s->log_namespace
, $s->log_title
);
350 $time = $wgLang->timeanddate( wfTimestamp(TS_MW
, $s->log_timestamp
), true );
352 // Enter the existence or non-existence of this page into the link cache,
353 // for faster makeLinkObj() in LogPage::actionText()
354 $linkCache =& LinkCache
::singleton();
356 $linkCache->addGoodLinkObj( $s->page_id
, $title );
358 $linkCache->addBadLinkObj( $title );
361 $userLink = $this->skin
->userLink( $s->log_user
, $s->user_name
) . $this->skin
->userToolLinksRedContribs( $s->log_user
, $s->user_name
);
362 $comment = $this->skin
->commentBlock( $s->log_comment
);
363 $paramArray = LogPage
::extractParams( $s->log_params
);
365 // show revertmove link
366 if ( $s->log_type
== 'move' && isset( $paramArray[0] ) ) {
367 $destTitle = Title
::newFromText( $paramArray[0] );
369 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Movepage' ),
370 wfMsg( 'revertmove' ),
371 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
372 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
373 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
374 '&wpMovetalk=0' ) . ')';
376 // show undelete link
377 } elseif ( $s->log_action
== 'delete' && $wgUser->isAllowed( 'delete' ) ) {
378 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Undelete' ),
379 wfMsg( 'undeletebtn' ) ,
380 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
383 } elseif ( $s->log_action
== 'block' && $wgUser->isAllowed( 'block' ) ) {
384 $revert = '(' . $skin->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Ipblocklist' ),
385 wfMsg( 'unblocklink' ),
386 'action=unblock&ip=' . urlencode( $s->log_title
) ) . ')';
387 // show change protection link
388 } elseif ( ( $s->log_action
== 'protect' ||
$s->log_action
== 'modify' ) && $wgUser->isAllowed( 'protect' ) ) {
389 $revert = '(' . $skin->makeKnownLinkObj( $title, wfMsg( 'protect_change' ), 'action=unprotect' ) . ')';
390 // show user tool links for self created users
391 // TODO: The extension should be handling this, get it out of core!
392 } elseif ( $s->log_action
== 'create2' ) {
393 if( isset( $paramArray[0] ) ) {
394 $revert = $this->skin
->userToolLinks( $paramArray[0], $s->log_title
, true );
396 # Fall back to a blue contributions link
397 $revert = $this->skin
->userToolLinks( 1, $s->log_title
);
399 # Suppress $comment from old entries, not needed and can contain incorrect links
403 $action = LogPage
::actionText( $s->log_type
, $s->log_action
, $title, $this->skin
, $paramArray, true, true );
404 $out = "<li>$time $userLink $action $comment $revert</li>\n";
409 * @param OutputPage &$out where to send output
412 function showHeader( &$out ) {
413 $type = $this->reader
->queryType();
414 if( LogPage
::isLogType( $type ) ) {
415 $out->setPageTitle( LogPage
::logName( $type ) );
416 $out->addWikiText( LogPage
::logHeader( $type ) );
421 * @param OutputPage &$out where to send output
424 function showOptions( &$out ) {
425 global $wgScript, $wgMiserMode;
426 $action = htmlspecialchars( $wgScript );
427 $title = SpecialPage
::getTitleFor( 'Log' );
428 $special = htmlspecialchars( $title->getPrefixedDBkey() );
429 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
431 Xml
::element( 'legend', array(), wfMsg( 'log' ) ) .
432 Xml
::hidden( 'title', $special ) . "\n" .
433 $this->getTypeMenu() . "\n" .
434 $this->getUserInput() . "\n" .
435 $this->getTitleInput() . "\n" .
436 (!$wgMiserMode?
($this->getTitlePattern()."\n"):"") .
437 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
438 "</fieldset></form>" );
442 * @return string Formatted HTML
445 function getTypeMenu() {
446 $out = "<select name='type'>\n";
448 $validTypes = LogPage
::validTypes();
449 $m = array(); // Temporary array
451 // First pass to load the log names
452 foreach( $validTypes as $type ) {
453 $text = LogPage
::logName( $type );
457 // Second pass to sort by name
460 // Third pass generates sorted XHTML content
461 foreach( $m as $text => $type ) {
462 $selected = ($type == $this->reader
->queryType());
463 $out .= Xml
::option( $text, $type, $selected ) . "\n";
471 * @return string Formatted HTML
474 function getUserInput() {
475 $user = $this->reader
->queryUser();
476 return Xml
::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 12, $user );
480 * @return string Formatted HTML
483 function getTitleInput() {
484 $title = $this->reader
->queryTitle();
485 return Xml
::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
489 * @return boolean Checkbox
492 function getTitlePattern() {
493 $pattern = $this->reader
->queryPattern();
494 return Xml
::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern );
498 * @param OutputPage &$out where to send output
501 function showPrevNext( &$out ) {
502 global $wgContLang,$wgRequest;
504 $pieces[] = 'type=' . urlencode( $this->reader
->queryType() );
505 $pieces[] = 'user=' . urlencode( $this->reader
->queryUser() );
506 $pieces[] = 'page=' . urlencode( $this->reader
->queryTitle() );
507 $pieces[] = 'pattern=' . urlencode( $this->reader
->queryPattern() );
508 $bits = implode( '&', $pieces );
509 list( $limit, $offset ) = $wgRequest->getLimitOffset();
511 # TODO: use timestamps instead of offsets to make it more natural
512 # to go huge distances in time
513 $html = wfViewPrevNext( $offset, $limit,
514 $wgContLang->specialpage( 'Log' ),
516 $this->numResults
< $limit);
517 $out->addHTML( '<p>' . $html . '</p>' );