MediaWiki  REL1_31
LogPager.php
Go to the documentation of this file.
1 <?php
31  private $types = [];
32 
34  private $performer = '';
35 
37  private $title = '';
38 
40  private $pattern = '';
41 
43  private $typeCGI = '';
44 
46  private $action = '';
47 
50 
52  private $actionRestrictionsEnforced = false;
53 
56 
70  public function __construct( $list, $types = [], $performer = '', $title = '',
71  $pattern = '', $conds = [], $year = false, $month = false, $tagFilter = '',
72  $action = '', $logId = false
73  ) {
74  parent::__construct( $list->getContext() );
75  $this->mConds = $conds;
76 
77  $this->mLogEventsList = $list;
78 
79  $this->limitType( $types ); // also excludes hidden types
80  $this->limitPerformer( $performer );
81  $this->limitTitle( $title, $pattern );
82  $this->limitAction( $action );
83  $this->getDateCond( $year, $month );
84  $this->mTagFilter = $tagFilter;
85  $this->limitLogId( $logId );
86 
87  $this->mDb = wfGetDB( DB_REPLICA, 'logpager' );
88  }
89 
90  public function getDefaultQuery() {
91  $query = parent::getDefaultQuery();
92  $query['type'] = $this->typeCGI; // arrays won't work here
93  $query['user'] = $this->performer;
94  $query['month'] = $this->mMonth;
95  $query['year'] = $this->mYear;
96 
97  return $query;
98  }
99 
100  // Call ONLY after calling $this->limitType() already!
101  public function getFilterParams() {
103  $filters = [];
104  if ( count( $this->types ) ) {
105  return $filters;
106  }
107  foreach ( $wgFilterLogTypes as $type => $default ) {
108  $hide = $this->getRequest()->getInt( "hide_{$type}_log", $default );
109 
110  $filters[$type] = $hide;
111  if ( $hide ) {
112  $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
113  }
114  }
115 
116  return $filters;
117  }
118 
126  private function limitType( $types ) {
128 
129  $user = $this->getUser();
130  // If $types is not an array, make it an array
131  $types = ( $types === '' ) ? [] : (array)$types;
132  // Don't even show header for private logs; don't recognize it...
133  $needReindex = false;
134  foreach ( $types as $type ) {
135  if ( isset( $wgLogRestrictions[$type] )
136  && !$user->isAllowed( $wgLogRestrictions[$type] )
137  ) {
138  $needReindex = true;
139  $types = array_diff( $types, [ $type ] );
140  }
141  }
142  if ( $needReindex ) {
143  // Lots of this code makes assumptions that
144  // the first entry in the array is $types[0].
145  $types = array_values( $types );
146  }
147  $this->types = $types;
148  // Don't show private logs to unprivileged users.
149  // Also, only show them upon specific request to avoid suprises.
150  $audience = $types ? 'user' : 'public';
151  $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience, $user );
152  if ( $hideLogs !== false ) {
153  $this->mConds[] = $hideLogs;
154  }
155  if ( count( $types ) ) {
156  $this->mConds['log_type'] = $types;
157  // Set typeCGI; used in url param for paging
158  if ( count( $types ) == 1 ) {
159  $this->typeCGI = $types[0];
160  }
161  }
162  }
163 
170  private function limitPerformer( $name ) {
171  if ( $name == '' ) {
172  return;
173  }
174  $usertitle = Title::makeTitleSafe( NS_USER, $name );
175  if ( is_null( $usertitle ) ) {
176  return;
177  }
178  // Normalize username first so that non-existent users used
179  // in maintenance scripts work
180  $name = $usertitle->getText();
181 
182  // Assume no joins required for log_user
183  $this->mConds[] = ActorMigration::newMigration()->getWhere(
184  wfGetDB( DB_REPLICA ), 'log_user', User::newFromName( $name, false )
185  )['conds'];
186 
188 
189  $this->performer = $name;
190  }
191 
200  private function limitTitle( $page, $pattern ) {
202 
203  if ( $page instanceof Title ) {
204  $title = $page;
205  } else {
206  $title = Title::newFromText( $page );
207  if ( strlen( $page ) == 0 || !$title instanceof Title ) {
208  return;
209  }
210  }
211 
212  $this->title = $title->getPrefixedText();
213  $ns = $title->getNamespace();
214  $db = $this->mDb;
215 
216  $doUserRightsLogLike = false;
217  if ( $this->types == [ 'rights' ] ) {
218  $parts = explode( $wgUserrightsInterwikiDelimiter, $title->getDBkey() );
219  if ( count( $parts ) == 2 ) {
220  list( $name, $database ) = array_map( 'trim', $parts );
221  if ( strstr( $database, '*' ) ) { // Search for wildcard in database name
222  $doUserRightsLogLike = true;
223  }
224  }
225  }
226 
240  $this->mConds['log_namespace'] = $ns;
241  if ( $doUserRightsLogLike ) {
243  foreach ( explode( '*', $database ) as $databasepart ) {
244  $params[] = $databasepart;
245  $params[] = $db->anyString();
246  }
247  array_pop( $params ); // Get rid of the last % we added.
248  $this->mConds[] = 'log_title' . $db->buildLike( $params );
249  } elseif ( $pattern && !$wgMiserMode ) {
250  $this->mConds[] = 'log_title' . $db->buildLike( $title->getDBkey(), $db->anyString() );
251  $this->pattern = $pattern;
252  } else {
253  $this->mConds['log_title'] = $title->getDBkey();
254  }
255  $this->enforceActionRestrictions();
256  }
257 
263  private function limitAction( $action ) {
265  // Allow to filter the log by actions
267  if ( $type === '' ) {
268  // nothing to do
269  return;
270  }
271  $actions = $wgActionFilteredLogs;
272  if ( isset( $actions[$type] ) ) {
273  // log type can be filtered by actions
274  $this->mLogEventsList->setAllowedActions( array_keys( $actions[$type] ) );
275  if ( $action !== '' && isset( $actions[$type][$action] ) ) {
276  // add condition to query
277  $this->mConds['log_action'] = $actions[$type][$action];
278  $this->action = $action;
279  }
280  }
281  }
282 
287  protected function limitLogId( $logId ) {
288  if ( !$logId ) {
289  return;
290  }
291  $this->mConds['log_id'] = $logId;
292  }
293 
299  public function getQueryInfo() {
301 
302  $tables = $basic['tables'];
303  $fields = $basic['fields'];
304  $conds = $basic['conds'];
305  $options = $basic['options'];
306  $joins = $basic['join_conds'];
307 
308  # Add log_search table if there are conditions on it.
309  # This filters the results to only include log rows that have
310  # log_search records with the specified ls_field and ls_value values.
311  if ( array_key_exists( 'ls_field', $this->mConds ) ) {
312  $tables[] = 'log_search';
313  $options['IGNORE INDEX'] = [ 'log_search' => 'ls_log_id' ];
314  $options['USE INDEX'] = [ 'logging' => 'PRIMARY' ];
315  if ( !$this->hasEqualsClause( 'ls_field' )
316  || !$this->hasEqualsClause( 'ls_value' )
317  ) {
318  # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
319  # to match a specific (ls_field,ls_value) tuple, then there will be
320  # no duplicate log rows. Otherwise, we need to remove the duplicates.
321  $options[] = 'DISTINCT';
322  }
323  }
324  # Don't show duplicate rows when using log_search
325  $joins['log_search'] = [ 'INNER JOIN', 'ls_log_id=log_id' ];
326 
327  $info = [
328  'tables' => $tables,
329  'fields' => $fields,
330  'conds' => array_merge( $conds, $this->mConds ),
331  'options' => $options,
332  'join_conds' => $joins,
333  ];
334  # Add ChangeTags filter query
335  ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
336  $info['join_conds'], $info['options'], $this->mTagFilter );
337 
338  return $info;
339  }
340 
346  protected function hasEqualsClause( $field ) {
347  return (
348  array_key_exists( $field, $this->mConds ) &&
349  ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
350  );
351  }
352 
353  function getIndexField() {
354  return 'log_timestamp';
355  }
356 
357  public function getStartBody() {
358  # Do a link batch query
359  if ( $this->getNumRows() > 0 ) {
360  $lb = new LinkBatch;
361  foreach ( $this->mResult as $row ) {
362  $lb->add( $row->log_namespace, $row->log_title );
363  $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
364  $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
365  $formatter = LogFormatter::newFromRow( $row );
366  foreach ( $formatter->getPreloadTitles() as $title ) {
367  $lb->addObj( $title );
368  }
369  }
370  $lb->execute();
371  $this->mResult->seek( 0 );
372  }
373 
374  return '';
375  }
376 
377  public function formatRow( $row ) {
378  return $this->mLogEventsList->logLine( $row );
379  }
380 
381  public function getType() {
382  return $this->types;
383  }
384 
390  public function getPerformer() {
391  return $this->performer;
392  }
393 
397  public function getPage() {
398  return $this->title;
399  }
400 
401  public function getPattern() {
402  return $this->pattern;
403  }
404 
405  public function getYear() {
406  return $this->mYear;
407  }
408 
409  public function getMonth() {
410  return $this->mMonth;
411  }
412 
413  public function getTagFilter() {
414  return $this->mTagFilter;
415  }
416 
417  public function getAction() {
418  return $this->action;
419  }
420 
421  public function doQuery() {
422  // Workaround MySQL optimizer bug
423  $this->mDb->setBigSelects();
424  parent::doQuery();
425  $this->mDb->setBigSelects( 'default' );
426  }
427 
431  private function enforceActionRestrictions() {
432  if ( $this->actionRestrictionsEnforced ) {
433  return;
434  }
435  $this->actionRestrictionsEnforced = true;
436  $user = $this->getUser();
437  if ( !$user->isAllowed( 'deletedhistory' ) ) {
438  $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0';
439  } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
440  $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_ACTION ) .
441  ' != ' . LogPage::SUPPRESSED_USER;
442  }
443  }
444 
448  private function enforcePerformerRestrictions() {
449  // Same as enforceActionRestrictions(), except for _USER instead of _ACTION bits.
450  if ( $this->performerRestrictionsEnforced ) {
451  return;
452  }
453  $this->performerRestrictionsEnforced = true;
454  $user = $this->getUser();
455  if ( !$user->isAllowed( 'deletedhistory' ) ) {
456  $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0';
457  } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
458  $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_USER ) .
460  }
461  }
462 }
$wgActionFilteredLogs
$wgActionFilteredLogs
List of log types that can be filtered by action types.
Definition: DefaultSettings.php:7811
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:247
ReverseChronologicalPager\getDateCond
getDateCond( $year, $month, $day=-1)
Set and return the mOffset timestamp such that we can get all revisions with a timestamp up to the sp...
Definition: ReverseChronologicalPager.php:73
LogPage\SUPPRESSED_ACTION
const SUPPRESSED_ACTION
Definition: LogPage.php:39
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:273
LogPage\SUPPRESSED_USER
const SUPPRESSED_USER
Definition: LogPage.php:38
LogPager\getPattern
getPattern()
Definition: LogPager.php:401
LogPager\getType
getType()
Definition: LogPager.php:381
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
LinkBatch\add
add( $ns, $dbkey)
Definition: LinkBatch.php:80
LogPager\getPerformer
getPerformer()
Guaranteed to either return a valid title string or a Zero-Length String.
Definition: LogPager.php:390
array
the array() calling protocol came about after MediaWiki 1.4rc1.
LogPager\limitTitle
limitTitle( $page, $pattern)
Set the log reader to return only entries affecting the given page.
Definition: LogPager.php:200
LogPager\getMonth
getMonth()
Definition: LogPager.php:409
LogPager\getFilterParams
getFilterParams()
Definition: LogPager.php:101
LogPager\$types
array $types
Log types.
Definition: LogPager.php:31
DatabaseLogEntry\getSelectQueryData
static getSelectQueryData()
Returns array of information that is needed for querying log entries.
Definition: LogEntry.php:172
LogPager\getQueryInfo
getQueryInfo()
Constructs the most part of the query.
Definition: LogPager.php:299
LogPager\$performerRestrictionsEnforced
bool $performerRestrictionsEnforced
Definition: LogPager.php:49
Title\getPrefixedText
getPrefixedText()
Get the prefixed title with spaces.
Definition: Title.php:1625
LogPager
Definition: LogPager.php:29
$params
$params
Definition: styleTest.css.php:40
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:591
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:71
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:89
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
IndexPager\$mDb
$mDb
Definition: IndexPager.php:83
LogPager\getPage
getPage()
Definition: LogPager.php:397
ChangeTags\modifyDisplayQuery
static modifyDisplayQuery(&$tables, &$fields, &$conds, &$join_conds, &$options, $filter_tag='')
Applies all tags-related changes to a query.
Definition: ChangeTags.php:723
LogPager\$title
string Title $title
Events limited to those about Title when set.
Definition: LogPager.php:37
Title\getDBkey
getDBkey()
Get the main part with underscores.
Definition: Title.php:947
LogPager\$action
string $action
Definition: LogPager.php:46
LogPager\formatRow
formatRow( $row)
Abstract formatting function.
Definition: LogPager.php:377
LogFormatter\newFromRow
static newFromRow( $row)
Handy shortcut for constructing a formatter directly from database row.
Definition: LogFormatter.php:76
LogPager\getIndexField
getIndexField()
This function should be overridden to return the name of the index fi- eld.
Definition: LogPager.php:353
Title\getNamespace
getNamespace()
Get the namespace index, i.e.
Definition: Title.php:970
$wgFilterLogTypes
$wgFilterLogTypes
Show/hide links on Special:Log will be shown for these log types.
Definition: DefaultSettings.php:7699
LogPage\DELETED_USER
const DELETED_USER
Definition: LogPage.php:34
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2812
title
title
Definition: parserTests.txt:219
$wgUserrightsInterwikiDelimiter
$wgUserrightsInterwikiDelimiter
Character used as a delimiter when testing for interwiki userrights (In Special:UserRights,...
Definition: DefaultSettings.php:4941
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
LogPager\$actionRestrictionsEnforced
bool $actionRestrictionsEnforced
Definition: LogPager.php:52
LogPager\__construct
__construct( $list, $types=[], $performer='', $title='', $pattern='', $conds=[], $year=false, $month=false, $tagFilter='', $action='', $logId=false)
Definition: LogPager.php:70
LogEventsList
Definition: LogEventsList.php:30
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
LogPage\DELETED_ACTION
const DELETED_ACTION
Definition: LogPage.php:32
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:2001
LogPager\getStartBody
getStartBody()
Hook into getBody(), allows text to be inserted at the start.
Definition: LogPager.php:357
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:562
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:77
$wgLogRestrictions
$wgLogRestrictions
This restricts log access to those who have a certain right Users without this will not see it in the...
Definition: DefaultSettings.php:7676
LogEventsList\getExcludeClause
static getExcludeClause( $db, $audience='public', User $user=null)
SQL clause to skip forbidden log types for this user.
Definition: LogEventsList.php:772
LogPager\$mLogEventsList
LogEventsList $mLogEventsList
Definition: LogPager.php:55
LogPager\$typeCGI
string $typeCGI
Definition: LogPager.php:43
LogPager\doQuery
doQuery()
Do the query, using information from the object context.
Definition: LogPager.php:421
LogPager\getAction
getAction()
Definition: LogPager.php:417
ReverseChronologicalPager\$mYear
$mYear
Definition: ReverseChronologicalPager.php:30
LogPager\getTagFilter
getTagFilter()
Definition: LogPager.php:413
LogPager\$performer
string $performer
Events limited to those by performer when set.
Definition: LogPager.php:34
LogPager\limitType
limitType( $types)
Set the log reader to return only entries of the given type.
Definition: LogPager.php:126
LogPager\limitPerformer
limitPerformer( $name)
Set the log reader to return only entries by the given user.
Definition: LogPager.php:170
Title
Represents a title within MediaWiki.
Definition: Title.php:39
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
LogPager\hasEqualsClause
hasEqualsClause( $field)
Checks if $this->mConds has $field matched to a single value.
Definition: LogPager.php:346
$wgMiserMode
$wgMiserMode
Disable database-intensive features.
Definition: DefaultSettings.php:2191
LogPager\$pattern
string $pattern
Definition: LogPager.php:40
LogPager\enforceActionRestrictions
enforceActionRestrictions()
Paranoia: avoid brute force searches (T19342)
Definition: LogPager.php:431
LogPager\getDefaultQuery
getDefaultQuery()
Get an array of query parameters that should be put into self-links.
Definition: LogPager.php:90
$tables
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist & $tables
Definition: hooks.txt:1015
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:22
ReverseChronologicalPager\$mMonth
$mMonth
Definition: ReverseChronologicalPager.php:31
LogPager\enforcePerformerRestrictions
enforcePerformerRestrictions()
Paranoia: avoid brute force searches (T19342)
Definition: LogPager.php:448
NS_USER
const NS_USER
Definition: Defines.php:76
ReverseChronologicalPager
Efficient paging for SQL queries.
Definition: ReverseChronologicalPager.php:28
LogPager\limitAction
limitAction( $action)
Set the log_action field to a specified value (or values)
Definition: LogPager.php:263
$query
null for the wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1620
IndexPager\getNumRows
getNumRows()
Get the number of rows in the result set.
Definition: IndexPager.php:555
LogPager\limitLogId
limitLogId( $logId)
Limit to the (single) specified log ID.
Definition: LogPager.php:287
LogPager\getYear
getYear()
Definition: LogPager.php:405
$type
$type
Definition: testCompression.php:48