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');
32 * This query action adds a list of a specified user's contributions to the output.
36 class ApiQueryContributions
extends ApiQueryBase
{
38 public function __construct($query, $moduleName) {
39 parent
:: __construct($query, $moduleName, 'uc');
42 private $params, $userTitle;
43 private $fld_title = false, $fld_timestamp = false, $fld_comment = false, $fld_flags = false;
45 public function execute() {
47 // Parse some parameters
48 $this->params
= $this->extractRequestParams();
49 $prop = $this->params
['prop'];
50 if (!is_null($prop)) {
51 $prop = array_flip($prop);
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']);
59 // TODO: if the query is going only against the revision table, should this be done?
60 $this->selectNamedDB('contributions', DB_SLAVE
, 'contributions');
64 $this->getUserTitle();
65 $this->prepareQuery();
67 //Do the actual query.
68 $res = $this->select( __METHOD__
);
70 //Initialise some variables
73 $limit = $this->params
['limit'];
76 while ( $row = $db->fetchObject( $res ) ) {
77 if (++
$count > $limit) {
78 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
79 $this->setContinueEnumParameter('start', $row->rev_timestamp
);
83 $vals = $this->extractRowInfo($row);
88 //Free the database record so the connection can get on with other stuff
89 $db->freeResult($res);
91 //And send the whole shebang out as output.
92 $this->getResult()->setIndexedTagName($data, 'item');
93 $this->getResult()->addValue('query', $this->getModuleName(), $data);
97 * Convert 'user' parameter into a proper user login name.
98 * This method also validates that this user actually exists in the database.
100 private function getUserTitle() {
102 $user = $this->params
['user'];
104 $this->dieUsage("User parameter may not be empty", 'param_user');
106 $userTitle = Title
::makeTitleSafe( NS_USER
, $user );
107 if ( is_null( $userTitle ) )
108 $this->dieUsage("User name $user is not valid", 'param_user');
110 $userid = $this->getDB()->selectField('user', 'user_id', array (
111 'user_name' => $userTitle->getText()
115 $this->dieUsage("User name $user not found", 'param_user');
117 $this->userTitle
= $userTitle;
121 * Prepares the query and returns the limit of rows requested
123 private function prepareQuery() {
125 if ($this->fld_title ||
$this->fld_flags
) {
126 //We're after the revision table, and the corresponding page row for
127 //anything we retrieve.
128 list ($tbl_page, $tbl_revision) = $this->getDB()->tableNamesN('page', 'revision');
129 $this->addTables("$tbl_revision LEFT OUTER JOIN $tbl_page ON page_id=rev_page");
131 $this->addTables("revision");
134 // We only want pages by the specified user.
135 $this->addWhereFld('rev_user_text', $this->userTitle
->getText());
137 // ... and in the specified timeframe.
138 $this->addWhereRange('rev_timestamp',
139 $this->params
['dir'], $this->params
['start'], $this->params
['end'] );
141 $show = $this->params
['show'];
142 if (!is_null($show)) {
143 $show = array_flip($show);
144 if (isset ($show['minor']) && isset ($show['!minor']))
145 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
147 $this->addWhereIf('rev_minor_edit = 0', isset ($show['!minor']));
148 $this->addWhereIf('rev_minor_edit != 0', isset ($show['minor']));
151 $this->addOption('LIMIT', $this->params
['limit'] +
1);
153 // We want to know the namespace, title, new-ness, and ID of a page,
154 // and the id, text-id, timestamp, minor-status, summary and page
156 $this->addFields(array(
159 'rev_timestamp', // Always include timestamp to enable request continuation
160 // 'rev_text_id', // Should this field be exposed?
163 $this->addFieldsIf('rev_comment', $this->fld_comment
);
164 $this->addFieldsIf('rev_minor_edit', $this->fld_flags
);
166 // These fields depend only work if the page table is joined
167 $this->addFieldsIf('page_is_new', $this->fld_flags
);
168 $this->addFieldsIf('page_namespace', $this->fld_title
);
169 $this->addFieldsIf('page_title', $this->fld_title
);
173 * Extract fields from the database row and append them to a result array
175 private function extractRowInfo($row) {
177 $title = Title
:: makeTitle($row->page_namespace
, $row->page_title
);
178 if (!$title->userCanRead())
183 $vals['pageid'] = intval($row->rev_page
);
184 $vals['revid'] = intval($row->rev_id
);
186 if ($this->fld_title
)
187 ApiQueryBase
:: addTitleInfo($vals, $title);
189 // Should this field be exposed?
190 // $vals['textid'] = intval($row->rev_text_id);
192 if ($this->fld_timestamp
)
193 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rev_timestamp
);
195 if ($this->fld_flags
) {
196 if ($row->page_is_new
)
198 if ($row->rev_minor_edit
)
202 if ($this->fld_comment
&& !empty ($row->rev_comment
))
203 $vals['comment'] = $row->rev_comment
;
208 protected function getAllowedParams() {
211 ApiBase
:: PARAM_DFLT
=> 10,
212 ApiBase
:: PARAM_TYPE
=> 'limit',
213 ApiBase
:: PARAM_MIN
=> 1,
214 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
215 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
218 ApiBase
:: PARAM_TYPE
=> 'timestamp'
221 ApiBase
:: PARAM_TYPE
=> 'timestamp'
224 ApiBase
:: PARAM_TYPE
=> 'user'
227 ApiBase
:: PARAM_DFLT
=> 'older',
228 ApiBase
:: PARAM_TYPE
=> array (
234 ApiBase
:: PARAM_ISMULTI
=> true,
235 ApiBase
:: PARAM_DFLT
=> 'title|timestamp|flags|comment',
236 ApiBase
:: PARAM_TYPE
=> array (
245 ApiBase
:: PARAM_ISMULTI
=> true,
246 ApiBase
:: PARAM_TYPE
=> array (
254 protected function getParamDescription() {
256 'limit' => 'The maximum number of contributions to return.',
257 'start' => 'The start timestamp to return from.',
258 'end' => 'The end timestamp to return to.',
259 'user' => 'The user to retrieve contributions for.',
260 'dir' => 'The direction to search (older or newer).',
261 'prop' => 'Include additional pieces of information',
262 'show' => 'Show only items that meet this criteria, e.g. non minor edits only: show=!minor',
266 protected function getDescription() {
267 return 'Get edits by a user..';
270 protected function getExamples() {
272 'api.php?action=query&list=usercontribs&ucuser=YurikBot'
276 public function getVersion() {
277 return __CLASS__
. ': $Id$';