* API: Allow for query extensions
[lhc/web/wiklou.git] / includes / api / ApiQueryUserContributions.php
1 <?php
2
3 /*
4 * Created on Oct 16, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
29 }
30
31 /**
32 * @addtogroup API
33 */
34 class ApiQueryContributions extends ApiQueryBase {
35
36 public function __construct($query, $moduleName) {
37 parent :: __construct($query, $moduleName, 'uc');
38 }
39
40 public function execute() {
41
42 $this->selectNamedDB('contributions', DB_SLAVE, 'contributions');
43
44 //Blank all our variables
45 $limit = $user = $start = $end = $dir = null;
46
47 //Get our parameters out
48 extract($this->extractRequestParams());
49
50 //Get a database instance
51 $db = $this->getDB();
52
53 if (is_null($user))
54 $this->dieUsage("User parameter may not be empty", 'param_user');
55 $userid = $db->selectField('user', 'user_id', array (
56 'user_name' => $user
57 ));
58 if (!$userid)
59 $this->dieUsage("User name $user not found", 'param_user');
60
61 //Get the table names
62 list ($tbl_page, $tbl_revision) = $db->tableNamesN('page', 'revision');
63
64 //We're after the revision table, and the corresponding page row for
65 //anything we retrieve.
66 $this->addTables("$tbl_revision LEFT OUTER JOIN $tbl_page ON " .
67 "page_id=rev_page");
68
69 //We want to know the namespace, title, new-ness, and ID of a page,
70 // and the id, text-id, timestamp, minor-status, summary and page
71 // of a revision.
72 $this->addFields(array('page_namespace', 'page_title', 'page_is_new',
73 'rev_id', 'rev_text_id', 'rev_timestamp', 'rev_minor_edit',
74 'rev_comment', 'rev_page'));
75
76 // We only want pages by the specified user.
77 $this->addWhereFld('rev_user_text', $user);
78 // ... and in the specified timeframe.
79 $this->addWhereRange('rev_timestamp', $dir, $start, $end );
80
81 $this->addOption('LIMIT', $limit + 1);
82
83 //Initialise some variables
84 $data = array ();
85 $count = 0;
86
87 //Do the actual query.
88 $res = $this->select( __METHOD__ );
89
90 //Fetch each row
91 while ( $row = $db->fetchObject( $res ) ) {
92 if (++ $count > $limit) {
93 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
94 $this->setContinueEnumParameter('start', $row->rev_timestamp);
95 break;
96 }
97
98 //There's a fancy function in ApiQueryBase that does
99 // most of the work for us. Use that for the page
100 // and revision.
101 $revvals = $this->addRowInfo('rev', $row);
102 $pagevals = $this->addRowInfo('page', $row);
103
104 //If we got data on the revision only, use only
105 // that data.
106 if($revvals && !$pagevals) {
107 $data[] = $revvals;
108 }
109 //If we got data on the page only, use only
110 // that data.
111 else if($pagevals && !$revvals) {
112 $data[] = $pagevals;
113 }
114 //... and if we got data on both the revision and
115 // the page, merge the data and send it out.
116 else if($pagevals && $revvals) {
117 $data[] = array_merge($revvals, $pagevals);
118 }
119 }
120
121 //Free the database record so the connection can get on with other stuff
122 $db->freeResult($res);
123
124 //And send the whole shebang out as output.
125 $this->getResult()->setIndexedTagName($data, 'item');
126 $this->getResult()->addValue('query', $this->getModuleName(), $data);
127 }
128
129 protected function getAllowedParams() {
130 return array (
131 'limit' => array (
132 ApiBase :: PARAM_DFLT => 10,
133 ApiBase :: PARAM_TYPE => 'limit',
134 ApiBase :: PARAM_MIN => 1,
135 ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1,
136 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
137 ),
138 'start' => array (
139 ApiBase :: PARAM_TYPE => 'timestamp'
140 ),
141 'end' => array (
142 ApiBase :: PARAM_TYPE => 'timestamp'
143 ),
144 'user' => null,
145 'dir' => array (
146 ApiBase :: PARAM_DFLT => 'older',
147 ApiBase :: PARAM_TYPE => array (
148 'newer',
149 'older'
150 )
151 )
152 );
153 }
154
155 protected function getParamDescription() {
156 return array (
157 'limit' => 'The maximum number of contributions to return.',
158 'start' => 'The start timestamp to return from.',
159 'end' => 'The end timestamp to return to.',
160 'user' => 'The user to retrieve contributions for.',
161 'dir' => 'The direction to search (older or newer).'
162 );
163 }
164
165 protected function getDescription() {
166 return 'Get edits by a user..';
167 }
168
169 protected function getExamples() {
170 return array (
171 'api.php?action=query&list=usercontribs&ucuser=YurikBot'
172 );
173 }
174
175 public function getVersion() {
176 return __CLASS__ . ': $Id$';
177 }
178 }
179 ?>