MediaWiki 1.41.2
ApiQueryWatchlistRaw.php
Go to the documentation of this file.
1<?php
29
37
38 private WatchedItemQueryService $watchedItemQueryService;
39 private Language $contentLanguage;
40 private NamespaceInfo $namespaceInfo;
41 private GenderCache $genderCache;
42
51 public function __construct(
52 ApiQuery $query,
53 $moduleName,
54 WatchedItemQueryService $watchedItemQueryService,
55 Language $contentLanguage,
56 NamespaceInfo $namespaceInfo,
57 GenderCache $genderCache
58 ) {
59 parent::__construct( $query, $moduleName, 'wr' );
60 $this->watchedItemQueryService = $watchedItemQueryService;
61 $this->contentLanguage = $contentLanguage;
62 $this->namespaceInfo = $namespaceInfo;
63 $this->genderCache = $genderCache;
64 }
65
66 public function execute() {
67 $this->run();
68 }
69
70 public function executeGenerator( $resultPageSet ) {
71 $this->run( $resultPageSet );
72 }
73
78 private function run( $resultPageSet = null ) {
79 $params = $this->extractRequestParams();
80
81 $user = $this->getWatchlistUser( $params );
82
83 $prop = array_fill_keys( (array)$params['prop'], true );
84 $show = array_fill_keys( (array)$params['show'], true );
85 if ( isset( $show[WatchedItemQueryService::FILTER_CHANGED] )
86 && isset( $show[WatchedItemQueryService::FILTER_NOT_CHANGED] )
87 ) {
88 $this->dieWithError( 'apierror-show' );
89 }
90
91 $options = [];
92 if ( $params['namespace'] ) {
93 $options['namespaceIds'] = $params['namespace'];
94 }
95 if ( isset( $show[WatchedItemQueryService::FILTER_CHANGED] ) ) {
96 $options['filter'] = WatchedItemQueryService::FILTER_CHANGED;
97 }
98 if ( isset( $show[WatchedItemQueryService::FILTER_NOT_CHANGED] ) ) {
100 }
101
102 if ( isset( $params['continue'] ) ) {
103 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
104 $options['startFrom'] = TitleValue::tryNew( $cont[0], $cont[1] );
105 $this->dieContinueUsageIf( !$options['startFrom'] );
106 }
107
108 if ( isset( $params['fromtitle'] ) ) {
109 $options['from'] = $this->parsePrefixedTitlePart( $params['fromtitle'] );
110 }
111
112 if ( isset( $params['totitle'] ) ) {
113 $options['until'] = $this->parsePrefixedTitlePart( $params['totitle'] );
114 }
115
116 $options['sort'] = WatchedItemStore::SORT_ASC;
117 if ( $params['dir'] === 'descending' ) {
118 $options['sort'] = WatchedItemStore::SORT_DESC;
119 }
120 $options['limit'] = $params['limit'] + 1;
121
122 $titles = [];
123 $count = 0;
124 $items = $this->watchedItemQueryService->getWatchedItemsForUser( $user, $options );
125
126 // Get gender information
127 if ( $items !== [] && $resultPageSet === null &&
128 $this->contentLanguage->needsGenderDistinction()
129 ) {
130 $usernames = [];
131 foreach ( $items as $item ) {
132 $linkTarget = $item->getTarget();
133 if ( $this->namespaceInfo->hasGenderDistinction( $linkTarget->getNamespace() ) ) {
134 $usernames[] = $linkTarget->getText();
135 }
136 }
137 if ( $usernames !== [] ) {
138 $this->genderCache->doQuery( $usernames, __METHOD__ );
139 }
140 }
141
142 foreach ( $items as $item ) {
143 $ns = $item->getTarget()->getNamespace();
144 $dbKey = $item->getTarget()->getDBkey();
145 if ( ++$count > $params['limit'] ) {
146 // We've reached the one extra which shows that there are
147 // additional pages to be had. Stop here...
148 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
149 break;
150 }
151 $t = Title::makeTitle( $ns, $dbKey );
152
153 if ( $resultPageSet === null ) {
154 $vals = [];
155 ApiQueryBase::addTitleInfo( $vals, $t );
156 if ( isset( $prop['changed'] ) && $item->getNotificationTimestamp() !== null ) {
157 $vals['changed'] = wfTimestamp( TS_ISO_8601, $item->getNotificationTimestamp() );
158 }
159 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
160 if ( !$fit ) {
161 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
162 break;
163 }
164 } else {
165 $titles[] = $t;
166 }
167 }
168 if ( $resultPageSet === null ) {
169 $this->getResult()->addIndexedTagName( $this->getModuleName(), 'wr' );
170 } else {
171 $resultPageSet->populateFromTitles( $titles );
172 }
173 }
174
175 public function getAllowedParams() {
176 return [
177 'continue' => [
178 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
179 ],
180 'namespace' => [
181 ParamValidator::PARAM_ISMULTI => true,
182 ParamValidator::PARAM_TYPE => 'namespace'
183 ],
184 'limit' => [
185 ParamValidator::PARAM_DEFAULT => 10,
186 ParamValidator::PARAM_TYPE => 'limit',
187 IntegerDef::PARAM_MIN => 1,
188 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
189 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
190 ],
191 'prop' => [
192 ParamValidator::PARAM_ISMULTI => true,
193 ParamValidator::PARAM_TYPE => [
194 'changed',
195 ],
197 ],
198 'show' => [
199 ParamValidator::PARAM_ISMULTI => true,
200 ParamValidator::PARAM_TYPE => [
201 WatchedItemQueryService::FILTER_CHANGED,
202 WatchedItemQueryService::FILTER_NOT_CHANGED
203 ]
204 ],
205 'owner' => [
206 ParamValidator::PARAM_TYPE => 'user',
207 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ],
208 ],
209 'token' => [
210 ParamValidator::PARAM_TYPE => 'string',
211 ParamValidator::PARAM_SENSITIVE => true,
212 ],
213 'dir' => [
214 ParamValidator::PARAM_DEFAULT => 'ascending',
215 ParamValidator::PARAM_TYPE => [
216 'ascending',
217 'descending'
218 ],
219 ],
220 'fromtitle' => [
221 ParamValidator::PARAM_TYPE => 'string'
222 ],
223 'totitle' => [
224 ParamValidator::PARAM_TYPE => 'string'
225 ],
226 ];
227 }
228
229 protected function getExamplesMessages() {
230 return [
231 'action=query&list=watchlistraw'
232 => 'apihelp-query+watchlistraw-example-simple',
233 'action=query&generator=watchlistraw&gwrshow=changed&prop=info'
234 => 'apihelp-query+watchlistraw-example-generator',
235 ];
236 }
237
238 public function getHelpUrls() {
239 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlistraw';
240 }
241}
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:1515
getWatchlistUser( $params)
Gets the user for whom to get the watchlist.
Definition ApiBase.php:1228
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition ApiBase.php:1745
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1706
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:209
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:234
getResult()
Get the result object.
Definition ApiBase.php:667
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:807
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:169
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:236
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:528
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:43
Look up "gender" user preference.
Base class for language-specific code.
Definition Language.php:63
Type definition for user types.
Definition UserDef.php:27
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Represents the target of a wiki link.
Represents a title within MediaWiki.
Definition Title.php:76
Service for formatting and validating API parameters.
Type definition for integer types.