phpunit: Avoid use of deprecated getMock for PHPUnit 5 compat
[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 use MediaWiki\MediaWikiServices;
28
29 /**
30 * This query action allows clients to retrieve a list of pages
31 * on the logged-in user's watchlist.
32 *
33 * @ingroup API
34 */
35 class ApiQueryWatchlistRaw extends ApiQueryGeneratorBase {
36
37 public function __construct( ApiQuery $query, $moduleName ) {
38 parent::__construct( $query, $moduleName, 'wr' );
39 }
40
41 public function execute() {
42 $this->run();
43 }
44
45 public function executeGenerator( $resultPageSet ) {
46 $this->run( $resultPageSet );
47 }
48
49 /**
50 * @param ApiPageSet $resultPageSet
51 * @return void
52 */
53 private function run( $resultPageSet = null ) {
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[WatchedItemQueryService::FILTER_CHANGED] )
61 && isset( $show[WatchedItemQueryService::FILTER_NOT_CHANGED] )
62 ) {
63 $this->dieWithError( 'apierror-show' );
64 }
65
66 $options = [];
67 if ( $params['namespace'] ) {
68 $options['namespaceIds'] = $params['namespace'];
69 }
70 if ( isset( $show[WatchedItemQueryService::FILTER_CHANGED] ) ) {
71 $options['filter'] = WatchedItemQueryService::FILTER_CHANGED;
72 }
73 if ( isset( $show[WatchedItemQueryService::FILTER_NOT_CHANGED] ) ) {
74 $options['filter'] = WatchedItemQueryService::FILTER_NOT_CHANGED;
75 }
76
77 if ( isset( $params['continue'] ) ) {
78 $cont = explode( '|', $params['continue'] );
79 $this->dieContinueUsageIf( count( $cont ) != 2 );
80 $ns = intval( $cont[0] );
81 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
82 $title = $cont[1];
83 $options['startFrom'] = new TitleValue( $ns, $title );
84 }
85
86 if ( isset( $params['fromtitle'] ) ) {
87 list( $ns, $title ) = $this->prefixedTitlePartToKey( $params['fromtitle'] );
88 $options['from'] = new TitleValue( $ns, $title );
89 }
90
91 if ( isset( $params['totitle'] ) ) {
92 list( $ns, $title ) = $this->prefixedTitlePartToKey( $params['totitle'] );
93 $options['until'] = new TitleValue( $ns, $title );
94 }
95
96 $options['sort'] = WatchedItemStore::SORT_ASC;
97 if ( $params['dir'] === 'descending' ) {
98 $options['sort'] = WatchedItemStore::SORT_DESC;
99 }
100 $options['limit'] = $params['limit'] + 1;
101
102 $titles = [];
103 $count = 0;
104 $items = MediaWikiServices::getInstance()->getWatchedItemQueryService()
105 ->getWatchedItemsForUser( $user, $options );
106 foreach ( $items as $item ) {
107 $ns = $item->getLinkTarget()->getNamespace();
108 $dbKey = $item->getLinkTarget()->getDBkey();
109 if ( ++$count > $params['limit'] ) {
110 // We've reached the one extra which shows that there are
111 // additional pages to be had. Stop here...
112 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
113 break;
114 }
115 $t = Title::makeTitle( $ns, $dbKey );
116
117 if ( is_null( $resultPageSet ) ) {
118 $vals = [];
119 ApiQueryBase::addTitleInfo( $vals, $t );
120 if ( isset( $prop['changed'] ) && !is_null( $item->getNotificationTimestamp() ) ) {
121 $vals['changed'] = wfTimestamp( TS_ISO_8601, $item->getNotificationTimestamp() );
122 }
123 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
124 if ( !$fit ) {
125 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
126 break;
127 }
128 } else {
129 $titles[] = $t;
130 }
131 }
132 if ( is_null( $resultPageSet ) ) {
133 $this->getResult()->addIndexedTagName( $this->getModuleName(), 'wr' );
134 } else {
135 $resultPageSet->populateFromTitles( $titles );
136 }
137 }
138
139 public function getAllowedParams() {
140 return [
141 'continue' => [
142 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
143 ],
144 'namespace' => [
145 ApiBase::PARAM_ISMULTI => true,
146 ApiBase::PARAM_TYPE => 'namespace'
147 ],
148 'limit' => [
149 ApiBase::PARAM_DFLT => 10,
150 ApiBase::PARAM_TYPE => 'limit',
151 ApiBase::PARAM_MIN => 1,
152 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
153 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
154 ],
155 'prop' => [
156 ApiBase::PARAM_ISMULTI => true,
157 ApiBase::PARAM_TYPE => [
158 'changed',
159 ],
160 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
161 ],
162 'show' => [
163 ApiBase::PARAM_ISMULTI => true,
164 ApiBase::PARAM_TYPE => [
165 WatchedItemQueryService::FILTER_CHANGED,
166 WatchedItemQueryService::FILTER_NOT_CHANGED
167 ]
168 ],
169 'owner' => [
170 ApiBase::PARAM_TYPE => 'user'
171 ],
172 'token' => [
173 ApiBase::PARAM_TYPE => 'string'
174 ],
175 'dir' => [
176 ApiBase::PARAM_DFLT => 'ascending',
177 ApiBase::PARAM_TYPE => [
178 'ascending',
179 'descending'
180 ],
181 ],
182 'fromtitle' => [
183 ApiBase::PARAM_TYPE => 'string'
184 ],
185 'totitle' => [
186 ApiBase::PARAM_TYPE => 'string'
187 ],
188 ];
189 }
190
191 protected function getExamplesMessages() {
192 return [
193 'action=query&list=watchlistraw'
194 => 'apihelp-query+watchlistraw-example-simple',
195 'action=query&generator=watchlistraw&gwrshow=changed&prop=info'
196 => 'apihelp-query+watchlistraw-example-generator',
197 ];
198 }
199
200 public function getHelpUrls() {
201 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlistraw';
202 }
203 }