Merge "Add Special:CreateAccount to Special:Specialpages"
[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', 'log_user_type_time' );
257 } elseif ( count( $this->types ) == 1 ) {
258 $index['logging'] = 'type_time'; // @TODO: sucks for change tags
259 }
260 if ( count( $index ) ) {
261 $options['USE INDEX'] = $index;
262 }
263 # Don't show duplicate rows when using log_search
264 $joins['log_search'] = array( 'INNER JOIN', 'ls_log_id=log_id' );
265
266 $info = array(
267 'tables' => $tables,
268 'fields' => $fields,
269 'conds' => array_merge( $conds, $this->mConds ),
270 'options' => $options,
271 'join_conds' => $joins,
272 );
273 # Add ChangeTags filter query
274 ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
275 $info['join_conds'], $info['options'], $this->mTagFilter );
276 return $info;
277 }
278
279 /**
280 * Checks if $this->mConds has $field matched to a *single* value
281 * @param $field
282 * @return bool
283 */
284 protected function hasEqualsClause( $field ) {
285 return (
286 array_key_exists( $field, $this->mConds ) &&
287 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
288 );
289 }
290
291 function getIndexField() {
292 return 'log_timestamp';
293 }
294
295 public function getStartBody() {
296 wfProfileIn( __METHOD__ );
297 # Do a link batch query
298 if ( $this->getNumRows() > 0 ) {
299 $lb = new LinkBatch;
300 foreach ( $this->mResult as $row ) {
301 $lb->add( $row->log_namespace, $row->log_title );
302 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
303 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
304 $formatter = LogFormatter::newFromRow( $row );
305 foreach ( $formatter->getPreloadTitles() as $title ) {
306 $lb->addObj( $title );
307 }
308 }
309 $lb->execute();
310 $this->mResult->seek( 0 );
311 }
312 wfProfileOut( __METHOD__ );
313 return '';
314 }
315
316 public function formatRow( $row ) {
317 return $this->mLogEventsList->logLine( $row );
318 }
319
320 public function getType() {
321 return $this->types;
322 }
323
324 /**
325 * @return string
326 */
327 public function getPerformer() {
328 return $this->performer;
329 }
330
331 /**
332 * @return string
333 */
334 public function getPage() {
335 return $this->title;
336 }
337
338 public function getPattern() {
339 return $this->pattern;
340 }
341
342 public function getYear() {
343 return $this->mYear;
344 }
345
346 public function getMonth() {
347 return $this->mMonth;
348 }
349
350 public function getTagFilter() {
351 return $this->mTagFilter;
352 }
353
354 public function doQuery() {
355 // Workaround MySQL optimizer bug
356 $this->mDb->setBigSelects();
357 parent::doQuery();
358 $this->mDb->setBigSelects( 'default' );
359 }
360 }