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