Merge "Less wild whitespace"
[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 $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
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 return $filters;
87 }
88
89 /**
90 * Set the log reader to return only entries of the given type.
91 * Type restrictions enforced here
92 *
93 * @param $types String or array: Log types ('upload', 'delete', etc);
94 * empty string means no restriction
95 */
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])
105 ) {
106 $needReindex = true;
107 $types = array_diff( $types, array( $type ) );
108 }
109 }
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 );
114 }
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;
122 }
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];
127 }
128 }
129
130 /**
131 * Set the log reader to return only entries by the given user.
132 *
133 * @param $name String: (In)valid user name
134 * @return bool
135 */
136 private function limitPerformer( $name ) {
137 if( $name == '' ) {
138 return false;
139 }
140 $usertitle = Title::makeTitleSafe( NS_USER, $name );
141 if( is_null($usertitle) ) {
142 return false;
143 }
144 /* Fetch userid at first, if known, provides awesome query plan afterwards */
145 $userid = User::idFromName( $name );
146 if( !$userid ) {
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";
150 } else {
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;
159 }
160 $this->performer = $usertitle->getText();
161 }
162 }
163
164 /**
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.)
167 *
168 * @param $page String or Title object: Title name
169 * @param $pattern String
170 * @return bool
171 */
172 private function limitTitle( $page, $pattern ) {
173 global $wgMiserMode;
174
175 if ( $page instanceof Title ) {
176 $title = $page;
177 } else {
178 $title = Title::newFromText( $page );
179 if( strlen( $page ) == 0 || !$title instanceof Title ) {
180 return false;
181 }
182 }
183
184 $this->title = $title->getPrefixedText();
185 $ns = $title->getNamespace();
186 $db = $this->mDb;
187
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
193 # is on.
194 #
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;
203 } else {
204 $this->mConds['log_namespace'] = $ns;
205 $this->mConds['log_title'] = $title->getDBkey();
206 }
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;
214 }
215 }
216
217 /**
218 * Constructs the most part of the query. Extra conditions are sprinkled in
219 * all over this class.
220 * @return array
221 */
222 public function getQueryInfo() {
223 $basic = DatabaseLogEntry::getSelectQueryData();
224
225 $tables = $basic['tables'];
226 $fields = $basic['fields'];
227 $conds = $basic['conds'];
228 $options = $basic['options'];
229 $joins = $basic['join_conds'];
230
231 $index = array();
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' ) )
241 {
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';
246 }
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';
254 }
255 } elseif( count($this->types) == 1 ) {
256 $index['logging'] = 'type_time';
257 } else {
258 $index['logging'] = 'times';
259 }
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' );
263
264 $info = array(
265 'tables' => $tables,
266 'fields' => $fields,
267 'conds' => array_merge( $conds, $this->mConds ),
268 'options' => $options,
269 'join_conds' => $joins,
270 );
271 # Add ChangeTags filter query
272 ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
273 $info['join_conds'], $info['options'], $this->mTagFilter );
274 return $info;
275 }
276
277 /**
278 * Checks if $this->mConds has $field matched to a *single* value
279 * @param $field
280 * @return bool
281 */
282 protected function hasEqualsClause( $field ) {
283 return (
284 array_key_exists( $field, $this->mConds ) &&
285 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
286 );
287 }
288
289 function getIndexField() {
290 return 'log_timestamp';
291 }
292
293 public function getStartBody() {
294 wfProfileIn( __METHOD__ );
295 # Do a link batch query
296 if( $this->getNumRows() > 0 ) {
297 $lb = new LinkBatch;
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 );
305 }
306 }
307 $lb->execute();
308 $this->mResult->seek( 0 );
309 }
310 wfProfileOut( __METHOD__ );
311 return '';
312 }
313
314 public function formatRow( $row ) {
315 return $this->mLogEventsList->logLine( $row );
316 }
317
318 public function getType() {
319 return $this->types;
320 }
321
322 /**
323 * @return string
324 */
325 public function getPerformer() {
326 return $this->performer;
327 }
328
329 /**
330 * @return string
331 */
332 public function getPage() {
333 return $this->title;
334 }
335
336 public function getPattern() {
337 return $this->pattern;
338 }
339
340 public function getYear() {
341 return $this->mYear;
342 }
343
344 public function getMonth() {
345 return $this->mMonth;
346 }
347
348 public function getTagFilter() {
349 return $this->mTagFilter;
350 }
351
352 public function doQuery() {
353 // Workaround MySQL optimizer bug
354 $this->mDb->setBigSelects();
355 parent::doQuery();
356 $this->mDb->setBigSelects( 'default' );
357 }
358 }