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
135 private function limitPerformer( $name ) {
139 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
140 if( is_null($usertitle) ) {
143 /* Fetch userid at first, if known, provides awesome query plan afterwards */
144 $userid = User
::idFromName( $name );
146 /* It should be nicer to abort query at all,
147 but for now it won't pass anywhere behind the optimizer */
148 $this->mConds
[] = "NULL";
150 $this->mConds
['log_user'] = $userid;
151 // Paranoia: avoid brute force searches (bug 17342)
152 $user = $this->getUser();
153 if( !$user->isAllowed( 'deletedhistory' ) ) {
154 $this->mConds
[] = $this->mDb
->bitAnd('log_deleted', LogPage
::DELETED_USER
) . ' = 0';
155 } elseif( !$user->isAllowed( 'suppressrevision' ) ) {
156 $this->mConds
[] = $this->mDb
->bitAnd('log_deleted', LogPage
::SUPPRESSED_USER
) .
157 ' != ' . LogPage
::SUPPRESSED_USER
;
159 $this->performer
= $usertitle->getText();
164 * Set the log reader to return only entries affecting the given page.
165 * (For the block and rights logs, this is a user page.)
167 * @param $page String or Title object: Title name
168 * @param $pattern String
170 private function limitTitle( $page, $pattern ) {
173 if ( $page instanceof Title
) {
176 $title = Title
::newFromText( $page );
177 if( strlen( $page ) == 0 ||
!$title instanceof Title
) {
182 $this->title
= $title->getPrefixedText();
183 $ns = $title->getNamespace();
186 # Using the (log_namespace, log_title, log_timestamp) index with a
187 # range scan (LIKE) on the first two parts, instead of simple equality,
188 # makes it unusable for sorting. Sorted retrieval using another index
189 # would be possible, but then we might have to scan arbitrarily many
190 # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
193 # This is not a problem with simple title matches, because then we can
194 # use the page_time index. That should have no more than a few hundred
195 # log entries for even the busiest pages, so it can be safely scanned
196 # in full to satisfy an impossible condition on user or similar.
197 if( $pattern && !$wgMiserMode ) {
198 $this->mConds
['log_namespace'] = $ns;
199 $this->mConds
[] = 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() );
200 $this->pattern
= $pattern;
202 $this->mConds
['log_namespace'] = $ns;
203 $this->mConds
['log_title'] = $title->getDBkey();
205 // Paranoia: avoid brute force searches (bug 17342)
206 $user = $this->getUser();
207 if( !$user->isAllowed( 'deletedhistory' ) ) {
208 $this->mConds
[] = $db->bitAnd('log_deleted', LogPage
::DELETED_ACTION
) . ' = 0';
209 } elseif( !$user->isAllowed( 'suppressrevision' ) ) {
210 $this->mConds
[] = $db->bitAnd('log_deleted', LogPage
::SUPPRESSED_ACTION
) .
211 ' != ' . LogPage
::SUPPRESSED_ACTION
;
216 * Constructs the most part of the query. Extra conditions are sprinkled in
217 * all over this class.
220 public function getQueryInfo() {
221 $basic = DatabaseLogEntry
::getSelectQueryData();
223 $tables = $basic['tables'];
224 $fields = $basic['fields'];
225 $conds = $basic['conds'];
226 $options = $basic['options'];
227 $joins = $basic['join_conds'];
230 # Add log_search table if there are conditions on it.
231 # This filters the results to only include log rows that have
232 # log_search records with the specified ls_field and ls_value values.
233 if( array_key_exists( 'ls_field', $this->mConds
) ) {
234 $tables[] = 'log_search';
235 $index['log_search'] = 'ls_field_val';
236 $index['logging'] = 'PRIMARY';
237 if ( !$this->hasEqualsClause( 'ls_field' )
238 ||
!$this->hasEqualsClause( 'ls_value' ) )
240 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
241 # to match a specific (ls_field,ls_value) tuple, then there will be
242 # no duplicate log rows. Otherwise, we need to remove the duplicates.
243 $options[] = 'DISTINCT';
245 # Avoid usage of the wrong index by limiting
246 # the choices of available indexes. This mainly
247 # avoids site-breaking filesorts.
248 } elseif( $this->title ||
$this->pattern ||
$this->performer
) {
249 $index['logging'] = array( 'page_time', 'user_time' );
250 if( count($this->types
) == 1 ) {
251 $index['logging'][] = 'log_user_type_time';
253 } elseif( count($this->types
) == 1 ) {
254 $index['logging'] = 'type_time';
256 $index['logging'] = 'times';
258 $options['USE INDEX'] = $index;
259 # Don't show duplicate rows when using log_search
260 $joins['log_search'] = array( 'INNER JOIN', 'ls_log_id=log_id' );
265 'conds' => array_merge( $conds, $this->mConds
),
266 'options' => $options,
267 'join_conds' => $joins,
269 # Add ChangeTags filter query
270 ChangeTags
::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
271 $info['join_conds'], $info['options'], $this->mTagFilter
);
276 * Checks if $this->mConds has $field matched to a *single* value
280 protected function hasEqualsClause( $field ) {
282 array_key_exists( $field, $this->mConds
) &&
283 ( !is_array( $this->mConds
[$field] ) ||
count( $this->mConds
[$field] ) == 1 )
287 function getIndexField() {
288 return 'log_timestamp';
291 public function getStartBody() {
292 wfProfileIn( __METHOD__
);
293 # Do a link batch query
294 if( $this->getNumRows() > 0 ) {
296 foreach ( $this->mResult
as $row ) {
297 $lb->add( $row->log_namespace
, $row->log_title
);
298 $lb->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
299 $lb->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
302 $this->mResult
->seek( 0 );
304 wfProfileOut( __METHOD__
);
308 public function formatRow( $row ) {
309 return $this->mLogEventsList
->logLine( $row );
312 public function getType() {
319 public function getPerformer() {
320 return $this->performer
;
326 public function getPage() {
330 public function getPattern() {
331 return $this->pattern
;
334 public function getYear() {
338 public function getMonth() {
339 return $this->mMonth
;
342 public function getTagFilter() {
343 return $this->mTagFilter
;
346 public function doQuery() {
347 // Workaround MySQL optimizer bug
348 $this->mDb
->setBigSelects();
350 $this->mDb
->setBigSelects( 'default' );