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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
|
<?php
/**
* Contain classes to list log entries
*
* Copyright © 2004 Brooke Vibber <bvibber@wikimedia.org>
* https://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
*
* @file
*/
namespace MediaWiki\Pager;
use DatabaseLogEntry;
use LogEventsList;
use LogFormatterFactory;
use LogPage;
use MediaWiki\Cache\LinkBatchFactory;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\PageReference;
use MediaWiki\Title\Title;
use MediaWiki\User\ActorNormalization;
use Wikimedia\Rdbms\IExpression;
use Wikimedia\Rdbms\LikeValue;
/**
* @ingroup Pager
*/
class LogPager extends ReverseChronologicalPager {
/** @var array Log types */
private $types = [];
/** @var string Events limited to those by performer when set */
private $performer = '';
/** @var string Events limited to those about this page when set */
private $page = '';
/** @var bool */
private $pattern = false;
/** @var string */
private $typeCGI = '';
/** @var string */
private $action = '';
/** @var bool */
private $performerRestrictionsEnforced = false;
/** @var bool */
private $actionRestrictionsEnforced = false;
/** @var array */
private $mConds;
/** @var string */
private $mTagFilter;
/** @var bool */
private $mTagInvert;
/** @var LogEventsList */
public $mLogEventsList;
/** @var LinkBatchFactory */
private $linkBatchFactory;
/** @var ActorNormalization */
private $actorNormalization;
private LogFormatterFactory $logFormatterFactory;
/**
* @param LogEventsList $list
* @param string|array $types Log types to show
* @param string $performer The user who made the log entries
* @param string|PageReference $page The page the log entries are for
* @param bool $pattern Do a prefix search rather than an exact title match
* @param array $conds Extra conditions for the query
* @param int|bool $year The year to start from. Default: false
* @param int|bool $month The month to start from. Default: false
* @param int|bool $day The day to start from. Default: false
* @param string $tagFilter Tag
* @param string $action Specific action (subtype) requested
* @param int $logId Log entry ID, to limit to a single log entry.
* @param LinkBatchFactory|null $linkBatchFactory
* @param ActorNormalization|null $actorNormalization
* @param LogFormatterFactory|null $logFormatterFactory
* @param bool $tagInvert whether tags are filtered for (false) or out (true)
*/
public function __construct( $list, $types = [], $performer = '', $page = '',
$pattern = false, $conds = [], $year = false, $month = false, $day = false,
$tagFilter = '', $action = '', $logId = 0,
?LinkBatchFactory $linkBatchFactory = null,
?ActorNormalization $actorNormalization = null,
?LogFormatterFactory $logFormatterFactory = null,
$tagInvert = false
) {
parent::__construct( $list->getContext() );
$services = MediaWikiServices::getInstance();
$this->mConds = $conds;
$this->mLogEventsList = $list;
// Class is used directly in extensions - T266480
$this->linkBatchFactory = $linkBatchFactory ?? $services->getLinkBatchFactory();
$this->actorNormalization = $actorNormalization ?? $services->getActorNormalization();
$this->logFormatterFactory = $logFormatterFactory ?? $services->getLogFormatterFactory();
$this->limitLogId( $logId ); // set before types per T269761
$this->limitType( $types ); // also excludes hidden types
$this->limitFilterTypes();
$this->limitPerformer( $performer );
$this->limitTitle( $page, $pattern );
$this->limitAction( $action );
$this->getDateCond( $year, $month, $day );
$this->mTagFilter = (string)$tagFilter;
$this->mTagInvert = (bool)$tagInvert;
}
public function getDefaultQuery() {
$query = parent::getDefaultQuery();
$query['type'] = $this->typeCGI; // arrays won't work here
$query['user'] = $this->performer;
$query['day'] = $this->mDay;
$query['month'] = $this->mMonth;
$query['year'] = $this->mYear;
return $query;
}
private function limitFilterTypes() {
if ( $this->hasEqualsClause( 'log_id' ) ) { // T220834
return;
}
$filterTypes = $this->getFilterParams();
$excludeTypes = [];
foreach ( $filterTypes as $type => $hide ) {
if ( $hide ) {
$excludeTypes[] = $type;
}
}
if ( $excludeTypes ) {
$this->mConds[] = $this->mDb->expr( 'log_type', '!=', $excludeTypes );
}
}
public function getFilterParams() {
$filters = [];
if ( count( $this->types ) ) {
return $filters;
}
// FIXME: This is broken, values from HTMLForm should be used.
$wpfilters = $this->getRequest()->getArray( "wpfilters" );
$filterLogTypes = $this->getConfig()->get( MainConfigNames::FilterLogTypes );
foreach ( $filterLogTypes as $type => $default ) {
// Back-compat: Check old URL params if the new param wasn't passed
if ( $wpfilters === null ) {
$hide = $this->getRequest()->getBool( "hide_{$type}_log", $default );
} else {
$hide = !in_array( $type, $wpfilters );
}
$filters[$type] = $hide;
}
return $filters;
}
/**
* Set the log reader to return only entries of the given type.
* Type restrictions enforced here
*
* @param string|array $types Log types ('upload', 'delete', etc);
* empty string means no restriction
*/
private function limitType( $types ) {
$restrictions = $this->getConfig()->get( MainConfigNames::LogRestrictions );
// If $types is not an array, make it an array
$types = ( $types === '' ) ? [] : (array)$types;
// Don't even show header for private logs; don't recognize it...
$needReindex = false;
foreach ( $types as $type ) {
if ( isset( $restrictions[$type] )
&& !$this->getAuthority()->isAllowed( $restrictions[$type] )
) {
$needReindex = true;
$types = array_diff( $types, [ $type ] );
}
}
if ( $needReindex ) {
// Lots of this code makes assumptions that
// the first entry in the array is $types[0].
$types = array_values( $types );
}
$this->types = $types;
// Don't show private logs to unprivileged users.
// Also, only show them upon specific request to avoid surprises.
// Exception: if we are showing only a single log entry based on the log id,
// we don't require that "specific request" so that the links-in-logs feature
// works. See T269761
$audience = ( $types || $this->hasEqualsClause( 'log_id' ) ) ? 'user' : 'public';
$hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience, $this->getAuthority() );
if ( $hideLogs !== false ) {
$this->mConds[] = $hideLogs;
}
if ( count( $types ) ) {
$this->mConds['log_type'] = $types;
// Set typeCGI; used in url param for paging
if ( count( $types ) == 1 ) {
$this->typeCGI = $types[0];
}
}
}
/**
* Set the log reader to return only entries by the given user.
*
* @param string $name (In)valid user name
* @return void
*/
private function limitPerformer( $name ) {
if ( $name == '' ) {
return;
}
$actorId = $this->actorNormalization->findActorIdByName( $name, $this->mDb );
if ( !$actorId ) {
// Unknown user, match nothing.
$this->mConds[] = '1 = 0';
return;
}
$this->mConds[ 'log_actor' ] = $actorId;
$this->enforcePerformerRestrictions();
$this->performer = $name;
}
/**
* 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|PageReference $page
* @param bool $pattern
* @return void
*/
private function limitTitle( $page, $pattern ) {
if ( !$page instanceof PageReference ) {
// NOTE: For some types of logs, the title may be something strange, like "User:#12345"!
$page = Title::newFromText( $page );
if ( !$page ) {
return;
}
}
$titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
$this->page = $titleFormatter->getPrefixedDBkey( $page );
$ns = $page->getNamespace();
$db = $this->mDb;
$interwikiDelimiter = $this->getConfig()->get( MainConfigNames::UserrightsInterwikiDelimiter );
$doUserRightsLogLike = false;
if ( $this->types == [ 'rights' ] ) {
$parts = explode( $interwikiDelimiter, $page->getDBkey() );
if ( count( $parts ) == 2 ) {
[ $name, $database ] = array_map( 'trim', $parts );
if ( str_contains( $database, '*' ) ) {
$doUserRightsLogLike = true;
}
}
}
/**
* Using the (log_namespace, log_title, log_timestamp) index with a
* range scan (LIKE) on the first two parts, instead of simple equality,
* makes it unusable for sorting. Sorted retrieval using another index
* would be possible, but then we might have to scan arbitrarily many
* nodes of that index. Therefore, we need to avoid this if $wgMiserMode
* is on.
*
* This is not a problem with simple title matches, because then we can
* use the log_page_time index. That should have no more than a few hundred
* log entries for even the busiest pages, so it can be safely scanned
* in full to satisfy an impossible condition on user or similar.
*/
$this->mConds['log_namespace'] = $ns;
if ( $doUserRightsLogLike ) {
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable $name is set when reached here
$params = [ $name . $interwikiDelimiter ];
// @phan-suppress-next-next-line PhanPossiblyUndeclaredVariable $database is set when reached here
// @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal $database is set when reached here
$databaseParts = explode( '*', $database );
$databasePartCount = count( $databaseParts );
foreach ( $databaseParts as $i => $databasepart ) {
$params[] = $databasepart;
if ( $i < $databasePartCount - 1 ) {
$params[] = $db->anyString();
}
}
$this->mConds[] = $db->expr( 'log_title', IExpression::LIKE, new LikeValue( ...$params ) );
} elseif ( $pattern && !$this->getConfig()->get( MainConfigNames::MiserMode ) ) {
$this->mConds[] = $db->expr(
'log_title',
IExpression::LIKE,
new LikeValue( $page->getDBkey(), $db->anyString() )
);
$this->pattern = $pattern;
} else {
$this->mConds['log_title'] = $page->getDBkey();
}
$this->enforceActionRestrictions();
}
/**
* Set the log_action field to a specified value (or values)
*
* @param string $action
*/
private function limitAction( $action ) {
// Allow to filter the log by actions
$type = $this->typeCGI;
if ( $type === '' ) {
// nothing to do
return;
}
$actions = $this->getConfig()->get( MainConfigNames::ActionFilteredLogs );
if ( isset( $actions[$type] ) ) {
// log type can be filtered by actions
if ( $action !== '' && isset( $actions[$type][$action] ) ) {
// add condition to query
$this->mConds['log_action'] = $actions[$type][$action];
$this->action = $action;
}
}
}
/**
* Limit to the (single) specified log ID.
* @param int $logId The log entry ID.
*/
protected function limitLogId( $logId ) {
if ( !$logId ) {
return;
}
$this->mConds['log_id'] = $logId;
}
/**
* Constructs the most part of the query. Extra conditions are sprinkled in
* all over this class.
* @return array
*/
public function getQueryInfo() {
$queryBuilder = DatabaseLogEntry::newSelectQueryBuilder( $this->mDb )
->where( $this->mConds );
# Add log_search table if there are conditions on it.
# This filters the results to only include log rows that have
# log_search records with the specified ls_field and ls_value values.
if ( array_key_exists( 'ls_field', $this->mConds ) ) {
$queryBuilder->join( 'log_search', null, 'ls_log_id=log_id' );
$queryBuilder->ignoreIndex( [ 'log_search' => 'ls_log_id' ] );
$queryBuilder->useIndex( [ 'logging' => 'PRIMARY' ] );
if ( !$this->hasEqualsClause( 'ls_field' )
|| !$this->hasEqualsClause( 'ls_value' )
) {
# Since (ls_field,ls_value,ls_logid) is unique, if the condition is
# to match a specific (ls_field,ls_value) tuple, then there will be
# no duplicate log rows. Otherwise, we need to remove the duplicates.
$queryBuilder->distinct();
}
} elseif ( array_key_exists( 'log_actor', $this->mConds ) ) {
// Optimizer doesn't pick the right index when a user has lots of log actions (T303089)
$index = 'log_actor_time';
foreach ( $this->getFilterParams() as $hide ) {
if ( !$hide ) {
$index = 'log_actor_type_time';
break;
}
}
$queryBuilder->useIndex( [ 'logging' => $index ] );
}
// T221458: MySQL/MariaDB (10.1.37) can sometimes irrationally decide that querying `actor` before
// `logging` and filesorting is somehow better than querying $limit+1 rows from `logging`.
// Tell it not to reorder the query. But not when tag filtering or log_search was used, as it
// seems as likely to be harmed as helped in that case.
if ( $this->mTagFilter === '' && !array_key_exists( 'ls_field', $this->mConds ) ) {
$queryBuilder->straightJoinOption();
}
$maxExecTime = $this->getConfig()->get( MainConfigNames::MaxExecutionTimeForExpensiveQueries );
if ( $maxExecTime ) {
$queryBuilder->setMaxExecutionTime( $maxExecTime );
}
# Add ChangeTags filter query
MediaWikiServices::getInstance()->getChangeTagsStore()->modifyDisplayQueryBuilder(
$queryBuilder,
'logging',
$this->mTagFilter,
$this->mTagInvert
);
return $queryBuilder->getQueryInfo();
}
/**
* Checks if $this->mConds has $field matched to a *single* value
* @param string $field
* @return bool
*/
protected function hasEqualsClause( $field ) {
return (
array_key_exists( $field, $this->mConds ) &&
( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
);
}
public function getIndexField() {
return [ [ 'log_timestamp', 'log_id' ] ];
}
protected function doBatchLookups() {
$lb = $this->linkBatchFactory->newLinkBatch();
foreach ( $this->mResult as $row ) {
$lb->add( $row->log_namespace, $row->log_title );
$lb->add( NS_USER, $row->log_user_text );
$lb->add( NS_USER_TALK, $row->log_user_text );
$formatter = $this->logFormatterFactory->newFromRow( $row );
foreach ( $formatter->getPreloadTitles() as $title ) {
$lb->addObj( $title );
}
}
$lb->execute();
}
public function formatRow( $row ) {
return $this->mLogEventsList->logLine( $row );
}
public function getType() {
return $this->types;
}
/**
* Guaranteed to either return a valid title string or a Zero-Length String
*
* @return string
*/
public function getPerformer() {
return $this->performer;
}
/**
* @return string
*/
public function getPage() {
return $this->page;
}
/**
* @return bool
*/
public function getPattern() {
return $this->pattern;
}
public function getYear() {
return $this->mYear;
}
public function getMonth() {
return $this->mMonth;
}
public function getDay() {
return $this->mDay;
}
public function getTagFilter() {
return $this->mTagFilter;
}
public function getTagInvert() {
return $this->mTagInvert;
}
public function getAction() {
return $this->action;
}
/**
* Paranoia: avoid brute force searches (T19342)
*/
private function enforceActionRestrictions() {
if ( $this->actionRestrictionsEnforced ) {
return;
}
$this->actionRestrictionsEnforced = true;
if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
$this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0';
} elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
$this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_ACTION ) .
' != ' . LogPage::SUPPRESSED_USER;
}
}
/**
* Paranoia: avoid brute force searches (T19342)
*/
private function enforcePerformerRestrictions() {
// Same as enforceActionRestrictions(), except for _USER instead of _ACTION bits.
if ( $this->performerRestrictionsEnforced ) {
return;
}
$this->performerRestrictionsEnforced = true;
if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
$this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0';
} elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
$this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_USER ) .
' != ' . LogPage::SUPPRESSED_ACTION;
}
}
}
/** @deprecated class alias since 1.41 */
class_alias( LogPager::class, 'LogPager' );
|