Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / logging / LogPager.php
blobbf489ab9aa1f95c2281173d73d734f71bd64cab7
1 <?php
2 /**
3 * Contain classes to list log entries
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>, 2008 Aaron Schulz
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
23 * @file
26 /**
27 * @ingroup Pager
29 class LogPager extends ReverseChronologicalPager {
30 /** @var array Log types */
31 private $types = array();
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 */
37 private $title = '';
39 /** @var string */
40 private $pattern = '';
42 /** @var string */
43 private $typeCGI = '';
45 /** @var LogEventsList */
46 public $mLogEventsList;
48 /**
49 * Constructor
51 * @param LogEventsList $list
52 * @param string|array $types Log types to show
53 * @param string $performer The user who made the log entries
54 * @param string|Title $title The page title the log entries are for
55 * @param string $pattern Do a prefix search rather than an exact title match
56 * @param array $conds Extra conditions for the query
57 * @param int|bool $year The year to start from. Default: false
58 * @param int|bool $month The month to start from. Default: false
59 * @param string $tagFilter Tag
61 public function __construct( $list, $types = array(), $performer = '', $title = '', $pattern = '',
62 $conds = array(), $year = false, $month = false, $tagFilter = '' ) {
63 parent::__construct( $list->getContext() );
64 $this->mConds = $conds;
66 $this->mLogEventsList = $list;
68 $this->limitType( $types ); // also excludes hidden types
69 $this->limitPerformer( $performer );
70 $this->limitTitle( $title, $pattern );
71 $this->getDateCond( $year, $month );
72 $this->mTagFilter = $tagFilter;
74 $this->mDb = wfGetDB( DB_SLAVE, 'logpager' );
77 public function getDefaultQuery() {
78 $query = parent::getDefaultQuery();
79 $query['type'] = $this->typeCGI; // arrays won't work here
80 $query['user'] = $this->performer;
81 $query['month'] = $this->mMonth;
82 $query['year'] = $this->mYear;
84 return $query;
87 // Call ONLY after calling $this->limitType() already!
88 public function getFilterParams() {
89 global $wgFilterLogTypes;
90 $filters = array();
91 if ( count( $this->types ) ) {
92 return $filters;
94 foreach ( $wgFilterLogTypes as $type => $default ) {
95 // Avoid silly filtering
96 if ( $type !== 'patrol' || $this->getUser()->useNPPatrol() ) {
97 $hide = $this->getRequest()->getInt( "hide_{$type}_log", $default );
98 $filters[$type] = $hide;
99 if ( $hide ) {
100 $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
105 return $filters;
109 * Set the log reader to return only entries of the given type.
110 * Type restrictions enforced here
112 * @param string|array $types Log types ('upload', 'delete', etc);
113 * empty string means no restriction
115 private function limitType( $types ) {
116 global $wgLogRestrictions;
118 $user = $this->getUser();
119 // If $types is not an array, make it an array
120 $types = ( $types === '' ) ? array() : (array)$types;
121 // Don't even show header for private logs; don't recognize it...
122 $needReindex = false;
123 foreach ( $types as $type ) {
124 if ( isset( $wgLogRestrictions[$type] )
125 && !$user->isAllowed( $wgLogRestrictions[$type] )
127 $needReindex = true;
128 $types = array_diff( $types, array( $type ) );
131 if ( $needReindex ) {
132 // Lots of this code makes assumptions that
133 // the first entry in the array is $types[0].
134 $types = array_values( $types );
136 $this->types = $types;
137 // Don't show private logs to unprivileged users.
138 // Also, only show them upon specific request to avoid suprises.
139 $audience = $types ? 'user' : 'public';
140 $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience, $user );
141 if ( $hideLogs !== false ) {
142 $this->mConds[] = $hideLogs;
144 if ( count( $types ) ) {
145 $this->mConds['log_type'] = $types;
146 // Set typeCGI; used in url param for paging
147 if ( count( $types ) == 1 ) {
148 $this->typeCGI = $types[0];
154 * Set the log reader to return only entries by the given user.
156 * @param string $name (In)valid user name
157 * @return void
159 private function limitPerformer( $name ) {
160 if ( $name == '' ) {
161 return;
163 $usertitle = Title::makeTitleSafe( NS_USER, $name );
164 if ( is_null( $usertitle ) ) {
165 return;
167 /* Fetch userid at first, if known, provides awesome query plan afterwards */
168 $userid = User::idFromName( $name );
169 if ( !$userid ) {
170 $this->mConds['log_user_text'] = IP::sanitizeIP( $name );
171 } else {
172 $this->mConds['log_user'] = $userid;
174 // Paranoia: avoid brute force searches (bug 17342)
175 $user = $this->getUser();
176 if ( !$user->isAllowed( 'deletedhistory' ) ) {
177 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0';
178 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
179 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_USER ) .
180 ' != ' . LogPage::SUPPRESSED_USER;
183 $this->performer = $usertitle->getText();
187 * Set the log reader to return only entries affecting the given page.
188 * (For the block and rights logs, this is a user page.)
190 * @param string|Title $page Title name
191 * @param string $pattern
192 * @return void
194 private function limitTitle( $page, $pattern ) {
195 global $wgMiserMode;
197 if ( $page instanceof Title ) {
198 $title = $page;
199 } else {
200 $title = Title::newFromText( $page );
201 if ( strlen( $page ) == 0 || !$title instanceof Title ) {
202 return;
206 $this->title = $title->getPrefixedText();
207 $ns = $title->getNamespace();
208 $db = $this->mDb;
210 $doUserRightsLogLike = false;
211 if ( $this->types == array( 'rights' ) ) {
212 global $wgUserrightsInterwikiDelimiter;
213 $parts = explode( $wgUserrightsInterwikiDelimiter, $title->getDBKey() );
214 if ( count( $parts ) == 2 ) {
215 list( $name, $database ) = array_map( 'trim', $parts );
216 if ( strstr( $database, '*' ) ) { // Search for wildcard in database name
217 $doUserRightsLogLike = true;
222 # Using the (log_namespace, log_title, log_timestamp) index with a
223 # range scan (LIKE) on the first two parts, instead of simple equality,
224 # makes it unusable for sorting. Sorted retrieval using another index
225 # would be possible, but then we might have to scan arbitrarily many
226 # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
227 # is on.
229 # This is not a problem with simple title matches, because then we can
230 # use the page_time index. That should have no more than a few hundred
231 # log entries for even the busiest pages, so it can be safely scanned
232 # in full to satisfy an impossible condition on user or similar.
233 $this->mConds['log_namespace'] = $ns;
234 if ( $doUserRightsLogLike ) {
235 $params = array( $name . $wgUserrightsInterwikiDelimiter );
236 foreach ( explode( '*', $database ) as $databasepart ) {
237 $params[] = $databasepart;
238 $params[] = $db->anyString();
240 array_pop( $params ); // Get rid of the last % we added.
241 $this->mConds[] = 'log_title' . $db->buildLike( $params );
242 } elseif ( $pattern && !$wgMiserMode ) {
243 $this->mConds[] = 'log_title' . $db->buildLike( $title->getDBkey(), $db->anyString() );
244 $this->pattern = $pattern;
245 } else {
246 $this->mConds['log_title'] = $title->getDBkey();
248 // Paranoia: avoid brute force searches (bug 17342)
249 $user = $this->getUser();
250 if ( !$user->isAllowed( 'deletedhistory' ) ) {
251 $this->mConds[] = $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0';
252 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
253 $this->mConds[] = $db->bitAnd( 'log_deleted', LogPage::SUPPRESSED_ACTION ) .
254 ' != ' . LogPage::SUPPRESSED_ACTION;
259 * Constructs the most part of the query. Extra conditions are sprinkled in
260 * all over this class.
261 * @return array
263 public function getQueryInfo() {
264 $basic = DatabaseLogEntry::getSelectQueryData();
266 $tables = $basic['tables'];
267 $fields = $basic['fields'];
268 $conds = $basic['conds'];
269 $options = $basic['options'];
270 $joins = $basic['join_conds'];
272 $index = array();
273 # Add log_search table if there are conditions on it.
274 # This filters the results to only include log rows that have
275 # log_search records with the specified ls_field and ls_value values.
276 if ( array_key_exists( 'ls_field', $this->mConds ) ) {
277 $tables[] = 'log_search';
278 $index['log_search'] = 'ls_field_val';
279 $index['logging'] = 'PRIMARY';
280 if ( !$this->hasEqualsClause( 'ls_field' )
281 || !$this->hasEqualsClause( 'ls_value' )
283 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
284 # to match a specific (ls_field,ls_value) tuple, then there will be
285 # no duplicate log rows. Otherwise, we need to remove the duplicates.
286 $options[] = 'DISTINCT';
289 if ( count( $index ) ) {
290 $options['USE INDEX'] = $index;
292 # Don't show duplicate rows when using log_search
293 $joins['log_search'] = array( 'INNER JOIN', 'ls_log_id=log_id' );
295 $info = array(
296 'tables' => $tables,
297 'fields' => $fields,
298 'conds' => array_merge( $conds, $this->mConds ),
299 'options' => $options,
300 'join_conds' => $joins,
302 # Add ChangeTags filter query
303 ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
304 $info['join_conds'], $info['options'], $this->mTagFilter );
306 return $info;
310 * Checks if $this->mConds has $field matched to a *single* value
311 * @param string $field
312 * @return bool
314 protected function hasEqualsClause( $field ) {
315 return (
316 array_key_exists( $field, $this->mConds ) &&
317 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
321 function getIndexField() {
322 return 'log_timestamp';
325 public function getStartBody() {
326 # Do a link batch query
327 if ( $this->getNumRows() > 0 ) {
328 $lb = new LinkBatch;
329 foreach ( $this->mResult as $row ) {
330 $lb->add( $row->log_namespace, $row->log_title );
331 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
332 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
333 $formatter = LogFormatter::newFromRow( $row );
334 foreach ( $formatter->getPreloadTitles() as $title ) {
335 $lb->addObj( $title );
338 $lb->execute();
339 $this->mResult->seek( 0 );
342 return '';
345 public function formatRow( $row ) {
346 return $this->mLogEventsList->logLine( $row );
349 public function getType() {
350 return $this->types;
354 * @return string
356 public function getPerformer() {
357 return $this->performer;
361 * @return string
363 public function getPage() {
364 return $this->title;
367 public function getPattern() {
368 return $this->pattern;
371 public function getYear() {
372 return $this->mYear;
375 public function getMonth() {
376 return $this->mMonth;
379 public function getTagFilter() {
380 return $this->mTagFilter;
383 public function doQuery() {
384 // Workaround MySQL optimizer bug
385 $this->mDb->setBigSelects();
386 parent::doQuery();
387 $this->mDb->setBigSelects( 'default' );