Move closing parenthesis from multi line if and function to own line
[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();
31 private $performer = '';
32 private $title = '';
33 private $pattern = '';
34 private $typeCGI = '';
35 public $mLogEventsList;
36
37 /**
38 * Constructor
39 *
40 * @param LogEventsList $list
41 * @param string|array $types Log types to show
42 * @param string $performer the user who made the log entries
43 * @param string|Title $title the page title the log entries are for
44 * @param string $pattern do a prefix search rather than an exact title match
45 * @param array $conds extra conditions for the query
46 * @param int|bool $year The year to start from. Default: false
47 * @param int|bool $month The month to start from. Default: false
48 * @param string $tagFilter tag
49 */
50 public function __construct( $list, $types = array(), $performer = '', $title = '', $pattern = '',
51 $conds = array(), $year = false, $month = false, $tagFilter = '' ) {
52 parent::__construct( $list->getContext() );
53 $this->mConds = $conds;
54
55 $this->mLogEventsList = $list;
56
57 $this->limitType( $types ); // also excludes hidden types
58 $this->limitPerformer( $performer );
59 $this->limitTitle( $title, $pattern );
60 $this->getDateCond( $year, $month );
61 $this->mTagFilter = $tagFilter;
62 }
63
64 public function getDefaultQuery() {
65 $query = parent::getDefaultQuery();
66 $query['type'] = $this->typeCGI; // arrays won't work here
67 $query['user'] = $this->performer;
68 $query['month'] = $this->mMonth;
69 $query['year'] = $this->mYear;
70 return $query;
71 }
72
73 // Call ONLY after calling $this->limitType() already!
74 public function getFilterParams() {
75 global $wgFilterLogTypes;
76 $filters = array();
77 if ( count( $this->types ) ) {
78 return $filters;
79 }
80 foreach ( $wgFilterLogTypes as $type => $default ) {
81 // Avoid silly filtering
82 if ( $type !== 'patrol' || $this->getUser()->useNPPatrol() ) {
83 $hide = $this->getRequest()->getInt( "hide_{$type}_log", $default );
84 $filters[$type] = $hide;
85 if ( $hide ) {
86 $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
87 }
88 }
89 }
90 return $filters;
91 }
92
93 /**
94 * Set the log reader to return only entries of the given type.
95 * Type restrictions enforced here
96 *
97 * @param string $types or array: Log types ('upload', 'delete', etc);
98 * empty string means no restriction
99 */
100 private function limitType( $types ) {
101 global $wgLogRestrictions;
102
103 $user = $this->getUser();
104 // If $types is not an array, make it an array
105 $types = ( $types === '' ) ? array() : (array)$types;
106 // Don't even show header for private logs; don't recognize it...
107 $needReindex = false;
108 foreach ( $types as $type ) {
109 if ( isset( $wgLogRestrictions[$type] )
110 && !$user->isAllowed( $wgLogRestrictions[$type] )
111 ) {
112 $needReindex = true;
113 $types = array_diff( $types, array( $type ) );
114 }
115 }
116 if ( $needReindex ) {
117 // Lots of this code makes assumptions that
118 // the first entry in the array is $types[0].
119 $types = array_values( $types );
120 }
121 $this->types = $types;
122 // Don't show private logs to unprivileged users.
123 // Also, only show them upon specific request to avoid suprises.
124 $audience = $types ? 'user' : 'public';
125 $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience, $user );
126 if ( $hideLogs !== false ) {
127 $this->mConds[] = $hideLogs;
128 }
129 if ( count( $types ) ) {
130 $this->mConds['log_type'] = $types;
131 // Set typeCGI; used in url param for paging
132 if ( count( $types ) == 1 ) {
133 $this->typeCGI = $types[0];
134 }
135 }
136 }
137
138 /**
139 * Set the log reader to return only entries by the given user.
140 *
141 * @param string $name (In)valid user name
142 * @return bool
143 */
144 private function limitPerformer( $name ) {
145 if ( $name == '' ) {
146 return false;
147 }
148 $usertitle = Title::makeTitleSafe( NS_USER, $name );
149 if ( is_null( $usertitle ) ) {
150 return false;
151 }
152 /* Fetch userid at first, if known, provides awesome query plan afterwards */
153 $userid = User::idFromName( $name );
154 if ( !$userid ) {
155 /* It should be nicer to abort query at all,
156 but for now it won't pass anywhere behind the optimizer */
157 $this->mConds[] = "NULL";
158 } else {
159 $this->mConds['log_user'] = $userid;
160 // Paranoia: avoid brute force searches (bug 17342)
161 $user = $this->getUser();
162 if ( !$user->isAllowed( 'deletedhistory' ) ) {
163 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0';
164 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
165 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_USER ) .
166 ' != ' . LogPage::SUPPRESSED_USER;
167 }
168 $this->performer = $usertitle->getText();
169 }
170 }
171
172 /**
173 * Set the log reader to return only entries affecting the given page.
174 * (For the block and rights logs, this is a user page.)
175 *
176 * @param string $page or Title object: Title name
177 * @param $pattern String
178 * @return bool
179 */
180 private function limitTitle( $page, $pattern ) {
181 global $wgMiserMode;
182
183 if ( $page instanceof Title ) {
184 $title = $page;
185 } else {
186 $title = Title::newFromText( $page );
187 if ( strlen( $page ) == 0 || !$title instanceof Title ) {
188 return false;
189 }
190 }
191
192 $this->title = $title->getPrefixedText();
193 $ns = $title->getNamespace();
194 $db = $this->mDb;
195
196 # Using the (log_namespace, log_title, log_timestamp) index with a
197 # range scan (LIKE) on the first two parts, instead of simple equality,
198 # makes it unusable for sorting. Sorted retrieval using another index
199 # would be possible, but then we might have to scan arbitrarily many
200 # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
201 # is on.
202 #
203 # This is not a problem with simple title matches, because then we can
204 # use the page_time index. That should have no more than a few hundred
205 # log entries for even the busiest pages, so it can be safely scanned
206 # in full to satisfy an impossible condition on user or similar.
207 if ( $pattern && !$wgMiserMode ) {
208 $this->mConds['log_namespace'] = $ns;
209 $this->mConds[] = 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() );
210 $this->pattern = $pattern;
211 } else {
212 $this->mConds['log_namespace'] = $ns;
213 $this->mConds['log_title'] = $title->getDBkey();
214 }
215 // Paranoia: avoid brute force searches (bug 17342)
216 $user = $this->getUser();
217 if ( !$user->isAllowed( 'deletedhistory' ) ) {
218 $this->mConds[] = $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0';
219 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
220 $this->mConds[] = $db->bitAnd( 'log_deleted', LogPage::SUPPRESSED_ACTION ) .
221 ' != ' . LogPage::SUPPRESSED_ACTION;
222 }
223 }
224
225 /**
226 * Constructs the most part of the query. Extra conditions are sprinkled in
227 * all over this class.
228 * @return array
229 */
230 public function getQueryInfo() {
231 $basic = DatabaseLogEntry::getSelectQueryData();
232
233 $tables = $basic['tables'];
234 $fields = $basic['fields'];
235 $conds = $basic['conds'];
236 $options = $basic['options'];
237 $joins = $basic['join_conds'];
238
239 $index = array();
240 # Add log_search table if there are conditions on it.
241 # This filters the results to only include log rows that have
242 # log_search records with the specified ls_field and ls_value values.
243 if ( array_key_exists( 'ls_field', $this->mConds ) ) {
244 $tables[] = 'log_search';
245 $index['log_search'] = 'ls_field_val';
246 $index['logging'] = 'PRIMARY';
247 if ( !$this->hasEqualsClause( 'ls_field' )
248 || !$this->hasEqualsClause( 'ls_value' )
249 ) {
250 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
251 # to match a specific (ls_field,ls_value) tuple, then there will be
252 # no duplicate log rows. Otherwise, we need to remove the duplicates.
253 $options[] = 'DISTINCT';
254 }
255 }
256 if ( count( $index ) ) {
257 $options['USE INDEX'] = $index;
258 }
259 # Don't show duplicate rows when using log_search
260 $joins['log_search'] = array( 'INNER JOIN', 'ls_log_id=log_id' );
261
262 $info = array(
263 'tables' => $tables,
264 'fields' => $fields,
265 'conds' => array_merge( $conds, $this->mConds ),
266 'options' => $options,
267 'join_conds' => $joins,
268 );
269 # Add ChangeTags filter query
270 ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
271 $info['join_conds'], $info['options'], $this->mTagFilter );
272 return $info;
273 }
274
275 /**
276 * Checks if $this->mConds has $field matched to a *single* value
277 * @param $field
278 * @return bool
279 */
280 protected function hasEqualsClause( $field ) {
281 return (
282 array_key_exists( $field, $this->mConds ) &&
283 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
284 );
285 }
286
287 function getIndexField() {
288 return 'log_timestamp';
289 }
290
291 public function getStartBody() {
292 wfProfileIn( __METHOD__ );
293 # Do a link batch query
294 if ( $this->getNumRows() > 0 ) {
295 $lb = new LinkBatch;
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 ) );
300 $formatter = LogFormatter::newFromRow( $row );
301 foreach ( $formatter->getPreloadTitles() as $title ) {
302 $lb->addObj( $title );
303 }
304 }
305 $lb->execute();
306 $this->mResult->seek( 0 );
307 }
308 wfProfileOut( __METHOD__ );
309 return '';
310 }
311
312 public function formatRow( $row ) {
313 return $this->mLogEventsList->logLine( $row );
314 }
315
316 public function getType() {
317 return $this->types;
318 }
319
320 /**
321 * @return string
322 */
323 public function getPerformer() {
324 return $this->performer;
325 }
326
327 /**
328 * @return string
329 */
330 public function getPage() {
331 return $this->title;
332 }
333
334 public function getPattern() {
335 return $this->pattern;
336 }
337
338 public function getYear() {
339 return $this->mYear;
340 }
341
342 public function getMonth() {
343 return $this->mMonth;
344 }
345
346 public function getTagFilter() {
347 return $this->mTagFilter;
348 }
349
350 public function doQuery() {
351 // Workaround MySQL optimizer bug
352 $this->mDb->setBigSelects();
353 parent::doQuery();
354 $this->mDb->setBigSelects( 'default' );
355 }
356 }