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
::makeTitleSafe( 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 $sql = "SELECT /*! STRAIGHT_JOIN */ log_type, log_action, log_timestamp,
156 log_namespace, log_title, page_id,
157 log_comment, log_params FROM $logging ";
158 if( !empty( $this->joinClauses
) ) {
159 $sql .= implode( ' ', $this->joinClauses
);
161 if( !empty( $this->whereClauses
) ) {
162 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses
);
164 $sql .= " ORDER BY log_timestamp DESC ";
165 $sql = $this->db
->limitResult($sql, $this->limit
, $this->offset
);
170 * Execute the query and start returning results.
171 * @return ResultWrapper result object to return the relevant rows
174 $res = $this->db
->query( $this->getQuery(), 'LogReader::getRows' );
175 return $this->db
->resultObject( $res );
179 * @return string The query type that this LogReader has been limited to.
181 function queryType() {
186 * @return string The username type that this LogReader has been limited to, if any.
188 function queryUser() {
193 * @return string The text of the title that this LogReader has been limited to.
195 function queryTitle() {
196 if( is_null( $this->title
) ) {
199 return $this->title
->getPrefixedText();
207 * @subpackage SpecialPage
211 * @var LogReader $reader
217 * @param LogReader &$reader where to get our data from
219 function LogViewer( &$reader ) {
221 $this->skin
=& $wgUser->getSkin();
222 $this->reader
=& $reader;
226 * Take over the whole output page in $wgOut with the log display.
230 $this->showHeader( $wgOut );
231 $this->showOptions( $wgOut );
232 $result = $this->getLogRows();
233 $this->showPrevNext( $wgOut );
234 $this->doShowList( $wgOut, $result );
235 $this->showPrevNext( $wgOut );
239 * Load the data from the linked LogReader
240 * Preload the link cache
241 * Initialise numResults
243 * Must be called before calling showPrevNext
245 * @return object database result set
247 function getLogRows() {
248 $result = $this->reader
->getRows();
249 $this->numResults
= 0;
251 // Fetch results and form a batch link existence query
252 $batch = new LinkBatch
;
253 while ( $s = $result->fetchObject() ) {
255 $title = Title
::makeTitleSafe( NS_USER
, $s->user_name
);
256 $batch->addObj( $title );
258 // Move destination link
259 if ( $s->log_type
== 'move' ) {
260 $paramArray = LogPage
::extractParams( $s->log_params
);
261 $title = Title
::newFromText( $paramArray[0] );
262 $batch->addObj( $title );
273 * Output just the list of entries given by the linked LogReader,
274 * with extraneous UI elements. Use for displaying log fragments in
275 * another page (eg at Special:Undelete)
276 * @param OutputPage $out where to send output
278 function showList( &$out ) {
279 $this->doShowList( $out, $this->getLogRows() );
282 function doShowList( &$out, $result ) {
283 // Rewind result pointer and go through it again, making the HTML
284 if ($this->numResults
> 0) {
287 while( $s = $result->fetchObject() ) {
288 $html .= $this->logLine( $s );
290 $html .= "\n</ul>\n";
291 $out->addHTML( $html );
293 $out->addWikiText( wfMsg( 'logempty' ) );
299 * @param Object $s a single row from the result set
300 * @return string Formatted HTML list item
303 function logLine( $s ) {
305 $title = Title
::makeTitle( $s->log_namespace
, $s->log_title
);
306 $time = $wgLang->timeanddate( wfTimestamp(TS_MW
, $s->log_timestamp
), true );
308 // Enter the existence or non-existence of this page into the link cache,
309 // for faster makeLinkObj() in LogPage::actionText()
310 $linkCache =& LinkCache
::singleton();
312 $linkCache->addGoodLinkObj( $s->page_id
, $title );
314 $linkCache->addBadLinkObj( $title );
317 $userLink = $this->skin
->userLink( $s->log_user
, $s->user_name
) . $this->skin
->userToolLinks( $s->log_user
, $s->user_name
);
318 $comment = $this->skin
->commentBlock( $s->log_comment
);
319 $paramArray = LogPage
::extractParams( $s->log_params
);
321 if ( $s->log_type
== 'move' && isset( $paramArray[0] ) ) {
322 $specialTitle = SpecialPage
::getTitleFor( 'Movepage' );
323 $destTitle = Title
::newFromText( $paramArray[0] );
325 $revert = '(' . $this->skin
->makeKnownLinkObj( $specialTitle, wfMsg( 'revertmove' ),
326 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
327 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
328 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
329 '&wpMovetalk=0' ) . ')';
333 $action = LogPage
::actionText( $s->log_type
, $s->log_action
, $title, $this->skin
, $paramArray, true, true );
334 $out = "<li>$time $userLink $action $comment $revert</li>\n";
339 * @param OutputPage &$out where to send output
342 function showHeader( &$out ) {
343 $type = $this->reader
->queryType();
344 if( LogPage
::isLogType( $type ) ) {
345 $out->setPageTitle( LogPage
::logName( $type ) );
346 $out->addWikiText( LogPage
::logHeader( $type ) );
351 * @param OutputPage &$out where to send output
354 function showOptions( &$out ) {
356 $action = htmlspecialchars( $wgScript );
357 $title = SpecialPage
::getTitleFor( 'Log' );
358 $special = htmlspecialchars( $title->getPrefixedDBkey() );
359 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
360 "<input type='hidden' name='title' value=\"$special\" />\n" .
361 $this->getTypeMenu() .
362 $this->getUserInput() .
363 $this->getTitleInput() .
364 "<input type='submit' value=\"" . wfMsg( 'allpagessubmit' ) . "\" />" .
369 * @return string Formatted HTML
372 function getTypeMenu() {
373 $out = "<select name='type'>\n";
374 foreach( LogPage
::validTypes() as $type ) {
375 $text = htmlspecialchars( LogPage
::logName( $type ) );
376 $selected = ($type == $this->reader
->queryType()) ?
' selected="selected"' : '';
377 $out .= "<option value=\"$type\"$selected>$text</option>\n";
379 $out .= "</select>\n";
384 * @return string Formatted HTML
387 function getUserInput() {
388 $user = htmlspecialchars( $this->reader
->queryUser() );
389 return wfMsg('specialloguserlabel') . "<input type='text' name='user' size='12' value=\"$user\" />\n";
393 * @return string Formatted HTML
396 function getTitleInput() {
397 $title = htmlspecialchars( $this->reader
->queryTitle() );
398 return wfMsg('speciallogtitlelabel') . "<input type='text' name='page' size='20' value=\"$title\" />\n";
402 * @param OutputPage &$out where to send output
405 function showPrevNext( &$out ) {
406 global $wgContLang,$wgRequest;
408 $pieces[] = 'type=' . urlencode( $this->reader
->queryType() );
409 $pieces[] = 'user=' . urlencode( $this->reader
->queryUser() );
410 $pieces[] = 'page=' . urlencode( $this->reader
->queryTitle() );
411 $bits = implode( '&', $pieces );
412 list( $limit, $offset ) = $wgRequest->getLimitOffset();
414 # TODO: use timestamps instead of offsets to make it more natural
415 # to go huge distances in time
416 $html = wfViewPrevNext( $offset, $limit,
417 $wgContLang->specialpage( 'Log' ),
419 $this->numResults
< $limit);
420 $out->addHTML( '<p>' . $html . '</p>' );