Correct the address of the FSF in some of the GPL headers
[lhc/web/wiklou.git] / includes / api / ApiQueryWatchlistRaw.php
1 <?php
2
3 /**
4 * Created on Oct 4, 2008
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2008 Roan Kattouw <Firstname>.<Lastname>@home.nl
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * This query action allows clients to retrieve a list of pages
33 * on the logged-in user's watchlist.
34 *
35 * @ingroup API
36 */
37 class ApiQueryWatchlistRaw extends ApiQueryGeneratorBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'wr' );
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
49 }
50
51 private function run( $resultPageSet = null ) {
52 $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
53
54 $params = $this->extractRequestParams();
55
56 $user = ApiQueryWatchlist::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( array( '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 $db = $this->getDB();
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' => 'Which additional properties to get (non-generator mode only)',
174 'show' => 'Only list items that meet these criteria',
175 'owner' => 'The name of the user whose watchlist you\'d like to access',
176 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist',
177 );
178 }
179
180 public function getDescription() {
181 return "Get all pages on the logged in user's watchlist";
182 }
183
184 public function getPossibleErrors() {
185 return array_merge( parent::getPossibleErrors(), array(
186 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
187 array( 'show' ),
188 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
189 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
190 ) );
191 }
192
193 protected function getExamples() {
194 return array(
195 'api.php?action=query&list=watchlistraw',
196 'api.php?action=query&generator=watchlistraw&gwrshow=changed&prop=revisions',
197 );
198 }
199
200 public function getVersion() {
201 return __CLASS__ . ': $Id$';
202 }
203 }