Single messages can be passed as strings to Api::dieUsageMsg()
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * This query action allows clients to retrieve a list of pages
34 * on the logged-in user's watchlist.
35 *
36 * @ingroup API
37 */
38 class ApiQueryWatchlistRaw extends ApiQueryGeneratorBase {
39
40 public function __construct( $query, $moduleName ) {
41 parent::__construct( $query, $moduleName, 'wr' );
42 }
43
44 public function execute() {
45 $this->run();
46 }
47
48 public function executeGenerator( $resultPageSet ) {
49 $this->run( $resultPageSet );
50 }
51
52 /**
53 * @param $resultPageSet ApiPageSet
54 * @return void
55 */
56 private function run( $resultPageSet = null ) {
57 $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
58
59 $params = $this->extractRequestParams();
60
61 $user = $this->getWatchlistUser( $params );
62
63 $prop = array_flip( (array)$params['prop'] );
64 $show = array_flip( (array)$params['show'] );
65 if ( isset( $show['changed'] ) && isset( $show['!changed'] ) ) {
66 $this->dieUsageMsg( 'show' );
67 }
68
69 $this->addTables( 'watchlist' );
70 $this->addFields( array( 'wl_namespace', 'wl_title' ) );
71 $this->addFieldsIf( 'wl_notificationtimestamp', isset( $prop['changed'] ) );
72 $this->addWhereFld( 'wl_user', $user->getId() );
73 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
74 $this->addWhereIf( 'wl_notificationtimestamp IS NOT NULL', isset( $show['changed'] ) );
75 $this->addWhereIf( 'wl_notificationtimestamp IS NULL', isset( $show['!changed'] ) );
76
77 if ( isset( $params['continue'] ) ) {
78 $cont = explode( '|', $params['continue'] );
79 if ( count( $cont ) != 2 ) {
80 $this->dieUsage( "Invalid continue param. You should pass the " .
81 "original value returned by the previous query", "_badcontinue" );
82 }
83 $ns = intval( $cont[0] );
84 $title = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
85 $this->addWhere(
86 "wl_namespace > '$ns' OR " .
87 "(wl_namespace = '$ns' AND " .
88 "wl_title >= '$title')"
89 );
90 }
91
92 // Don't ORDER BY wl_namespace if it's constant in the WHERE clause
93 if ( count( $params['namespace'] ) == 1 ) {
94 $this->addOption( 'ORDER BY', 'wl_title' );
95 } else {
96 $this->addOption( 'ORDER BY', 'wl_namespace, wl_title' );
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 . '|' .
107 $this->keyToTitle( $row->wl_title ) );
108 break;
109 }
110 $t = Title::makeTitle( $row->wl_namespace, $row->wl_title );
111
112 if ( is_null( $resultPageSet ) ) {
113 $vals = array();
114 ApiQueryBase::addTitleInfo( $vals, $t );
115 if ( isset( $prop['changed'] ) && !is_null( $row->wl_notificationtimestamp ) )
116 {
117 $vals['changed'] = wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
118 }
119 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
120 if ( !$fit ) {
121 $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' .
122 $this->keyToTitle( $row->wl_title ) );
123 break;
124 }
125 } else {
126 $titles[] = $t;
127 }
128 }
129 if ( is_null( $resultPageSet ) ) {
130 $this->getResult()->setIndexedTagName_internal( $this->getModuleName(), 'wr' );
131 } else {
132 $resultPageSet->populateFromTitles( $titles );
133 }
134 }
135
136 public function getAllowedParams() {
137 return array(
138 'continue' => null,
139 'namespace' => array(
140 ApiBase::PARAM_ISMULTI => true,
141 ApiBase::PARAM_TYPE => 'namespace'
142 ),
143 'limit' => array(
144 ApiBase::PARAM_DFLT => 10,
145 ApiBase::PARAM_TYPE => 'limit',
146 ApiBase::PARAM_MIN => 1,
147 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
148 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
149 ),
150 'prop' => array(
151 ApiBase::PARAM_ISMULTI => true,
152 ApiBase::PARAM_TYPE => array(
153 'changed',
154 )
155 ),
156 'show' => array(
157 ApiBase::PARAM_ISMULTI => true,
158 ApiBase::PARAM_TYPE => array(
159 'changed',
160 '!changed',
161 )
162 ),
163 'owner' => array(
164 ApiBase::PARAM_TYPE => 'user'
165 ),
166 'token' => array(
167 ApiBase::PARAM_TYPE => 'string'
168 )
169 );
170 }
171
172 public function getParamDescription() {
173 return array(
174 'continue' => 'When more results are available, use this to continue',
175 'namespace' => 'Only list pages in the given namespace(s)',
176 'limit' => 'How many total results to return per request',
177 'prop' => array(
178 'Which additional properties to get (non-generator mode only)',
179 ' changed - Adds timestamp of when the user was last notified about the edit',
180 ),
181 'show' => 'Only list items that meet these criteria',
182 'owner' => 'The name of the user whose watchlist you\'d like to access',
183 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist',
184 );
185 }
186
187 public function getDescription() {
188 return "Get all pages on the logged in user's watchlist";
189 }
190
191 public function getPossibleErrors() {
192 return array_merge( parent::getPossibleErrors(), array(
193 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
194 array( 'show' ),
195 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
196 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
197 ) );
198 }
199
200 protected function getExamples() {
201 return array(
202 'api.php?action=query&list=watchlistraw',
203 'api.php?action=query&generator=watchlistraw&gwrshow=changed&prop=revisions',
204 );
205 }
206
207 public function getVersion() {
208 return __CLASS__ . ': $Id$';
209 }
210 }