3 * Contain classes to list log entries
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>, 2008 Aaron Schulz
6 * http://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 private $types = array(), $performer = '', $title = '', $pattern = '';
31 private $typeCGI = '';
32 public $mLogEventsList;
37 * @param $list LogEventsList
38 * @param $types String or Array: log types to show
39 * @param $performer String: the user who made the log entries
40 * @param $title String|Title: the page title the log entries are for
41 * @param $pattern String: do a prefix search rather than an exact title match
42 * @param $conds Array: extra conditions for the query
43 * @param $year Integer: the year to start from
44 * @param $month Integer: the month to start from
45 * @param $tagFilter String: tag
47 public function __construct( $list, $types = array(), $performer = '', $title = '', $pattern = '',
48 $conds = array(), $year = false, $month = false, $tagFilter = '' ) {
49 parent
::__construct( $list->getContext() );
50 $this->mConds
= $conds;
52 $this->mLogEventsList
= $list;
54 $this->limitType( $types ); // also excludes hidden types
55 $this->limitPerformer( $performer );
56 $this->limitTitle( $title, $pattern );
57 $this->getDateCond( $year, $month );
58 $this->mTagFilter
= $tagFilter;
61 public function getDefaultQuery() {
62 $query = parent
::getDefaultQuery();
63 $query['type'] = $this->typeCGI
; // arrays won't work here
64 $query['user'] = $this->performer
;
65 $query['month'] = $this->mMonth
;
66 $query['year'] = $this->mYear
;
70 // Call ONLY after calling $this->limitType() already!
71 public function getFilterParams() {
72 global $wgFilterLogTypes;
74 if( count($this->types
) ) {
77 foreach( $wgFilterLogTypes as $type => $default ) {
78 // Avoid silly filtering
79 if( $type !== 'patrol' ||
$this->getUser()->useNPPatrol() ) {
80 $hide = $this->getRequest()->getInt( "hide_{$type}_log", $default );
81 $filters[$type] = $hide;
83 $this->mConds
[] = 'log_type != ' . $this->mDb
->addQuotes( $type );
90 * Set the log reader to return only entries of the given type.
91 * Type restrictions enforced here
93 * @param $types String or array: Log types ('upload', 'delete', etc);
94 * empty string means no restriction
96 private function limitType( $types ) {
97 global $wgLogRestrictions;
98 // If $types is not an array, make it an array
99 $types = ($types === '') ?
array() : (array)$types;
100 // Don't even show header for private logs; don't recognize it...
101 $needReindex = false;
102 foreach ( $types as $type ) {
103 if( isset( $wgLogRestrictions[$type] )
104 && !$this->getUser()->isAllowed($wgLogRestrictions[$type])
107 $types = array_diff( $types, array( $type ) );
110 if ( $needReindex ) {
111 // Lots of this code makes assumptions that
112 // the first entry in the array is $types[0].
113 $types = array_values( $types );
115 $this->types
= $types;
116 // Don't show private logs to unprivileged users.
117 // Also, only show them upon specific request to avoid suprises.
118 $audience = $types ?
'user' : 'public';
119 $hideLogs = LogEventsList
::getExcludeClause( $this->mDb
, $audience );
120 if( $hideLogs !== false ) {
121 $this->mConds
[] = $hideLogs;
123 if( count($types) ) {
124 $this->mConds
['log_type'] = $types;
125 // Set typeCGI; used in url param for paging
126 if( count($types) == 1 ) $this->typeCGI
= $types[0];
131 * Set the log reader to return only entries by the given user.
133 * @param $name String: (In)valid user name
136 private function limitPerformer( $name ) {
140 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
141 if( is_null($usertitle) ) {
144 /* Fetch userid at first, if known, provides awesome query plan afterwards */
145 $userid = User
::idFromName( $name );
147 /* It should be nicer to abort query at all,
148 but for now it won't pass anywhere behind the optimizer */
149 $this->mConds
[] = "NULL";
151 $this->mConds
['log_user'] = $userid;
152 // Paranoia: avoid brute force searches (bug 17342)
153 $user = $this->getUser();
154 if( !$user->isAllowed( 'deletedhistory' ) ) {
155 $this->mConds
[] = $this->mDb
->bitAnd('log_deleted', LogPage
::DELETED_USER
) . ' = 0';
156 } elseif( !$user->isAllowed( 'suppressrevision' ) ) {
157 $this->mConds
[] = $this->mDb
->bitAnd('log_deleted', LogPage
::SUPPRESSED_USER
) .
158 ' != ' . LogPage
::SUPPRESSED_USER
;
160 $this->performer
= $usertitle->getText();
165 * Set the log reader to return only entries affecting the given page.
166 * (For the block and rights logs, this is a user page.)
168 * @param $page String or Title object: Title name
169 * @param $pattern String
172 private function limitTitle( $page, $pattern ) {
175 if ( $page instanceof Title
) {
178 $title = Title
::newFromText( $page );
179 if( strlen( $page ) == 0 ||
!$title instanceof Title
) {
184 $this->title
= $title->getPrefixedText();
185 $ns = $title->getNamespace();
188 # Using the (log_namespace, log_title, log_timestamp) index with a
189 # range scan (LIKE) on the first two parts, instead of simple equality,
190 # makes it unusable for sorting. Sorted retrieval using another index
191 # would be possible, but then we might have to scan arbitrarily many
192 # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
195 # This is not a problem with simple title matches, because then we can
196 # use the page_time index. That should have no more than a few hundred
197 # log entries for even the busiest pages, so it can be safely scanned
198 # in full to satisfy an impossible condition on user or similar.
199 if( $pattern && !$wgMiserMode ) {
200 $this->mConds
['log_namespace'] = $ns;
201 $this->mConds
[] = 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() );
202 $this->pattern
= $pattern;
204 $this->mConds
['log_namespace'] = $ns;
205 $this->mConds
['log_title'] = $title->getDBkey();
207 // Paranoia: avoid brute force searches (bug 17342)
208 $user = $this->getUser();
209 if( !$user->isAllowed( 'deletedhistory' ) ) {
210 $this->mConds
[] = $db->bitAnd('log_deleted', LogPage
::DELETED_ACTION
) . ' = 0';
211 } elseif( !$user->isAllowed( 'suppressrevision' ) ) {
212 $this->mConds
[] = $db->bitAnd('log_deleted', LogPage
::SUPPRESSED_ACTION
) .
213 ' != ' . LogPage
::SUPPRESSED_ACTION
;
218 * Constructs the most part of the query. Extra conditions are sprinkled in
219 * all over this class.
222 public function getQueryInfo() {
223 $basic = DatabaseLogEntry
::getSelectQueryData();
225 $tables = $basic['tables'];
226 $fields = $basic['fields'];
227 $conds = $basic['conds'];
228 $options = $basic['options'];
229 $joins = $basic['join_conds'];
232 # Add log_search table if there are conditions on it.
233 # This filters the results to only include log rows that have
234 # log_search records with the specified ls_field and ls_value values.
235 if( array_key_exists( 'ls_field', $this->mConds
) ) {
236 $tables[] = 'log_search';
237 $index['log_search'] = 'ls_field_val';
238 $index['logging'] = 'PRIMARY';
239 if ( !$this->hasEqualsClause( 'ls_field' )
240 ||
!$this->hasEqualsClause( 'ls_value' ) )
242 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
243 # to match a specific (ls_field,ls_value) tuple, then there will be
244 # no duplicate log rows. Otherwise, we need to remove the duplicates.
245 $options[] = 'DISTINCT';
247 # Avoid usage of the wrong index by limiting
248 # the choices of available indexes. This mainly
249 # avoids site-breaking filesorts.
250 } elseif( $this->title ||
$this->pattern ||
$this->performer
) {
251 $index['logging'] = array( 'page_time', 'user_time' );
252 if( count($this->types
) == 1 ) {
253 $index['logging'][] = 'log_user_type_time';
255 } elseif( count($this->types
) == 1 ) {
256 $index['logging'] = 'type_time';
258 $index['logging'] = 'times';
260 $options['USE INDEX'] = $index;
261 # Don't show duplicate rows when using log_search
262 $joins['log_search'] = array( 'INNER JOIN', 'ls_log_id=log_id' );
267 'conds' => array_merge( $conds, $this->mConds
),
268 'options' => $options,
269 'join_conds' => $joins,
271 # Add ChangeTags filter query
272 ChangeTags
::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
273 $info['join_conds'], $info['options'], $this->mTagFilter
);
278 * Checks if $this->mConds has $field matched to a *single* value
282 protected function hasEqualsClause( $field ) {
284 array_key_exists( $field, $this->mConds
) &&
285 ( !is_array( $this->mConds
[$field] ) ||
count( $this->mConds
[$field] ) == 1 )
289 function getIndexField() {
290 return 'log_timestamp';
293 public function getStartBody() {
294 wfProfileIn( __METHOD__
);
295 # Do a link batch query
296 if( $this->getNumRows() > 0 ) {
298 foreach ( $this->mResult
as $row ) {
299 $lb->add( $row->log_namespace
, $row->log_title
);
300 $lb->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
301 $lb->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
302 $formatter = LogFormatter
::newFromRow( $row );
303 foreach ( $formatter->getPreloadTitles() as $title ) {
304 $lb->addObj( $title );
308 $this->mResult
->seek( 0 );
310 wfProfileOut( __METHOD__
);
314 public function formatRow( $row ) {
315 return $this->mLogEventsList
->logLine( $row );
318 public function getType() {
325 public function getPerformer() {
326 return $this->performer
;
332 public function getPage() {
336 public function getPattern() {
337 return $this->pattern
;
340 public function getYear() {
344 public function getMonth() {
345 return $this->mMonth
;
348 public function getTagFilter() {
349 return $this->mTagFilter
;
352 public function doQuery() {
353 // Workaround MySQL optimizer bug
354 $this->mDb
->setBigSelects();
356 $this->mDb
->setBigSelects( 'default' );