MediaWiki 1.40.4
ApiQueryWatchlistRaw.php
Go to the documentation of this file.
1<?php
27
35
37 private $watchedItemQueryService;
38
40 private $contentLanguage;
41
43 private $namespaceInfo;
44
46 private $genderCache;
47
56 public function __construct(
57 ApiQuery $query,
58 $moduleName,
59 WatchedItemQueryService $watchedItemQueryService,
60 Language $contentLanguage,
61 NamespaceInfo $namespaceInfo,
62 GenderCache $genderCache
63 ) {
64 parent::__construct( $query, $moduleName, 'wr' );
65 $this->watchedItemQueryService = $watchedItemQueryService;
66 $this->contentLanguage = $contentLanguage;
67 $this->namespaceInfo = $namespaceInfo;
68 $this->genderCache = $genderCache;
69 }
70
71 public function execute() {
72 $this->run();
73 }
74
75 public function executeGenerator( $resultPageSet ) {
76 $this->run( $resultPageSet );
77 }
78
83 private function run( $resultPageSet = null ) {
84 $params = $this->extractRequestParams();
85
86 $user = $this->getWatchlistUser( $params );
87
88 $prop = array_fill_keys( (array)$params['prop'], true );
89 $show = array_fill_keys( (array)$params['show'], true );
90 if ( isset( $show[WatchedItemQueryService::FILTER_CHANGED] )
91 && isset( $show[WatchedItemQueryService::FILTER_NOT_CHANGED] )
92 ) {
93 $this->dieWithError( 'apierror-show' );
94 }
95
96 $options = [];
97 if ( $params['namespace'] ) {
98 $options['namespaceIds'] = $params['namespace'];
99 }
100 if ( isset( $show[WatchedItemQueryService::FILTER_CHANGED] ) ) {
101 $options['filter'] = WatchedItemQueryService::FILTER_CHANGED;
102 }
103 if ( isset( $show[WatchedItemQueryService::FILTER_NOT_CHANGED] ) ) {
105 }
106
107 if ( isset( $params['continue'] ) ) {
108 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
109 $options['startFrom'] = TitleValue::tryNew( $cont[0], $cont[1] );
110 $this->dieContinueUsageIf( !$options['startFrom'] );
111 }
112
113 if ( isset( $params['fromtitle'] ) ) {
114 $options['from'] = $this->parsePrefixedTitlePart( $params['fromtitle'] );
115 }
116
117 if ( isset( $params['totitle'] ) ) {
118 $options['until'] = $this->parsePrefixedTitlePart( $params['totitle'] );
119 }
120
121 $options['sort'] = WatchedItemStore::SORT_ASC;
122 if ( $params['dir'] === 'descending' ) {
123 $options['sort'] = WatchedItemStore::SORT_DESC;
124 }
125 $options['limit'] = $params['limit'] + 1;
126
127 $titles = [];
128 $count = 0;
129 $items = $this->watchedItemQueryService->getWatchedItemsForUser( $user, $options );
130
131 // Get gender information
132 if ( $items !== [] && $resultPageSet === null &&
133 $this->contentLanguage->needsGenderDistinction()
134 ) {
135 $usernames = [];
136 foreach ( $items as $item ) {
137 $linkTarget = $item->getTarget();
138 if ( $this->namespaceInfo->hasGenderDistinction( $linkTarget->getNamespace() ) ) {
139 $usernames[] = $linkTarget->getText();
140 }
141 }
142 if ( $usernames !== [] ) {
143 $this->genderCache->doQuery( $usernames, __METHOD__ );
144 }
145 }
146
147 foreach ( $items as $item ) {
148 $ns = $item->getTarget()->getNamespace();
149 $dbKey = $item->getTarget()->getDBkey();
150 if ( ++$count > $params['limit'] ) {
151 // We've reached the one extra which shows that there are
152 // additional pages to be had. Stop here...
153 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
154 break;
155 }
156 $t = Title::makeTitle( $ns, $dbKey );
157
158 if ( $resultPageSet === null ) {
159 $vals = [];
161 if ( isset( $prop['changed'] ) && $item->getNotificationTimestamp() !== null ) {
162 $vals['changed'] = wfTimestamp( TS_ISO_8601, $item->getNotificationTimestamp() );
163 }
164 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
165 if ( !$fit ) {
166 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
167 break;
168 }
169 } else {
170 $titles[] = $t;
171 }
172 }
173 if ( $resultPageSet === null ) {
174 $this->getResult()->addIndexedTagName( $this->getModuleName(), 'wr' );
175 } else {
176 $resultPageSet->populateFromTitles( $titles );
177 }
178 }
179
180 public function getAllowedParams() {
181 return [
182 'continue' => [
183 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
184 ],
185 'namespace' => [
186 ParamValidator::PARAM_ISMULTI => true,
187 ParamValidator::PARAM_TYPE => 'namespace'
188 ],
189 'limit' => [
190 ParamValidator::PARAM_DEFAULT => 10,
191 ParamValidator::PARAM_TYPE => 'limit',
192 IntegerDef::PARAM_MIN => 1,
193 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
194 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
195 ],
196 'prop' => [
197 ParamValidator::PARAM_ISMULTI => true,
198 ParamValidator::PARAM_TYPE => [
199 'changed',
200 ],
202 ],
203 'show' => [
204 ParamValidator::PARAM_ISMULTI => true,
205 ParamValidator::PARAM_TYPE => [
206 WatchedItemQueryService::FILTER_CHANGED,
207 WatchedItemQueryService::FILTER_NOT_CHANGED
208 ]
209 ],
210 'owner' => [
211 ParamValidator::PARAM_TYPE => 'user',
212 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ],
213 ],
214 'token' => [
215 ParamValidator::PARAM_TYPE => 'string',
216 ParamValidator::PARAM_SENSITIVE => true,
217 ],
218 'dir' => [
219 ParamValidator::PARAM_DEFAULT => 'ascending',
220 ParamValidator::PARAM_TYPE => [
221 'ascending',
222 'descending'
223 ],
224 ],
225 'fromtitle' => [
226 ParamValidator::PARAM_TYPE => 'string'
227 ],
228 'totitle' => [
229 ParamValidator::PARAM_TYPE => 'string'
230 ],
231 ];
232 }
233
234 protected function getExamplesMessages() {
235 return [
236 'action=query&list=watchlistraw'
237 => 'apihelp-query+watchlistraw-example-simple',
238 'action=query&generator=watchlistraw&gwrshow=changed&prop=info'
239 => 'apihelp-query+watchlistraw-example-generator',
240 ];
241 }
242
243 public function getHelpUrls() {
244 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlistraw';
245 }
246}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1460
getWatchlistUser( $params)
Gets the user for whom to get the watchlist.
Definition ApiBase.php:1190
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition ApiBase.php:1688
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1649
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:204
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:229
getResult()
Get the result object.
Definition ApiBase.php:637
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:773
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:166
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:231
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:506
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
parsePrefixedTitlePart( $titlePart, $defaultNamespace=NS_MAIN)
Convert an input title or title prefix into a TitleValue.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
This query action allows clients to retrieve a list of pages on the logged-in user's watchlist.
getHelpUrls()
Return links to more detailed help pages about the module.
getExamplesMessages()
Returns usage examples for this module.
__construct(ApiQuery $query, $moduleName, WatchedItemQueryService $watchedItemQueryService, Language $contentLanguage, NamespaceInfo $namespaceInfo, GenderCache $genderCache)
executeGenerator( $resultPageSet)
Execute this module as a generator.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
This is the main query class.
Definition ApiQuery.php:42
Caches user genders when needed to use correct namespace aliases.
Base class for language-specific code.
Definition Language.php:56
Type definition for user types.
Definition UserDef.php:27
Represents a title within MediaWiki.
Definition Title.php:82
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
static tryNew( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue, or returns null if the parameters are not valid.
Service for formatting and validating API parameters.
Type definition for integer types.