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();
140 $feed =
new $feedClasses[$params[
'feedformat']] (
142 htmlspecialchars( $msg ),
147 }
catch ( Exception $e ) {
149 $this->
getMain()->setCacheMaxAge( 0 );
153 . $this->
msg(
'watchlist' )->inContentLanguage()->text()
158 $feedFormat = $params[
'feedformat'] ??
'rss';
159 $msg = $this->
msg(
'watchlist' )->inContentLanguage()->escaped();
160 $feed =
new $feedClasses[$feedFormat]( $feedTitle, $msg, $feedUrl );
163 foreach ( $e->getStatusValue()->getMessages() as $msg ) {
167 $errorTitle = $this->
msg(
'api-feed-error-title', $msg->getApiCode() )->text();
168 $errorText = $msg->text();
169 $feedItems[] =
new FeedItem( $errorTitle, $errorText,
'',
'',
'' );
173 $errorCode =
'internal_api_error';
174 $errorTitle = $this->
msg(
'api-feed-error-title', $errorCode )->text();
175 $errorText = $e->getMessage();
176 $feedItems[] =
new FeedItem( $errorTitle, $errorText,
'',
'',
'' );
187 private function createFeedItem( $info ) {
188 if ( !isset( $info[
'title'] ) ) {
193 $titleStr = $info[
'title'];
194 $title = Title::newFromText( $titleStr );
196 if ( !$title || $title->isExternal() ) {
199 if ( isset( $info[
'pageid'] ) ) {
200 $title = Title::newFromID( $info[
'pageid'] );
201 $curidParam = [
'curid' => $info[
'pageid'] ];
203 if ( !$title || $title->isExternal() ) {
207 if ( isset( $info[
'revid'] ) ) {
208 if ( $info[
'revid'] === 0 && isset( $info[
'logid'] ) ) {
210 $titleUrl = $logTitle->getFullURL( [
'logid' => $info[
'logid'] ] );
212 $titleUrl = $title->getFullURL( [
'diff' => $info[
'revid'] ] );
215 $titleUrl = $title->getFullURL( $curidParam );
217 $comment = $info[
'comment'] ??
null;
223 if ( $this->linkToSections && $comment !==
null &&
224 preg_match(
'!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment,
$matches )
226 $titleUrl .= $this->parserFactory->getMainInstance()->guessSectionNameFromWikiText(
$matches[ 2 ] );
229 $timestamp = $info[
'timestamp'];
231 if ( isset( $info[
'user'] ) ) {
232 $user = $info[
'user'];
233 $completeText =
"$comment ($user)";
236 $completeText = (string)$comment;
239 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
243 private function getWatchlistModule() {
244 $this->watchlistModule ??= $this->
getMain()->getModuleManager()->getModule(
'query' )
245 ->getModuleManager()->getModule(
'watchlist' );
247 return $this->watchlistModule;
255 ParamValidator::PARAM_DEFAULT =>
'rss',
256 ParamValidator::PARAM_TYPE => $feedFormatNames
259 ParamValidator::PARAM_DEFAULT => 24,
260 ParamValidator::PARAM_TYPE =>
'integer',
261 IntegerDef::PARAM_MIN => 1,
262 IntegerDef::PARAM_MAX => 72,
264 'linktosections' =>
false,
268 'allrev' =>
'allrev',
269 'owner' =>
'wlowner',
270 'token' =>
'wltoken',
273 'excludeuser' =>
'wlexcludeuser',
276 $wlparams = $this->getWatchlistModule()->getAllowedParams( $flags );
277 foreach ( $copyParams as $from => $to ) {
278 $p = $wlparams[$from];
279 if ( !is_array( $p ) ) {
280 $p = [ ParamValidator::PARAM_DEFAULT => $p ];
285 if ( isset( $p[ParamValidator::PARAM_TYPE] ) && is_array( $p[ParamValidator::PARAM_TYPE] ) &&
288 foreach ( $p[ParamValidator::PARAM_TYPE] as $v ) {
303 'action=feedwatchlist'
304 =>
'apihelp-feedwatchlist-example-default',
305 'action=feedwatchlist&allrev=&hours=6'
306 =>
'apihelp-feedwatchlist-example-all6hrs',
312 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlist_feed';
317class_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,...