1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
<?php
# Copyright (C) 2004 Brion Vibber <brion@pobox.com>
# http://www.mediawiki.org/
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# http://www.gnu.org/copyleft/gpl.html
/**
*
* @package MediaWiki
* @subpackage SpecialPage
*/
/**
* constructor
*/
function wfSpecialLog( $par = '' ) {
global $wgRequest;
$logReader = new LogReader( $wgRequest );
if( $wgRequest->getVal( 'type' ) == '' && $par != '' ) {
$logReader->limitType( $par );
}
$logViewer = new LogViewer( $logReader );
$logViewer->show();
}
/**
*
* @package MediaWiki
* @subpackage SpecialPage
*/
class LogReader {
var $db, $joinClauses, $whereClauses;
var $type = '', $user = '', $title = null;
/**
* @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
*/
function LogReader( $request ) {
$this->db =& wfGetDB( DB_SLAVE );
$this->setupQuery( $request );
}
/**
* Basic setup and applies the limiting factors from the WebRequest object.
* @param WebRequest $request
* @private
*/
function setupQuery( $request ) {
$page = $this->db->tableName( 'page' );
$user = $this->db->tableName( 'user' );
$this->joinClauses = array(
"LEFT OUTER JOIN $page ON log_namespace=page_namespace AND log_title=page_title",
"INNER JOIN $user ON user_id=log_user" );
$this->whereClauses = array();
$this->limitType( $request->getVal( 'type' ) );
$this->limitUser( $request->getText( 'user' ) );
$this->limitTitle( $request->getText( 'page' ) );
$this->limitTime( $request->getVal( 'from' ), '>=' );
$this->limitTime( $request->getVal( 'until' ), '<=' );
list( $this->limit, $this->offset ) = $request->getLimitOffset();
}
/**
* Set the log reader to return only entries of the given type.
* @param string $type A log type ('upload', 'delete', etc)
* @private
*/
function limitType( $type ) {
if( empty( $type ) ) {
return false;
}
$this->type = $type;
$safetype = $this->db->strencode( $type );
$this->whereClauses[] = "log_type='$safetype'";
}
/**
* Set the log reader to return only entries by the given user.
* @param string $name (In)valid user name
* @private
*/
function limitUser( $name ) {
if ( $name == '' )
return false;
$usertitle = Title::makeTitle( NS_USER, $name );
if ( is_null( $usertitle ) )
return false;
$this->user = $usertitle->getText();
/* Fetch userid at first, if known, provides awesome query plan afterwards */
$userid = $this->db->selectField('user','user_id',array('user_name'=>$this->user));
if (!$userid)
/* It should be nicer to abort query at all,
but for now it won't pass anywhere behind the optimizer */
$this->whereClauses[] = "NULL";
else
$this->whereClauses[] = "log_user=$userid";
}
/**
* Set the log reader to return only entries affecting the given page.
* (For the block and rights logs, this is a user page.)
* @param string $page Title name as text
* @private
*/
function limitTitle( $page ) {
$title = Title::newFromText( $page );
if( empty( $page ) || is_null( $title ) ) {
return false;
}
$this->title =& $title;
$safetitle = $this->db->strencode( $title->getDBkey() );
$ns = $title->getNamespace();
$this->whereClauses[] = "log_namespace=$ns AND log_title='$safetitle'";
}
/**
* Set the log reader to return only entries in a given time range.
* @param string $time Timestamp of one endpoint
* @param string $direction either ">=" or "<=" operators
* @private
*/
function limitTime( $time, $direction ) {
# Direction should be a comparison operator
if( empty( $time ) ) {
return false;
}
$safetime = $this->db->strencode( wfTimestamp( TS_MW, $time ) );
$this->whereClauses[] = "log_timestamp $direction '$safetime'";
}
/**
* Build an SQL query from all the set parameters.
* @return string the SQL query
* @private
*/
function getQuery() {
$logging = $this->db->tableName( "logging" );
$user = $this->db->tableName( 'user' );
$sql = "SELECT /*! STRAIGHT_JOIN */ log_type, log_action, log_timestamp,
log_user, user_name,
log_namespace, log_title, page_id,
log_comment, log_params FROM $logging ";
if( !empty( $this->joinClauses ) ) {
$sql .= implode( ' ', $this->joinClauses );
}
if( !empty( $this->whereClauses ) ) {
$sql .= " WHERE " . implode( ' AND ', $this->whereClauses );
}
$sql .= " ORDER BY log_timestamp DESC ";
$sql = $this->db->limitResult($sql, $this->limit, $this->offset );
return $sql;
}
/**
* Execute the query and start returning results.
* @return ResultWrapper result object to return the relevant rows
*/
function getRows() {
$res = $this->db->query( $this->getQuery(), 'LogReader::getRows' );
return $this->db->resultObject( $res );
}
/**
* @return string The query type that this LogReader has been limited to.
*/
function queryType() {
return $this->type;
}
/**
* @return string The username type that this LogReader has been limited to, if any.
*/
function queryUser() {
return $this->user;
}
/**
* @return string The text of the title that this LogReader has been limited to.
*/
function queryTitle() {
if( is_null( $this->title ) ) {
return '';
} else {
return $this->title->getPrefixedText();
}
}
}
/**
*
* @package MediaWiki
* @subpackage SpecialPage
*/
class LogViewer {
/**
* @var LogReader $reader
*/
var $reader;
var $numResults = 0;
/**
* @param LogReader &$reader where to get our data from
*/
function LogViewer( &$reader ) {
global $wgUser;
$this->skin =& $wgUser->getSkin();
$this->reader =& $reader;
}
/**
* Take over the whole output page in $wgOut with the log display.
*/
function show() {
global $wgOut;
$this->showHeader( $wgOut );
$this->showOptions( $wgOut );
$result = $this->getLogRows();
$this->showPrevNext( $wgOut );
$this->doShowList( $wgOut, $result );
$this->showPrevNext( $wgOut );
}
/**
* Load the data from the linked LogReader
* Preload the link cache
* Initialise numResults
*
* Must be called before calling showPrevNext
*
* @return object database result set
*/
function getLogRows() {
$result = $this->reader->getRows();
$this->numResults = 0;
// Fetch results and form a batch link existence query
$batch = new LinkBatch;
while ( $s = $result->fetchObject() ) {
// User link
$title = Title::makeTitleSafe( NS_USER, $s->user_name );
$batch->addObj( $title );
// Move destination link
if ( $s->log_type == 'move' ) {
$paramArray = LogPage::extractParams( $s->log_params );
$title = Title::newFromText( $paramArray[0] );
$batch->addObj( $title );
}
++$this->numResults;
}
$batch->execute();
return $result;
}
/**
* Output just the list of entries given by the linked LogReader,
* with extraneous UI elements. Use for displaying log fragments in
* another page (eg at Special:Undelete)
* @param OutputPage $out where to send output
*/
function showList( &$out ) {
$this->doShowList( $out, $this->getLogRows() );
}
function doShowList( &$out, $result ) {
// Rewind result pointer and go through it again, making the HTML
if ($this->numResults > 0) {
$html = "\n<ul>\n";
$result->seek( 0 );
while( $s = $result->fetchObject() ) {
$html .= $this->logLine( $s );
}
$html .= "\n</ul>\n";
$out->addHTML( $html );
} else {
$out->addWikiText( wfMsg( 'logempty' ) );
}
$result->free();
}
/**
* @param Object $s a single row from the result set
* @return string Formatted HTML list item
* @private
*/
function logLine( $s ) {
global $wgLang;
$title = Title::makeTitle( $s->log_namespace, $s->log_title );
$user = Title::makeTitleSafe( NS_USER, $s->user_name );
$time = $wgLang->timeanddate( wfTimestamp(TS_MW, $s->log_timestamp), true );
// Enter the existence or non-existence of this page into the link cache,
// for faster makeLinkObj() in LogPage::actionText()
$linkCache =& LinkCache::singleton();
if( $s->page_id ) {
$linkCache->addGoodLinkObj( $s->page_id, $title );
} else {
$linkCache->addBadLinkObj( $title );
}
$userLink = $this->skin->userLink( $s->log_user, $s->user_name ) . $this->skin->userToolLinks( $s->log_user, $s->user_name );
$comment = $this->skin->commentBlock( $s->log_comment );
$paramArray = LogPage::extractParams( $s->log_params );
$revert = '';
if ( $s->log_type == 'move' && isset( $paramArray[0] ) ) {
$specialTitle = Title::makeTitle( NS_SPECIAL, 'Movepage' );
$destTitle = Title::newFromText( $paramArray[0] );
if ( $destTitle ) {
$revert = '(' . $this->skin->makeKnownLinkObj( $specialTitle, wfMsg( 'revertmove' ),
'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
'&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
'&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
'&wpMovetalk=0' ) . ')';
}
}
$action = LogPage::actionText( $s->log_type, $s->log_action, $title, $this->skin, $paramArray, true, true );
$out = "<li>$time $userLink $action $comment $revert</li>\n";
return $out;
}
/**
* @param OutputPage &$out where to send output
* @private
*/
function showHeader( &$out ) {
$type = $this->reader->queryType();
if( LogPage::isLogType( $type ) ) {
$out->setPageTitle( LogPage::logName( $type ) );
$out->addWikiText( LogPage::logHeader( $type ) );
}
}
/**
* @param OutputPage &$out where to send output
* @private
*/
function showOptions( &$out ) {
global $wgScript;
$action = htmlspecialchars( $wgScript );
$title = Title::makeTitle( NS_SPECIAL, 'Log' );
$special = htmlspecialchars( $title->getPrefixedDBkey() );
$out->addHTML( "<form action=\"$action\" method=\"get\">\n" .
"<input type='hidden' name='title' value=\"$special\" />\n" .
$this->getTypeMenu() .
$this->getUserInput() .
$this->getTitleInput() .
"<input type='submit' value=\"" . wfMsg( 'allpagessubmit' ) . "\" />" .
"</form>" );
}
/**
* @return string Formatted HTML
* @private
*/
function getTypeMenu() {
$out = "<select name='type'>\n";
foreach( LogPage::validTypes() as $type ) {
$text = htmlspecialchars( LogPage::logName( $type ) );
$selected = ($type == $this->reader->queryType()) ? ' selected="selected"' : '';
$out .= "<option value=\"$type\"$selected>$text</option>\n";
}
$out .= "</select>\n";
return $out;
}
/**
* @return string Formatted HTML
* @private
*/
function getUserInput() {
$user = htmlspecialchars( $this->reader->queryUser() );
return wfMsg('specialloguserlabel') . "<input type='text' name='user' size='12' value=\"$user\" />\n";
}
/**
* @return string Formatted HTML
* @private
*/
function getTitleInput() {
$title = htmlspecialchars( $this->reader->queryTitle() );
return wfMsg('speciallogtitlelabel') . "<input type='text' name='page' size='20' value=\"$title\" />\n";
}
/**
* @param OutputPage &$out where to send output
* @private
*/
function showPrevNext( &$out ) {
global $wgContLang,$wgRequest;
$pieces = array();
$pieces[] = 'type=' . urlencode( $this->reader->queryType() );
$pieces[] = 'user=' . urlencode( $this->reader->queryUser() );
$pieces[] = 'page=' . urlencode( $this->reader->queryTitle() );
$bits = implode( '&', $pieces );
list( $limit, $offset ) = $wgRequest->getLimitOffset();
# TODO: use timestamps instead of offsets to make it more natural
# to go huge distances in time
$html = wfViewPrevNext( $offset, $limit,
$wgContLang->specialpage( 'Log' ),
$bits,
$this->numResults < $limit);
$out->addHTML( '<p>' . $html . '</p>' );
}
}
?>
|