API: Enhancing ucuserprefix performance
[mediawiki.git] / includes / api / ApiQueryUserContributions.php
blobf0097b1cd0bfb5d528d085a610f9266290ef7f29
1 <?php
3 /*
4 * Created on Oct 16, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
31 /**
32 * This query action adds a list of a specified user's contributions to the output.
34 * @addtogroup API
36 class ApiQueryContributions extends ApiQueryBase {
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'uc');
42 private $params, $username;
43 private $fld_ids = false, $fld_title = false, $fld_timestamp = false,
44 $fld_comment = false, $fld_flags = false;
46 public function execute() {
48 // Parse some parameters
49 $this->params = $this->extractRequestParams();
51 $prop = array_flip($this->params['prop']);
52 $this->fld_ids = isset($prop['ids']);
53 $this->fld_title = isset($prop['title']);
54 $this->fld_comment = isset($prop['comment']);
55 $this->fld_flags = isset($prop['flags']);
56 $this->fld_timestamp = isset($prop['timestamp']);
58 // TODO: if the query is going only against the revision table, should this be done?
59 $this->selectNamedDB('contributions', DB_SLAVE, 'contributions');
60 $db = $this->getDB();
63 if(isset($this->params['userprefix']))
65 if(!preg_match('/^\d{1,3}\.\d{1,3}\./', $this->params['userprefix']))
66 $this->dieUsage("ucuserprefix must contain at least two octets terminated by a period", 'baduserprefix');
67 $this->prefixMode = true;
68 $this->userprefix = $this->params['userprefix'];
70 else
72 $this->usernames = array();
73 if(!is_array($this->params['user']))
74 $this->params['user'] = array($this->params['user']);
75 foreach($this->params['user'] as $u)
76 $this->prepareUsername($u);
77 $this->prefixMode = false;
79 $this->prepareQuery();
81 //Do the actual query.
82 $res = $this->select( __METHOD__ );
84 //Initialise some variables
85 $data = array ();
86 $count = 0;
87 $limit = $this->params['limit'];
89 //Fetch each row
90 while ( $row = $db->fetchObject( $res ) ) {
91 if (++ $count > $limit) {
92 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
93 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rev_timestamp));
94 break;
97 $vals = $this->extractRowInfo($row);
98 if ($vals)
99 $data[] = $vals;
102 //Free the database record so the connection can get on with other stuff
103 $db->freeResult($res);
105 //And send the whole shebang out as output.
106 $this->getResult()->setIndexedTagName($data, 'item');
107 $this->getResult()->addValue('query', $this->getModuleName(), $data);
111 * Validate the 'user' parameter and set the value to compare
112 * against `revision`.`rev_user_text`
114 private function prepareUsername($user) {
115 if( $user ) {
116 $name = User::isIP( $user )
117 ? $user
118 : User::getCanonicalName( $user, 'valid' );
119 if( $name === false ) {
120 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
121 } else {
122 $this->usernames[] = $name;
124 } else {
125 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
130 * Prepares the query and returns the limit of rows requested
132 private function prepareQuery() {
134 //We're after the revision table, and the corresponding page row for
135 //anything we retrieve.
136 list ($tbl_page, $tbl_revision) = $this->getDB()->tableNamesN('page', 'revision');
137 $this->addTables("$tbl_revision LEFT OUTER JOIN $tbl_page ON page_id=rev_page");
139 $this->addWhereFld('rev_deleted', 0);
140 // We only want pages by the specified users.
141 if($this->prefixMode)
143 $this->addWhere(array('rev_user' => 0));
144 $this->addWhere("rev_user_text LIKE '" . $this->getDb()->escapeLike($this->userprefix) . "%'");
146 else
147 $this->addWhereFld( 'rev_user_text', $this->usernames );
148 // ... and in the specified timeframe.
149 $this->addWhereRange('rev_timestamp',
150 $this->params['dir'], $this->params['start'], $this->params['end'] );
151 $this->addWhereFld('page_namespace', $this->params['namespace']);
153 $show = $this->params['show'];
154 if (!is_null($show)) {
155 $show = array_flip($show);
156 if (isset ($show['minor']) && isset ($show['!minor']))
157 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
159 $this->addWhereIf('rev_minor_edit = 0', isset ($show['!minor']));
160 $this->addWhereIf('rev_minor_edit != 0', isset ($show['minor']));
162 $this->addOption('LIMIT', $this->params['limit'] + 1);
164 // Mandatory fields: timestamp allows request continuation
165 // ns+title checks if the user has access rights for this page
166 // user_text is necessary if multiple users were specified
167 $this->addFields(array(
168 'rev_timestamp',
169 'page_namespace',
170 'page_title',
171 'rev_user_text',
174 $this->addFieldsIf('rev_page', $this->fld_ids);
175 $this->addFieldsIf('rev_id', $this->fld_ids);
176 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // Should this field be exposed?
177 $this->addFieldsIf('rev_comment', $this->fld_comment);
178 $this->addFieldsIf('rev_minor_edit', $this->fld_flags);
179 $this->addFieldsIf('page_is_new', $this->fld_flags);
183 * Extract fields from the database row and append them to a result array
185 private function extractRowInfo($row) {
187 $vals = array();
189 $vals['user'] = $row->rev_user_text;
190 if ($this->fld_ids) {
191 $vals['pageid'] = intval($row->rev_page);
192 $vals['revid'] = intval($row->rev_id);
193 // $vals['textid'] = intval($row->rev_text_id); // todo: Should this field be exposed?
196 if ($this->fld_title)
197 ApiQueryBase :: addTitleInfo($vals,
198 Title :: makeTitle($row->page_namespace, $row->page_title));
200 if ($this->fld_timestamp)
201 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
203 if ($this->fld_flags) {
204 if ($row->page_is_new)
205 $vals['new'] = '';
206 if ($row->rev_minor_edit)
207 $vals['minor'] = '';
210 if ($this->fld_comment && !empty ($row->rev_comment))
211 $vals['comment'] = $row->rev_comment;
213 return $vals;
216 public function getAllowedParams() {
217 return array (
218 'limit' => array (
219 ApiBase :: PARAM_DFLT => 10,
220 ApiBase :: PARAM_TYPE => 'limit',
221 ApiBase :: PARAM_MIN => 1,
222 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
223 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
225 'start' => array (
226 ApiBase :: PARAM_TYPE => 'timestamp'
228 'end' => array (
229 ApiBase :: PARAM_TYPE => 'timestamp'
231 'user' => array (
232 ApiBase :: PARAM_ISMULTI => true
234 'userprefix' => null,
235 'dir' => array (
236 ApiBase :: PARAM_DFLT => 'older',
237 ApiBase :: PARAM_TYPE => array (
238 'newer',
239 'older'
242 'namespace' => array (
243 ApiBase :: PARAM_ISMULTI => true,
244 ApiBase :: PARAM_TYPE => 'namespace'
246 'prop' => array (
247 ApiBase :: PARAM_ISMULTI => true,
248 ApiBase :: PARAM_DFLT => 'ids|title|timestamp|flags|comment',
249 ApiBase :: PARAM_TYPE => array (
250 'ids',
251 'title',
252 'timestamp',
253 'comment',
254 'flags'
257 'show' => array (
258 ApiBase :: PARAM_ISMULTI => true,
259 ApiBase :: PARAM_TYPE => array (
260 'minor',
261 '!minor',
267 public function getParamDescription() {
268 return array (
269 'limit' => 'The maximum number of contributions to return.',
270 'start' => 'The start timestamp to return from.',
271 'end' => 'The end timestamp to return to.',
272 'user' => 'The user to retrieve contributions for.',
273 'userprefix' => array(
274 'Retrieve contibutions for all IP addresses that begin with this value.',
275 'Overrides ucuser. Must contain at least two octets terminated by a period'
277 'dir' => 'The direction to search (older or newer).',
278 'namespace' => 'Only list contributions in these namespaces',
279 'prop' => 'Include additional pieces of information',
280 'show' => 'Show only items that meet this criteria, e.g. non minor edits only: show=!minor',
284 public function getDescription() {
285 return 'Get all edits by a user';
288 protected function getExamples() {
289 return array (
290 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
291 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
295 public function getVersion() {
296 return __CLASS__ . ': $Id$';