Localisation updates for core and extension messages from translatewiki.net (2010...
[lhc/web/wiklou.git] / includes / api / ApiQueryWatchlistRaw.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on Oct 4, 2008
6 *
7 * Copyright © 2008 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 private function run( $resultPageSet = null ) {
53 $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
54
55 $params = $this->extractRequestParams();
56
57 $user = $this->getWatchlistUser( $params );
58
59 $prop = array_flip( (array)$params['prop'] );
60 $show = array_flip( (array)$params['show'] );
61 if ( isset( $show['changed'] ) && isset( $show['!changed'] ) ) {
62 $this->dieUsageMsg( array( 'show' ) );
63 }
64
65 $this->addTables( 'watchlist' );
66 $this->addFields( array( 'wl_namespace', 'wl_title' ) );
67 $this->addFieldsIf( 'wl_notificationtimestamp', isset( $prop['changed'] ) );
68 $this->addWhereFld( 'wl_user', $user->getId() );
69 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
70 $this->addWhereIf( 'wl_notificationtimestamp IS NOT NULL', isset( $show['changed'] ) );
71 $this->addWhereIf( 'wl_notificationtimestamp IS NULL', isset( $show['!changed'] ) );
72
73 if ( isset( $params['continue'] ) ) {
74 $cont = explode( '|', $params['continue'] );
75 if ( count( $cont ) != 2 ) {
76 $this->dieUsage( "Invalid continue param. You should pass the " .
77 "original value returned by the previous query", "_badcontinue" );
78 }
79 $ns = intval( $cont[0] );
80 $title = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
81 $this->addWhere(
82 "wl_namespace > '$ns' OR " .
83 "(wl_namespace = '$ns' AND " .
84 "wl_title >= '$title')"
85 );
86 }
87
88 // Don't ORDER BY wl_namespace if it's constant in the WHERE clause
89 if ( count( $params['namespace'] ) == 1 ) {
90 $this->addOption( 'ORDER BY', 'wl_title' );
91 } else {
92 $this->addOption( 'ORDER BY', 'wl_namespace, wl_title' );
93 }
94 $this->addOption( 'LIMIT', $params['limit'] + 1 );
95 $res = $this->select( __METHOD__ );
96
97 $titles = array();
98 $count = 0;
99 foreach ( $res as $row ) {
100 if ( ++$count > $params['limit'] ) {
101 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
102 $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' .
103 $this->keyToTitle( $row->wl_title ) );
104 break;
105 }
106 $t = Title::makeTitle( $row->wl_namespace, $row->wl_title );
107
108 if ( is_null( $resultPageSet ) ) {
109 $vals = array();
110 ApiQueryBase::addTitleInfo( $vals, $t );
111 if ( isset( $prop['changed'] ) && !is_null( $row->wl_notificationtimestamp ) )
112 {
113 $vals['changed'] = wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
114 }
115 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
116 if ( !$fit ) {
117 $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' .
118 $this->keyToTitle( $row->wl_title ) );
119 break;
120 }
121 } else {
122 $titles[] = $t;
123 }
124 }
125 if ( is_null( $resultPageSet ) ) {
126 $this->getResult()->setIndexedTagName_internal( $this->getModuleName(), 'wr' );
127 } else {
128 $resultPageSet->populateFromTitles( $titles );
129 }
130 }
131
132 public function getAllowedParams() {
133 return array(
134 'continue' => null,
135 'namespace' => array(
136 ApiBase::PARAM_ISMULTI => true,
137 ApiBase::PARAM_TYPE => 'namespace'
138 ),
139 'limit' => array(
140 ApiBase::PARAM_DFLT => 10,
141 ApiBase::PARAM_TYPE => 'limit',
142 ApiBase::PARAM_MIN => 1,
143 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
144 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
145 ),
146 'prop' => array(
147 ApiBase::PARAM_ISMULTI => true,
148 ApiBase::PARAM_TYPE => array(
149 'changed',
150 )
151 ),
152 'show' => array(
153 ApiBase::PARAM_ISMULTI => true,
154 ApiBase::PARAM_TYPE => array(
155 'changed',
156 '!changed',
157 )
158 ),
159 'owner' => array(
160 ApiBase::PARAM_TYPE => 'user'
161 ),
162 'token' => array(
163 ApiBase::PARAM_TYPE => 'string'
164 )
165 );
166 }
167
168 public function getParamDescription() {
169 return array(
170 'continue' => 'When more results are available, use this to continue',
171 'namespace' => 'Only list pages in the given namespace(s)',
172 'limit' => 'How many total results to return per request',
173 'prop' => array(
174 'Which additional properties to get (non-generator mode only)',
175 ' changed - Adds timestamp of when the user was last notified about the edit',
176 ),
177 'show' => 'Only list items that meet these criteria',
178 'owner' => 'The name of the user whose watchlist you\'d like to access',
179 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist',
180 );
181 }
182
183 public function getDescription() {
184 return "Get all pages on the logged in user's watchlist";
185 }
186
187 public function getPossibleErrors() {
188 return array_merge( parent::getPossibleErrors(), array(
189 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
190 array( 'show' ),
191 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
192 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
193 ) );
194 }
195
196 protected function getExamples() {
197 return array(
198 'api.php?action=query&list=watchlistraw',
199 'api.php?action=query&generator=watchlistraw&gwrshow=changed&prop=revisions',
200 );
201 }
202
203 public function getVersion() {
204 return __CLASS__ . ': $Id$';
205 }
206 }