FU r106752: use "media-" instead of "images-" in container names. Long live books...
[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()->strencode( $this->titleToKey( $cont[1] ) );
80 $this->addWhere(
81 "wl_namespace > '$ns' OR " .
82 "(wl_namespace = '$ns' AND " .
83 "wl_title >= '$title')"
84 );
85 }
86
87 // Don't ORDER BY wl_namespace if it's constant in the WHERE clause
88 if ( count( $params['namespace'] ) == 1 ) {
89 $this->addOption( 'ORDER BY', 'wl_title' );
90 } else {
91 $this->addOption( 'ORDER BY', 'wl_namespace, wl_title' );
92 }
93 $this->addOption( 'LIMIT', $params['limit'] + 1 );
94 $res = $this->select( __METHOD__ );
95
96 $titles = array();
97 $count = 0;
98 foreach ( $res as $row ) {
99 if ( ++$count > $params['limit'] ) {
100 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
101 $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' .
102 $this->keyToTitle( $row->wl_title ) );
103 break;
104 }
105 $t = Title::makeTitle( $row->wl_namespace, $row->wl_title );
106
107 if ( is_null( $resultPageSet ) ) {
108 $vals = array();
109 ApiQueryBase::addTitleInfo( $vals, $t );
110 if ( isset( $prop['changed'] ) && !is_null( $row->wl_notificationtimestamp ) )
111 {
112 $vals['changed'] = wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
113 }
114 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
115 if ( !$fit ) {
116 $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' .
117 $this->keyToTitle( $row->wl_title ) );
118 break;
119 }
120 } else {
121 $titles[] = $t;
122 }
123 }
124 if ( is_null( $resultPageSet ) ) {
125 $this->getResult()->setIndexedTagName_internal( $this->getModuleName(), 'wr' );
126 } else {
127 $resultPageSet->populateFromTitles( $titles );
128 }
129 }
130
131 public function getAllowedParams() {
132 return array(
133 'continue' => null,
134 'namespace' => array(
135 ApiBase::PARAM_ISMULTI => true,
136 ApiBase::PARAM_TYPE => 'namespace'
137 ),
138 'limit' => array(
139 ApiBase::PARAM_DFLT => 10,
140 ApiBase::PARAM_TYPE => 'limit',
141 ApiBase::PARAM_MIN => 1,
142 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
143 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
144 ),
145 'prop' => array(
146 ApiBase::PARAM_ISMULTI => true,
147 ApiBase::PARAM_TYPE => array(
148 'changed',
149 )
150 ),
151 'show' => array(
152 ApiBase::PARAM_ISMULTI => true,
153 ApiBase::PARAM_TYPE => array(
154 'changed',
155 '!changed',
156 )
157 ),
158 'owner' => array(
159 ApiBase::PARAM_TYPE => 'user'
160 ),
161 'token' => array(
162 ApiBase::PARAM_TYPE => 'string'
163 )
164 );
165 }
166
167 public function getParamDescription() {
168 return array(
169 'continue' => 'When more results are available, use this to continue',
170 'namespace' => 'Only list pages in the given namespace(s)',
171 'limit' => 'How many total results to return per request',
172 'prop' => array(
173 'Which additional properties to get (non-generator mode only)',
174 ' changed - Adds timestamp of when the user was last notified about the edit',
175 ),
176 'show' => 'Only list items that meet these criteria',
177 'owner' => 'The name of the user whose watchlist you\'d like to access',
178 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist',
179 );
180 }
181
182 public function getDescription() {
183 return "Get all pages on the logged in user's watchlist";
184 }
185
186 public function getPossibleErrors() {
187 return array_merge( parent::getPossibleErrors(), array(
188 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
189 array( 'show' ),
190 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
191 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
192 ) );
193 }
194
195 public function getExamples() {
196 return array(
197 'api.php?action=query&list=watchlistraw',
198 'api.php?action=query&generator=watchlistraw&gwrshow=changed&prop=revisions',
199 );
200 }
201
202 public function getVersion() {
203 return __CLASS__ . ': $Id$';
204 }
205 }