5 * Created on Oct 4, 2008
7 * Copyright © 2008 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
27 use MediaWiki\MediaWikiServices
;
30 * This query action allows clients to retrieve a list of pages
31 * on the logged-in user's watchlist.
35 class ApiQueryWatchlistRaw
extends ApiQueryGeneratorBase
{
37 public function __construct( ApiQuery
$query, $moduleName ) {
38 parent
::__construct( $query, $moduleName, 'wr' );
41 public function execute() {
45 public function executeGenerator( $resultPageSet ) {
46 $this->run( $resultPageSet );
50 * @param ApiPageSet $resultPageSet
53 private function run( $resultPageSet = null ) {
54 $params = $this->extractRequestParams();
56 $user = $this->getWatchlistUser( $params );
58 $prop = array_flip( (array)$params['prop'] );
59 $show = array_flip( (array)$params['show'] );
60 if ( isset( $show[WatchedItemQueryService
::FILTER_CHANGED
] )
61 && isset( $show[WatchedItemQueryService
::FILTER_NOT_CHANGED
] )
63 $this->dieWithError( 'apierror-show' );
67 if ( $params['namespace'] ) {
68 $options['namespaceIds'] = $params['namespace'];
70 if ( isset( $show[WatchedItemQueryService
::FILTER_CHANGED
] ) ) {
71 $options['filter'] = WatchedItemQueryService
::FILTER_CHANGED
;
73 if ( isset( $show[WatchedItemQueryService
::FILTER_NOT_CHANGED
] ) ) {
74 $options['filter'] = WatchedItemQueryService
::FILTER_NOT_CHANGED
;
77 if ( isset( $params['continue'] ) ) {
78 $cont = explode( '|', $params['continue'] );
79 $this->dieContinueUsageIf( count( $cont ) != 2 );
80 $ns = intval( $cont[0] );
81 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
83 $options['startFrom'] = new TitleValue( $ns, $title );
86 if ( isset( $params['fromtitle'] ) ) {
87 list( $ns, $title ) = $this->prefixedTitlePartToKey( $params['fromtitle'] );
88 $options['from'] = new TitleValue( $ns, $title );
91 if ( isset( $params['totitle'] ) ) {
92 list( $ns, $title ) = $this->prefixedTitlePartToKey( $params['totitle'] );
93 $options['until'] = new TitleValue( $ns, $title );
96 $options['sort'] = WatchedItemStore
::SORT_ASC
;
97 if ( $params['dir'] === 'descending' ) {
98 $options['sort'] = WatchedItemStore
::SORT_DESC
;
100 $options['limit'] = $params['limit'] +
1;
104 $items = MediaWikiServices
::getInstance()->getWatchedItemQueryService()
105 ->getWatchedItemsForUser( $user, $options );
106 foreach ( $items as $item ) {
107 $ns = $item->getLinkTarget()->getNamespace();
108 $dbKey = $item->getLinkTarget()->getDBkey();
109 if ( ++
$count > $params['limit'] ) {
110 // We've reached the one extra which shows that there are
111 // additional pages to be had. Stop here...
112 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
115 $t = Title
::makeTitle( $ns, $dbKey );
117 if ( is_null( $resultPageSet ) ) {
119 ApiQueryBase
::addTitleInfo( $vals, $t );
120 if ( isset( $prop['changed'] ) && !is_null( $item->getNotificationTimestamp() ) ) {
121 $vals['changed'] = wfTimestamp( TS_ISO_8601
, $item->getNotificationTimestamp() );
123 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
125 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
132 if ( is_null( $resultPageSet ) ) {
133 $this->getResult()->addIndexedTagName( $this->getModuleName(), 'wr' );
135 $resultPageSet->populateFromTitles( $titles );
139 public function getAllowedParams() {
142 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
145 ApiBase
::PARAM_ISMULTI
=> true,
146 ApiBase
::PARAM_TYPE
=> 'namespace'
149 ApiBase
::PARAM_DFLT
=> 10,
150 ApiBase
::PARAM_TYPE
=> 'limit',
151 ApiBase
::PARAM_MIN
=> 1,
152 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
153 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
156 ApiBase
::PARAM_ISMULTI
=> true,
157 ApiBase
::PARAM_TYPE
=> [
160 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
163 ApiBase
::PARAM_ISMULTI
=> true,
164 ApiBase
::PARAM_TYPE
=> [
165 WatchedItemQueryService
::FILTER_CHANGED
,
166 WatchedItemQueryService
::FILTER_NOT_CHANGED
170 ApiBase
::PARAM_TYPE
=> 'user'
173 ApiBase
::PARAM_TYPE
=> 'string'
176 ApiBase
::PARAM_DFLT
=> 'ascending',
177 ApiBase
::PARAM_TYPE
=> [
183 ApiBase
::PARAM_TYPE
=> 'string'
186 ApiBase
::PARAM_TYPE
=> 'string'
191 protected function getExamplesMessages() {
193 'action=query&list=watchlistraw'
194 => 'apihelp-query+watchlistraw-example-simple',
195 'action=query&generator=watchlistraw&gwrshow=changed&prop=info'
196 => 'apihelp-query+watchlistraw-example-generator',
200 public function getHelpUrls() {
201 return 'https://www.mediawiki.org/wiki/API:Watchlistraw';