Merge "Http::getProxy() method to get proxy configuration"
[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( ApiQuery $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 ApiPageSet $resultPageSet
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( [ '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 $this->dieContinueUsageIf( count( $cont ) != 2 );
75 $ns = intval( $cont[0] );
76 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
77 $title = $this->getDB()->addQuotes( $cont[1] );
78 $op = $params['dir'] == 'ascending' ? '>' : '<';
79 $this->addWhere(
80 "wl_namespace $op $ns OR " .
81 "(wl_namespace = $ns AND " .
82 "wl_title $op= $title)"
83 );
84 }
85
86 if ( isset( $params['fromtitle'] ) ) {
87 list( $ns, $title ) = $this->prefixedTitlePartToKey( $params['fromtitle'] );
88 $title = $this->getDB()->addQuotes( $title );
89 $op = $params['dir'] == 'ascending' ? '>' : '<';
90 $this->addWhere(
91 "wl_namespace $op $ns OR " .
92 "(wl_namespace = $ns AND " .
93 "wl_title $op= $title)"
94 );
95 }
96
97 if ( isset( $params['totitle'] ) ) {
98 list( $ns, $title ) = $this->prefixedTitlePartToKey( $params['totitle'] );
99 $title = $this->getDB()->addQuotes( $title );
100 $op = $params['dir'] == 'ascending' ? '<' : '>'; // Reversed from above!
101 $this->addWhere(
102 "wl_namespace $op $ns OR " .
103 "(wl_namespace = $ns AND " .
104 "wl_title $op= $title)"
105 );
106 }
107
108 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
109 // Don't ORDER BY wl_namespace if it's constant in the WHERE clause
110 if ( count( $params['namespace'] ) == 1 ) {
111 $this->addOption( 'ORDER BY', 'wl_title' . $sort );
112 } else {
113 $this->addOption( 'ORDER BY', [
114 'wl_namespace' . $sort,
115 'wl_title' . $sort
116 ] );
117 }
118 $this->addOption( 'LIMIT', $params['limit'] + 1 );
119 $res = $this->select( __METHOD__ );
120
121 $titles = [];
122 $count = 0;
123 foreach ( $res as $row ) {
124 if ( ++$count > $params['limit'] ) {
125 // We've reached the one extra which shows that there are
126 // additional pages to be had. Stop here...
127 $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' . $row->wl_title );
128 break;
129 }
130 $t = Title::makeTitle( $row->wl_namespace, $row->wl_title );
131
132 if ( is_null( $resultPageSet ) ) {
133 $vals = [];
134 ApiQueryBase::addTitleInfo( $vals, $t );
135 if ( isset( $prop['changed'] ) && !is_null( $row->wl_notificationtimestamp ) ) {
136 $vals['changed'] = wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
137 }
138 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
139 if ( !$fit ) {
140 $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' . $row->wl_title );
141 break;
142 }
143 } else {
144 $titles[] = $t;
145 }
146 }
147 if ( is_null( $resultPageSet ) ) {
148 $this->getResult()->addIndexedTagName( $this->getModuleName(), 'wr' );
149 } else {
150 $resultPageSet->populateFromTitles( $titles );
151 }
152 }
153
154 public function getAllowedParams() {
155 return [
156 'continue' => [
157 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
158 ],
159 'namespace' => [
160 ApiBase::PARAM_ISMULTI => true,
161 ApiBase::PARAM_TYPE => 'namespace'
162 ],
163 'limit' => [
164 ApiBase::PARAM_DFLT => 10,
165 ApiBase::PARAM_TYPE => 'limit',
166 ApiBase::PARAM_MIN => 1,
167 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
168 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
169 ],
170 'prop' => [
171 ApiBase::PARAM_ISMULTI => true,
172 ApiBase::PARAM_TYPE => [
173 'changed',
174 ],
175 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
176 ],
177 'show' => [
178 ApiBase::PARAM_ISMULTI => true,
179 ApiBase::PARAM_TYPE => [
180 'changed',
181 '!changed',
182 ]
183 ],
184 'owner' => [
185 ApiBase::PARAM_TYPE => 'user'
186 ],
187 'token' => [
188 ApiBase::PARAM_TYPE => 'string'
189 ],
190 'dir' => [
191 ApiBase::PARAM_DFLT => 'ascending',
192 ApiBase::PARAM_TYPE => [
193 'ascending',
194 'descending'
195 ],
196 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
197 ],
198 'fromtitle' => [
199 ApiBase::PARAM_TYPE => 'string'
200 ],
201 'totitle' => [
202 ApiBase::PARAM_TYPE => 'string'
203 ],
204 ];
205 }
206
207 protected function getExamplesMessages() {
208 return [
209 'action=query&list=watchlistraw'
210 => 'apihelp-query+watchlistraw-example-simple',
211 'action=query&generator=watchlistraw&gwrshow=changed&prop=info'
212 => 'apihelp-query+watchlistraw-example-generator',
213 ];
214 }
215
216 public function getHelpUrls() {
217 return 'https://www.mediawiki.org/wiki/API:Watchlistraw';
218 }
219 }