Increase Opera minimum for Grades A and C to 15
[lhc/web/wiklou.git] / includes / logging / LogPager.php
1 <?php
2 /**
3 * Contain classes to list log entries
4 *
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 */
25
26 /**
27 * @ingroup Pager
28 */
29 class LogPager extends ReverseChronologicalPager {
30 /** @var array Log types */
31 private $types = [];
32
33 /** @var string Events limited to those by performer when set */
34 private $performer = '';
35
36 /** @var string|Title Events limited to those about Title when set */
37 private $title = '';
38
39 /** @var string */
40 private $pattern = '';
41
42 /** @var string */
43 private $typeCGI = '';
44
45 /** @var string */
46 private $action = '';
47
48 /** @var LogEventsList */
49 public $mLogEventsList;
50
51 /**
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 */
63 public function __construct( $list, $types = [], $performer = '', $title = '',
64 $pattern = '', $conds = [], $year = false, $month = false, $tagFilter = '',
65 $action = ''
66 ) {
67 parent::__construct( $list->getContext() );
68 $this->mConds = $conds;
69
70 $this->mLogEventsList = $list;
71
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;
78
79 $this->mDb = wfGetDB( DB_REPLICA, 'logpager' );
80 }
81
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;
88
89 return $query;
90 }
91
92 // Call ONLY after calling $this->limitType() already!
93 public function getFilterParams() {
94 global $wgFilterLogTypes;
95 $filters = [];
96 if ( count( $this->types ) ) {
97 return $filters;
98 }
99 foreach ( $wgFilterLogTypes as $type => $default ) {
100 $hide = $this->getRequest()->getInt( "hide_{$type}_log", $default );
101
102 $filters[$type] = $hide;
103 if ( $hide ) {
104 $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
105 }
106 }
107
108 return $filters;
109 }
110
111 /**
112 * Set the log reader to return only entries of the given type.
113 * Type restrictions enforced here
114 *
115 * @param string|array $types Log types ('upload', 'delete', etc);
116 * empty string means no restriction
117 */
118 private function limitType( $types ) {
119 global $wgLogRestrictions;
120
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] )
129 ) {
130 $needReindex = true;
131 $types = array_diff( $types, [ $type ] );
132 }
133 }
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 );
138 }
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;
146 }
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];
152 }
153 }
154 }
155
156 /**
157 * Set the log reader to return only entries by the given user.
158 *
159 * @param string $name (In)valid user name
160 * @return void
161 */
162 private function limitPerformer( $name ) {
163 if ( $name == '' ) {
164 return;
165 }
166 $usertitle = Title::makeTitleSafe( NS_USER, $name );
167 if ( is_null( $usertitle ) ) {
168 return;
169 }
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 );
175 if ( !$userid ) {
176 $this->mConds['log_user_text'] = IP::sanitizeIP( $name );
177 } else {
178 $this->mConds['log_user'] = $userid;
179 }
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;
187 }
188
189 $this->performer = $name;
190 }
191
192 /**
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.)
195 *
196 * @param string|Title $page Title name
197 * @param string $pattern
198 * @return void
199 */
200 private function limitTitle( $page, $pattern ) {
201 global $wgMiserMode, $wgUserrightsInterwikiDelimiter;
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
227 /**
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
233 * is on.
234 *
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.
239 */
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();
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 // 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;
262 }
263 }
264
265 /**
266 * Set the log_action field to a specified value (or values)
267 *
268 * @param string $action
269 */
270 private function limitAction( $action ) {
271 global $wgActionFilteredLogs;
272 // Allow to filter the log by actions
273 $type = $this->typeCGI;
274 if ( $type === '' ) {
275 // nothing to do
276 return;
277 }
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;
286 }
287 }
288 }
289
290 /**
291 * Constructs the most part of the query. Extra conditions are sprinkled in
292 * all over this class.
293 * @return array
294 */
295 public function getQueryInfo() {
296 $basic = DatabaseLogEntry::getSelectQueryData();
297
298 $tables = $basic['tables'];
299 $fields = $basic['fields'];
300 $conds = $basic['conds'];
301 $options = $basic['options'];
302 $joins = $basic['join_conds'];
303
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' )
313 ) {
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';
318 }
319 }
320 # Don't show duplicate rows when using log_search
321 $joins['log_search'] = [ 'INNER JOIN', 'ls_log_id=log_id' ];
322
323 $info = [
324 'tables' => $tables,
325 'fields' => $fields,
326 'conds' => array_merge( $conds, $this->mConds ),
327 'options' => $options,
328 'join_conds' => $joins,
329 ];
330 # Add ChangeTags filter query
331 ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
332 $info['join_conds'], $info['options'], $this->mTagFilter );
333
334 return $info;
335 }
336
337 /**
338 * Checks if $this->mConds has $field matched to a *single* value
339 * @param string $field
340 * @return bool
341 */
342 protected function hasEqualsClause( $field ) {
343 return (
344 array_key_exists( $field, $this->mConds ) &&
345 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
346 );
347 }
348
349 function getIndexField() {
350 return 'log_timestamp';
351 }
352
353 public function getStartBody() {
354 # Do a link batch query
355 if ( $this->getNumRows() > 0 ) {
356 $lb = new LinkBatch;
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 );
364 }
365 }
366 $lb->execute();
367 $this->mResult->seek( 0 );
368 }
369
370 return '';
371 }
372
373 public function formatRow( $row ) {
374 return $this->mLogEventsList->logLine( $row );
375 }
376
377 public function getType() {
378 return $this->types;
379 }
380
381 /**
382 * Guaranteed to either return a valid title string or a Zero-Length String
383 *
384 * @return string
385 */
386 public function getPerformer() {
387 return $this->performer;
388 }
389
390 /**
391 * @return string
392 */
393 public function getPage() {
394 return $this->title;
395 }
396
397 public function getPattern() {
398 return $this->pattern;
399 }
400
401 public function getYear() {
402 return $this->mYear;
403 }
404
405 public function getMonth() {
406 return $this->mMonth;
407 }
408
409 public function getTagFilter() {
410 return $this->mTagFilter;
411 }
412
413 public function getAction() {
414 return $this->action;
415 }
416
417 public function doQuery() {
418 // Workaround MySQL optimizer bug
419 $this->mDb->setBigSelects();
420 parent::doQuery();
421 $this->mDb->setBigSelects( 'default' );
422 }
423 }