(bug 47070) check content model namespace on import.
[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
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;
81
82 return $query;
83 }
84
85 // Call ONLY after calling $this->limitType() already!
86 public function getFilterParams() {
87 global $wgFilterLogTypes;
88 $filters = array();
89 if ( count( $this->types ) ) {
90 return $filters;
91 }
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;
97 if ( $hide ) {
98 $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
99 }
100 }
101 }
102
103 return $filters;
104 }
105
106 /**
107 * Set the log reader to return only entries of the given type.
108 * Type restrictions enforced here
109 *
110 * @param string|array $types Log types ('upload', 'delete', etc);
111 * empty string means no restriction
112 */
113 private function limitType( $types ) {
114 global $wgLogRestrictions;
115
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] )
124 ) {
125 $needReindex = true;
126 $types = array_diff( $types, array( $type ) );
127 }
128 }
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 );
133 }
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;
141 }
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];
147 }
148 }
149 }
150
151 /**
152 * Set the log reader to return only entries by the given user.
153 *
154 * @param string $name (In)valid user name
155 * @return bool
156 */
157 private function limitPerformer( $name ) {
158 if ( $name == '' ) {
159 return false;
160 }
161 $usertitle = Title::makeTitleSafe( NS_USER, $name );
162 if ( is_null( $usertitle ) ) {
163 return false;
164 }
165 /* Fetch userid at first, if known, provides awesome query plan afterwards */
166 $userid = User::idFromName( $name );
167 if ( !$userid ) {
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";
171 } else {
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;
180 }
181 $this->performer = $usertitle->getText();
182 }
183 }
184
185 /**
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.)
188 *
189 * @param string|Title $page Title name
190 * @param string $pattern
191 * @return bool
192 */
193 private function limitTitle( $page, $pattern ) {
194 global $wgMiserMode;
195
196 if ( $page instanceof Title ) {
197 $title = $page;
198 } else {
199 $title = Title::newFromText( $page );
200 if ( strlen( $page ) == 0 || !$title instanceof Title ) {
201 return false;
202 }
203 }
204
205 $this->title = $title->getPrefixedText();
206 $ns = $title->getNamespace();
207 $db = $this->mDb;
208
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
214 # is on.
215 #
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;
224 } else {
225 $this->mConds['log_namespace'] = $ns;
226 $this->mConds['log_title'] = $title->getDBkey();
227 }
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;
235 }
236 }
237
238 /**
239 * Constructs the most part of the query. Extra conditions are sprinkled in
240 * all over this class.
241 * @return array
242 */
243 public function getQueryInfo() {
244 $basic = DatabaseLogEntry::getSelectQueryData();
245
246 $tables = $basic['tables'];
247 $fields = $basic['fields'];
248 $conds = $basic['conds'];
249 $options = $basic['options'];
250 $joins = $basic['join_conds'];
251
252 $index = array();
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' )
262 ) {
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';
267 }
268 }
269 if ( count( $index ) ) {
270 $options['USE INDEX'] = $index;
271 }
272 # Don't show duplicate rows when using log_search
273 $joins['log_search'] = array( 'INNER JOIN', 'ls_log_id=log_id' );
274
275 $info = array(
276 'tables' => $tables,
277 'fields' => $fields,
278 'conds' => array_merge( $conds, $this->mConds ),
279 'options' => $options,
280 'join_conds' => $joins,
281 );
282 # Add ChangeTags filter query
283 ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
284 $info['join_conds'], $info['options'], $this->mTagFilter );
285
286 return $info;
287 }
288
289 /**
290 * Checks if $this->mConds has $field matched to a *single* value
291 * @param $field
292 * @return bool
293 */
294 protected function hasEqualsClause( $field ) {
295 return (
296 array_key_exists( $field, $this->mConds ) &&
297 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
298 );
299 }
300
301 function getIndexField() {
302 return 'log_timestamp';
303 }
304
305 public function getStartBody() {
306 wfProfileIn( __METHOD__ );
307 # Do a link batch query
308 if ( $this->getNumRows() > 0 ) {
309 $lb = new LinkBatch;
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 );
317 }
318 }
319 $lb->execute();
320 $this->mResult->seek( 0 );
321 }
322 wfProfileOut( __METHOD__ );
323
324 return '';
325 }
326
327 public function formatRow( $row ) {
328 return $this->mLogEventsList->logLine( $row );
329 }
330
331 public function getType() {
332 return $this->types;
333 }
334
335 /**
336 * @return string
337 */
338 public function getPerformer() {
339 return $this->performer;
340 }
341
342 /**
343 * @return string
344 */
345 public function getPage() {
346 return $this->title;
347 }
348
349 public function getPattern() {
350 return $this->pattern;
351 }
352
353 public function getYear() {
354 return $this->mYear;
355 }
356
357 public function getMonth() {
358 return $this->mMonth;
359 }
360
361 public function getTagFilter() {
362 return $this->mTagFilter;
363 }
364
365 public function doQuery() {
366 // Workaround MySQL optimizer bug
367 $this->mDb->setBigSelects();
368 parent::doQuery();
369 $this->mDb->setBigSelects( 'default' );
370 }
371 }