Cleaned up and optimized wfBaseConvert();
[lhc/web/wiklou.git] / includes / api / ApiQueryWatchlistRaw.php
1 <?php
2 /**
3 *
4 *
5 * Created on Oct 4, 2008
6 *
7 * Copyright © 2008 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * This query action allows clients to retrieve a list of pages
29 * on the logged-in user's watchlist.
30 *
31 * @ingroup API
32 */
33 class ApiQueryWatchlistRaw extends ApiQueryGeneratorBase {
34
35 public function __construct( $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'wr' );
37 }
38
39 public function execute() {
40 $this->run();
41 }
42
43 public function executeGenerator( $resultPageSet ) {
44 $this->run( $resultPageSet );
45 }
46
47 /**
48 * @param $resultPageSet ApiPageSet
49 * @return void
50 */
51 private function run( $resultPageSet = null ) {
52 $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
53
54 $params = $this->extractRequestParams();
55
56 $user = $this->getWatchlistUser( $params );
57
58 $prop = array_flip( (array)$params['prop'] );
59 $show = array_flip( (array)$params['show'] );
60 if ( isset( $show['changed'] ) && isset( $show['!changed'] ) ) {
61 $this->dieUsageMsg( 'show' );
62 }
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', $user->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
72 if ( isset( $params['continue'] ) ) {
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 }
78 $ns = intval( $cont[0] );
79 $title = $this->getDB()->addQuotes( $cont[1] );
80 $op = $params['dir'] == 'ascending' ? '>' : '<';
81 $this->addWhere(
82 "wl_namespace $op $ns OR " .
83 "(wl_namespace = $ns AND " .
84 "wl_title $op= $title)"
85 );
86 }
87
88 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
89 // Don't ORDER BY wl_namespace if it's constant in the WHERE clause
90 if ( count( $params['namespace'] ) == 1 ) {
91 $this->addOption( 'ORDER BY', 'wl_title' . $sort );
92 } else {
93 $this->addOption( 'ORDER BY', array(
94 'wl_namespace' . $sort,
95 'wl_title' . $sort
96 ));
97 }
98 $this->addOption( 'LIMIT', $params['limit'] + 1 );
99 $res = $this->select( __METHOD__ );
100
101 $titles = array();
102 $count = 0;
103 foreach ( $res as $row ) {
104 if ( ++$count > $params['limit'] ) {
105 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
106 $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' . $row->wl_title );
107 break;
108 }
109 $t = Title::makeTitle( $row->wl_namespace, $row->wl_title );
110
111 if ( is_null( $resultPageSet ) ) {
112 $vals = array();
113 ApiQueryBase::addTitleInfo( $vals, $t );
114 if ( isset( $prop['changed'] ) && !is_null( $row->wl_notificationtimestamp ) )
115 {
116 $vals['changed'] = wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
117 }
118 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
119 if ( !$fit ) {
120 $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' . $row->wl_title );
121 break;
122 }
123 } else {
124 $titles[] = $t;
125 }
126 }
127 if ( is_null( $resultPageSet ) ) {
128 $this->getResult()->setIndexedTagName_internal( $this->getModuleName(), 'wr' );
129 } else {
130 $resultPageSet->populateFromTitles( $titles );
131 }
132 }
133
134 public function getAllowedParams() {
135 return array(
136 'continue' => null,
137 'namespace' => array(
138 ApiBase::PARAM_ISMULTI => true,
139 ApiBase::PARAM_TYPE => 'namespace'
140 ),
141 'limit' => array(
142 ApiBase::PARAM_DFLT => 10,
143 ApiBase::PARAM_TYPE => 'limit',
144 ApiBase::PARAM_MIN => 1,
145 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
146 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
147 ),
148 'prop' => array(
149 ApiBase::PARAM_ISMULTI => true,
150 ApiBase::PARAM_TYPE => array(
151 'changed',
152 )
153 ),
154 'show' => array(
155 ApiBase::PARAM_ISMULTI => true,
156 ApiBase::PARAM_TYPE => array(
157 'changed',
158 '!changed',
159 )
160 ),
161 'owner' => array(
162 ApiBase::PARAM_TYPE => 'user'
163 ),
164 'token' => array(
165 ApiBase::PARAM_TYPE => 'string'
166 ),
167 'dir' => array(
168 ApiBase::PARAM_DFLT => 'ascending',
169 ApiBase::PARAM_TYPE => array(
170 'ascending',
171 'descending'
172 ),
173 ),
174 );
175 }
176
177 public function getParamDescription() {
178 return array(
179 'continue' => 'When more results are available, use this to continue',
180 'namespace' => 'Only list pages in the given namespace(s)',
181 'limit' => 'How many total results to return per request',
182 'prop' => array(
183 'Which additional properties to get (non-generator mode only)',
184 ' changed - Adds timestamp of when the user was last notified about the edit',
185 ),
186 'show' => 'Only list items that meet these criteria',
187 'owner' => 'The name of the user whose watchlist you\'d like to access',
188 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist',
189 'dir' => 'Direction to sort the titles and namespaces in',
190 );
191 }
192
193 public function getResultProperties() {
194 return array(
195 '' => array(
196 'ns' => 'namespace',
197 'title' => 'string'
198 ),
199 'changed' => array(
200 'changed' => array(
201 ApiBase::PROP_TYPE => 'timestamp',
202 ApiBase::PROP_NULLABLE => true
203 )
204 )
205 );
206 }
207
208 public function getDescription() {
209 return "Get all pages on the logged in user's watchlist";
210 }
211
212 public function getPossibleErrors() {
213 return array_merge( parent::getPossibleErrors(), array(
214 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
215 array( 'show' ),
216 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
217 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
218 ) );
219 }
220
221 public function getExamples() {
222 return array(
223 'api.php?action=query&list=watchlistraw',
224 'api.php?action=query&generator=watchlistraw&gwrshow=changed&prop=revisions',
225 );
226 }
227
228 public function getVersion() {
229 return __CLASS__ . ': $Id$';
230 }
231 }