Followup to r45954: it helps to save before committing
[lhc/web/wiklou.git] / includes / api / ApiQueryWatchlistRaw.php
1 <?php
2
3 /*
4 * Created on Oct 4, 2008
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2008 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 * This query action allows clients to retrieve a list of pages
33 * on the logged-in user's watchlist.
34 *
35 * @ingroup API
36 */
37 class ApiQueryWatchlistRaw extends ApiQueryGeneratorBase {
38
39 public function __construct($query, $moduleName) {
40 parent :: __construct($query, $moduleName, 'wr');
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function executeGenerator($resultPageSet) {
48 $this->run($resultPageSet);
49 }
50
51 private function run($resultPageSet = null) {
52 global $wgUser;
53
54 $this->selectNamedDB('watchlist', DB_SLAVE, 'watchlist');
55
56 if (!$wgUser->isLoggedIn())
57 $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
58 $params = $this->extractRequestParams();
59 $prop = array_flip((array)$params['prop']);
60 $show = array_flip((array)$params['show']);
61 if(isset($show['changed']) && isset($show['!changed']))
62 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
63
64 $this->addTables('watchlist');
65 $this->addFields(array('wl_namespace', 'wl_title'));
66 $this->addFieldsIf('wl_notificationtimestamp', isset($prop['changed']));
67 $this->addWhereFld('wl_user', $wgUser->getId());
68 $this->addWhereFld('wl_namespace', $params['namespace']);
69 $this->addWhereIf('wl_notificationtimestamp IS NOT NULL', isset($show['changed']));
70 $this->addWhereIf('wl_notificationtimestamp IS NULL', isset($show['!changed']));
71 if(isset($params['continue']))
72 {
73 $cont = explode('|', $params['continue']);
74 if(count($cont) != 2)
75 $this->dieUsage("Invalid continue param. You should pass the " .
76 "original value returned by the previous query", "_badcontinue");
77 $ns = intval($cont[0]);
78 $title = $this->getDB()->strencode($this->titleToKey($cont[1]));
79 $this->addWhere("wl_namespace > '$ns' OR ".
80 "(wl_namespace = '$ns' AND ".
81 "wl_title >= '$title')");
82 }
83 // Don't ORDER BY wl_namespace if it's constant in the WHERE clause
84 if(count($params['namespace']) == 1)
85 $this->addOption('ORDER BY', 'wl_title');
86 else
87 $this->addOption('ORDER BY', 'wl_namespace, wl_title');
88 $this->addOption('LIMIT', $params['limit'] + 1);
89 $res = $this->select(__METHOD__);
90
91 $db = $this->getDB();
92 $data = array();
93 $titles = array();
94 $count = 0;
95 while($row = $db->fetchObject($res))
96 {
97 if(++$count > $params['limit'])
98 {
99 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
100 $this->setContinueEnumParameter('continue', $row->wl_namespace . '|' .
101 $this->keyToTitle($row->wl_title));
102 break;
103 }
104 $t = Title::makeTitle($row->wl_namespace, $row->wl_title);
105 if(is_null($resultPageSet))
106 {
107 $vals = array();
108 ApiQueryBase::addTitleInfo($vals, $t);
109 if(isset($prop['changed']) && !is_null($row->wl_notificationtimestamp))
110 $vals['changed'] = wfTimestamp(TS_ISO_8601, $row->wl_notificationtimestamp);
111 $data[] = $vals;
112 }
113 else
114 $titles[] = $t;
115 }
116 if(is_null($resultPageSet))
117 {
118 $this->getResult()->setIndexedTagName($data, 'wr');
119 $this->getResult()->addValue(null, $this->getModuleName(), $data);
120 }
121 else
122 $resultPageSet->populateFromTitles($titles);
123 }
124
125 public function getAllowedParams() {
126 return array (
127 'continue' => null,
128 'namespace' => array (
129 ApiBase :: PARAM_ISMULTI => true,
130 ApiBase :: PARAM_TYPE => 'namespace'
131 ),
132 'limit' => array (
133 ApiBase :: PARAM_DFLT => 10,
134 ApiBase :: PARAM_TYPE => 'limit',
135 ApiBase :: PARAM_MIN => 1,
136 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
137 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
138 ),
139 'prop' => array (
140 ApiBase :: PARAM_ISMULTI => true,
141 ApiBase :: PARAM_TYPE => array (
142 'changed',
143 )
144 ),
145 'show' => array (
146 ApiBase :: PARAM_ISMULTI => true,
147 ApiBase :: PARAM_TYPE => array (
148 'changed',
149 '!changed',
150 )
151 )
152 );
153 }
154
155 public function getParamDescription() {
156 return array (
157 'continue' => 'When more results are available, use this to continue',
158 'namespace' => 'Only list pages in the given namespace(s).',
159 'limit' => 'How many total results to return per request.',
160 'prop' => 'Which additional properties to get (non-generator mode only).',
161 'show' => 'Only list items that meet these criteria.',
162 );
163 }
164
165 public function getDescription() {
166 return "Get all pages on the logged in user's watchlist";
167 }
168
169 protected function getExamples() {
170 return array (
171 'api.php?action=query&list=watchlistraw',
172 'api.php?action=query&generator=watchlistraw&gwrshow=changed&prop=revisions',
173 );
174 }
175
176 public function getVersion() {
177 return __CLASS__ . ': $Id$';
178 }
179 }