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 );
127 if( empty( $page ) ||
is_null( $title ) ) {
130 $this->title
=& $title;
131 $this->pattern
= $pattern;
132 $ns = $title->getNamespace();
133 if ( $pattern && !$wgMiserMode ) {
134 $safetitle = $this->db
->escapeLike( $title->getDBkey() ); // use escapeLike to avoid expensive search patterns like 't%st%'
135 $this->whereClauses
[] = "log_namespace=$ns AND log_title LIKE '$safetitle%'";
137 $safetitle = $this->db
->strencode( $title->getDBkey() );
138 $this->whereClauses
[] = "log_namespace=$ns AND log_title = '$safetitle'";
143 * Set the log reader to return only entries in a given time range.
144 * @param string $time Timestamp of one endpoint
145 * @param string $direction either ">=" or "<=" operators
148 function limitTime( $time, $direction ) {
149 # Direction should be a comparison operator
150 if( empty( $time ) ) {
153 $safetime = $this->db
->strencode( wfTimestamp( TS_MW
, $time ) );
154 $this->whereClauses
[] = "log_timestamp $direction '$safetime'";
158 * Build an SQL query from all the set parameters.
159 * @return string the SQL query
162 function getQuery() {
163 $logging = $this->db
->tableName( "logging" );
164 $sql = "SELECT /*! STRAIGHT_JOIN */ log_type, log_action, log_timestamp,
166 log_namespace, log_title, page_id,
167 log_comment, log_params FROM $logging ";
168 if( !empty( $this->joinClauses
) ) {
169 $sql .= implode( ' ', $this->joinClauses
);
171 if( !empty( $this->whereClauses
) ) {
172 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses
);
174 $sql .= " ORDER BY log_timestamp DESC ";
175 $sql = $this->db
->limitResult($sql, $this->limit
, $this->offset
);
180 * Execute the query and start returning results.
181 * @return ResultWrapper result object to return the relevant rows
184 $res = $this->db
->query( $this->getQuery(), 'LogReader::getRows' );
185 return $this->db
->resultObject( $res );
189 * @return string The query type that this LogReader has been limited to.
191 function queryType() {
196 * @return string The username type that this LogReader has been limited to, if any.
198 function queryUser() {
203 * @return boolean The checkbox, if titles should be searched by a pattern too
205 function queryPattern() {
206 return $this->pattern
;
210 * @return string The text of the title that this LogReader has been limited to.
212 function queryTitle() {
213 if( is_null( $this->title
) ) {
216 return $this->title
->getPrefixedText();
221 * Is there at least one row?
225 public function hasRows() {
227 $limit = $this->limit
;
229 $res = $this->db
->query( $this->getQuery() );
230 $this->limit
= $limit;
231 $ret = $this->db
->numRows( $res ) > 0;
232 $this->db
->freeResult( $res );
240 * @addtogroup SpecialPage
244 * @var LogReader $reader
250 * @param LogReader &$reader where to get our data from
252 function LogViewer( &$reader ) {
254 $this->skin
= $wgUser->getSkin();
255 $this->reader
=& $reader;
259 * Take over the whole output page in $wgOut with the log display.
263 $this->showHeader( $wgOut );
264 $this->showOptions( $wgOut );
265 $result = $this->getLogRows();
266 if ( $this->numResults
> 0 ) {
267 $this->showPrevNext( $wgOut );
268 $this->doShowList( $wgOut, $result );
269 $this->showPrevNext( $wgOut );
271 $this->showError( $wgOut );
276 * Load the data from the linked LogReader
277 * Preload the link cache
278 * Initialise numResults
280 * Must be called before calling showPrevNext
282 * @return object database result set
284 function getLogRows() {
285 $result = $this->reader
->getRows();
286 $this->numResults
= 0;
288 // Fetch results and form a batch link existence query
289 $batch = new LinkBatch
;
290 while ( $s = $result->fetchObject() ) {
292 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $s->user_name
) );
293 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $s->user_name
) );
295 // Move destination link
296 if ( $s->log_type
== 'move' ) {
297 $paramArray = LogPage
::extractParams( $s->log_params
);
298 $title = Title
::newFromText( $paramArray[0] );
299 $batch->addObj( $title );
310 * Output just the list of entries given by the linked LogReader,
311 * with extraneous UI elements. Use for displaying log fragments in
312 * another page (eg at Special:Undelete)
313 * @param OutputPage $out where to send output
315 function showList( &$out ) {
316 $result = $this->getLogRows();
317 if ( $this->numResults
> 0 ) {
318 $this->doShowList( $out, $result );
320 $this->showError( $out );
324 function doShowList( &$out, $result ) {
325 // Rewind result pointer and go through it again, making the HTML
328 while( $s = $result->fetchObject() ) {
329 $html .= $this->logLine( $s );
331 $html .= "\n</ul>\n";
332 $out->addHTML( $html );
336 function showError( &$out ) {
337 $out->addWikiText( wfMsg( 'logempty' ) );
341 * @param Object $s a single row from the result set
342 * @return string Formatted HTML list item
345 function logLine( $s ) {
346 global $wgLang, $wgUser;;
347 $skin = $wgUser->getSkin();
348 $title = Title
::makeTitle( $s->log_namespace
, $s->log_title
);
349 $time = $wgLang->timeanddate( wfTimestamp(TS_MW
, $s->log_timestamp
), true );
351 // Enter the existence or non-existence of this page into the link cache,
352 // for faster makeLinkObj() in LogPage::actionText()
353 $linkCache =& LinkCache
::singleton();
355 $linkCache->addGoodLinkObj( $s->page_id
, $title );
357 $linkCache->addBadLinkObj( $title );
360 $userLink = $this->skin
->userLink( $s->log_user
, $s->user_name
) . $this->skin
->userToolLinksRedContribs( $s->log_user
, $s->user_name
);
361 $comment = $this->skin
->commentBlock( $s->log_comment
);
362 $paramArray = LogPage
::extractParams( $s->log_params
);
364 // show revertmove link
365 if ( $s->log_type
== 'move' && isset( $paramArray[0] ) ) {
366 $destTitle = Title
::newFromText( $paramArray[0] );
368 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Movepage' ),
369 wfMsg( 'revertmove' ),
370 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
371 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
372 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
373 '&wpMovetalk=0' ) . ')';
375 // show undelete link
376 } elseif ( $s->log_action
== 'delete' && $wgUser->isAllowed( 'delete' ) ) {
377 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Undelete' ),
378 wfMsg( 'undeletebtn' ) ,
379 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
382 } elseif ( $s->log_action
== 'block' && $wgUser->isAllowed( 'block' ) ) {
383 $revert = '(' . $skin->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Ipblocklist' ),
384 wfMsg( 'unblocklink' ),
385 'action=unblock&ip=' . urlencode( $s->log_title
) ) . ')';
386 // show change protection link
387 } elseif ( $s->log_action
== 'protect' && $wgUser->isAllowed( 'protect' ) ) {
388 $revert = '(' . $skin->makeKnownLink( $title->getPrefixedDBkey() ,
389 wfMsg( 'protect_change' ),
390 'action=unprotect' ) . ')';
391 // show user tool links for self created users
392 } elseif ( $s->log_action
== 'create2' ) {
393 $revert = $this->skin
->userToolLinksRedContribs( $s->log_user
, $s->log_title
);
394 // do not show $comment for self created accounts. It includes wrong user tool links:
395 // 'blockip' for users w/o block allowance and broken links for very long usernames (bug 4756)
399 $action = LogPage
::actionText( $s->log_type
, $s->log_action
, $title, $this->skin
, $paramArray, true, true );
400 $out = "<li>$time $userLink $action $comment $revert</li>\n";
405 * @param OutputPage &$out where to send output
408 function showHeader( &$out ) {
409 $type = $this->reader
->queryType();
410 if( LogPage
::isLogType( $type ) ) {
411 $out->setPageTitle( LogPage
::logName( $type ) );
412 $out->addWikiText( LogPage
::logHeader( $type ) );
417 * @param OutputPage &$out where to send output
420 function showOptions( &$out ) {
421 global $wgScript, $wgMiserMode;
422 $action = htmlspecialchars( $wgScript );
423 $title = SpecialPage
::getTitleFor( 'Log' );
424 $special = htmlspecialchars( $title->getPrefixedDBkey() );
425 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
427 Xml
::element( 'legend', array(), wfMsg( 'log' ) ) .
428 Xml
::hidden( 'title', $special ) . "\n" .
429 $this->getTypeMenu() . "\n" .
430 $this->getUserInput() . "\n" .
431 $this->getTitleInput() . "\n" .
432 (!$wgMiserMode?
($this->getTitlePattern()."\n"):"") .
433 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
434 "</fieldset></form>" );
438 * @return string Formatted HTML
441 function getTypeMenu() {
442 $out = "<select name='type'>\n";
444 $validTypes = LogPage
::validTypes();
445 $m = array(); // Temporary array
447 // First pass to load the log names
448 foreach( $validTypes as $type ) {
449 $text = LogPage
::logName( $type );
453 // Second pass to sort by name
456 // Third pass generates sorted XHTML content
457 foreach( $m as $text => $type ) {
458 $selected = ($type == $this->reader
->queryType());
459 $out .= Xml
::option( $text, $type, $selected ) . "\n";
467 * @return string Formatted HTML
470 function getUserInput() {
471 $user = $this->reader
->queryUser();
472 return Xml
::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 12, $user );
476 * @return string Formatted HTML
479 function getTitleInput() {
480 $title = $this->reader
->queryTitle();
481 return Xml
::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
485 * @return boolean Checkbox
488 function getTitlePattern() {
489 $pattern = $this->reader
->queryPattern();
490 return Xml
::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern );
494 * @param OutputPage &$out where to send output
497 function showPrevNext( &$out ) {
498 global $wgContLang,$wgRequest;
500 $pieces[] = 'type=' . urlencode( $this->reader
->queryType() );
501 $pieces[] = 'user=' . urlencode( $this->reader
->queryUser() );
502 $pieces[] = 'page=' . urlencode( $this->reader
->queryTitle() );
503 $pieces[] = 'pattern=' . urlencode( $this->reader
->queryPattern() );
504 $bits = implode( '&', $pieces );
505 list( $limit, $offset ) = $wgRequest->getLimitOffset();
507 # TODO: use timestamps instead of offsets to make it more natural
508 # to go huge distances in time
509 $html = wfViewPrevNext( $offset, $limit,
510 $wgContLang->specialpage( 'Log' ),
512 $this->numResults
< $limit);
513 $out->addHTML( '<p>' . $html . '</p>' );