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