21use Wikimedia\Timestamp\TimestampFormat as TS;
33 private $watchlistModule =
null;
35 private $linkToSections =
false;
42 parent::__construct( $main, $action );
61 '@phan-var array<string,class-string<ChannelFeed>> $feedClasses';
71 if ( !isset( $feedClasses[$params[
'feedformat']] ) ) {
76 $endTime =
wfTimestamp( TS::MW, time() - (
int)$params[
'hours'] * 60 * 60 );
82 'siprop' =>
'general',
83 'list' =>
'watchlist',
84 'wlprop' =>
'title|user|comment|timestamp|ids|loginfo',
90 if ( $params[
'wlowner'] !==
null ) {
91 $fauxReqArr[
'wlowner'] = $params[
'wlowner'];
93 if ( $params[
'wltoken'] !==
null ) {
94 $fauxReqArr[
'wltoken'] = $params[
'wltoken'];
96 if ( $params[
'wlexcludeuser'] !==
null ) {
97 $fauxReqArr[
'wlexcludeuser'] = $params[
'wlexcludeuser'];
99 if ( $params[
'wlshow'] !==
null ) {
100 $fauxReqArr[
'wlshow'] = ParamValidator::implodeMultiValue( $params[
'wlshow'] );
102 if ( $params[
'wltype'] !==
null ) {
103 $fauxReqArr[
'wltype'] = ParamValidator::implodeMultiValue( $params[
'wltype'] );
108 if ( $params[
'linktosections'] ) {
109 $this->linkToSections =
true;
113 if ( $params[
'allrev'] ) {
114 $fauxReqArr[
'wlallrev'] =
'';
119 $module =
new ApiMain( $fauxReq );
122 $data = $module->getResult()->getResultData( [
'query',
'watchlist' ] );
123 foreach ( (array)$data as $key => $info ) {
127 $feedItem = $this->createFeedItem( $info );
129 $feedItems[] = $feedItem;
133 $msg = $this->
msg(
'watchlist' )->inContentLanguage()->text();
139 $feed =
new $feedClasses[$params[
'feedformat']] (
141 htmlspecialchars( $msg ),
146 }
catch ( Exception $e ) {
148 $this->
getMain()->setCacheMaxAge( 0 );
152 $this->
msg(
'watchlist' )->inContentLanguage()->text() .
156 $feedFormat = $params[
'feedformat'] ??
'rss';
157 $msg = $this->
msg(
'watchlist' )->inContentLanguage()->escaped();
158 $feed =
new $feedClasses[$feedFormat]( $feedTitle, $msg, $feedUrl );
161 foreach ( $e->getStatusValue()->getMessages() as $msg ) {
165 $errorTitle = $this->
msg(
'api-feed-error-title', $msg->getApiCode() )->text();
166 $errorText = $msg->text();
167 $feedItems[] =
new FeedItem( $errorTitle, $errorText,
'',
'',
'' );
171 $errorCode =
'internal_api_error';
172 $errorTitle = $this->
msg(
'api-feed-error-title', $errorCode )->text();
173 $errorText = $e->getMessage();
174 $feedItems[] =
new FeedItem( $errorTitle, $errorText,
'',
'',
'' );
185 private function createFeedItem( $info ) {
186 if ( !isset( $info[
'title'] ) ) {
191 $titleStr = $info[
'title'];
192 $title = Title::newFromText( $titleStr );
194 if ( !$title || $title->isExternal() ) {
197 if ( isset( $info[
'pageid'] ) ) {
198 $title = Title::newFromID( $info[
'pageid'] );
199 $curidParam = [
'curid' => $info[
'pageid'] ];
201 if ( !$title || $title->isExternal() ) {
205 if ( isset( $info[
'revid'] ) ) {
206 if ( $info[
'revid'] === 0 && isset( $info[
'logid'] ) ) {
208 $titleUrl = $logTitle->getFullURL( [
'logid' => $info[
'logid'] ] );
210 $titleUrl = $title->getFullURL( [
'diff' => $info[
'revid'] ] );
213 $titleUrl = $title->getFullURL( $curidParam );
215 $comment = $info[
'comment'] ??
null;
221 if ( $this->linkToSections && $comment !==
null &&
222 preg_match(
'!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment,
$matches )
224 $titleUrl .= $this->parserFactory->getMainInstance()->guessSectionNameFromWikiText(
$matches[ 2 ] );
227 $timestamp = $info[
'timestamp'];
229 if ( isset( $info[
'user'] ) ) {
230 $user = $info[
'user'];
231 $completeText =
"$comment ($user)";
234 $completeText = (string)$comment;
237 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
241 private function getWatchlistModule() {
242 $this->watchlistModule ??= $this->
getMain()->getModuleManager()->getModule(
'query' )
243 ->getModuleManager()->getModule(
'watchlist' );
245 return $this->watchlistModule;
253 ParamValidator::PARAM_DEFAULT =>
'rss',
254 ParamValidator::PARAM_TYPE => $feedFormatNames
257 ParamValidator::PARAM_DEFAULT => 24,
258 ParamValidator::PARAM_TYPE =>
'integer',
259 IntegerDef::PARAM_MIN => 1,
260 IntegerDef::PARAM_MAX => 72,
262 'linktosections' =>
false,
266 'allrev' =>
'allrev',
267 'owner' =>
'wlowner',
268 'token' =>
'wltoken',
271 'excludeuser' =>
'wlexcludeuser',
274 $wlparams = $this->getWatchlistModule()->getAllowedParams( $flags );
275 foreach ( $copyParams as $from => $to ) {
276 $p = $wlparams[$from];
277 if ( !is_array( $p ) ) {
278 $p = [ ParamValidator::PARAM_DEFAULT => $p ];
283 if ( isset( $p[ParamValidator::PARAM_TYPE] ) && is_array( $p[ParamValidator::PARAM_TYPE] ) &&
286 foreach ( $p[ParamValidator::PARAM_TYPE] as $v ) {
301 'action=feedwatchlist'
302 =>
'apihelp-feedwatchlist-example-default',
303 'action=feedwatchlist&allrev=&hours=6'
304 =>
'apihelp-feedwatchlist-example-all6hrs',
310 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlist_feed';
315class_alias( ApiFeedWatchlist::class,
'ApiFeedWatchlist' );
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
This is the main API class, used for both external and internal processing.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
A class containing constants representing the names of configuration variables.
const FeedClasses
Name constant for the FeedClasses setting, for use with Config::get()
const Feed
Name constant for the Feed setting, for use with Config::get()
const Sitename
Name constant for the Sitename setting, for use with Config::get()
const LanguageCode
Name constant for the LanguageCode setting, for use with Config::get()
const FeedLimit
Name constant for the FeedLimit setting, for use with Config::get()
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...