*** empty log message ***
[mediawiki.git] / includes / SpecialLog.php
blob4a04482d6def592931269f0e0250096c5cedb7bb
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
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.
9 #
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' ) && !empty( $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 * @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 * @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 Valid user name
93 * @private
95 function limitUser( $name ) {
96 $title = Title::makeTitle( NS_USER, $name );
97 if( empty( $name ) || is_null( $title ) ) {
98 return false;
100 $this->user = str_replace( '_', ' ', $title->getDBkey() );
101 $safename = $this->db->strencode( $this->user );
102 $user = $this->db->tableName( 'user' );
103 $this->whereClauses[] = "user_name='$safename'";
107 * Set the log reader to return only entries affecting the given page.
108 * (For the block and rights logs, this is a user page.)
109 * @param string $page Title name as text
110 * @private
112 function limitTitle( $page ) {
113 $title = Title::newFromText( $page );
114 if( empty( $page ) || is_null( $title ) ) {
115 return false;
117 $this->title =& $title;
118 $safetitle = $this->db->strencode( $title->getDBkey() );
119 $ns = $title->getNamespace();
120 $this->whereClauses[] = "log_namespace=$ns AND log_title='$safetitle'";
124 * Set the log reader to return only entries in a given time range.
125 * @param string $time Timestamp of one endpoint
126 * @param string $direction either ">=" or "<=" operators
127 * @private
129 function limitTime( $time, $direction ) {
130 # Direction should be a comparison operator
131 if( empty( $time ) ) {
132 return false;
134 $safetime = $this->db->strencode( wfTimestamp( TS_MW, $time ) );
135 $this->whereClauses[] = "log_timestamp $direction '$safetime'";
139 * Build an SQL query from all the set parameters.
140 * @return string the SQL query
141 * @private
143 function getQuery() {
144 $logging = $this->db->tableName( "logging" );
145 $user = $this->db->tableName( 'user' );
146 $sql = "SELECT log_type, log_action, log_timestamp,
147 log_user, user_name,
148 log_namespace, log_title, page_id,
149 log_comment, log_params FROM $user, $logging ";
150 if( !empty( $this->joinClauses ) ) {
151 $sql .= implode( ',', $this->joinClauses );
153 if( !empty( $this->whereClauses ) ) {
154 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses );
156 $sql .= " ORDER BY log_timestamp DESC ";
157 $sql .= $this->db->limitResult( $this->limit, $this->offset );
158 return $sql;
162 * Execute the query and start returning results.
163 * @return ResultWrapper result object to return the relevant rows
165 function getRows() {
166 return $this->db->resultObject( $this->db->query( $this->getQuery() ) );
170 * @return string The query type that this LogReader has been limited to.
172 function queryType() {
173 return $this->type;
177 * @return string The username type that this LogReader has been limited to, if any.
179 function queryUser() {
180 return $this->user;
184 * @return string The text of the title that this LogReader has been limited to.
186 function queryTitle() {
187 if( is_null( $this->title ) ) {
188 return '';
189 } else {
190 return $this->title->getPrefixedText();
197 * @package MediaWiki
198 * @subpackage SpecialPage
200 class LogViewer {
202 * @var LogReader $reader
204 var $reader;
207 * @param LogReader &$reader where to get our data from
209 function LogViewer( &$reader ) {
210 global $wgUser;
211 $this->skin =& $wgUser->getSkin();
212 $this->reader =& $reader;
216 * Take over the whole output page in $wgOut with the log display.
218 function show() {
219 global $wgOut;
220 $this->showHeader( $wgOut );
221 $this->showOptions( $wgOut );
222 $this->showPrevNext( $wgOut );
223 $this->showList( $wgOut );
224 $this->showPrevNext( $wgOut );
228 * Output just the list of entries given by the linked LogReader,
229 * with extraneous UI elements. Use for displaying log fragments in
230 * another page (eg at Special:Undelete)
231 * @param OutputPage $out where to send output
233 function showList( &$out ) {
234 global $wgLinkCache;
235 $result = $this->reader->getRows();
237 // Fetch results and form a batch link existence query
238 $batch = new LinkBatch;
239 while ( $s = $result->fetchObject() ) {
240 // User link
241 $title = Title::makeTitleSafe( NS_USER, $s->user_name );
242 $batch->addObj( $title );
244 // Move destination link
245 if ( $s->log_type == 'move' ) {
246 $paramArray = LogPage::extractParams( $s->log_params );
247 $title = Title::newFromText( $paramArray[0] );
248 $batch->addObj( $title );
251 $batch->execute( $wgLinkCache );
253 // Rewind result pointer and go through it again, making the HTML
254 $result->seek( 0 );
255 $html = "\n<ul>\n";
256 while( $s = $result->fetchObject() ) {
257 $html .= $this->logLine( $s );
259 $result->free();
260 $html .= "\n</ul>\n";
261 $out->addHTML( $html );
265 * @param Object $s a single row from the result set
266 * @return string Formatted HTML list item
267 * @private
269 function logLine( $s ) {
270 global $wgLang, $wgLinkCache;
271 $title = Title::makeTitle( $s->log_namespace, $s->log_title );
272 $user = Title::makeTitleSafe( NS_USER, $s->user_name );
273 $time = $wgLang->timeanddate( $s->log_timestamp, true );
275 // Enter the existence or non-existence of this page into the link cache,
276 // for faster makeLinkObj() in LogPage::actionText()
277 if( $s->page_id ) {
278 $wgLinkCache->addGoodLink( $s->page_id, $title->getPrefixedText() );
279 } else {
280 $wgLinkCache->addBadLink( $title->getPrefixedText() );
283 $userLink = $this->skin->makeLinkObj( $user, htmlspecialchars( $s->user_name ) );
284 $comment = $this->skin->commentBlock( $s->log_comment );
285 $paramArray = LogPage::extractParams( $s->log_params );
286 $revert = '';
287 if ( $s->log_type == 'move' && isset( $paramArray[0] ) ) {
288 $specialTitle = Title::makeTitle( NS_SPECIAL, 'Movepage' );
289 $destTitle = Title::newFromText( $paramArray[0] );
290 if ( $destTitle ) {
291 $revert = '(' . $this->skin->makeKnownLinkObj( $specialTitle, wfMsg( 'revertmove' ),
292 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
293 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
294 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
295 '&wpMovetalk=0' ) . ')';
299 $action = LogPage::actionText( $s->log_type, $s->log_action, $title, $this->skin, $paramArray, true );
300 $out = "<li>$time $userLink $action $comment $revert</li>\n";
301 return $out;
305 * @param OutputPage &$out where to send output
306 * @private
308 function showHeader( &$out ) {
309 $type = $this->reader->queryType();
310 if( LogPage::isLogType( $type ) ) {
311 $out->setPageTitle( LogPage::logName( $type ) );
312 $out->addWikiText( LogPage::logHeader( $type ) );
317 * @param OutputPage &$out where to send output
318 * @private
320 function showOptions( &$out ) {
321 global $wgScript;
322 $action = htmlspecialchars( $wgScript );
323 $title = Title::makeTitle( NS_SPECIAL, 'Log' );
324 $special = htmlspecialchars( $title->getPrefixedDBkey() );
325 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
326 "<input type='hidden' name='title' value=\"$special\" />\n" .
327 $this->getTypeMenu() .
328 $this->getUserInput() .
329 $this->getTitleInput() .
330 "<input type='submit' value=\"" . wfMsg( 'querybtn' ) . "\" />" .
331 "</form>" );
335 * @return string Formatted HTML
336 * @private
338 function getTypeMenu() {
339 $out = "<select name='type'>\n";
340 foreach( LogPage::validTypes() as $type ) {
341 $text = htmlspecialchars( LogPage::logName( $type ) );
342 $selected = ($type == $this->reader->queryType()) ? ' selected="selected"' : '';
343 $out .= "<option value=\"$type\"$selected>$text</option>\n";
345 $out .= "</select>\n";
346 return $out;
350 * @return string Formatted HTML
351 * @private
353 function getUserInput() {
354 $user = htmlspecialchars( $this->reader->queryUser() );
355 return wfMsg('specialloguserlabel') . "<input type='text' name='user' size='12' value=\"$user\" />\n";
359 * @return string Formatted HTML
360 * @private
362 function getTitleInput() {
363 $title = htmlspecialchars( $this->reader->queryTitle() );
364 return wfMsg('speciallogtitlelabel') . "<input type='text' name='page' size='20' value=\"$title\" />\n";
368 * @param OutputPage &$out where to send output
369 * @private
371 function showPrevNext( &$out ) {
372 global $wgContLang,$wgRequest;
373 $pieces = array();
374 $pieces[] = 'type=' . htmlspecialchars( $this->reader->queryType() );
375 $pieces[] = 'user=' . htmlspecialchars( $this->reader->queryUser() );
376 $pieces[] = 'page=' . htmlspecialchars( $this->reader->queryTitle() );
377 $bits = implode( '&', $pieces );
378 list( $limit, $offset ) = $wgRequest->getLimitOffset();
380 # TODO: use timestamps instead of offsets to make it more natural
381 # to go huge distances in time
382 $html = wfViewPrevNext( $offset, $limit,
383 $wgContLang->specialpage( 'Log' ),
384 $bits,
385 false);
386 $out->addHTML( '<p>' . $html . '</p>' );