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 /** @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 */
40 private $pattern = '';
43 private $typeCGI = '';
45 /** @var LogEventsList */
46 public $mLogEventsList;
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;
75 public function getDefaultQuery() {
76 $query = parent
::getDefaultQuery();
77 $query['type'] = $this->typeCGI
; // arrays won't work here
78 $query['user'] = $this->performer
;
79 $query['month'] = $this->mMonth
;
80 $query['year'] = $this->mYear
;
85 // Call ONLY after calling $this->limitType() already!
86 public function getFilterParams() {
87 global $wgFilterLogTypes;
89 if ( count( $this->types
) ) {
92 foreach ( $wgFilterLogTypes as $type => $default ) {
93 // Avoid silly filtering
94 if ( $type !== 'patrol' ||
$this->getUser()->useNPPatrol() ) {
95 $hide = $this->getRequest()->getInt( "hide_{$type}_log", $default );
96 $filters[$type] = $hide;
98 $this->mConds
[] = 'log_type != ' . $this->mDb
->addQuotes( $type );
107 * Set the log reader to return only entries of the given type.
108 * Type restrictions enforced here
110 * @param string|array $types Log types ('upload', 'delete', etc);
111 * empty string means no restriction
113 private function limitType( $types ) {
114 global $wgLogRestrictions;
116 $user = $this->getUser();
117 // If $types is not an array, make it an array
118 $types = ( $types === '' ) ?
array() : (array)$types;
119 // Don't even show header for private logs; don't recognize it...
120 $needReindex = false;
121 foreach ( $types as $type ) {
122 if ( isset( $wgLogRestrictions[$type] )
123 && !$user->isAllowed( $wgLogRestrictions[$type] )
126 $types = array_diff( $types, array( $type ) );
129 if ( $needReindex ) {
130 // Lots of this code makes assumptions that
131 // the first entry in the array is $types[0].
132 $types = array_values( $types );
134 $this->types
= $types;
135 // Don't show private logs to unprivileged users.
136 // Also, only show them upon specific request to avoid suprises.
137 $audience = $types ?
'user' : 'public';
138 $hideLogs = LogEventsList
::getExcludeClause( $this->mDb
, $audience, $user );
139 if ( $hideLogs !== false ) {
140 $this->mConds
[] = $hideLogs;
142 if ( count( $types ) ) {
143 $this->mConds
['log_type'] = $types;
144 // Set typeCGI; used in url param for paging
145 if ( count( $types ) == 1 ) {
146 $this->typeCGI
= $types[0];
152 * Set the log reader to return only entries by the given user.
154 * @param string $name (In)valid user name
157 private function limitPerformer( $name ) {
161 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
162 if ( is_null( $usertitle ) ) {
165 /* Fetch userid at first, if known, provides awesome query plan afterwards */
166 $userid = User
::idFromName( $name );
168 /* It should be nicer to abort query at all,
169 but for now it won't pass anywhere behind the optimizer */
170 $this->mConds
[] = "NULL";
172 $this->mConds
['log_user'] = $userid;
173 // Paranoia: avoid brute force searches (bug 17342)
174 $user = $this->getUser();
175 if ( !$user->isAllowed( 'deletedhistory' ) ) {
176 $this->mConds
[] = $this->mDb
->bitAnd( 'log_deleted', LogPage
::DELETED_USER
) . ' = 0';
177 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
178 $this->mConds
[] = $this->mDb
->bitAnd( 'log_deleted', LogPage
::SUPPRESSED_USER
) .
179 ' != ' . LogPage
::SUPPRESSED_USER
;
181 $this->performer
= $usertitle->getText();
186 * Set the log reader to return only entries affecting the given page.
187 * (For the block and rights logs, this is a user page.)
189 * @param string|Title $page Title name
190 * @param string $pattern
193 private function limitTitle( $page, $pattern ) {
196 if ( $page instanceof Title
) {
199 $title = Title
::newFromText( $page );
200 if ( strlen( $page ) == 0 ||
!$title instanceof Title
) {
205 $this->title
= $title->getPrefixedText();
206 $ns = $title->getNamespace();
209 # Using the (log_namespace, log_title, log_timestamp) index with a
210 # range scan (LIKE) on the first two parts, instead of simple equality,
211 # makes it unusable for sorting. Sorted retrieval using another index
212 # would be possible, but then we might have to scan arbitrarily many
213 # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
216 # This is not a problem with simple title matches, because then we can
217 # use the page_time index. That should have no more than a few hundred
218 # log entries for even the busiest pages, so it can be safely scanned
219 # in full to satisfy an impossible condition on user or similar.
220 if ( $pattern && !$wgMiserMode ) {
221 $this->mConds
['log_namespace'] = $ns;
222 $this->mConds
[] = 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() );
223 $this->pattern
= $pattern;
225 $this->mConds
['log_namespace'] = $ns;
226 $this->mConds
['log_title'] = $title->getDBkey();
228 // Paranoia: avoid brute force searches (bug 17342)
229 $user = $this->getUser();
230 if ( !$user->isAllowed( 'deletedhistory' ) ) {
231 $this->mConds
[] = $db->bitAnd( 'log_deleted', LogPage
::DELETED_ACTION
) . ' = 0';
232 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
233 $this->mConds
[] = $db->bitAnd( 'log_deleted', LogPage
::SUPPRESSED_ACTION
) .
234 ' != ' . LogPage
::SUPPRESSED_ACTION
;
239 * Constructs the most part of the query. Extra conditions are sprinkled in
240 * all over this class.
243 public function getQueryInfo() {
244 $basic = DatabaseLogEntry
::getSelectQueryData();
246 $tables = $basic['tables'];
247 $fields = $basic['fields'];
248 $conds = $basic['conds'];
249 $options = $basic['options'];
250 $joins = $basic['join_conds'];
253 # Add log_search table if there are conditions on it.
254 # This filters the results to only include log rows that have
255 # log_search records with the specified ls_field and ls_value values.
256 if ( array_key_exists( 'ls_field', $this->mConds
) ) {
257 $tables[] = 'log_search';
258 $index['log_search'] = 'ls_field_val';
259 $index['logging'] = 'PRIMARY';
260 if ( !$this->hasEqualsClause( 'ls_field' )
261 ||
!$this->hasEqualsClause( 'ls_value' )
263 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
264 # to match a specific (ls_field,ls_value) tuple, then there will be
265 # no duplicate log rows. Otherwise, we need to remove the duplicates.
266 $options[] = 'DISTINCT';
269 if ( count( $index ) ) {
270 $options['USE INDEX'] = $index;
272 # Don't show duplicate rows when using log_search
273 $joins['log_search'] = array( 'INNER JOIN', 'ls_log_id=log_id' );
278 'conds' => array_merge( $conds, $this->mConds
),
279 'options' => $options,
280 'join_conds' => $joins,
282 # Add ChangeTags filter query
283 ChangeTags
::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
284 $info['join_conds'], $info['options'], $this->mTagFilter
);
290 * Checks if $this->mConds has $field matched to a *single* value
294 protected function hasEqualsClause( $field ) {
296 array_key_exists( $field, $this->mConds
) &&
297 ( !is_array( $this->mConds
[$field] ) ||
count( $this->mConds
[$field] ) == 1 )
301 function getIndexField() {
302 return 'log_timestamp';
305 public function getStartBody() {
306 wfProfileIn( __METHOD__
);
307 # Do a link batch query
308 if ( $this->getNumRows() > 0 ) {
310 foreach ( $this->mResult
as $row ) {
311 $lb->add( $row->log_namespace
, $row->log_title
);
312 $lb->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
313 $lb->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
314 $formatter = LogFormatter
::newFromRow( $row );
315 foreach ( $formatter->getPreloadTitles() as $title ) {
316 $lb->addObj( $title );
320 $this->mResult
->seek( 0 );
322 wfProfileOut( __METHOD__
);
327 public function formatRow( $row ) {
328 return $this->mLogEventsList
->logLine( $row );
331 public function getType() {
338 public function getPerformer() {
339 return $this->performer
;
345 public function getPage() {
349 public function getPattern() {
350 return $this->pattern
;
353 public function getYear() {
357 public function getMonth() {
358 return $this->mMonth
;
361 public function getTagFilter() {
362 return $this->mTagFilter
;
365 public function doQuery() {
366 // Workaround MySQL optimizer bug
367 $this->mDb
->setBigSelects();
369 $this->mDb
->setBigSelects( 'default' );