e0bb692463ca57bfb269b96db123ae6a70a80351
[lhc/web/wiklou.git] / includes / api / ApiQueryUserContributions.php
1 <?php
2
3
4 /*
5 * Created on Oct 16, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 *
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.
15 *
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.
20 *
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
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiQueryBase.php');
30 }
31
32 class ApiQueryContributions extends ApiQueryBase {
33
34 public function __construct($query, $moduleName) {
35 parent :: __construct($query, $moduleName, 'uc');
36 }
37
38 public function execute() {
39
40 //Blank all our variables
41 $limit = $user = $start = $end = $dir = null;
42
43 //Get our parameters out
44 extract($this->extractRequestParams());
45
46 //Get a database instance
47 $db = & $this->getDB();
48
49 if (is_null($user))
50 $this->dieUsage("User parameter may not be empty", 'param_user');
51 $userid = $db->selectField('user', 'user_id', array (
52 'user_name' => $user
53 ));
54 if (!$userid)
55 $this->dieUsage("User name $user not found", 'param_user');
56
57 //Get the table names
58 list ($tbl_page, $tbl_revision) = $db->tableNamesN('page', 'revision');
59
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 " .
63 "page_id=rev_page");
64
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
67 // of a revision.
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'));
71
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 );
76
77 $this->addOption('LIMIT', $limit + 1);
78
79 //Initialise some variables
80 $data = array ();
81 $count = 0;
82
83 //Do the actual query.
84 $res = $this->select( __METHOD__ );
85
86 //Fetch each row
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);
91 break;
92 }
93
94 //There's a fancy function in ApiQueryBase that does
95 // most of the work for us. Use that for the page
96 // and revision.
97 $revvals = $this->addRowInfo('rev', $row);
98 $pagevals = $this->addRowInfo('page', $row);
99
100 //If we got data on the revision only, use only
101 // that data.
102 if($revvals && !$pagevals) {
103 $data[] = $revvals;
104 }
105 //If we got data on the page only, use only
106 // that data.
107 else if($pagevals && !$revvals) {
108 $data[] = $pagevals;
109 }
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);
114 }
115 }
116
117 //Free the database record so the connection can get on with other stuff
118 $db->freeResult($res);
119
120 //And send the whole shebang out as output.
121 $this->getResult()->setIndexedTagName($data, 'item');
122 $this->getResult()->addValue('query', $this->getModuleName(), $data);
123 }
124
125 protected function getAllowedParams() {
126 return array (
127 'limit' => array (
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
133 ),
134 'start' => array (
135 ApiBase :: PARAM_TYPE => 'timestamp'
136 ),
137 'end' => array (
138 ApiBase :: PARAM_TYPE => 'timestamp'
139 ),
140 'user' => null,
141 'dir' => array (
142 ApiBase :: PARAM_DFLT => 'older',
143 ApiBase :: PARAM_TYPE => array (
144 'newer',
145 'older'
146 )
147 )
148 );
149 }
150
151 protected function getParamDescription() {
152 return array (
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).'
158 );
159 }
160
161 protected function getDescription() {
162 return 'Get edits by a user..';
163 }
164
165 protected function getExamples() {
166 return array (
167 'api.php?action=query&list=usercontribs&ucuser=YurikBot'
168 );
169 }
170
171 public function getVersion() {
172 return __CLASS__ . ': $Id$';
173 }
174 }
175 ?>