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 = '';
48 /** @var LogEventsList */
49 public $mLogEventsList;
52 * @param LogEventsList $list
53 * @param string|array $types Log types to show
54 * @param string $performer The user who made the log entries
55 * @param string|Title $title The page title the log entries are for
56 * @param string $pattern Do a prefix search rather than an exact title match
57 * @param array $conds Extra conditions for the query
58 * @param int|bool $year The year to start from. Default: false
59 * @param int|bool $month The month to start from. Default: false
60 * @param string $tagFilter Tag
61 * @param string $action Specific action (subtype) requested
62 * @param int $logId Log entry ID, to limit to a single log entry.
64 public function __construct( $list, $types = [], $performer = '', $title = '',
65 $pattern = '', $conds = [], $year = false, $month = false, $tagFilter = '',
66 $action = '', $logId = false
68 parent::__construct( $list->getContext() );
69 $this->mConds = $conds;
71 $this->mLogEventsList = $list;
73 $this->limitType( $types ); // also excludes hidden types
74 $this->limitPerformer( $performer );
75 $this->limitTitle( $title, $pattern );
76 $this->limitAction( $action );
77 $this->getDateCond( $year, $month );
78 $this->mTagFilter = $tagFilter;
79 $this->limitLogId( $logId );
81 $this->mDb = wfGetDB( DB_REPLICA, 'logpager' );
84 public function getDefaultQuery() {
85 $query = parent::getDefaultQuery();
86 $query['type'] = $this->typeCGI; // arrays won't work here
87 $query['user'] = $this->performer;
88 $query['month'] = $this->mMonth;
89 $query['year'] = $this->mYear;
94 // Call ONLY after calling $this->limitType() already!
95 public function getFilterParams() {
96 global $wgFilterLogTypes;
98 if ( count( $this->types ) ) {
101 foreach ( $wgFilterLogTypes as $type => $default ) {
102 // Avoid silly filtering
103 if ( $type !== 'patrol' || $this->getUser()->useNPPatrol() ) {
104 $hide = $this->getRequest()->getInt( "hide_{$type}_log", $default );
105 $filters[$type] = $hide;
107 $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
116 * Set the log reader to return only entries of the given type.
117 * Type restrictions enforced here
119 * @param string|array $types Log types ('upload', 'delete', etc);
120 * empty string means no restriction
122 private function limitType( $types ) {
123 global $wgLogRestrictions;
125 $user = $this->getUser();
126 // If $types is not an array, make it an array
127 $types = ( $types === '' ) ? [] : (array)$types;
128 // Don't even show header for private logs; don't recognize it...
129 $needReindex = false;
130 foreach ( $types as $type ) {
131 if ( isset( $wgLogRestrictions[$type] )
132 && !$user->isAllowed( $wgLogRestrictions[$type] )
135 $types = array_diff( $types, [ $type ] );
138 if ( $needReindex ) {
139 // Lots of this code makes assumptions that
140 // the first entry in the array is $types[0].
141 $types = array_values( $types );
143 $this->types = $types;
144 // Don't show private logs to unprivileged users.
145 // Also, only show them upon specific request to avoid suprises.
146 $audience = $types ? 'user' : 'public';
147 $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience, $user );
148 if ( $hideLogs !== false ) {
149 $this->mConds[] = $hideLogs;
151 if ( count( $types ) ) {
152 $this->mConds['log_type'] = $types;
153 // Set typeCGI; used in url param for paging
154 if ( count( $types ) == 1 ) {
155 $this->typeCGI = $types[0];
161 * Set the log reader to return only entries by the given user.
163 * @param string $name (In)valid user name
166 private function limitPerformer( $name ) {
170 $usertitle = Title::makeTitleSafe( NS_USER, $name );
171 if ( is_null( $usertitle ) ) {
174 // Normalize username first so that non-existent users used
175 // in maintenance scripts work
176 $name = $usertitle->getText();
177 /* Fetch userid at first, if known, provides awesome query plan afterwards */
178 $userid = User::idFromName( $name );
180 $this->mConds['log_user_text'] = IP::sanitizeIP( $name );
182 $this->mConds['log_user'] = $userid;
184 // Paranoia: avoid brute force searches (T19342)
185 $user = $this->getUser();
186 if ( !$user->isAllowed( 'deletedhistory' ) ) {
187 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0';
188 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
189 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_USER ) .
190 ' != ' . LogPage::SUPPRESSED_USER;
193 $this->performer = $name;
197 * Set the log reader to return only entries affecting the given page.
198 * (For the block and rights logs, this is a user page.)
200 * @param string|Title $page Title name
201 * @param string $pattern
204 private function limitTitle( $page, $pattern ) {
205 global $wgMiserMode, $wgUserrightsInterwikiDelimiter;
207 if ( $page instanceof Title ) {
210 $title = Title::newFromText( $page );
211 if ( strlen( $page ) == 0 || !$title instanceof Title ) {
216 $this->title = $title->getPrefixedText();
217 $ns = $title->getNamespace();
220 $doUserRightsLogLike = false;
221 if ( $this->types == [ 'rights' ] ) {
222 $parts = explode( $wgUserrightsInterwikiDelimiter, $title->getDBkey() );
223 if ( count( $parts ) == 2 ) {
224 list( $name, $database ) = array_map( 'trim', $parts );
225 if ( strstr( $database, '*' ) ) { // Search for wildcard in database name
226 $doUserRightsLogLike = true;
232 * Using the (log_namespace, log_title, log_timestamp) index with a
233 * range scan (LIKE) on the first two parts, instead of simple equality,
234 * makes it unusable for sorting. Sorted retrieval using another index
235 * would be possible, but then we might have to scan arbitrarily many
236 * nodes of that index. Therefore, we need to avoid this if $wgMiserMode
239 * This is not a problem with simple title matches, because then we can
240 * use the page_time index. That should have no more than a few hundred
241 * log entries for even the busiest pages, so it can be safely scanned
242 * in full to satisfy an impossible condition on user or similar.
244 $this->mConds['log_namespace'] = $ns;
245 if ( $doUserRightsLogLike ) {
246 $params = [ $name . $wgUserrightsInterwikiDelimiter ];
247 foreach ( explode( '*', $database ) as $databasepart ) {
248 $params[] = $databasepart;
249 $params[] = $db->anyString();
251 array_pop( $params ); // Get rid of the last % we added.
252 $this->mConds[] = 'log_title' . $db->buildLike( $params );
253 } elseif ( $pattern && !$wgMiserMode ) {
254 $this->mConds[] = 'log_title' . $db->buildLike( $title->getDBkey(), $db->anyString() );
255 $this->pattern = $pattern;
257 $this->mConds['log_title'] = $title->getDBkey();
259 // Paranoia: avoid brute force searches (T19342)
260 $user = $this->getUser();
261 if ( !$user->isAllowed( 'deletedhistory' ) ) {
262 $this->mConds[] = $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0';
263 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
264 $this->mConds[] = $db->bitAnd( 'log_deleted', LogPage::SUPPRESSED_ACTION ) .
265 ' != ' . LogPage::SUPPRESSED_ACTION;
270 * Set the log_action field to a specified value (or values)
272 * @param string $action
274 private function limitAction( $action ) {
275 global $wgActionFilteredLogs;
276 // Allow to filter the log by actions
277 $type = $this->typeCGI;
278 if ( $type === '' ) {
282 $actions = $wgActionFilteredLogs;
283 if ( isset( $actions[$type] ) ) {
284 // log type can be filtered by actions
285 $this->mLogEventsList->setAllowedActions( array_keys( $actions[$type] ) );
286 if ( $action !== '' && isset( $actions[$type][$action] ) ) {
287 // add condition to query
288 $this->mConds['log_action'] = $actions[$type][$action];
289 $this->action = $action;
295 * Limit to the (single) specified log ID.
296 * @param int $logId The log entry ID.
298 protected function limitLogId( $logId ) {
302 $this->mConds['log_id'] = $logId;
306 * Constructs the most part of the query. Extra conditions are sprinkled in
307 * all over this class.
310 public function getQueryInfo() {
311 $basic = DatabaseLogEntry::getSelectQueryData();
313 $tables = $basic['tables'];
314 $fields = $basic['fields'];
315 $conds = $basic['conds'];
316 $options = $basic['options'];
317 $joins = $basic['join_conds'];
319 # Add log_search table if there are conditions on it.
320 # This filters the results to only include log rows that have
321 # log_search records with the specified ls_field and ls_value values.
322 if ( array_key_exists( 'ls_field', $this->mConds ) ) {
323 $tables[] = 'log_search';
324 $options['IGNORE INDEX'] = [ 'log_search' => 'ls_log_id' ];
325 $options['USE INDEX'] = [ 'logging' => 'PRIMARY' ];
326 if ( !$this->hasEqualsClause( 'ls_field' )
327 || !$this->hasEqualsClause( 'ls_value' )
329 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
330 # to match a specific (ls_field,ls_value) tuple, then there will be
331 # no duplicate log rows. Otherwise, we need to remove the duplicates.
332 $options[] = 'DISTINCT';
335 # Don't show duplicate rows when using log_search
336 $joins['log_search'] = [ 'INNER JOIN', 'ls_log_id=log_id' ];
341 'conds' => array_merge( $conds, $this->mConds ),
342 'options' => $options,
343 'join_conds' => $joins,
345 # Add ChangeTags filter query
346 ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
347 $info['join_conds'], $info['options'], $this->mTagFilter );
353 * Checks if $this->mConds has $field matched to a *single* value
354 * @param string $field
357 protected function hasEqualsClause( $field ) {
359 array_key_exists( $field, $this->mConds ) &&
360 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
364 function getIndexField() {
365 return 'log_timestamp';
368 public function getStartBody() {
369 # Do a link batch query
370 if ( $this->getNumRows() > 0 ) {
372 foreach ( $this->mResult as $row ) {
373 $lb->add( $row->log_namespace, $row->log_title );
374 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
375 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
376 $formatter = LogFormatter::newFromRow( $row );
377 foreach ( $formatter->getPreloadTitles() as $title ) {
378 $lb->addObj( $title );
382 $this->mResult->seek( 0 );
388 public function formatRow( $row ) {
389 return $this->mLogEventsList->logLine( $row );
392 public function getType() {
397 * Guaranteed to either return a valid title string or a Zero-Length String
401 public function getPerformer() {
402 return $this->performer;
408 public function getPage() {
412 public function getPattern() {
413 return $this->pattern;
416 public function getYear() {
420 public function getMonth() {
421 return $this->mMonth;
424 public function getTagFilter() {
425 return $this->mTagFilter;
428 public function getAction() {
429 return $this->action;
432 public function doQuery() {
433 // Workaround MySQL optimizer bug
434 $this->mDb->setBigSelects();
436 $this->mDb->setBigSelects( 'default' );