Merge "(bug 17602) fix Monobook action tabs not quite touching the page body"
[lhc/web/wiklou.git] / includes / logging / LogPager.php
1 <?php
2 /**
3 * Contain classes to list log entries
4 *
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>, 2008 Aaron Schulz
6 * http://www.mediawiki.org/
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 */
25
26 /**
27 * @ingroup Pager
28 */
29 class LogPager extends ReverseChronologicalPager {
30 private $types = array(), $performer = '', $title = '', $pattern = '';
31 private $typeCGI = '';
32 public $mLogEventsList;
33
34 /**
35 * Constructor
36 *
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
46 */
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;
51
52 $this->mLogEventsList = $list;
53
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;
59 }
60
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;
67 return $query;
68 }
69
70 // Call ONLY after calling $this->limitType() already!
71 public function getFilterParams() {
72 global $wgFilterLogTypes;
73 $filters = array();
74 if ( count( $this->types ) ) {
75 return $filters;
76 }
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;
82 if ( $hide ) {
83 $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
84 }
85 }
86 }
87 return $filters;
88 }
89
90 /**
91 * Set the log reader to return only entries of the given type.
92 * Type restrictions enforced here
93 *
94 * @param string $types or array: Log types ('upload', 'delete', etc);
95 * empty string means no restriction
96 */
97 private function limitType( $types ) {
98 global $wgLogRestrictions;
99
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] )
108 ) {
109 $needReindex = true;
110 $types = array_diff( $types, array( $type ) );
111 }
112 }
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 );
117 }
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;
125 }
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];
131 }
132 }
133 }
134
135 /**
136 * Set the log reader to return only entries by the given user.
137 *
138 * @param string $name (In)valid user name
139 * @return bool
140 */
141 private function limitPerformer( $name ) {
142 if ( $name == '' ) {
143 return false;
144 }
145 $usertitle = Title::makeTitleSafe( NS_USER, $name );
146 if ( is_null( $usertitle ) ) {
147 return false;
148 }
149 /* Fetch userid at first, if known, provides awesome query plan afterwards */
150 $userid = User::idFromName( $name );
151 if ( !$userid ) {
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";
155 } else {
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;
164 }
165 $this->performer = $usertitle->getText();
166 }
167 }
168
169 /**
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.)
172 *
173 * @param string $page or Title object: Title name
174 * @param $pattern String
175 * @return bool
176 */
177 private function limitTitle( $page, $pattern ) {
178 global $wgMiserMode;
179
180 if ( $page instanceof Title ) {
181 $title = $page;
182 } else {
183 $title = Title::newFromText( $page );
184 if ( strlen( $page ) == 0 || !$title instanceof Title ) {
185 return false;
186 }
187 }
188
189 $this->title = $title->getPrefixedText();
190 $ns = $title->getNamespace();
191 $db = $this->mDb;
192
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
198 # is on.
199 #
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;
208 } else {
209 $this->mConds['log_namespace'] = $ns;
210 $this->mConds['log_title'] = $title->getDBkey();
211 }
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;
219 }
220 }
221
222 /**
223 * Constructs the most part of the query. Extra conditions are sprinkled in
224 * all over this class.
225 * @return array
226 */
227 public function getQueryInfo() {
228 $basic = DatabaseLogEntry::getSelectQueryData();
229
230 $tables = $basic['tables'];
231 $fields = $basic['fields'];
232 $conds = $basic['conds'];
233 $options = $basic['options'];
234 $joins = $basic['join_conds'];
235
236 $index = array();
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' ) )
246 {
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';
251 }
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';
259 }
260 } elseif ( count( $this->types ) == 1 ) {
261 $index['logging'] = 'type_time';
262 } else {
263 $index['logging'] = 'times';
264 }
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' );
268
269 $info = array(
270 'tables' => $tables,
271 'fields' => $fields,
272 'conds' => array_merge( $conds, $this->mConds ),
273 'options' => $options,
274 'join_conds' => $joins,
275 );
276 # Add ChangeTags filter query
277 ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
278 $info['join_conds'], $info['options'], $this->mTagFilter );
279 return $info;
280 }
281
282 /**
283 * Checks if $this->mConds has $field matched to a *single* value
284 * @param $field
285 * @return bool
286 */
287 protected function hasEqualsClause( $field ) {
288 return (
289 array_key_exists( $field, $this->mConds ) &&
290 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
291 );
292 }
293
294 function getIndexField() {
295 return 'log_timestamp';
296 }
297
298 public function getStartBody() {
299 wfProfileIn( __METHOD__ );
300 # Do a link batch query
301 if ( $this->getNumRows() > 0 ) {
302 $lb = new LinkBatch;
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 );
310 }
311 }
312 $lb->execute();
313 $this->mResult->seek( 0 );
314 }
315 wfProfileOut( __METHOD__ );
316 return '';
317 }
318
319 public function formatRow( $row ) {
320 return $this->mLogEventsList->logLine( $row );
321 }
322
323 public function getType() {
324 return $this->types;
325 }
326
327 /**
328 * @return string
329 */
330 public function getPerformer() {
331 return $this->performer;
332 }
333
334 /**
335 * @return string
336 */
337 public function getPage() {
338 return $this->title;
339 }
340
341 public function getPattern() {
342 return $this->pattern;
343 }
344
345 public function getYear() {
346 return $this->mYear;
347 }
348
349 public function getMonth() {
350 return $this->mMonth;
351 }
352
353 public function getTagFilter() {
354 return $this->mTagFilter;
355 }
356
357 public function doQuery() {
358 // Workaround MySQL optimizer bug
359 $this->mDb->setBigSelects();
360 parent::doQuery();
361 $this->mDb->setBigSelects( 'default' );
362 }
363 }