3 * Contain classes to list log entries
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
29 class LogPager
extends ReverseChronologicalPager
{
30 /** @var array Log types */
33 /** @var string Events limited to those by performer when set */
34 private $performer = '';
36 /** @var string|Title Events limited to those about Title when set */
40 private $pattern = '';
43 private $typeCGI = '';
49 private $performerRestrictionsEnforced = false;
52 private $actionRestrictionsEnforced = false;
54 /** @var LogEventsList */
55 public $mLogEventsList;
58 * @param LogEventsList $list
59 * @param string|array $types Log types to show
60 * @param string $performer The user who made the log entries
61 * @param string|Title $title The page title the log entries are for
62 * @param string $pattern Do a prefix search rather than an exact title match
63 * @param array $conds Extra conditions for the query
64 * @param int|bool $year The year to start from. Default: false
65 * @param int|bool $month The month to start from. Default: false
66 * @param string $tagFilter Tag
67 * @param string $action Specific action (subtype) requested
69 public function __construct( $list, $types = [], $performer = '', $title = '',
70 $pattern = '', $conds = [], $year = false, $month = false, $tagFilter = '',
73 parent
::__construct( $list->getContext() );
74 $this->mConds
= $conds;
76 $this->mLogEventsList
= $list;
78 $this->limitType( $types ); // also excludes hidden types
79 $this->limitPerformer( $performer );
80 $this->limitTitle( $title, $pattern );
81 $this->limitAction( $action );
82 $this->getDateCond( $year, $month );
83 $this->mTagFilter
= $tagFilter;
85 $this->mDb
= wfGetDB( DB_REPLICA
, 'logpager' );
88 public function getDefaultQuery() {
89 $query = parent
::getDefaultQuery();
90 $query['type'] = $this->typeCGI
; // arrays won't work here
91 $query['user'] = $this->performer
;
92 $query['month'] = $this->mMonth
;
93 $query['year'] = $this->mYear
;
98 // Call ONLY after calling $this->limitType() already!
99 public function getFilterParams() {
100 global $wgFilterLogTypes;
102 if ( count( $this->types
) ) {
105 foreach ( $wgFilterLogTypes as $type => $default ) {
106 $hide = $this->getRequest()->getInt( "hide_{$type}_log", $default );
108 $filters[$type] = $hide;
110 $this->mConds
[] = 'log_type != ' . $this->mDb
->addQuotes( $type );
118 * Set the log reader to return only entries of the given type.
119 * Type restrictions enforced here
121 * @param string|array $types Log types ('upload', 'delete', etc);
122 * empty string means no restriction
124 private function limitType( $types ) {
125 global $wgLogRestrictions;
127 $user = $this->getUser();
128 // If $types is not an array, make it an array
129 $types = ( $types === '' ) ?
[] : (array)$types;
130 // Don't even show header for private logs; don't recognize it...
131 $needReindex = false;
132 foreach ( $types as $type ) {
133 if ( isset( $wgLogRestrictions[$type] )
134 && !$user->isAllowed( $wgLogRestrictions[$type] )
137 $types = array_diff( $types, [ $type ] );
140 if ( $needReindex ) {
141 // Lots of this code makes assumptions that
142 // the first entry in the array is $types[0].
143 $types = array_values( $types );
145 $this->types
= $types;
146 // Don't show private logs to unprivileged users.
147 // Also, only show them upon specific request to avoid suprises.
148 $audience = $types ?
'user' : 'public';
149 $hideLogs = LogEventsList
::getExcludeClause( $this->mDb
, $audience, $user );
150 if ( $hideLogs !== false ) {
151 $this->mConds
[] = $hideLogs;
153 if ( count( $types ) ) {
154 $this->mConds
['log_type'] = $types;
155 // Set typeCGI; used in url param for paging
156 if ( count( $types ) == 1 ) {
157 $this->typeCGI
= $types[0];
163 * Set the log reader to return only entries by the given user.
165 * @param string $name (In)valid user name
168 private function limitPerformer( $name ) {
172 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
173 if ( is_null( $usertitle ) ) {
176 // Normalize username first so that non-existent users used
177 // in maintenance scripts work
178 $name = $usertitle->getText();
179 /* Fetch userid at first, if known, provides awesome query plan afterwards */
180 $userid = User
::idFromName( $name );
182 $this->mConds
['log_user_text'] = IP
::sanitizeIP( $name );
184 $this->mConds
['log_user'] = $userid;
186 $this->enforcePerformerRestrictions();
188 $this->performer
= $name;
192 * Set the log reader to return only entries affecting the given page.
193 * (For the block and rights logs, this is a user page.)
195 * @param string|Title $page Title name
196 * @param string $pattern
199 private function limitTitle( $page, $pattern ) {
200 global $wgMiserMode, $wgUserrightsInterwikiDelimiter;
202 if ( $page instanceof Title
) {
205 $title = Title
::newFromText( $page );
206 if ( strlen( $page ) == 0 ||
!$title instanceof Title
) {
211 $this->title
= $title->getPrefixedText();
212 $ns = $title->getNamespace();
215 $doUserRightsLogLike = false;
216 if ( $this->types
== [ 'rights' ] ) {
217 $parts = explode( $wgUserrightsInterwikiDelimiter, $title->getDBkey() );
218 if ( count( $parts ) == 2 ) {
219 list( $name, $database ) = array_map( 'trim', $parts );
220 if ( strstr( $database, '*' ) ) { // Search for wildcard in database name
221 $doUserRightsLogLike = true;
227 * Using the (log_namespace, log_title, log_timestamp) index with a
228 * range scan (LIKE) on the first two parts, instead of simple equality,
229 * makes it unusable for sorting. Sorted retrieval using another index
230 * would be possible, but then we might have to scan arbitrarily many
231 * nodes of that index. Therefore, we need to avoid this if $wgMiserMode
234 * This is not a problem with simple title matches, because then we can
235 * use the page_time index. That should have no more than a few hundred
236 * log entries for even the busiest pages, so it can be safely scanned
237 * in full to satisfy an impossible condition on user or similar.
239 $this->mConds
['log_namespace'] = $ns;
240 if ( $doUserRightsLogLike ) {
241 $params = [ $name . $wgUserrightsInterwikiDelimiter ];
242 foreach ( explode( '*', $database ) as $databasepart ) {
243 $params[] = $databasepart;
244 $params[] = $db->anyString();
246 array_pop( $params ); // Get rid of the last % we added.
247 $this->mConds
[] = 'log_title' . $db->buildLike( $params );
248 } elseif ( $pattern && !$wgMiserMode ) {
249 $this->mConds
[] = 'log_title' . $db->buildLike( $title->getDBkey(), $db->anyString() );
250 $this->pattern
= $pattern;
252 $this->mConds
['log_title'] = $title->getDBkey();
254 $this->enforceActionRestrictions();
258 * Set the log_action field to a specified value (or values)
260 * @param string $action
262 private function limitAction( $action ) {
263 global $wgActionFilteredLogs;
264 // Allow to filter the log by actions
265 $type = $this->typeCGI
;
266 if ( $type === '' ) {
270 $actions = $wgActionFilteredLogs;
271 if ( isset( $actions[$type] ) ) {
272 // log type can be filtered by actions
273 $this->mLogEventsList
->setAllowedActions( array_keys( $actions[$type] ) );
274 if ( $action !== '' && isset( $actions[$type][$action] ) ) {
275 // add condition to query
276 $this->mConds
['log_action'] = $actions[$type][$action];
277 $this->action
= $action;
283 * Constructs the most part of the query. Extra conditions are sprinkled in
284 * all over this class.
287 public function getQueryInfo() {
288 $basic = DatabaseLogEntry
::getSelectQueryData();
290 $tables = $basic['tables'];
291 $fields = $basic['fields'];
292 $conds = $basic['conds'];
293 $options = $basic['options'];
294 $joins = $basic['join_conds'];
296 # Add log_search table if there are conditions on it.
297 # This filters the results to only include log rows that have
298 # log_search records with the specified ls_field and ls_value values.
299 if ( array_key_exists( 'ls_field', $this->mConds
) ) {
300 $tables[] = 'log_search';
301 $options['IGNORE INDEX'] = [ 'log_search' => 'ls_log_id' ];
302 $options['USE INDEX'] = [ 'logging' => 'PRIMARY' ];
303 if ( !$this->hasEqualsClause( 'ls_field' )
304 ||
!$this->hasEqualsClause( 'ls_value' )
306 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
307 # to match a specific (ls_field,ls_value) tuple, then there will be
308 # no duplicate log rows. Otherwise, we need to remove the duplicates.
309 $options[] = 'DISTINCT';
312 # Don't show duplicate rows when using log_search
313 $joins['log_search'] = [ 'INNER JOIN', 'ls_log_id=log_id' ];
318 'conds' => array_merge( $conds, $this->mConds
),
319 'options' => $options,
320 'join_conds' => $joins,
322 # Add ChangeTags filter query
323 ChangeTags
::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
324 $info['join_conds'], $info['options'], $this->mTagFilter
);
330 * Checks if $this->mConds has $field matched to a *single* value
331 * @param string $field
334 protected function hasEqualsClause( $field ) {
336 array_key_exists( $field, $this->mConds
) &&
337 ( !is_array( $this->mConds
[$field] ) ||
count( $this->mConds
[$field] ) == 1 )
341 function getIndexField() {
342 return 'log_timestamp';
345 public function getStartBody() {
346 # Do a link batch query
347 if ( $this->getNumRows() > 0 ) {
349 foreach ( $this->mResult
as $row ) {
350 $lb->add( $row->log_namespace
, $row->log_title
);
351 $lb->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
352 $lb->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
353 $formatter = LogFormatter
::newFromRow( $row );
354 foreach ( $formatter->getPreloadTitles() as $title ) {
355 $lb->addObj( $title );
359 $this->mResult
->seek( 0 );
365 public function formatRow( $row ) {
366 return $this->mLogEventsList
->logLine( $row );
369 public function getType() {
374 * Guaranteed to either return a valid title string or a Zero-Length String
378 public function getPerformer() {
379 return $this->performer
;
385 public function getPage() {
389 public function getPattern() {
390 return $this->pattern
;
393 public function getYear() {
397 public function getMonth() {
398 return $this->mMonth
;
401 public function getTagFilter() {
402 return $this->mTagFilter
;
405 public function getAction() {
406 return $this->action
;
409 public function doQuery() {
410 // Workaround MySQL optimizer bug
411 $this->mDb
->setBigSelects();
413 $this->mDb
->setBigSelects( 'default' );
417 * Paranoia: avoid brute force searches (T19342)
419 private function enforceActionRestrictions() {
420 if ( $this->actionRestrictionsEnforced
) {
423 $this->actionRestrictionsEnforced
= true;
424 $user = $this->getUser();
425 if ( !$user->isAllowed( 'deletedhistory' ) ) {
426 $this->mConds
[] = $this->mDb
->bitAnd( 'log_deleted', LogPage
::DELETED_USER
) . ' = 0';
427 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
428 $this->mConds
[] = $this->mDb
->bitAnd( 'log_deleted', LogPage
::SUPPRESSED_USER
) .
429 ' != ' . LogPage
::SUPPRESSED_USER
;
434 * Paranoia: avoid brute force searches (T19342)
436 private function enforcePerformerRestrictions() {
437 // Same as enforceActionRestrictions(), except for _USER instead of _ACTION bits.
438 if ( $this->performerRestrictionsEnforced
) {
441 $this->performerRestrictionsEnforced
= true;
442 $user = $this->getUser();
443 if ( !$user->isAllowed( 'deletedhistory' ) ) {
444 $this->mConds
[] = $this->mDb
->bitAnd( 'log_deleted', LogPage
::DELETED_ACTION
) . ' = 0';
445 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
446 $this->mConds
[] = $this->mDb
->bitAnd( 'log_deleted', LogPage
::SUPPRESSED_ACTION
) .
447 ' != ' . LogPage
::SUPPRESSED_ACTION
;