redundant file removal
[mediawiki.git] / includes / SpecialLog.php
blob52b388b662f5fba792a0d1a931d2e25b9e1d7962
1 <?php
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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
20 /**
22 * @package MediaWiki
23 * @subpackage SpecialPage
26 /**
27 * constructor
29 function wfSpecialLog( $par = '' ) {
30 global $wgRequest;
31 $logReader =& new LogReader( $wgRequest );
32 if( $wgRequest->getVal( 'type' ) == '' && $par != '' ) {
33 $logReader->limitType( $par );
35 $logViewer =& new LogViewer( $logReader );
36 $logViewer->show();
39 /**
41 * @package MediaWiki
42 * @subpackage SpecialPage
44 class LogReader {
45 var $db, $joinClauses, $whereClauses;
46 var $type = '', $user = '', $title = null;
48 /**
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 );
56 /**
57 * Basic setup and applies the limiting factors from the WebRequest object.
58 * @param WebRequest $request
59 * @access private
61 function setupQuery( $request ) {
62 $page = $this->db->tableName( 'page' );
63 $user = $this->db->tableName( 'user' );
64 $this->joinClauses = array( "LEFT OUTER JOIN $page ON log_namespace=page_namespace AND log_title=page_title" );
65 $this->whereClauses = array( 'user_id=log_user' );
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();
76 /**
77 * Set the log reader to return only entries of the given type.
78 * @param string $type A log type ('upload', 'delete', etc)
79 * @access private
81 function limitType( $type ) {
82 if( empty( $type ) ) {
83 return false;
85 $this->type = $type;
86 $safetype = $this->db->strencode( $type );
87 $this->whereClauses[] = "log_type='$safetype'";
90 /**
91 * Set the log reader to return only entries by the given user.
92 * @param string $name (In)valid user name
93 * @access private
95 function limitUser( $name ) {
96 if ( $name == '' )
97 return false;
98 $title = Title::makeTitle( NS_USER, $name );
99 if ( is_null( $title ) )
100 return false;
101 $this->user = $title->getText();
102 $safename = $this->db->strencode( $this->user );
103 $user = $this->db->tableName( 'user' );
104 $this->whereClauses[] = "user_name='$safename'";
108 * Set the log reader to return only entries affecting the given page.
109 * (For the block and rights logs, this is a user page.)
110 * @param string $page Title name as text
111 * @access private
113 function limitTitle( $page ) {
114 $title = Title::newFromText( $page );
115 if( empty( $page ) || is_null( $title ) ) {
116 return false;
118 $this->title =& $title;
119 $safetitle = $this->db->strencode( $title->getDBkey() );
120 $ns = $title->getNamespace();
121 $this->whereClauses[] = "log_namespace=$ns AND log_title='$safetitle'";
125 * Set the log reader to return only entries in a given time range.
126 * @param string $time Timestamp of one endpoint
127 * @param string $direction either ">=" or "<=" operators
128 * @access private
130 function limitTime( $time, $direction ) {
131 # Direction should be a comparison operator
132 if( empty( $time ) ) {
133 return false;
135 $safetime = $this->db->strencode( wfTimestamp( TS_MW, $time ) );
136 $this->whereClauses[] = "log_timestamp $direction '$safetime'";
140 * Build an SQL query from all the set parameters.
141 * @return string the SQL query
142 * @access private
144 function getQuery() {
145 $logging = $this->db->tableName( "logging" );
146 $user = $this->db->tableName( 'user' );
147 $sql = "SELECT log_type, log_action, log_timestamp,
148 log_user, user_name,
149 log_namespace, log_title, page_id,
150 log_comment, log_params FROM $user, $logging ";
151 if ($this->type=="" && $this->db->indexExists('logging','times')) {
152 $sql .= ' /*! FORCE INDEX (times) */ ';
154 if( !empty( $this->joinClauses ) ) {
155 $sql .= implode( ',', $this->joinClauses );
157 if( !empty( $this->whereClauses ) ) {
158 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses );
160 $sql .= " ORDER BY log_timestamp DESC ";
161 $sql = $this->db->limitResult($sql, $this->limit, $this->offset );
162 return $sql;
166 * Execute the query and start returning results.
167 * @return ResultWrapper result object to return the relevant rows
169 function getRows() {
170 $res = $this->db->query( $this->getQuery() );
171 return $this->db->resultObject( $res );
175 * @return string The query type that this LogReader has been limited to.
177 function queryType() {
178 return $this->type;
182 * @return string The username type that this LogReader has been limited to, if any.
184 function queryUser() {
185 return $this->user;
189 * @return string The text of the title that this LogReader has been limited to.
191 function queryTitle() {
192 if( is_null( $this->title ) ) {
193 return '';
194 } else {
195 return $this->title->getPrefixedText();
202 * @package MediaWiki
203 * @subpackage SpecialPage
205 class LogViewer {
207 * @var LogReader $reader
209 var $reader;
210 var $numResults = 0;
213 * @param LogReader &$reader where to get our data from
215 function LogViewer( &$reader ) {
216 global $wgUser;
217 $this->skin =& $wgUser->getSkin();
218 $this->reader =& $reader;
222 * Take over the whole output page in $wgOut with the log display.
224 function show() {
225 global $wgOut;
226 $this->showHeader( $wgOut );
227 $this->showOptions( $wgOut );
228 $result = $this->getLogRows();
229 $this->showPrevNext( $wgOut );
230 $this->doShowList( $wgOut, $result );
231 $this->showPrevNext( $wgOut );
235 * Load the data from the linked LogReader
236 * Preload the link cache
237 * Initialise numResults
239 * Must be called before calling showPrevNext
241 * @return object database result set
243 function getLogRows() {
244 $result = $this->reader->getRows();
245 $this->numResults = 0;
247 // Fetch results and form a batch link existence query
248 $batch = new LinkBatch;
249 while ( $s = $result->fetchObject() ) {
250 // User link
251 $title = Title::makeTitleSafe( NS_USER, $s->user_name );
252 $batch->addObj( $title );
254 // Move destination link
255 if ( $s->log_type == 'move' ) {
256 $paramArray = LogPage::extractParams( $s->log_params );
257 $title = Title::newFromText( $paramArray[0] );
258 $batch->addObj( $title );
260 ++$this->numResults;
262 $batch->execute();
264 return $result;
269 * Output just the list of entries given by the linked LogReader,
270 * with extraneous UI elements. Use for displaying log fragments in
271 * another page (eg at Special:Undelete)
272 * @param OutputPage $out where to send output
274 function showList( &$out ) {
275 $this->doShowList( $out, $this->getLogRows() );
278 function doShowList( &$out, $result ) {
279 // Rewind result pointer and go through it again, making the HTML
280 if ($this->numResults > 0) {
281 $html = "\n<ul>\n";
282 $result->seek( 0 );
283 while( $s = $result->fetchObject() ) {
284 $html .= $this->logLine( $s );
286 $html .= "\n</ul>\n";
287 $out->addHTML( $html );
288 } else {
289 $out->addWikiText( wfMsg( 'logempty' ) );
291 $result->free();
295 * @param Object $s a single row from the result set
296 * @return string Formatted HTML list item
297 * @access private
299 function logLine( $s ) {
300 global $wgLang;
301 $title = Title::makeTitle( $s->log_namespace, $s->log_title );
302 $user = Title::makeTitleSafe( NS_USER, $s->user_name );
303 $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $s->log_timestamp), true );
305 // Enter the existence or non-existence of this page into the link cache,
306 // for faster makeLinkObj() in LogPage::actionText()
307 $linkCache =& LinkCache::singleton();
308 if( $s->page_id ) {
309 $linkCache->addGoodLinkObj( $s->page_id, $title );
310 } else {
311 $linkCache->addBadLinkObj( $title );
314 $userLink = $this->skin->makeLinkObj( $user, htmlspecialchars( $s->user_name ) );
315 $comment = $this->skin->commentBlock( $s->log_comment );
316 $paramArray = LogPage::extractParams( $s->log_params );
317 $revert = '';
318 if ( $s->log_type == 'move' && isset( $paramArray[0] ) ) {
319 $specialTitle = Title::makeTitle( NS_SPECIAL, 'Movepage' );
320 $destTitle = Title::newFromText( $paramArray[0] );
321 if ( $destTitle ) {
322 $revert = '(' . $this->skin->makeKnownLinkObj( $specialTitle, wfMsg( 'revertmove' ),
323 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
324 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
325 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
326 '&wpMovetalk=0' ) . ')';
330 $action = LogPage::actionText( $s->log_type, $s->log_action, $title, $this->skin, $paramArray, true, true );
331 $out = "<li>$time $userLink $action $comment $revert</li>\n";
332 return $out;
336 * @param OutputPage &$out where to send output
337 * @access private
339 function showHeader( &$out ) {
340 $type = $this->reader->queryType();
341 if( LogPage::isLogType( $type ) ) {
342 $out->setPageTitle( LogPage::logName( $type ) );
343 $out->addWikiText( LogPage::logHeader( $type ) );
348 * @param OutputPage &$out where to send output
349 * @access private
351 function showOptions( &$out ) {
352 global $wgScript;
353 $action = htmlspecialchars( $wgScript );
354 $title = Title::makeTitle( NS_SPECIAL, 'Log' );
355 $special = htmlspecialchars( $title->getPrefixedDBkey() );
356 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
357 "<input type='hidden' name='title' value=\"$special\" />\n" .
358 $this->getTypeMenu() .
359 $this->getUserInput() .
360 $this->getTitleInput() .
361 "<input type='submit' value=\"" . wfMsg( 'allpagessubmit' ) . "\" />" .
362 "</form>" );
366 * @return string Formatted HTML
367 * @access private
369 function getTypeMenu() {
370 $out = "<select name='type'>\n";
371 foreach( LogPage::validTypes() as $type ) {
372 $text = htmlspecialchars( LogPage::logName( $type ) );
373 $selected = ($type == $this->reader->queryType()) ? ' selected="selected"' : '';
374 $out .= "<option value=\"$type\"$selected>$text</option>\n";
376 $out .= "</select>\n";
377 return $out;
381 * @return string Formatted HTML
382 * @access private
384 function getUserInput() {
385 $user = htmlspecialchars( $this->reader->queryUser() );
386 return wfMsg('specialloguserlabel') . "<input type='text' name='user' size='12' value=\"$user\" />\n";
390 * @return string Formatted HTML
391 * @access private
393 function getTitleInput() {
394 $title = htmlspecialchars( $this->reader->queryTitle() );
395 return wfMsg('speciallogtitlelabel') . "<input type='text' name='page' size='20' value=\"$title\" />\n";
399 * @param OutputPage &$out where to send output
400 * @access private
402 function showPrevNext( &$out ) {
403 global $wgContLang,$wgRequest;
404 $pieces = array();
405 $pieces[] = 'type=' . urlencode( $this->reader->queryType() );
406 $pieces[] = 'user=' . urlencode( $this->reader->queryUser() );
407 $pieces[] = 'page=' . urlencode( $this->reader->queryTitle() );
408 $bits = implode( '&', $pieces );
409 list( $limit, $offset ) = $wgRequest->getLimitOffset();
411 # TODO: use timestamps instead of offsets to make it more natural
412 # to go huge distances in time
413 $html = wfViewPrevNext( $offset, $limit,
414 $wgContLang->specialpage( 'Log' ),
415 $bits,
416 $this->numResults < $limit);
417 $out->addHTML( '<p>' . $html . '</p>' );