Made RepoGroup use ProcessCacheLRU
[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
71 return $query;
72 }
73
74 // Call ONLY after calling $this->limitType() already!
75 public function getFilterParams() {
76 global $wgFilterLogTypes;
77 $filters = array();
78 if ( count( $this->types ) ) {
79 return $filters;
80 }
81 foreach ( $wgFilterLogTypes as $type => $default ) {
82 // Avoid silly filtering
83 if ( $type !== 'patrol' || $this->getUser()->useNPPatrol() ) {
84 $hide = $this->getRequest()->getInt( "hide_{$type}_log", $default );
85 $filters[$type] = $hide;
86 if ( $hide ) {
87 $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
88 }
89 }
90 }
91
92 return $filters;
93 }
94
95 /**
96 * Set the log reader to return only entries of the given type.
97 * Type restrictions enforced here
98 *
99 * @param string $types or array: Log types ('upload', 'delete', etc);
100 * empty string means no restriction
101 */
102 private function limitType( $types ) {
103 global $wgLogRestrictions;
104
105 $user = $this->getUser();
106 // If $types is not an array, make it an array
107 $types = ( $types === '' ) ? array() : (array)$types;
108 // Don't even show header for private logs; don't recognize it...
109 $needReindex = false;
110 foreach ( $types as $type ) {
111 if ( isset( $wgLogRestrictions[$type] )
112 && !$user->isAllowed( $wgLogRestrictions[$type] )
113 ) {
114 $needReindex = true;
115 $types = array_diff( $types, array( $type ) );
116 }
117 }
118 if ( $needReindex ) {
119 // Lots of this code makes assumptions that
120 // the first entry in the array is $types[0].
121 $types = array_values( $types );
122 }
123 $this->types = $types;
124 // Don't show private logs to unprivileged users.
125 // Also, only show them upon specific request to avoid suprises.
126 $audience = $types ? 'user' : 'public';
127 $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience, $user );
128 if ( $hideLogs !== false ) {
129 $this->mConds[] = $hideLogs;
130 }
131 if ( count( $types ) ) {
132 $this->mConds['log_type'] = $types;
133 // Set typeCGI; used in url param for paging
134 if ( count( $types ) == 1 ) {
135 $this->typeCGI = $types[0];
136 }
137 }
138 }
139
140 /**
141 * Set the log reader to return only entries by the given user.
142 *
143 * @param string $name (In)valid user name
144 * @return bool
145 */
146 private function limitPerformer( $name ) {
147 if ( $name == '' ) {
148 return false;
149 }
150 $usertitle = Title::makeTitleSafe( NS_USER, $name );
151 if ( is_null( $usertitle ) ) {
152 return false;
153 }
154 /* Fetch userid at first, if known, provides awesome query plan afterwards */
155 $userid = User::idFromName( $name );
156 if ( !$userid ) {
157 /* It should be nicer to abort query at all,
158 but for now it won't pass anywhere behind the optimizer */
159 $this->mConds[] = "NULL";
160 } else {
161 $this->mConds['log_user'] = $userid;
162 // Paranoia: avoid brute force searches (bug 17342)
163 $user = $this->getUser();
164 if ( !$user->isAllowed( 'deletedhistory' ) ) {
165 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0';
166 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
167 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_USER ) .
168 ' != ' . LogPage::SUPPRESSED_USER;
169 }
170 $this->performer = $usertitle->getText();
171 }
172 }
173
174 /**
175 * Set the log reader to return only entries affecting the given page.
176 * (For the block and rights logs, this is a user page.)
177 *
178 * @param string $page or Title object: Title name
179 * @param $pattern String
180 * @return bool
181 */
182 private function limitTitle( $page, $pattern ) {
183 global $wgMiserMode;
184
185 if ( $page instanceof Title ) {
186 $title = $page;
187 } else {
188 $title = Title::newFromText( $page );
189 if ( strlen( $page ) == 0 || !$title instanceof Title ) {
190 return false;
191 }
192 }
193
194 $this->title = $title->getPrefixedText();
195 $ns = $title->getNamespace();
196 $db = $this->mDb;
197
198 # Using the (log_namespace, log_title, log_timestamp) index with a
199 # range scan (LIKE) on the first two parts, instead of simple equality,
200 # makes it unusable for sorting. Sorted retrieval using another index
201 # would be possible, but then we might have to scan arbitrarily many
202 # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
203 # is on.
204 #
205 # This is not a problem with simple title matches, because then we can
206 # use the page_time index. That should have no more than a few hundred
207 # log entries for even the busiest pages, so it can be safely scanned
208 # in full to satisfy an impossible condition on user or similar.
209 if ( $pattern && !$wgMiserMode ) {
210 $this->mConds['log_namespace'] = $ns;
211 $this->mConds[] = 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() );
212 $this->pattern = $pattern;
213 } else {
214 $this->mConds['log_namespace'] = $ns;
215 $this->mConds['log_title'] = $title->getDBkey();
216 }
217 // Paranoia: avoid brute force searches (bug 17342)
218 $user = $this->getUser();
219 if ( !$user->isAllowed( 'deletedhistory' ) ) {
220 $this->mConds[] = $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0';
221 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
222 $this->mConds[] = $db->bitAnd( 'log_deleted', LogPage::SUPPRESSED_ACTION ) .
223 ' != ' . LogPage::SUPPRESSED_ACTION;
224 }
225 }
226
227 /**
228 * Constructs the most part of the query. Extra conditions are sprinkled in
229 * all over this class.
230 * @return array
231 */
232 public function getQueryInfo() {
233 $basic = DatabaseLogEntry::getSelectQueryData();
234
235 $tables = $basic['tables'];
236 $fields = $basic['fields'];
237 $conds = $basic['conds'];
238 $options = $basic['options'];
239 $joins = $basic['join_conds'];
240
241 $index = array();
242 # Add log_search table if there are conditions on it.
243 # This filters the results to only include log rows that have
244 # log_search records with the specified ls_field and ls_value values.
245 if ( array_key_exists( 'ls_field', $this->mConds ) ) {
246 $tables[] = 'log_search';
247 $index['log_search'] = 'ls_field_val';
248 $index['logging'] = 'PRIMARY';
249 if ( !$this->hasEqualsClause( 'ls_field' )
250 || !$this->hasEqualsClause( 'ls_value' )
251 ) {
252 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
253 # to match a specific (ls_field,ls_value) tuple, then there will be
254 # no duplicate log rows. Otherwise, we need to remove the duplicates.
255 $options[] = 'DISTINCT';
256 }
257 }
258 if ( count( $index ) ) {
259 $options['USE INDEX'] = $index;
260 }
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
275 return $info;
276 }
277
278 /**
279 * Checks if $this->mConds has $field matched to a *single* value
280 * @param $field
281 * @return bool
282 */
283 protected function hasEqualsClause( $field ) {
284 return (
285 array_key_exists( $field, $this->mConds ) &&
286 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
287 );
288 }
289
290 function getIndexField() {
291 return 'log_timestamp';
292 }
293
294 public function getStartBody() {
295 wfProfileIn( __METHOD__ );
296 # Do a link batch query
297 if ( $this->getNumRows() > 0 ) {
298 $lb = new LinkBatch;
299 foreach ( $this->mResult as $row ) {
300 $lb->add( $row->log_namespace, $row->log_title );
301 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
302 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
303 $formatter = LogFormatter::newFromRow( $row );
304 foreach ( $formatter->getPreloadTitles() as $title ) {
305 $lb->addObj( $title );
306 }
307 }
308 $lb->execute();
309 $this->mResult->seek( 0 );
310 }
311 wfProfileOut( __METHOD__ );
312
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 }