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 LogEventsList $list
38 * @param string $types or Array: log types to show
39 * @param string $performer the user who made the log entries
40 * @param string|Title $title the page title the log entries are for
41 * @param string $pattern do a prefix search rather than an exact title match
42 * @param array $conds extra conditions for the query
43 * @param int $year The year to start from
44 * @param int $month The month to start from
45 * @param string $tagFilter 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 );
91 * Set the log reader to return only entries of the given type.
92 * Type restrictions enforced here
94 * @param string $types or array: Log types ('upload', 'delete', etc);
95 * empty string means no restriction
97 private function limitType( $types ) {
98 global $wgLogRestrictions;
100 $user = $this->getUser();
101 // If $types is not an array, make it an array
102 $types = ( $types === '' ) ?
array() : (array)$types;
103 // Don't even show header for private logs; don't recognize it...
104 $needReindex = false;
105 foreach ( $types as $type ) {
106 if ( isset( $wgLogRestrictions[$type] )
107 && !$user->isAllowed( $wgLogRestrictions[$type] )
110 $types = array_diff( $types, array( $type ) );
113 if ( $needReindex ) {
114 // Lots of this code makes assumptions that
115 // the first entry in the array is $types[0].
116 $types = array_values( $types );
118 $this->types
= $types;
119 // Don't show private logs to unprivileged users.
120 // Also, only show them upon specific request to avoid suprises.
121 $audience = $types ?
'user' : 'public';
122 $hideLogs = LogEventsList
::getExcludeClause( $this->mDb
, $audience, $user );
123 if ( $hideLogs !== false ) {
124 $this->mConds
[] = $hideLogs;
126 if ( count( $types ) ) {
127 $this->mConds
['log_type'] = $types;
128 // Set typeCGI; used in url param for paging
129 if ( count( $types ) == 1 ) {
130 $this->typeCGI
= $types[0];
136 * Set the log reader to return only entries by the given user.
138 * @param string $name (In)valid user name
141 private function limitPerformer( $name ) {
145 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
146 if ( is_null( $usertitle ) ) {
149 /* Fetch userid at first, if known, provides awesome query plan afterwards */
150 $userid = User
::idFromName( $name );
152 /* It should be nicer to abort query at all,
153 but for now it won't pass anywhere behind the optimizer */
154 $this->mConds
[] = "NULL";
156 $this->mConds
['log_user'] = $userid;
157 // Paranoia: avoid brute force searches (bug 17342)
158 $user = $this->getUser();
159 if ( !$user->isAllowed( 'deletedhistory' ) ) {
160 $this->mConds
[] = $this->mDb
->bitAnd( 'log_deleted', LogPage
::DELETED_USER
) . ' = 0';
161 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
162 $this->mConds
[] = $this->mDb
->bitAnd( 'log_deleted', LogPage
::SUPPRESSED_USER
) .
163 ' != ' . LogPage
::SUPPRESSED_USER
;
165 $this->performer
= $usertitle->getText();
170 * Set the log reader to return only entries affecting the given page.
171 * (For the block and rights logs, this is a user page.)
173 * @param string $page or Title object: Title name
174 * @param $pattern String
177 private function limitTitle( $page, $pattern ) {
180 if ( $page instanceof Title
) {
183 $title = Title
::newFromText( $page );
184 if ( strlen( $page ) == 0 ||
!$title instanceof Title
) {
189 $this->title
= $title->getPrefixedText();
190 $ns = $title->getNamespace();
193 # Using the (log_namespace, log_title, log_timestamp) index with a
194 # range scan (LIKE) on the first two parts, instead of simple equality,
195 # makes it unusable for sorting. Sorted retrieval using another index
196 # would be possible, but then we might have to scan arbitrarily many
197 # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
200 # This is not a problem with simple title matches, because then we can
201 # use the page_time index. That should have no more than a few hundred
202 # log entries for even the busiest pages, so it can be safely scanned
203 # in full to satisfy an impossible condition on user or similar.
204 if ( $pattern && !$wgMiserMode ) {
205 $this->mConds
['log_namespace'] = $ns;
206 $this->mConds
[] = 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() );
207 $this->pattern
= $pattern;
209 $this->mConds
['log_namespace'] = $ns;
210 $this->mConds
['log_title'] = $title->getDBkey();
212 // Paranoia: avoid brute force searches (bug 17342)
213 $user = $this->getUser();
214 if ( !$user->isAllowed( 'deletedhistory' ) ) {
215 $this->mConds
[] = $db->bitAnd( 'log_deleted', LogPage
::DELETED_ACTION
) . ' = 0';
216 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
217 $this->mConds
[] = $db->bitAnd( 'log_deleted', LogPage
::SUPPRESSED_ACTION
) .
218 ' != ' . LogPage
::SUPPRESSED_ACTION
;
223 * Constructs the most part of the query. Extra conditions are sprinkled in
224 * all over this class.
227 public function getQueryInfo() {
228 $basic = DatabaseLogEntry
::getSelectQueryData();
230 $tables = $basic['tables'];
231 $fields = $basic['fields'];
232 $conds = $basic['conds'];
233 $options = $basic['options'];
234 $joins = $basic['join_conds'];
237 # Add log_search table if there are conditions on it.
238 # This filters the results to only include log rows that have
239 # log_search records with the specified ls_field and ls_value values.
240 if ( array_key_exists( 'ls_field', $this->mConds
) ) {
241 $tables[] = 'log_search';
242 $index['log_search'] = 'ls_field_val';
243 $index['logging'] = 'PRIMARY';
244 if ( !$this->hasEqualsClause( 'ls_field' )
245 ||
!$this->hasEqualsClause( 'ls_value' ) )
247 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
248 # to match a specific (ls_field,ls_value) tuple, then there will be
249 # no duplicate log rows. Otherwise, we need to remove the duplicates.
250 $options[] = 'DISTINCT';
252 # Avoid usage of the wrong index by limiting
253 # the choices of available indexes. This mainly
254 # avoids site-breaking filesorts.
255 } elseif ( $this->title ||
$this->pattern ||
$this->performer
) {
256 $index['logging'] = array( 'page_time', 'user_time' );
257 if ( count( $this->types
) == 1 ) {
258 $index['logging'][] = 'log_user_type_time';
260 } elseif ( count( $this->types
) == 1 ) {
261 $index['logging'] = 'type_time';
263 $index['logging'] = 'times';
265 $options['USE INDEX'] = $index;
266 # Don't show duplicate rows when using log_search
267 $joins['log_search'] = array( 'INNER JOIN', 'ls_log_id=log_id' );
272 'conds' => array_merge( $conds, $this->mConds
),
273 'options' => $options,
274 'join_conds' => $joins,
276 # Add ChangeTags filter query
277 ChangeTags
::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
278 $info['join_conds'], $info['options'], $this->mTagFilter
);
283 * Checks if $this->mConds has $field matched to a *single* value
287 protected function hasEqualsClause( $field ) {
289 array_key_exists( $field, $this->mConds
) &&
290 ( !is_array( $this->mConds
[$field] ) ||
count( $this->mConds
[$field] ) == 1 )
294 function getIndexField() {
295 return 'log_timestamp';
298 public function getStartBody() {
299 wfProfileIn( __METHOD__
);
300 # Do a link batch query
301 if ( $this->getNumRows() > 0 ) {
303 foreach ( $this->mResult
as $row ) {
304 $lb->add( $row->log_namespace
, $row->log_title
);
305 $lb->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
306 $lb->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
307 $formatter = LogFormatter
::newFromRow( $row );
308 foreach ( $formatter->getPreloadTitles() as $title ) {
309 $lb->addObj( $title );
313 $this->mResult
->seek( 0 );
315 wfProfileOut( __METHOD__
);
319 public function formatRow( $row ) {
320 return $this->mLogEventsList
->logLine( $row );
323 public function getType() {
330 public function getPerformer() {
331 return $this->performer
;
337 public function getPage() {
341 public function getPattern() {
342 return $this->pattern
;
345 public function getYear() {
349 public function getMonth() {
350 return $this->mMonth
;
353 public function getTagFilter() {
354 return $this->mTagFilter
;
357 public function doQuery() {
358 // Workaround MySQL optimizer bug
359 $this->mDb
->setBigSelects();
361 $this->mDb
->setBigSelects( 'default' );