* code formatting
[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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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( "LEFT OUTER JOIN $page ON log_namespace=page_namespace AND log_title=page_title" );
65 $this->whereClauses = array( 'user_id=log_user' );
66
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' ), '<=' );
72
73 list( $this->limit, $this->offset ) = $request->getLimitOffset();
74 }
75
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
80 */
81 function limitType( $type ) {
82 if( empty( $type ) ) {
83 return false;
84 }
85 $this->type = $type;
86 $safetype = $this->db->strencode( $type );
87 $this->whereClauses[] = "log_type='$safetype'";
88 }
89
90 /**
91 * Set the log reader to return only entries by the given user.
92 * @param string $name (In)valid user name
93 * @private
94 */
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'";
105 }
106
107 /**
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 * @private
112 */
113 function limitTitle( $page ) {
114 $title = Title::newFromText( $page );
115 if( empty( $page ) || is_null( $title ) ) {
116 return false;
117 }
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'";
122 }
123
124 /**
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 * @private
129 */
130 function limitTime( $time, $direction ) {
131 # Direction should be a comparison operator
132 if( empty( $time ) ) {
133 return false;
134 }
135 $safetime = $this->db->strencode( wfTimestamp( TS_MW, $time ) );
136 $this->whereClauses[] = "log_timestamp $direction '$safetime'";
137 }
138
139 /**
140 * Build an SQL query from all the set parameters.
141 * @return string the SQL query
142 * @private
143 */
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( !empty( $this->joinClauses ) ) {
152 $sql .= implode( ',', $this->joinClauses );
153 }
154 if( !empty( $this->whereClauses ) ) {
155 $sql .= " WHERE " . implode( ' AND ', $this->whereClauses );
156 }
157 $sql .= " ORDER BY log_timestamp DESC ";
158 $sql = $this->db->limitResult($sql, $this->limit, $this->offset );
159 return $sql;
160 }
161
162 /**
163 * Execute the query and start returning results.
164 * @return ResultWrapper result object to return the relevant rows
165 */
166 function getRows() {
167 $res = $this->db->query( $this->getQuery() );
168 return $this->db->resultObject( $res );
169 }
170
171 /**
172 * @return string The query type that this LogReader has been limited to.
173 */
174 function queryType() {
175 return $this->type;
176 }
177
178 /**
179 * @return string The username type that this LogReader has been limited to, if any.
180 */
181 function queryUser() {
182 return $this->user;
183 }
184
185 /**
186 * @return string The text of the title that this LogReader has been limited to.
187 */
188 function queryTitle() {
189 if( is_null( $this->title ) ) {
190 return '';
191 } else {
192 return $this->title->getPrefixedText();
193 }
194 }
195 }
196
197 /**
198 *
199 * @package MediaWiki
200 * @subpackage SpecialPage
201 */
202 class LogViewer {
203 /**
204 * @var LogReader $reader
205 */
206 var $reader;
207 var $numResults = 0;
208
209 /**
210 * @param LogReader &$reader where to get our data from
211 */
212 function LogViewer( &$reader ) {
213 global $wgUser;
214 $this->skin =& $wgUser->getSkin();
215 $this->reader =& $reader;
216 }
217
218 /**
219 * Take over the whole output page in $wgOut with the log display.
220 */
221 function show() {
222 global $wgOut;
223 $this->showHeader( $wgOut );
224 $this->showOptions( $wgOut );
225 $result = $this->getLogRows();
226 $this->showPrevNext( $wgOut );
227 $this->doShowList( $wgOut, $result );
228 $this->showPrevNext( $wgOut );
229 }
230
231 /**
232 * Load the data from the linked LogReader
233 * Preload the link cache
234 * Initialise numResults
235 *
236 * Must be called before calling showPrevNext
237 *
238 * @return object database result set
239 */
240 function getLogRows() {
241 global $wgLinkCache;
242 $result = $this->reader->getRows();
243 $this->numResults = 0;
244
245 // Fetch results and form a batch link existence query
246 $batch = new LinkBatch;
247 while ( $s = $result->fetchObject() ) {
248 // User link
249 $title = Title::makeTitleSafe( NS_USER, $s->user_name );
250 $batch->addObj( $title );
251
252 // Move destination link
253 if ( $s->log_type == 'move' ) {
254 $paramArray = LogPage::extractParams( $s->log_params );
255 $title = Title::newFromText( $paramArray[0] );
256 $batch->addObj( $title );
257 }
258 ++$this->numResults;
259 }
260 $batch->execute( $wgLinkCache );
261
262 return $result;
263 }
264
265
266 /**
267 * Output just the list of entries given by the linked LogReader,
268 * with extraneous UI elements. Use for displaying log fragments in
269 * another page (eg at Special:Undelete)
270 * @param OutputPage $out where to send output
271 */
272 function showList( &$out ) {
273 $this->doShowList( $out, $this->getLogRows() );
274 }
275
276 function doShowList( &$out, $result ) {
277 // Rewind result pointer and go through it again, making the HTML
278 $html='';
279 if ($this->numResults > 0) {
280 $html = "\n<ul>\n";
281 $result->seek( 0 );
282 while( $s = $result->fetchObject() ) {
283 $html .= $this->logLine( $s );
284 }
285 $html .= "\n</ul>\n";
286 }
287 $result->free();
288 $out->addHTML( $html );
289 }
290
291 /**
292 * @param Object $s a single row from the result set
293 * @return string Formatted HTML list item
294 * @private
295 */
296 function logLine( $s ) {
297 global $wgLang, $wgLinkCache;
298 $title = Title::makeTitle( $s->log_namespace, $s->log_title );
299 $user = Title::makeTitleSafe( NS_USER, $s->user_name );
300 $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $s->log_timestamp), true );
301
302 // Enter the existence or non-existence of this page into the link cache,
303 // for faster makeLinkObj() in LogPage::actionText()
304 if( $s->page_id ) {
305 $wgLinkCache->addGoodLinkObj( $s->page_id, $title );
306 } else {
307 $wgLinkCache->addBadLinkObj( $title );
308 }
309
310 $userLink = $this->skin->makeLinkObj( $user, htmlspecialchars( $s->user_name ) );
311 $comment = $this->skin->commentBlock( $s->log_comment );
312 $paramArray = LogPage::extractParams( $s->log_params );
313 $revert = '';
314 if ( $s->log_type == 'move' && isset( $paramArray[0] ) ) {
315 $specialTitle = Title::makeTitle( NS_SPECIAL, 'Movepage' );
316 $destTitle = Title::newFromText( $paramArray[0] );
317 if ( $destTitle ) {
318 $revert = '(' . $this->skin->makeKnownLinkObj( $specialTitle, wfMsg( 'revertmove' ),
319 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
320 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
321 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
322 '&wpMovetalk=0' ) . ')';
323 }
324 }
325
326 $action = LogPage::actionText( $s->log_type, $s->log_action, $title, $this->skin, $paramArray, true, true );
327 $out = "<li>$time $userLink $action $comment $revert</li>\n";
328 return $out;
329 }
330
331 /**
332 * @param OutputPage &$out where to send output
333 * @private
334 */
335 function showHeader( &$out ) {
336 $type = $this->reader->queryType();
337 if( LogPage::isLogType( $type ) ) {
338 $out->setPageTitle( LogPage::logName( $type ) );
339 $out->addWikiText( LogPage::logHeader( $type ) );
340 }
341 }
342
343 /**
344 * @param OutputPage &$out where to send output
345 * @private
346 */
347 function showOptions( &$out ) {
348 global $wgScript;
349 $action = htmlspecialchars( $wgScript );
350 $title = Title::makeTitle( NS_SPECIAL, 'Log' );
351 $special = htmlspecialchars( $title->getPrefixedDBkey() );
352 $out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
353 "<input type='hidden' name='title' value=\"$special\" />\n" .
354 $this->getTypeMenu() .
355 $this->getUserInput() .
356 $this->getTitleInput() .
357 "<input type='submit' value=\"" . wfMsg( 'allpagessubmit' ) . "\" />" .
358 "</form>" );
359 }
360
361 /**
362 * @return string Formatted HTML
363 * @private
364 */
365 function getTypeMenu() {
366 $out = "<select name='type'>\n";
367 foreach( LogPage::validTypes() as $type ) {
368 $text = htmlspecialchars( LogPage::logName( $type ) );
369 $selected = ($type == $this->reader->queryType()) ? ' selected="selected"' : '';
370 $out .= "<option value=\"$type\"$selected>$text</option>\n";
371 }
372 $out .= "</select>\n";
373 return $out;
374 }
375
376 /**
377 * @return string Formatted HTML
378 * @private
379 */
380 function getUserInput() {
381 $user = htmlspecialchars( $this->reader->queryUser() );
382 return wfMsg('specialloguserlabel') . "<input type='text' name='user' size='12' value=\"$user\" />\n";
383 }
384
385 /**
386 * @return string Formatted HTML
387 * @private
388 */
389 function getTitleInput() {
390 $title = htmlspecialchars( $this->reader->queryTitle() );
391 return wfMsg('speciallogtitlelabel') . "<input type='text' name='page' size='20' value=\"$title\" />\n";
392 }
393
394 /**
395 * @param OutputPage &$out where to send output
396 * @private
397 */
398 function showPrevNext( &$out ) {
399 global $wgContLang,$wgRequest;
400 $pieces = array();
401 $pieces[] = 'type=' . urlencode( $this->reader->queryType() );
402 $pieces[] = 'user=' . urlencode( $this->reader->queryUser() );
403 $pieces[] = 'page=' . urlencode( $this->reader->queryTitle() );
404 $bits = implode( '&', $pieces );
405 list( $limit, $offset ) = $wgRequest->getLimitOffset();
406
407 # TODO: use timestamps instead of offsets to make it more natural
408 # to go huge distances in time
409 $html = wfViewPrevNext( $offset, $limit,
410 $wgContLang->specialpage( 'Log' ),
411 $bits,
412 $this->numResults < $limit);
413 $out->addHTML( '<p>' . $html . '</p>' );
414 }
415 }
416
417
418 ?>