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;
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' ) );
70 $this->limitTime( $request->getVal( 'from' ), '>=' );
71 $this->limitTime( $request->getVal( 'until' ), '<=' );
73 list( $this->limit
, $this->offset
) = $request->getLimitOffset();
77 * Set the log reader to return only entries of the given type.
78 * @param string $type A log type ('upload', 'delete', etc)
81 function limitType( $type ) {
82 if( empty( $type ) ) {
86 $safetype = $this->db
->strencode( $type );
87 $this->whereClauses
[] = "log_type='$safetype'";
91 * Set the log reader to return only entries by the given user.
92 * @param string $name (In)valid user name
95 function limitUser( $name ) {
98 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
99 if ( is_null( $usertitle ) )
101 $this->user
= $usertitle->getText();
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
));
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";
110 $this->whereClauses
[] = "log_user=$userid";
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
119 function limitTitle( $page ) {
120 $title = Title
::newFromText( $page );
121 if( empty( $page ) ||
is_null( $title ) ) {
124 $this->title
=& $title;
125 $safetitle = $this->db
->strencode( $title->getDBkey() );
126 $ns = $title->getNamespace();
127 $this->whereClauses
[] = "log_namespace=$ns AND log_title='$safetitle'";
131 * Set the log reader to return only entries in a given time range.
132 * @param string $time Timestamp of one endpoint
133 * @param string $direction either ">=" or "<=" operators
136 function limitTime( $time, $direction ) {
137 # Direction should be a comparison operator
138 if( empty( $time ) ) {
141 $safetime = $this->db
->strencode( wfTimestamp( TS_MW
, $time ) );
142 $this->whereClauses
[] = "log_timestamp $direction '$safetime'";
146 * Build an SQL query from all the set parameters.
147 * @return string the SQL query
150 function getQuery() {
151 $logging = $this->db
->tableName( "logging" );
152 $sql = "SELECT /*! STRAIGHT_JOIN */ log_type, log_action, log_timestamp,
154 log_namespace, log_title, page_id,
155 log_comment, log_params FROM $logging ";
156 if( !empty( $this->joinClauses
) ) {
157 $sql .= implode( ' ', $this->joinClauses
);
159 if( !empty( $this->whereClauses
) ) {
160 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses
);
162 $sql .= " ORDER BY log_timestamp DESC ";
163 $sql = $this->db
->limitResult($sql, $this->limit
, $this->offset
);
168 * Execute the query and start returning results.
169 * @return ResultWrapper result object to return the relevant rows
172 $res = $this->db
->query( $this->getQuery(), 'LogReader::getRows' );
173 return $this->db
->resultObject( $res );
177 * @return string The query type that this LogReader has been limited to.
179 function queryType() {
184 * @return string The username type that this LogReader has been limited to, if any.
186 function queryUser() {
191 * @return string The text of the title that this LogReader has been limited to.
193 function queryTitle() {
194 if( is_null( $this->title
) ) {
197 return $this->title
->getPrefixedText();
204 * @addtogroup SpecialPage
208 * @var LogReader $reader
214 * @param LogReader &$reader where to get our data from
216 function LogViewer( &$reader ) {
218 $this->skin
= $wgUser->getSkin();
219 $this->reader
=& $reader;
223 * Take over the whole output page in $wgOut with the log display.
227 $this->showHeader( $wgOut );
228 $this->showOptions( $wgOut );
229 $result = $this->getLogRows();
230 $this->showPrevNext( $wgOut );
231 $this->doShowList( $wgOut, $result );
232 $this->showPrevNext( $wgOut );
236 * Load the data from the linked LogReader
237 * Preload the link cache
238 * Initialise numResults
240 * Must be called before calling showPrevNext
242 * @return object database result set
244 function getLogRows() {
245 $result = $this->reader
->getRows();
246 $this->numResults
= 0;
248 // Fetch results and form a batch link existence query
249 $batch = new LinkBatch
;
250 while ( $s = $result->fetchObject() ) {
252 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $s->user_name
) );
253 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $s->user_name
) );
255 // Move destination link
256 if ( $s->log_type
== 'move' ) {
257 $paramArray = LogPage
::extractParams( $s->log_params
);
258 $title = Title
::newFromText( $paramArray[0] );
259 $batch->addObj( $title );
270 * Output just the list of entries given by the linked LogReader,
271 * with extraneous UI elements. Use for displaying log fragments in
272 * another page (eg at Special:Undelete)
273 * @param OutputPage $out where to send output
275 function showList( &$out ) {
276 $this->doShowList( $out, $this->getLogRows() );
279 function doShowList( &$out, $result ) {
280 // Rewind result pointer and go through it again, making the HTML
281 if ($this->numResults
> 0) {
284 while( $s = $result->fetchObject() ) {
285 $html .= $this->logLine( $s );
287 $html .= "\n</ul>\n";
288 $out->addHTML( $html );
290 $out->addWikiText( wfMsg( 'logempty' ) );
296 * @param Object $s a single row from the result set
297 * @return string Formatted HTML list item
300 function logLine( $s ) {
301 global $wgLang, $wgUser;;
302 $skin = $wgUser->getSkin();
303 $title = Title
::makeTitle( $s->log_namespace
, $s->log_title
);
304 $time = $wgLang->timeanddate( wfTimestamp(TS_MW
, $s->log_timestamp
), true );
306 // Enter the existence or non-existence of this page into the link cache,
307 // for faster makeLinkObj() in LogPage::actionText()
308 $linkCache =& LinkCache
::singleton();
310 $linkCache->addGoodLinkObj( $s->page_id
, $title );
312 $linkCache->addBadLinkObj( $title );
315 $userLink = $this->skin
->userLink( $s->log_user
, $s->user_name
) . $this->skin
->userToolLinksRedContribs( $s->log_user
, $s->user_name
);
316 $comment = $this->skin
->commentBlock( $s->log_comment
);
317 $paramArray = LogPage
::extractParams( $s->log_params
);
319 // show revertmove link
320 if ( $s->log_type
== 'move' && isset( $paramArray[0] ) ) {
321 $destTitle = Title
::newFromText( $paramArray[0] );
323 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Movepage' ),
324 wfMsg( 'revertmove' ),
325 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
326 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
327 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
328 '&wpMovetalk=0' ) . ')';
330 // show undelete link
331 } elseif ( $s->log_action
== 'delete' && $wgUser->isAllowed( 'delete' ) ) {
332 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Undelete' ),
333 wfMsg( 'undeletebtn' ) ,
334 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
337 } elseif ( $s->log_action
== 'block' && $wgUser->isAllowed( 'block' ) ) {
338 $revert = '(' . $skin->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Ipblocklist' ),
339 wfMsg( 'unblocklink' ),
340 'action=unblock&ip=' . urlencode( $s->log_title
) ) . ')';
341 // show change protection link
342 } elseif ( $s->log_action
== 'protect' && $wgUser->isAllowed( 'protect' ) ) {
343 $revert = '(' . $skin->makeKnownLink( $title->getPrefixedDBkey() ,
344 wfMsg( 'protect_change' ),
345 'action=unprotect' ) . ')';
348 $action = LogPage
::actionText( $s->log_type
, $s->log_action
, $title, $this->skin
, $paramArray, true, true );
349 $out = "<li>$time $userLink $action $comment $revert</li>\n";
354 * @param OutputPage &$out where to send output
357 function showHeader( &$out ) {
358 $type = $this->reader
->queryType();
359 if( LogPage
::isLogType( $type ) ) {
360 $out->setPageTitle( LogPage
::logName( $type ) );
361 $out->addWikiText( LogPage
::logHeader( $type ) );
366 * @param OutputPage &$out where to send output
369 function showOptions( &$out ) {
371 $action = htmlspecialchars( $wgScript );
372 $title = SpecialPage
::getTitleFor( 'Log' );
373 $special = htmlspecialchars( $title->getPrefixedDBkey() );
374 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
375 Xml
::hidden( 'title', $special ) . "\n" .
376 $this->getTypeMenu() . "\n" .
377 $this->getUserInput() . "\n" .
378 $this->getTitleInput() . "\n" .
379 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
384 * @return string Formatted HTML
387 function getTypeMenu() {
388 $out = "<select name='type'>\n";
390 $validTypes = LogPage
::validTypes();
391 $m = array(); // Temporary array
393 // First pass to load the log names
394 foreach( $validTypes as $type ) {
395 $text = LogPage
::logName( $type );
399 // Second pass to sort by name
402 // Third pass generates sorted XHTML content
403 foreach( $m as $text => $type ) {
404 $selected = ($type == $this->reader
->queryType());
405 $out .= Xml
::option( $text, $type, $selected ) . "\n";
413 * @return string Formatted HTML
416 function getUserInput() {
417 $user = $this->reader
->queryUser();
418 return Xml
::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 12, $user );
422 * @return string Formatted HTML
425 function getTitleInput() {
426 $title = $this->reader
->queryTitle();
427 return Xml
::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
431 * @param OutputPage &$out where to send output
434 function showPrevNext( &$out ) {
435 global $wgContLang,$wgRequest;
437 $pieces[] = 'type=' . urlencode( $this->reader
->queryType() );
438 $pieces[] = 'user=' . urlencode( $this->reader
->queryUser() );
439 $pieces[] = 'page=' . urlencode( $this->reader
->queryTitle() );
440 $bits = implode( '&', $pieces );
441 list( $limit, $offset ) = $wgRequest->getLimitOffset();
443 # TODO: use timestamps instead of offsets to make it more natural
444 # to go huge distances in time
445 $html = wfViewPrevNext( $offset, $limit,
446 $wgContLang->specialpage( 'Log' ),
448 $this->numResults
< $limit);
449 $out->addHTML( '<p>' . $html . '</p>' );