1d8ae69dc33115ddd39898959006325f87e6e144
[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 //Extract the table names, in case we have a prefix
58 extract($db->tableNames( 'page', 'revision'), EXTR_PREFIX_ALL, 'tbl');
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 //If we got data on the page only, use only
105 // that data.
106 else if($pagevals && !$revvals)
107 $data[] = $pagevals;
108 //... and if we got data on both the revision and
109 // the page, merge the data and send it out.
110 else if($pagevals && $revvals)
111 $data[] = array_merge($revvals, $pagevals);
112 }
113
114 //Free the database record so the connection can get on with other stuff
115 $db->freeResult($res);
116
117 //And send the whole shebang out as output.
118 $this->getResult()->setIndexedTagName($data, 'item');
119 $this->getResult()->addValue('query', $this->getModuleName(), $data);
120 }
121
122 protected function getAllowedParams() {
123 return array (
124 'limit' => array (
125 ApiBase :: PARAM_DFLT => 10,
126 ApiBase :: PARAM_TYPE => 'limit',
127 ApiBase :: PARAM_MIN => 1,
128 ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1,
129 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
130 ),
131 'start' => array (
132 ApiBase :: PARAM_TYPE => 'timestamp'
133 ),
134 'end' => array (
135 ApiBase :: PARAM_TYPE => 'timestamp'
136 ),
137 'user' => null,
138 'dir' => array (
139 ApiBase :: PARAM_DFLT => 'older',
140 ApiBase :: PARAM_TYPE => array (
141 'newer',
142 'older'
143 )
144 )
145 );
146 }
147
148 protected function getParamDescription() {
149 return array (
150 'limit' => 'The maximum number of contributions to return.',
151 'start' => 'The start timestamp to return from.',
152 'end' => 'The end timestamp to return to.',
153 'user' => 'The user to retrieve contributions for.',
154 'dir' => 'The direction to search (older or newer).'
155 );
156 }
157
158 protected function getDescription() {
159 return 'Get edits by a user..';
160 }
161
162 protected function getExamples() {
163 return array (
164 'api.php?action=query&list=usercontribs&ucuser=YurikBot'
165 );
166 }
167
168 public function getVersion() {
169 return __CLASS__ . ': $Id$';
170 }
171 }
172 ?>