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
63 public function __construct( $list, $types = [], $performer = '', $title = '',
64 $pattern = '', $conds = [], $year = false, $month = false, $tagFilter = '',
67 parent
::__construct( $list->getContext() );
68 $this->mConds
= $conds;
70 $this->mLogEventsList
= $list;
72 $this->limitType( $types ); // also excludes hidden types
73 $this->limitPerformer( $performer );
74 $this->limitTitle( $title, $pattern );
75 $this->limitAction( $action );
76 $this->getDateCond( $year, $month );
77 $this->mTagFilter
= $tagFilter;
79 $this->mDb
= wfGetDB( DB_REPLICA
, 'logpager' );
82 public function getDefaultQuery() {
83 $query = parent
::getDefaultQuery();
84 $query['type'] = $this->typeCGI
; // arrays won't work here
85 $query['user'] = $this->performer
;
86 $query['month'] = $this->mMonth
;
87 $query['year'] = $this->mYear
;
92 // Call ONLY after calling $this->limitType() already!
93 public function getFilterParams() {
94 global $wgFilterLogTypes;
96 if ( count( $this->types
) ) {
99 foreach ( $wgFilterLogTypes as $type => $default ) {
100 $hide = $this->getRequest()->getInt( "hide_{$type}_log", $default );
102 $filters[$type] = $hide;
104 $this->mConds
[] = 'log_type != ' . $this->mDb
->addQuotes( $type );
112 * Set the log reader to return only entries of the given type.
113 * Type restrictions enforced here
115 * @param string|array $types Log types ('upload', 'delete', etc);
116 * empty string means no restriction
118 private function limitType( $types ) {
119 global $wgLogRestrictions;
121 $user = $this->getUser();
122 // If $types is not an array, make it an array
123 $types = ( $types === '' ) ?
[] : (array)$types;
124 // Don't even show header for private logs; don't recognize it...
125 $needReindex = false;
126 foreach ( $types as $type ) {
127 if ( isset( $wgLogRestrictions[$type] )
128 && !$user->isAllowed( $wgLogRestrictions[$type] )
131 $types = array_diff( $types, [ $type ] );
134 if ( $needReindex ) {
135 // Lots of this code makes assumptions that
136 // the first entry in the array is $types[0].
137 $types = array_values( $types );
139 $this->types
= $types;
140 // Don't show private logs to unprivileged users.
141 // Also, only show them upon specific request to avoid suprises.
142 $audience = $types ?
'user' : 'public';
143 $hideLogs = LogEventsList
::getExcludeClause( $this->mDb
, $audience, $user );
144 if ( $hideLogs !== false ) {
145 $this->mConds
[] = $hideLogs;
147 if ( count( $types ) ) {
148 $this->mConds
['log_type'] = $types;
149 // Set typeCGI; used in url param for paging
150 if ( count( $types ) == 1 ) {
151 $this->typeCGI
= $types[0];
157 * Set the log reader to return only entries by the given user.
159 * @param string $name (In)valid user name
162 private function limitPerformer( $name ) {
166 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
167 if ( is_null( $usertitle ) ) {
170 // Normalize username first so that non-existent users used
171 // in maintenance scripts work
172 $name = $usertitle->getText();
173 /* Fetch userid at first, if known, provides awesome query plan afterwards */
174 $userid = User
::idFromName( $name );
176 $this->mConds
['log_user_text'] = IP
::sanitizeIP( $name );
178 $this->mConds
['log_user'] = $userid;
180 // Paranoia: avoid brute force searches (T19342)
181 $user = $this->getUser();
182 if ( !$user->isAllowed( 'deletedhistory' ) ) {
183 $this->mConds
[] = $this->mDb
->bitAnd( 'log_deleted', LogPage
::DELETED_USER
) . ' = 0';
184 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
185 $this->mConds
[] = $this->mDb
->bitAnd( 'log_deleted', LogPage
::SUPPRESSED_USER
) .
186 ' != ' . LogPage
::SUPPRESSED_USER
;
189 $this->performer
= $name;
193 * Set the log reader to return only entries affecting the given page.
194 * (For the block and rights logs, this is a user page.)
196 * @param string|Title $page Title name
197 * @param string $pattern
200 private function limitTitle( $page, $pattern ) {
201 global $wgMiserMode, $wgUserrightsInterwikiDelimiter;
203 if ( $page instanceof Title
) {
206 $title = Title
::newFromText( $page );
207 if ( strlen( $page ) == 0 ||
!$title instanceof Title
) {
212 $this->title
= $title->getPrefixedText();
213 $ns = $title->getNamespace();
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;
228 * Using the (log_namespace, log_title, log_timestamp) index with a
229 * range scan (LIKE) on the first two parts, instead of simple equality,
230 * makes it unusable for sorting. Sorted retrieval using another index
231 * would be possible, but then we might have to scan arbitrarily many
232 * nodes of that index. Therefore, we need to avoid this if $wgMiserMode
235 * This is not a problem with simple title matches, because then we can
236 * use the page_time index. That should have no more than a few hundred
237 * log entries for even the busiest pages, so it can be safely scanned
238 * in full to satisfy an impossible condition on user or similar.
240 $this->mConds
['log_namespace'] = $ns;
241 if ( $doUserRightsLogLike ) {
242 $params = [ $name . $wgUserrightsInterwikiDelimiter ];
243 foreach ( explode( '*', $database ) as $databasepart ) {
244 $params[] = $databasepart;
245 $params[] = $db->anyString();
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;
253 $this->mConds
['log_title'] = $title->getDBkey();
255 // Paranoia: avoid brute force searches (T19342)
256 $user = $this->getUser();
257 if ( !$user->isAllowed( 'deletedhistory' ) ) {
258 $this->mConds
[] = $db->bitAnd( 'log_deleted', LogPage
::DELETED_ACTION
) . ' = 0';
259 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
260 $this->mConds
[] = $db->bitAnd( 'log_deleted', LogPage
::SUPPRESSED_ACTION
) .
261 ' != ' . LogPage
::SUPPRESSED_ACTION
;
266 * Set the log_action field to a specified value (or values)
268 * @param string $action
270 private function limitAction( $action ) {
271 global $wgActionFilteredLogs;
272 // Allow to filter the log by actions
273 $type = $this->typeCGI
;
274 if ( $type === '' ) {
278 $actions = $wgActionFilteredLogs;
279 if ( isset( $actions[$type] ) ) {
280 // log type can be filtered by actions
281 $this->mLogEventsList
->setAllowedActions( array_keys( $actions[$type] ) );
282 if ( $action !== '' && isset( $actions[$type][$action] ) ) {
283 // add condition to query
284 $this->mConds
['log_action'] = $actions[$type][$action];
285 $this->action
= $action;
291 * Constructs the most part of the query. Extra conditions are sprinkled in
292 * all over this class.
295 public function getQueryInfo() {
296 $basic = DatabaseLogEntry
::getSelectQueryData();
298 $tables = $basic['tables'];
299 $fields = $basic['fields'];
300 $conds = $basic['conds'];
301 $options = $basic['options'];
302 $joins = $basic['join_conds'];
304 # Add log_search table if there are conditions on it.
305 # This filters the results to only include log rows that have
306 # log_search records with the specified ls_field and ls_value values.
307 if ( array_key_exists( 'ls_field', $this->mConds
) ) {
308 $tables[] = 'log_search';
309 $options['IGNORE INDEX'] = [ 'log_search' => 'ls_log_id' ];
310 $options['USE INDEX'] = [ 'logging' => 'PRIMARY' ];
311 if ( !$this->hasEqualsClause( 'ls_field' )
312 ||
!$this->hasEqualsClause( 'ls_value' )
314 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
315 # to match a specific (ls_field,ls_value) tuple, then there will be
316 # no duplicate log rows. Otherwise, we need to remove the duplicates.
317 $options[] = 'DISTINCT';
320 # Don't show duplicate rows when using log_search
321 $joins['log_search'] = [ 'INNER JOIN', 'ls_log_id=log_id' ];
326 'conds' => array_merge( $conds, $this->mConds
),
327 'options' => $options,
328 'join_conds' => $joins,
330 # Add ChangeTags filter query
331 ChangeTags
::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
332 $info['join_conds'], $info['options'], $this->mTagFilter
);
338 * Checks if $this->mConds has $field matched to a *single* value
339 * @param string $field
342 protected function hasEqualsClause( $field ) {
344 array_key_exists( $field, $this->mConds
) &&
345 ( !is_array( $this->mConds
[$field] ) ||
count( $this->mConds
[$field] ) == 1 )
349 function getIndexField() {
350 return 'log_timestamp';
353 public function getStartBody() {
354 # Do a link batch query
355 if ( $this->getNumRows() > 0 ) {
357 foreach ( $this->mResult
as $row ) {
358 $lb->add( $row->log_namespace
, $row->log_title
);
359 $lb->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
360 $lb->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
361 $formatter = LogFormatter
::newFromRow( $row );
362 foreach ( $formatter->getPreloadTitles() as $title ) {
363 $lb->addObj( $title );
367 $this->mResult
->seek( 0 );
373 public function formatRow( $row ) {
374 return $this->mLogEventsList
->logLine( $row );
377 public function getType() {
382 * Guaranteed to either return a valid title string or a Zero-Length String
386 public function getPerformer() {
387 return $this->performer
;
393 public function getPage() {
397 public function getPattern() {
398 return $this->pattern
;
401 public function getYear() {
405 public function getMonth() {
406 return $this->mMonth
;
409 public function getTagFilter() {
410 return $this->mTagFilter
;
413 public function getAction() {
414 return $this->action
;
417 public function doQuery() {
418 // Workaround MySQL optimizer bug
419 $this->mDb
->setBigSelects();
421 $this->mDb
->setBigSelects( 'default' );