188f9c939735530ee812b3f80bb122978e4c9874
[lhc/web/wiklou.git] / includes / SpecialLog.php
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.
14 #
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
19
20 /**
21 *
22 * @package MediaWiki
23 * @subpackage SpecialPage
24 */
25
26 /**
27 * constructor
28 */
29 function wfSpecialLog( $par = '' ) {
30 global $wgRequest;
31 $logReader = new LogReader( $wgRequest );
32 if( $wgRequest->getVal( 'type' ) == '' && $par != '' ) {
33 $logReader->limitType( $par );
34 }
35 $logViewer = new LogViewer( $logReader );
36 $logViewer->show();
37 }
38
39 /**
40 *
41 * @package MediaWiki
42 * @subpackage SpecialPage
43 */
44 class LogReader {
45 var $db, $joinClauses, $whereClauses;
46 var $type = '', $user = '', $title = null;
47
48 /**
49 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
50 */
51 function LogReader( $request ) {
52 $this->db =& wfGetDB( DB_SLAVE );
53 $this->setupQuery( $request );
54 }
55
56 /**
57 * Basic setup and applies the limiting factors from the WebRequest object.
58 * @param WebRequest $request
59 * @private
60 */
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();
68
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' ), '<=' );
74
75 list( $this->limit, $this->offset ) = $request->getLimitOffset();
76 }
77
78 /**
79 * Set the log reader to return only entries of the given type.
80 * @param string $type A log type ('upload', 'delete', etc)
81 * @private
82 */
83 function limitType( $type ) {
84 if( empty( $type ) ) {
85 return false;
86 }
87 $this->type = $type;
88 $safetype = $this->db->strencode( $type );
89 $this->whereClauses[] = "log_type='$safetype'";
90 }
91
92 /**
93 * Set the log reader to return only entries by the given user.
94 * @param string $name (In)valid user name
95 * @private
96 */
97 function limitUser( $name ) {
98 if ( $name == '' )
99 return false;
100 $usertitle = Title::makeTitleSafe( NS_USER, $name );
101 if ( is_null( $usertitle ) )
102 return false;
103 $this->user = $usertitle->getText();
104
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));
107 if (!$userid)
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";
111 else
112 $this->whereClauses[] = "log_user=$userid";
113 }
114
115 /**
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
119 * @private
120 */
121 function limitTitle( $page ) {
122 $title = Title::newFromText( $page );
123 if( empty( $page ) || is_null( $title ) ) {
124 return false;
125 }
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'";
130 }
131
132 /**
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
136 * @private
137 */
138 function limitTime( $time, $direction ) {
139 # Direction should be a comparison operator
140 if( empty( $time ) ) {
141 return false;
142 }
143 $safetime = $this->db->strencode( wfTimestamp( TS_MW, $time ) );
144 $this->whereClauses[] = "log_timestamp $direction '$safetime'";
145 }
146
147 /**
148 * Build an SQL query from all the set parameters.
149 * @return string the SQL query
150 * @private
151 */
152 function getQuery() {
153 $logging = $this->db->tableName( "logging" );
154 $sql = "SELECT /*! STRAIGHT_JOIN */ log_type, log_action, log_timestamp,
155 log_user, user_name,
156 log_namespace, log_title, page_id,
157 log_comment, log_params FROM $logging ";
158 if( !empty( $this->joinClauses ) ) {
159 $sql .= implode( ' ', $this->joinClauses );
160 }
161 if( !empty( $this->whereClauses ) ) {
162 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses );
163 }
164 $sql .= " ORDER BY log_timestamp DESC ";
165 $sql = $this->db->limitResult($sql, $this->limit, $this->offset );
166 return $sql;
167 }
168
169 /**
170 * Execute the query and start returning results.
171 * @return ResultWrapper result object to return the relevant rows
172 */
173 function getRows() {
174 $res = $this->db->query( $this->getQuery(), 'LogReader::getRows' );
175 return $this->db->resultObject( $res );
176 }
177
178 /**
179 * @return string The query type that this LogReader has been limited to.
180 */
181 function queryType() {
182 return $this->type;
183 }
184
185 /**
186 * @return string The username type that this LogReader has been limited to, if any.
187 */
188 function queryUser() {
189 return $this->user;
190 }
191
192 /**
193 * @return string The text of the title that this LogReader has been limited to.
194 */
195 function queryTitle() {
196 if( is_null( $this->title ) ) {
197 return '';
198 } else {
199 return $this->title->getPrefixedText();
200 }
201 }
202 }
203
204 /**
205 *
206 * @package MediaWiki
207 * @subpackage SpecialPage
208 */
209 class LogViewer {
210 /**
211 * @var LogReader $reader
212 */
213 var $reader;
214 var $numResults = 0;
215
216 /**
217 * @param LogReader &$reader where to get our data from
218 */
219 function LogViewer( &$reader ) {
220 global $wgUser;
221 $this->skin =& $wgUser->getSkin();
222 $this->reader =& $reader;
223 }
224
225 /**
226 * Take over the whole output page in $wgOut with the log display.
227 */
228 function show() {
229 global $wgOut;
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 );
236 }
237
238 /**
239 * Load the data from the linked LogReader
240 * Preload the link cache
241 * Initialise numResults
242 *
243 * Must be called before calling showPrevNext
244 *
245 * @return object database result set
246 */
247 function getLogRows() {
248 $result = $this->reader->getRows();
249 $this->numResults = 0;
250
251 // Fetch results and form a batch link existence query
252 $batch = new LinkBatch;
253 while ( $s = $result->fetchObject() ) {
254 // User link
255 $batch->addObj( Title::makeTitleSafe( NS_USER, $s->user_name ) );
256 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $s->user_name ) );
257
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 );
263 }
264 ++$this->numResults;
265 }
266 $batch->execute();
267
268 return $result;
269 }
270
271
272 /**
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
277 */
278 function showList( &$out ) {
279 $this->doShowList( $out, $this->getLogRows() );
280 }
281
282 function doShowList( &$out, $result ) {
283 // Rewind result pointer and go through it again, making the HTML
284 if ($this->numResults > 0) {
285 $html = "\n<ul>\n";
286 $result->seek( 0 );
287 while( $s = $result->fetchObject() ) {
288 $html .= $this->logLine( $s );
289 }
290 $html .= "\n</ul>\n";
291 $out->addHTML( $html );
292 } else {
293 $out->addWikiText( wfMsg( 'logempty' ) );
294 }
295 $result->free();
296 }
297
298 /**
299 * @param Object $s a single row from the result set
300 * @return string Formatted HTML list item
301 * @private
302 */
303 function logLine( $s ) {
304 global $wgLang;
305 $title = Title::makeTitle( $s->log_namespace, $s->log_title );
306 $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $s->log_timestamp), true );
307
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();
311 if( $s->page_id ) {
312 $linkCache->addGoodLinkObj( $s->page_id, $title );
313 } else {
314 $linkCache->addBadLinkObj( $title );
315 }
316
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 );
320 $revert = '';
321 if ( $s->log_type == 'move' && isset( $paramArray[0] ) ) {
322 $specialTitle = SpecialPage::getTitleFor( 'Movepage' );
323 $destTitle = Title::newFromText( $paramArray[0] );
324 if ( $destTitle ) {
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' ) . ')';
330 }
331 }
332
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";
335 return $out;
336 }
337
338 /**
339 * @param OutputPage &$out where to send output
340 * @private
341 */
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 ) );
347 }
348 }
349
350 /**
351 * @param OutputPage &$out where to send output
352 * @private
353 */
354 function showOptions( &$out ) {
355 global $wgScript;
356 $action = htmlspecialchars( $wgScript );
357 $title = SpecialPage::getTitleFor( 'Log' );
358 $special = htmlspecialchars( $title->getPrefixedDBkey() );
359 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
360 Xml::hidden( 'title', $special ) . "\n" .
361 $this->getTypeMenu() . "\n" .
362 $this->getUserInput() . "\n" .
363 $this->getTitleInput() . "\n" .
364 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
365 "</form>" );
366 }
367
368 /**
369 * @return string Formatted HTML
370 * @private
371 */
372 function getTypeMenu() {
373 $out = "<select name='type'>\n";
374 foreach( LogPage::validTypes() as $type ) {
375 $text = LogPage::logName( $type );
376 $selected = ($type == $this->reader->queryType());
377 $out .= Xml::option( $text, $type, $selected ) . "\n";
378 }
379 $out .= '</select>';
380 return $out;
381 }
382
383 /**
384 * @return string Formatted HTML
385 * @private
386 */
387 function getUserInput() {
388 $user = $this->reader->queryUser();
389 return Xml::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 12, $user );
390 }
391
392 /**
393 * @return string Formatted HTML
394 * @private
395 */
396 function getTitleInput() {
397 $title = $this->reader->queryTitle();
398 return Xml::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
399 }
400
401 /**
402 * @param OutputPage &$out where to send output
403 * @private
404 */
405 function showPrevNext( &$out ) {
406 global $wgContLang,$wgRequest;
407 $pieces = array();
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();
413
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' ),
418 $bits,
419 $this->numResults < $limit);
420 $out->addHTML( '<p>' . $html . '</p>' );
421 }
422 }
423
424
425 ?>