5 * Created on Oct 16, 2006
7 * API for MediaWiki 1.8+
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiQueryBase.php');
32 class ApiQueryContributions
extends ApiQueryBase
{
34 public function __construct($query, $moduleName) {
35 parent
:: __construct($query, $moduleName, 'uc');
38 public function execute() {
40 //Blank all our variables
41 $limit = $user = $start = $end = $dir = null;
43 //Get our parameters out
44 extract($this->extractRequestParams());
46 //Get a database instance
47 $db = & $this->getDB();
50 $this->dieUsage("User parameter may not be empty", 'param_user');
51 $userid = $db->selectField('user', 'user_id', array (
55 $this->dieUsage("User name $user not found", 'param_user');
58 list ($tbl_page, $tbl_revision) = $db->tableNamesN('page', 'revision');
60 //We're after the revision table, and the corresponding page row for
61 //anything we retrieve.
62 $this->addTables("$tbl_revision LEFT OUTER JOIN $tbl_page ON " .
65 //We want to know the namespace, title, new-ness, and ID of a page,
66 // and the id, text-id, timestamp, minor-status, summary and page
68 $this->addFields(array('page_namespace', 'page_title', 'page_is_new',
69 'rev_id', 'rev_text_id', 'rev_timestamp', 'rev_minor_edit',
70 'rev_comment', 'rev_page'));
72 // We only want pages by the specified user.
73 $this->addWhereFld('rev_user_text', $user);
74 // ... and in the specified timeframe.
75 $this->addWhereRange('rev_timestamp', $dir, $start, $end );
77 $this->addOption('LIMIT', $limit +
1);
79 //Initialise some variables
83 //Do the actual query.
84 $res = $this->select( __METHOD__
);
87 while ( $row = $db->fetchObject( $res ) ) {
88 if (++
$count > $limit) {
89 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
90 $this->setContinueEnumParameter('start', $row->rev_timestamp
);
94 //There's a fancy function in ApiQueryBase that does
95 // most of the work for us. Use that for the page
97 $revvals = $this->addRowInfo('rev', $row);
98 $pagevals = $this->addRowInfo('page', $row);
100 //If we got data on the revision only, use only
102 if($revvals && !$pagevals) {
105 //If we got data on the page only, use only
107 else if($pagevals && !$revvals) {
110 //... and if we got data on both the revision and
111 // the page, merge the data and send it out.
112 else if($pagevals && $revvals) {
113 $data[] = array_merge($revvals, $pagevals);
117 //Free the database record so the connection can get on with other stuff
118 $db->freeResult($res);
120 //And send the whole shebang out as output.
121 $this->getResult()->setIndexedTagName($data, 'item');
122 $this->getResult()->addValue('query', $this->getModuleName(), $data);
125 protected function getAllowedParams() {
128 ApiBase
:: PARAM_DFLT
=> 10,
129 ApiBase
:: PARAM_TYPE
=> 'limit',
130 ApiBase
:: PARAM_MIN
=> 1,
131 ApiBase
:: PARAM_MAX1
=> ApiBase
:: LIMIT_BIG1
,
132 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
135 ApiBase
:: PARAM_TYPE
=> 'timestamp'
138 ApiBase
:: PARAM_TYPE
=> 'timestamp'
142 ApiBase
:: PARAM_DFLT
=> 'older',
143 ApiBase
:: PARAM_TYPE
=> array (
151 protected function getParamDescription() {
153 'limit' => 'The maximum number of contributions to return.',
154 'start' => 'The start timestamp to return from.',
155 'end' => 'The end timestamp to return to.',
156 'user' => 'The user to retrieve contributions for.',
157 'dir' => 'The direction to search (older or newer).'
161 protected function getDescription() {
162 return 'Get edits by a user..';
165 protected function getExamples() {
167 'api.php?action=query&list=usercontribs&ucuser=YurikBot'
171 public function getVersion() {
172 return __CLASS__
. ': $Id$';