* Removed mysql5 SQL files, obviously we're collectively incapable of keeping them...
[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 * @addtogroup SpecialPage
23 */
24
25 /**
26 * constructor
27 */
28 function wfSpecialLog( $par = '' ) {
29 global $wgRequest;
30 $logReader = new LogReader( $wgRequest );
31 if( $wgRequest->getVal( 'type' ) == '' && $par != '' ) {
32 $logReader->limitType( $par );
33 }
34 $logViewer = new LogViewer( $logReader );
35 $logViewer->show();
36 }
37
38 /**
39 *
40 * @addtogroup SpecialPage
41 */
42 class LogReader {
43 var $db, $joinClauses, $whereClauses;
44 var $type = '', $user = '', $title = null, $pattern = false;
45
46 /**
47 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
48 */
49 function LogReader( $request ) {
50 $this->db = wfGetDB( DB_SLAVE );
51 $this->setupQuery( $request );
52 }
53
54 /**
55 * Basic setup and applies the limiting factors from the WebRequest object.
56 * @param WebRequest $request
57 * @private
58 */
59 function setupQuery( $request ) {
60 $page = $this->db->tableName( 'page' );
61 $user = $this->db->tableName( 'user' );
62 $this->joinClauses = array(
63 "LEFT OUTER JOIN $page ON log_namespace=page_namespace AND log_title=page_title",
64 "INNER JOIN $user ON user_id=log_user" );
65 $this->whereClauses = array();
66
67 $this->limitType( $request->getVal( 'type' ) );
68 $this->limitUser( $request->getText( 'user' ) );
69 $this->limitTitle( $request->getText( 'page' ) , $request->getBool( 'pattern' ) );
70 $this->limitTime( $request->getVal( 'from' ), '>=' );
71 $this->limitTime( $request->getVal( 'until' ), '<=' );
72
73 list( $this->limit, $this->offset ) = $request->getLimitOffset();
74
75 // XXX This all needs to use Pager, ugly hack for now.
76 global $wgMiserMode;
77 if ($wgMiserMode && ($this->offset >10000)) $this->offset=10000;
78 }
79
80 /**
81 * Set the log reader to return only entries of the given type.
82 * @param string $type A log type ('upload', 'delete', etc)
83 * @private
84 */
85 function limitType( $type ) {
86 if( empty( $type ) ) {
87 return false;
88 }
89 $this->type = $type;
90 $safetype = $this->db->strencode( $type );
91 $this->whereClauses[] = "log_type='$safetype'";
92 }
93
94 /**
95 * Set the log reader to return only entries by the given user.
96 * @param string $name (In)valid user name
97 * @private
98 */
99 function limitUser( $name ) {
100 if ( $name == '' )
101 return false;
102 $usertitle = Title::makeTitleSafe( NS_USER, $name );
103 if ( is_null( $usertitle ) )
104 return false;
105 $this->user = $usertitle->getText();
106
107 /* Fetch userid at first, if known, provides awesome query plan afterwards */
108 $userid = $this->db->selectField('user','user_id',array('user_name'=>$this->user));
109 if (!$userid)
110 /* It should be nicer to abort query at all,
111 but for now it won't pass anywhere behind the optimizer */
112 $this->whereClauses[] = "NULL";
113 else
114 $this->whereClauses[] = "log_user=$userid";
115 }
116
117 /**
118 * Set the log reader to return only entries affecting the given page.
119 * (For the block and rights logs, this is a user page.)
120 * @param string $page Title name as text
121 * @private
122 */
123 function limitTitle( $page , $pattern ) {
124 global $wgMiserMode;
125 $title = Title::newFromText( $page );
126 if( empty( $page ) || is_null( $title ) ) {
127 return false;
128 }
129 $this->title =& $title;
130 $this->pattern = $pattern;
131 $ns = $title->getNamespace();
132 if ( $pattern && !$wgMiserMode ) {
133 $safetitle = $this->db->escapeLike( $title->getDBkey() ); // use escapeLike to avoid expensive search patterns like 't%st%'
134 $this->whereClauses[] = "log_namespace=$ns AND log_title LIKE '$safetitle%'";
135 } else {
136 $safetitle = $this->db->strencode( $title->getDBkey() );
137 $this->whereClauses[] = "log_namespace=$ns AND log_title = '$safetitle'";
138 }
139 }
140
141 /**
142 * Set the log reader to return only entries in a given time range.
143 * @param string $time Timestamp of one endpoint
144 * @param string $direction either ">=" or "<=" operators
145 * @private
146 */
147 function limitTime( $time, $direction ) {
148 # Direction should be a comparison operator
149 if( empty( $time ) ) {
150 return false;
151 }
152 $safetime = $this->db->strencode( wfTimestamp( TS_MW, $time ) );
153 $this->whereClauses[] = "log_timestamp $direction '$safetime'";
154 }
155
156 /**
157 * Build an SQL query from all the set parameters.
158 * @return string the SQL query
159 * @private
160 */
161 function getQuery() {
162 $logging = $this->db->tableName( "logging" );
163 $sql = "SELECT /*! STRAIGHT_JOIN */ log_type, log_action, log_timestamp,
164 log_user, user_name,
165 log_namespace, log_title, page_id,
166 log_comment, log_params FROM $logging ";
167 if( !empty( $this->joinClauses ) ) {
168 $sql .= implode( ' ', $this->joinClauses );
169 }
170 if( !empty( $this->whereClauses ) ) {
171 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses );
172 }
173 $sql .= " ORDER BY log_timestamp DESC ";
174 $sql = $this->db->limitResult($sql, $this->limit, $this->offset );
175 return $sql;
176 }
177
178 /**
179 * Execute the query and start returning results.
180 * @return ResultWrapper result object to return the relevant rows
181 */
182 function getRows() {
183 $res = $this->db->query( $this->getQuery(), 'LogReader::getRows' );
184 return $this->db->resultObject( $res );
185 }
186
187 /**
188 * @return string The query type that this LogReader has been limited to.
189 */
190 function queryType() {
191 return $this->type;
192 }
193
194 /**
195 * @return string The username type that this LogReader has been limited to, if any.
196 */
197 function queryUser() {
198 return $this->user;
199 }
200
201 /**
202 * @return boolean The checkbox, if titles should be searched by a pattern too
203 */
204 function queryPattern() {
205 return $this->pattern;
206 }
207
208 /**
209 * @return string The text of the title that this LogReader has been limited to.
210 */
211 function queryTitle() {
212 if( is_null( $this->title ) ) {
213 return '';
214 } else {
215 return $this->title->getPrefixedText();
216 }
217 }
218 }
219
220 /**
221 *
222 * @addtogroup SpecialPage
223 */
224 class LogViewer {
225 /**
226 * @var LogReader $reader
227 */
228 var $reader;
229 var $numResults = 0;
230
231 /**
232 * @param LogReader &$reader where to get our data from
233 */
234 function LogViewer( &$reader ) {
235 global $wgUser;
236 $this->skin = $wgUser->getSkin();
237 $this->reader =& $reader;
238 }
239
240 /**
241 * Take over the whole output page in $wgOut with the log display.
242 */
243 function show() {
244 global $wgOut;
245 $this->showHeader( $wgOut );
246 $this->showOptions( $wgOut );
247 $result = $this->getLogRows();
248 if ( $this->numResults > 0 ) {
249 $this->showPrevNext( $wgOut );
250 $this->doShowList( $wgOut, $result );
251 $this->showPrevNext( $wgOut );
252 } else {
253 $this->showError( $wgOut );
254 }
255 }
256
257 /**
258 * Load the data from the linked LogReader
259 * Preload the link cache
260 * Initialise numResults
261 *
262 * Must be called before calling showPrevNext
263 *
264 * @return object database result set
265 */
266 function getLogRows() {
267 $result = $this->reader->getRows();
268 $this->numResults = 0;
269
270 // Fetch results and form a batch link existence query
271 $batch = new LinkBatch;
272 while ( $s = $result->fetchObject() ) {
273 // User link
274 $batch->addObj( Title::makeTitleSafe( NS_USER, $s->user_name ) );
275 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $s->user_name ) );
276
277 // Move destination link
278 if ( $s->log_type == 'move' ) {
279 $paramArray = LogPage::extractParams( $s->log_params );
280 $title = Title::newFromText( $paramArray[0] );
281 $batch->addObj( $title );
282 }
283 ++$this->numResults;
284 }
285 $batch->execute();
286
287 return $result;
288 }
289
290
291 /**
292 * Output just the list of entries given by the linked LogReader,
293 * with extraneous UI elements. Use for displaying log fragments in
294 * another page (eg at Special:Undelete)
295 * @param OutputPage $out where to send output
296 */
297 function showList( &$out ) {
298 $result = $this->getLogRows();
299 if ( $this->numResults > 0 ) {
300 $this->doShowList( $out, $result );
301 } else {
302 $this->showError( $out );
303 }
304 }
305
306 function doShowList( &$out, $result ) {
307 // Rewind result pointer and go through it again, making the HTML
308 $html = "\n<ul>\n";
309 $result->seek( 0 );
310 while( $s = $result->fetchObject() ) {
311 $html .= $this->logLine( $s );
312 }
313 $html .= "\n</ul>\n";
314 $out->addHTML( $html );
315 $result->free();
316 }
317
318 function showError( &$out ) {
319 $out->addWikiText( wfMsg( 'logempty' ) );
320 }
321
322 /**
323 * @param Object $s a single row from the result set
324 * @return string Formatted HTML list item
325 * @private
326 */
327 function logLine( $s ) {
328 global $wgLang, $wgUser;;
329 $skin = $wgUser->getSkin();
330 $title = Title::makeTitle( $s->log_namespace, $s->log_title );
331 $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $s->log_timestamp), true );
332
333 // Enter the existence or non-existence of this page into the link cache,
334 // for faster makeLinkObj() in LogPage::actionText()
335 $linkCache =& LinkCache::singleton();
336 if( $s->page_id ) {
337 $linkCache->addGoodLinkObj( $s->page_id, $title );
338 } else {
339 $linkCache->addBadLinkObj( $title );
340 }
341
342 $userLink = $this->skin->userLink( $s->log_user, $s->user_name ) . $this->skin->userToolLinksRedContribs( $s->log_user, $s->user_name );
343 $comment = $this->skin->commentBlock( $s->log_comment );
344 $paramArray = LogPage::extractParams( $s->log_params );
345 $revert = '';
346 // show revertmove link
347 if ( $s->log_type == 'move' && isset( $paramArray[0] ) ) {
348 $destTitle = Title::newFromText( $paramArray[0] );
349 if ( $destTitle ) {
350 $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Movepage' ),
351 wfMsg( 'revertmove' ),
352 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
353 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
354 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
355 '&wpMovetalk=0' ) . ')';
356 }
357 // show undelete link
358 } elseif ( $s->log_action == 'delete' && $wgUser->isAllowed( 'delete' ) ) {
359 $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Undelete' ),
360 wfMsg( 'undeletebtn' ) ,
361 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
362
363 // show unblock link
364 } elseif ( $s->log_action == 'block' && $wgUser->isAllowed( 'block' ) ) {
365 $revert = '(' . $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Ipblocklist' ),
366 wfMsg( 'unblocklink' ),
367 'action=unblock&ip=' . urlencode( $s->log_title ) ) . ')';
368 // show change protection link
369 } elseif ( $s->log_action == 'protect' && $wgUser->isAllowed( 'protect' ) ) {
370 $revert = '(' . $skin->makeKnownLink( $title->getPrefixedDBkey() ,
371 wfMsg( 'protect_change' ),
372 'action=unprotect' ) . ')';
373 // show user tool links for self created users
374 } elseif ( $s->log_action == 'create2' ) {
375 $revert = $this->skin->userToolLinksRedContribs( $s->log_user, $s->log_title );
376 // do not show $comment for self created accounts. It includes wrong user tool links:
377 // 'blockip' for users w/o block allowance and broken links for very long usernames (bug 4756)
378 $comment = '';
379 }
380
381 $action = LogPage::actionText( $s->log_type, $s->log_action, $title, $this->skin, $paramArray, true, true );
382 $out = "<li>$time $userLink $action $comment $revert</li>\n";
383 return $out;
384 }
385
386 /**
387 * @param OutputPage &$out where to send output
388 * @private
389 */
390 function showHeader( &$out ) {
391 $type = $this->reader->queryType();
392 if( LogPage::isLogType( $type ) ) {
393 $out->setPageTitle( LogPage::logName( $type ) );
394 $out->addWikiText( LogPage::logHeader( $type ) );
395 }
396 }
397
398 /**
399 * @param OutputPage &$out where to send output
400 * @private
401 */
402 function showOptions( &$out ) {
403 global $wgScript, $wgMiserMode;
404 $action = htmlspecialchars( $wgScript );
405 $title = SpecialPage::getTitleFor( 'Log' );
406 $special = htmlspecialchars( $title->getPrefixedDBkey() );
407 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
408 '<fieldset>' .
409 Xml::element( 'legend', array(), wfMsg( 'log' ) ) .
410 Xml::hidden( 'title', $special ) . "\n" .
411 $this->getTypeMenu() . "\n" .
412 $this->getUserInput() . "\n" .
413 $this->getTitleInput() . "\n" .
414 (!$wgMiserMode?($this->getTitlePattern()."\n"):"") .
415 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
416 "</fieldset></form>" );
417 }
418
419 /**
420 * @return string Formatted HTML
421 * @private
422 */
423 function getTypeMenu() {
424 $out = "<select name='type'>\n";
425
426 $validTypes = LogPage::validTypes();
427 $m = array(); // Temporary array
428
429 // First pass to load the log names
430 foreach( $validTypes as $type ) {
431 $text = LogPage::logName( $type );
432 $m[$text] = $type;
433 }
434
435 // Second pass to sort by name
436 ksort($m);
437
438 // Third pass generates sorted XHTML content
439 foreach( $m as $text => $type ) {
440 $selected = ($type == $this->reader->queryType());
441 $out .= Xml::option( $text, $type, $selected ) . "\n";
442 }
443
444 $out .= '</select>';
445 return $out;
446 }
447
448 /**
449 * @return string Formatted HTML
450 * @private
451 */
452 function getUserInput() {
453 $user = $this->reader->queryUser();
454 return Xml::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 12, $user );
455 }
456
457 /**
458 * @return string Formatted HTML
459 * @private
460 */
461 function getTitleInput() {
462 $title = $this->reader->queryTitle();
463 return Xml::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
464 }
465
466 /**
467 * @return boolean Checkbox
468 * @private
469 */
470 function getTitlePattern() {
471 $pattern = $this->reader->queryPattern();
472 return Xml::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern );
473 }
474
475 /**
476 * @param OutputPage &$out where to send output
477 * @private
478 */
479 function showPrevNext( &$out ) {
480 global $wgContLang,$wgRequest;
481 $pieces = array();
482 $pieces[] = 'type=' . urlencode( $this->reader->queryType() );
483 $pieces[] = 'user=' . urlencode( $this->reader->queryUser() );
484 $pieces[] = 'page=' . urlencode( $this->reader->queryTitle() );
485 $pieces[] = 'pattern=' . urlencode( $this->reader->queryPattern() );
486 $bits = implode( '&', $pieces );
487 list( $limit, $offset ) = $wgRequest->getLimitOffset();
488
489 # TODO: use timestamps instead of offsets to make it more natural
490 # to go huge distances in time
491 $html = wfViewPrevNext( $offset, $limit,
492 $wgContLang->specialpage( 'Log' ),
493 $bits,
494 $this->numResults < $limit);
495 $out->addHTML( '<p>' . $html . '</p>' );
496 }
497 }
498
499
500 ?>