MediaWiki  master
ApiQueryWatchlistRaw.php
Go to the documentation of this file.
1 <?php
25 
33 
38  public function __construct( ApiQuery $query, $moduleName ) {
39  parent::__construct( $query, $moduleName, 'wr' );
40  }
41 
42  public function execute() {
43  $this->run();
44  }
45 
46  public function executeGenerator( $resultPageSet ) {
47  $this->run( $resultPageSet );
48  }
49 
54  private function run( $resultPageSet = null ) {
55  $params = $this->extractRequestParams();
56 
57  $user = $this->getWatchlistUser( $params );
58 
59  $prop = array_flip( (array)$params['prop'] );
60  $show = array_flip( (array)$params['show'] );
61  if ( isset( $show[WatchedItemQueryService::FILTER_CHANGED] )
63  ) {
64  $this->dieWithError( 'apierror-show' );
65  }
66 
67  $options = [];
68  if ( $params['namespace'] ) {
69  $options['namespaceIds'] = $params['namespace'];
70  }
71  if ( isset( $show[WatchedItemQueryService::FILTER_CHANGED] ) ) {
72  $options['filter'] = WatchedItemQueryService::FILTER_CHANGED;
73  }
74  if ( isset( $show[WatchedItemQueryService::FILTER_NOT_CHANGED] ) ) {
76  }
77 
78  if ( isset( $params['continue'] ) ) {
79  $cont = explode( '|', $params['continue'] );
80  $this->dieContinueUsageIf( count( $cont ) != 2 );
81  $ns = (int)$cont[0];
82  $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
83  $title = $cont[1];
84  $options['startFrom'] = TitleValue::tryNew( $ns, $title );
85  $this->dieContinueUsageIf( !$options['startFrom'] );
86  }
87 
88  if ( isset( $params['fromtitle'] ) ) {
89  $options['from'] = $this->parsePrefixedTitlePart( $params['fromtitle'] );
90  }
91 
92  if ( isset( $params['totitle'] ) ) {
93  $options['until'] = $this->parsePrefixedTitlePart( $params['totitle'] );
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  $services = MediaWikiServices::getInstance();
105  $items = $services->getWatchedItemQueryService()
106  ->getWatchedItemsForUser( $user, $options );
107 
108  // Get gender information
109  if ( $items !== [] && $resultPageSet === null &&
110  $services->getContentLanguage()->needsGenderDistinction()
111  ) {
112  $nsInfo = $services->getNamespaceInfo();
113  $usernames = [];
114  foreach ( $items as $item ) {
115  $linkTarget = $item->getLinkTarget();
116  if ( $nsInfo->hasGenderDistinction( $linkTarget->getNamespace() ) ) {
117  $usernames[] = $linkTarget->getText();
118  }
119  }
120  if ( $usernames !== [] ) {
121  $services->getGenderCache()->doQuery( $usernames, __METHOD__ );
122  }
123  }
124 
125  foreach ( $items as $item ) {
126  $ns = $item->getLinkTarget()->getNamespace();
127  $dbKey = $item->getLinkTarget()->getDBkey();
128  if ( ++$count > $params['limit'] ) {
129  // We've reached the one extra which shows that there are
130  // additional pages to be had. Stop here...
131  $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
132  break;
133  }
134  $t = Title::makeTitle( $ns, $dbKey );
135 
136  if ( $resultPageSet === null ) {
137  $vals = [];
138  ApiQueryBase::addTitleInfo( $vals, $t );
139  if ( isset( $prop['changed'] ) && $item->getNotificationTimestamp() !== null ) {
140  $vals['changed'] = wfTimestamp( TS_ISO_8601, $item->getNotificationTimestamp() );
141  }
142  $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
143  if ( !$fit ) {
144  $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
145  break;
146  }
147  } else {
148  $titles[] = $t;
149  }
150  }
151  if ( $resultPageSet === null ) {
152  $this->getResult()->addIndexedTagName( $this->getModuleName(), 'wr' );
153  } else {
154  $resultPageSet->populateFromTitles( $titles );
155  }
156  }
157 
158  public function getAllowedParams() {
159  return [
160  'continue' => [
161  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
162  ],
163  'namespace' => [
164  ApiBase::PARAM_ISMULTI => true,
165  ApiBase::PARAM_TYPE => 'namespace'
166  ],
167  'limit' => [
168  ApiBase::PARAM_DFLT => 10,
169  ApiBase::PARAM_TYPE => 'limit',
170  ApiBase::PARAM_MIN => 1,
173  ],
174  'prop' => [
175  ApiBase::PARAM_ISMULTI => true,
177  'changed',
178  ],
180  ],
181  'show' => [
182  ApiBase::PARAM_ISMULTI => true,
186  ]
187  ],
188  'owner' => [
189  ApiBase::PARAM_TYPE => 'user',
190  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ],
191  ],
192  'token' => [
193  ApiBase::PARAM_TYPE => 'string',
194  ApiBase::PARAM_SENSITIVE => true,
195  ],
196  'dir' => [
197  ApiBase::PARAM_DFLT => 'ascending',
199  'ascending',
200  'descending'
201  ],
202  ],
203  'fromtitle' => [
204  ApiBase::PARAM_TYPE => 'string'
205  ],
206  'totitle' => [
207  ApiBase::PARAM_TYPE => 'string'
208  ],
209  ];
210  }
211 
212  protected function getExamplesMessages() {
213  return [
214  'action=query&list=watchlistraw'
215  => 'apihelp-query+watchlistraw-example-simple',
216  'action=query&generator=watchlistraw&gwrshow=changed&prop=info'
217  => 'apihelp-query+watchlistraw-example-generator',
218  ];
219  }
220 
221  public function getHelpUrls() {
222  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlistraw';
223  }
224 }
ApiQueryWatchlistRaw\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryWatchlistRaw.php:158
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:172
ApiQueryWatchlistRaw\__construct
__construct(ApiQuery $query, $moduleName)
Definition: ApiQueryWatchlistRaw.php:38
ApiQueryWatchlistRaw\executeGenerator
executeGenerator( $resultPageSet)
Execute this module as a generator.
Definition: ApiQueryWatchlistRaw.php:46
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1384
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:1832
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:72
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:561
ApiQueryWatchlistRaw\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryWatchlistRaw.php:42
ApiQueryWatchlistRaw
This query action allows clients to retrieve a list of pages on the logged-in user's watchlist.
Definition: ApiQueryWatchlistRaw.php:32
ApiBase\PARAM_SENSITIVE
const PARAM_SENSITIVE
Definition: ApiBase.php:83
Wikimedia\ParamValidator\ParamValidator::TypeDef\UserDef
Type definition for user types.
Definition: UserDef.php:25
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
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:55
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:163
WatchedItemStoreInterface\SORT_DESC
const SORT_DESC
Definition: WatchedItemStoreInterface.php:40
ApiBase\PARAM_MAX
const PARAM_MAX
Definition: ApiBase.php:73
ApiQueryWatchlistRaw\run
run( $resultPageSet=null)
Definition: ApiQueryWatchlistRaw.php:54
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:720
$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:626
ApiBase\getWatchlistUser
getWatchlistUser( $params)
Gets the user for whom to get the watchlist.
Definition: ApiBase.php:1136
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1567
ApiQueryWatchlistRaw\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryWatchlistRaw.php:221
WatchedItemStoreInterface\SORT_ASC
const SORT_ASC
Definition: WatchedItemStoreInterface.php:35
ApiQueryWatchlistRaw\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryWatchlistRaw.php:212
ApiQueryGeneratorBase
Stable to extend.
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:54
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
$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
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:466