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