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