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
23 * @subpackage SpecialPage
29 function wfSpecialLog( $par = '' ) {
31 $logReader = new LogReader( $wgRequest );
32 if( $wgRequest->getVal( 'type' ) == '' && $par != '' ) {
33 $logReader->limitType( $par );
35 $logViewer = new LogViewer( $logReader );
42 * @subpackage SpecialPage
45 var $db, $joinClauses, $whereClauses;
46 var $type = '', $user = '', $title = null;
49 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
51 function LogReader( $request ) {
52 $this->db
=& wfGetDB( DB_SLAVE
);
53 $this->setupQuery( $request );
57 * Basic setup and applies the limiting factors from the WebRequest object.
58 * @param WebRequest $request
61 function setupQuery( $request ) {
62 $page = $this->db
->tableName( 'page' );
63 $user = $this->db
->tableName( 'user' );
64 $this->joinClauses
= array(
65 "LEFT OUTER JOIN $page ON log_namespace=page_namespace AND log_title=page_title",
66 "INNER JOIN $user ON user_id=log_user" );
67 $this->whereClauses
= array();
69 $this->limitType( $request->getVal( 'type' ) );
70 $this->limitUser( $request->getText( 'user' ) );
71 $this->limitTitle( $request->getText( 'page' ) );
72 $this->limitTime( $request->getVal( 'from' ), '>=' );
73 $this->limitTime( $request->getVal( 'until' ), '<=' );
75 list( $this->limit
, $this->offset
) = $request->getLimitOffset();
79 * Set the log reader to return only entries of the given type.
80 * @param string $type A log type ('upload', 'delete', etc)
83 function limitType( $type ) {
84 if( empty( $type ) ) {
88 $safetype = $this->db
->strencode( $type );
89 $this->whereClauses
[] = "log_type='$safetype'";
93 * Set the log reader to return only entries by the given user.
94 * @param string $name (In)valid user name
97 function limitUser( $name ) {
100 $usertitle = Title
::makeTitle( NS_USER
, $name );
101 if ( is_null( $usertitle ) )
103 $this->user
= $usertitle->getText();
105 /* Fetch userid at first, if known, provides awesome query plan afterwards */
106 $userid = $this->db
->selectField('user','user_id',array('user_name'=>$this->user
));
108 /* It should be nicer to abort query at all,
109 but for now it won't pass anywhere behind the optimizer */
110 $this->whereClauses
[] = "NULL";
112 $this->whereClauses
[] = "log_user=$userid";
116 * Set the log reader to return only entries affecting the given page.
117 * (For the block and rights logs, this is a user page.)
118 * @param string $page Title name as text
121 function limitTitle( $page ) {
122 $title = Title
::newFromText( $page );
123 if( empty( $page ) ||
is_null( $title ) ) {
126 $this->title
=& $title;
127 $safetitle = $this->db
->strencode( $title->getDBkey() );
128 $ns = $title->getNamespace();
129 $this->whereClauses
[] = "log_namespace=$ns AND log_title='$safetitle'";
133 * Set the log reader to return only entries in a given time range.
134 * @param string $time Timestamp of one endpoint
135 * @param string $direction either ">=" or "<=" operators
138 function limitTime( $time, $direction ) {
139 # Direction should be a comparison operator
140 if( empty( $time ) ) {
143 $safetime = $this->db
->strencode( wfTimestamp( TS_MW
, $time ) );
144 $this->whereClauses
[] = "log_timestamp $direction '$safetime'";
148 * Build an SQL query from all the set parameters.
149 * @return string the SQL query
152 function getQuery() {
153 $logging = $this->db
->tableName( "logging" );
154 $user = $this->db
->tableName( 'user' );
155 $sql = "SELECT /*! STRAIGHT_JOIN */ log_type, log_action, log_timestamp,
157 log_namespace, log_title, page_id,
158 log_comment, log_params FROM $logging ";
159 if( !empty( $this->joinClauses
) ) {
160 $sql .= implode( ' ', $this->joinClauses
);
162 if( !empty( $this->whereClauses
) ) {
163 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses
);
165 $sql .= " ORDER BY log_timestamp DESC ";
166 $sql = $this->db
->limitResult($sql, $this->limit
, $this->offset
);
171 * Execute the query and start returning results.
172 * @return ResultWrapper result object to return the relevant rows
175 $res = $this->db
->query( $this->getQuery(), 'LogReader::getRows' );
176 return $this->db
->resultObject( $res );
180 * @return string The query type that this LogReader has been limited to.
182 function queryType() {
187 * @return string The username type that this LogReader has been limited to, if any.
189 function queryUser() {
194 * @return string The text of the title that this LogReader has been limited to.
196 function queryTitle() {
197 if( is_null( $this->title
) ) {
200 return $this->title
->getPrefixedText();
208 * @subpackage SpecialPage
212 * @var LogReader $reader
218 * @param LogReader &$reader where to get our data from
220 function LogViewer( &$reader ) {
222 $this->skin
=& $wgUser->getSkin();
223 $this->reader
=& $reader;
227 * Take over the whole output page in $wgOut with the log display.
231 $this->showHeader( $wgOut );
232 $this->showOptions( $wgOut );
233 $result = $this->getLogRows();
234 $this->showPrevNext( $wgOut );
235 $this->doShowList( $wgOut, $result );
236 $this->showPrevNext( $wgOut );
240 * Load the data from the linked LogReader
241 * Preload the link cache
242 * Initialise numResults
244 * Must be called before calling showPrevNext
246 * @return object database result set
248 function getLogRows() {
249 $result = $this->reader
->getRows();
250 $this->numResults
= 0;
252 // Fetch results and form a batch link existence query
253 $batch = new LinkBatch
;
254 while ( $s = $result->fetchObject() ) {
256 $title = Title
::makeTitleSafe( NS_USER
, $s->user_name
);
257 $batch->addObj( $title );
259 // Move destination link
260 if ( $s->log_type
== 'move' ) {
261 $paramArray = LogPage
::extractParams( $s->log_params
);
262 $title = Title
::newFromText( $paramArray[0] );
263 $batch->addObj( $title );
274 * Output just the list of entries given by the linked LogReader,
275 * with extraneous UI elements. Use for displaying log fragments in
276 * another page (eg at Special:Undelete)
277 * @param OutputPage $out where to send output
279 function showList( &$out ) {
280 $this->doShowList( $out, $this->getLogRows() );
283 function doShowList( &$out, $result ) {
284 // Rewind result pointer and go through it again, making the HTML
285 if ($this->numResults
> 0) {
288 while( $s = $result->fetchObject() ) {
289 $html .= $this->logLine( $s );
291 $html .= "\n</ul>\n";
292 $out->addHTML( $html );
294 $out->addWikiText( wfMsg( 'logempty' ) );
300 * @param Object $s a single row from the result set
301 * @return string Formatted HTML list item
304 function logLine( $s ) {
306 $title = Title
::makeTitle( $s->log_namespace
, $s->log_title
);
307 $user = Title
::makeTitleSafe( NS_USER
, $s->user_name
);
308 $time = $wgLang->timeanddate( wfTimestamp(TS_MW
, $s->log_timestamp
), true );
310 // Enter the existence or non-existence of this page into the link cache,
311 // for faster makeLinkObj() in LogPage::actionText()
312 $linkCache =& LinkCache
::singleton();
314 $linkCache->addGoodLinkObj( $s->page_id
, $title );
316 $linkCache->addBadLinkObj( $title );
319 $userLink = $this->skin
->userLink( $s->log_user
, $s->user_name
) . $this->skin
->userToolLinks( $s->log_user
, $s->user_name
);
320 $comment = $this->skin
->commentBlock( $s->log_comment
);
321 $paramArray = LogPage
::extractParams( $s->log_params
);
323 if ( $s->log_type
== 'move' && isset( $paramArray[0] ) ) {
324 $specialTitle = Title
::makeTitle( NS_SPECIAL
, 'Movepage' );
325 $destTitle = Title
::newFromText( $paramArray[0] );
327 $revert = '(' . $this->skin
->makeKnownLinkObj( $specialTitle, wfMsg( 'revertmove' ),
328 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
329 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
330 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
331 '&wpMovetalk=0' ) . ')';
335 $action = LogPage
::actionText( $s->log_type
, $s->log_action
, $title, $this->skin
, $paramArray, true, true );
336 $out = "<li>$time $userLink $action $comment $revert</li>\n";
341 * @param OutputPage &$out where to send output
344 function showHeader( &$out ) {
345 $type = $this->reader
->queryType();
346 if( LogPage
::isLogType( $type ) ) {
347 $out->setPageTitle( LogPage
::logName( $type ) );
348 $out->addWikiText( LogPage
::logHeader( $type ) );
353 * @param OutputPage &$out where to send output
356 function showOptions( &$out ) {
358 $action = htmlspecialchars( $wgScript );
359 $title = Title
::makeTitle( NS_SPECIAL
, 'Log' );
360 $special = htmlspecialchars( $title->getPrefixedDBkey() );
361 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
362 "<input type='hidden' name='title' value=\"$special\" />\n" .
363 $this->getTypeMenu() .
364 $this->getUserInput() .
365 $this->getTitleInput() .
366 "<input type='submit' value=\"" . wfMsg( 'allpagessubmit' ) . "\" />" .
371 * @return string Formatted HTML
374 function getTypeMenu() {
375 $out = "<select name='type'>\n";
376 foreach( LogPage
::validTypes() as $type ) {
377 $text = htmlspecialchars( LogPage
::logName( $type ) );
378 $selected = ($type == $this->reader
->queryType()) ?
' selected="selected"' : '';
379 $out .= "<option value=\"$type\"$selected>$text</option>\n";
381 $out .= "</select>\n";
386 * @return string Formatted HTML
389 function getUserInput() {
390 $user = htmlspecialchars( $this->reader
->queryUser() );
391 return wfMsg('specialloguserlabel') . "<input type='text' name='user' size='12' value=\"$user\" />\n";
395 * @return string Formatted HTML
398 function getTitleInput() {
399 $title = htmlspecialchars( $this->reader
->queryTitle() );
400 return wfMsg('speciallogtitlelabel') . "<input type='text' name='page' size='20' value=\"$title\" />\n";
404 * @param OutputPage &$out where to send output
407 function showPrevNext( &$out ) {
408 global $wgContLang,$wgRequest;
410 $pieces[] = 'type=' . urlencode( $this->reader
->queryType() );
411 $pieces[] = 'user=' . urlencode( $this->reader
->queryUser() );
412 $pieces[] = 'page=' . urlencode( $this->reader
->queryTitle() );
413 $bits = implode( '&', $pieces );
414 list( $limit, $offset ) = $wgRequest->getLimitOffset();
416 # TODO: use timestamps instead of offsets to make it more natural
417 # to go huge distances in time
418 $html = wfViewPrevNext( $offset, $limit,
419 $wgContLang->specialpage( 'Log' ),
421 $this->numResults
< $limit);
422 $out->addHTML( '<p>' . $html . '</p>' );