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