| 1 |
<?php |
| 2 |
|
| 3 |
" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use Exception; |
| 26 |
use MediaWiki\Feed\FeedItem; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\Parser\ParserFactory; |
| 29 |
use MediaWiki\Request\FauxRequest; |
| 30 |
use MediaWiki\SpecialPage\SpecialPage; |
| 31 |
use MediaWiki\Title\Title; |
| 32 |
use Wikimedia\ParamValidator\ParamValidator; |
| 33 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class ApiFeedWatchlist extends ApiBase { |
| 43 |
|
| 44 |
|
| 45 |
private $watchlistModule = null; |
| 46 |
|
| 47 |
private $linkToSections = false; |
| 48 |
|
| 49 |
private ParserFactory $parserFactory; |
| 50 |
|
| 51 |
public function __construct( |
| 52 |
ApiMain $main, |
| 53 |
string $action, |
| 54 |
ParserFactory $parserFactory |
| 55 |
) { |
| 56 |
parent::__construct( $main, $action ); |
| 57 |
$this->parserFactory = $parserFactory; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function getCustomPrinter() { |
| 66 |
return new ApiFormatFeedWrapper( $this->getMain() ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function execute() { |
| 74 |
$config = $this->getConfig(); |
| 75 |
$feedClasses = $config->get( MainConfigNames::FeedClasses ); |
| 76 |
$params = []; |
| 77 |
$feedItems = []; |
| 78 |
try { |
| 79 |
$params = $this->extractRequestParams(); |
| 80 |
|
| 81 |
if ( !$config->get( MainConfigNames::Feed ) ) { |
| 82 |
$this->dieWithError( 'feed-unavailable' ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( !isset( $feedClasses[$params['feedformat']] ) ) { |
| 86 |
$this->dieWithError( 'feed-invalid' ); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
$endTime = wfTimestamp( TS_MW, time() - (int)$params['hours'] * 60 * 60 ); |
| 91 |
|
| 92 |
|
| 93 |
$fauxReqArr = [ |
| 94 |
'action' => 'query', |
| 95 |
'meta' => 'siteinfo', |
| 96 |
'siprop' => 'general', |
| 97 |
'list' => 'watchlist', |
| 98 |
'wlprop' => 'title|user|comment|timestamp|ids|loginfo', |
| 99 |
'wldir' => 'older', |
| 100 |
'wlend' => $endTime, |
| 101 |
'wllimit' => min( 50, $this->getConfig()->get( MainConfigNames::FeedLimit ) ) |
| 102 |
]; |
| 103 |
|
| 104 |
if ( $params['wlowner'] !== null ) { |
| 105 |
$fauxReqArr['wlowner'] = $params['wlowner']; |
| 106 |
} |
| 107 |
if ( $params['wltoken'] !== null ) { |
| 108 |
$fauxReqArr['wltoken'] = $params['wltoken']; |
| 109 |
} |
| 110 |
if ( $params['wlexcludeuser'] !== null ) { |
| 111 |
$fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser']; |
| 112 |
} |
| 113 |
if ( $params['wlshow'] !== null ) { |
| 114 |
$fauxReqArr['wlshow'] = ParamValidator::implodeMultiValue( $params['wlshow'] ); |
| 115 |
} |
| 116 |
if ( $params['wltype'] !== null ) { |
| 117 |
$fauxReqArr['wltype'] = ParamValidator::implodeMultiValue( $params['wltype'] ); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
if ( $params['linktosections'] ) { |
| 123 |
$this->linkToSections = true; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
if ( $params['allrev'] ) { |
| 128 |
$fauxReqArr['wlallrev'] = ''; |
| 129 |
} |
| 130 |
|
| 131 |
$fauxReq = new FauxRequest( $fauxReqArr ); |
| 132 |
|
| 133 |
$module = new ApiMain( $fauxReq ); |
| 134 |
$module->execute(); |
| 135 |
|
| 136 |
$data = $module->getResult()->getResultData( [ 'query', 'watchlist' ] ); |
| 137 |
foreach ( (array)$data as $key => $info ) { |
| 138 |
if ( ApiResult::isMetadataKey( $key ) ) { |
| 139 |
continue; |
| 140 |
} |
| 141 |
$feedItem = $this->createFeedItem( $info ); |
| 142 |
if ( $feedItem ) { |
| 143 |
$feedItems[] = $feedItem; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
$msg = $this->msg( 'watchlist' )->inContentLanguage()->text(); |
| 148 |
|
| 149 |
$feedTitle = $this->getConfig()->get( MainConfigNames::Sitename ) . ' - ' . $msg . |
| 150 |
' [' . $this->getConfig()->get( MainConfigNames::LanguageCode ) . ']'; |
| 151 |
$feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL(); |
| 152 |
|
| 153 |
$feed = new $feedClasses[$params['feedformat']] ( |
| 154 |
$feedTitle, |
| 155 |
htmlspecialchars( $msg ), |
| 156 |
$feedUrl |
| 157 |
); |
| 158 |
|
| 159 |
ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems ); |
| 160 |
} catch ( Exception $e ) { |
| 161 |
|
| 162 |
$this->getMain()->setCacheMaxAge( 0 ); |
| 163 |
|
| 164 |
|
| 165 |
$feedTitle = $this->getConfig()->get( MainConfigNames::Sitename ) . ' - Error - ' . |
| 166 |
$this->msg( 'watchlist' )->inContentLanguage()->text() . |
| 167 |
' [' . $this->getConfig()->get( MainConfigNames::LanguageCode ) . ']'; |
| 168 |
$feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL(); |
| 169 |
|
| 170 |
$feedFormat = $params['feedformat'] ?? 'rss'; |
| 171 |
$msg = $this->msg( 'watchlist' )->inContentLanguage()->escaped(); |
| 172 |
$feed = new $feedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl ); |
| 173 |
|
| 174 |
if ( $e instanceof ApiUsageException ) { |
| 175 |
foreach ( $e->getStatusValue()->getMessages() as $msg ) { |
| 176 |
|
| 177 |
$msg = ApiMessage::create( $msg ) |
| 178 |
->inLanguage( $this->getLanguage() ); |
| 179 |
$errorTitle = $this->msg( 'api-feed-error-title', $msg->getApiCode() ); |
| 180 |
$errorText = $msg->text(); |
| 181 |
$feedItems[] = new FeedItem( $errorTitle, $errorText, '', '', '' ); |
| 182 |
} |
| 183 |
} else { |
| 184 |
|
| 185 |
$errorCode = 'internal_api_error'; |
| 186 |
$errorTitle = $this->msg( 'api-feed-error-title', $errorCode ); |
| 187 |
$errorText = $e->getMessage(); |
| 188 |
$feedItems[] = new FeedItem( $errorTitle, $errorText, '', '', '' ); |
| 189 |
} |
| 190 |
|
| 191 |
ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems ); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private function createFeedItem( $info ) { |
| 200 |
if ( !isset( $info['title'] ) ) { |
| 201 |
|
| 202 |
return null; |
| 203 |
} |
| 204 |
|
| 205 |
$titleStr = $info['title']; |
| 206 |
$title = Title::newFromText( $titleStr ); |
| 207 |
$curidParam = []; |
| 208 |
if ( !$title || $title->isExternal() ) { |
| 209 |
|
| 210 |
|
| 211 |
if ( isset( $info['pageid'] ) ) { |
| 212 |
$title = Title::newFromID( $info['pageid'] ); |
| 213 |
$curidParam = [ 'curid' => $info['pageid'] ]; |
| 214 |
} |
| 215 |
if ( !$title || $title->isExternal() ) { |
| 216 |
return null; |
| 217 |
} |
| 218 |
} |
| 219 |
if ( isset( $info['revid'] ) ) { |
| 220 |
if ( $info['revid'] === 0 && isset( $info['logid'] ) ) { |
| 221 |
$logTitle = Title::makeTitle( NS_SPECIAL, 'Log' ); |
| 222 |
$titleUrl = $logTitle->getFullURL( [ 'logid' => $info['logid'] ] ); |
| 223 |
} else { |
| 224 |
$titleUrl = $title->getFullURL( [ 'diff' => $info['revid'] ] ); |
| 225 |
} |
| 226 |
} else { |
| 227 |
$titleUrl = $title->getFullURL( $curidParam ); |
| 228 |
} |
| 229 |
$comment = $info['comment'] ?? null; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
if ( $this->linkToSections && $comment !== null && |
| 236 |
preg_match( '!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment, $matches ) |
| 237 |
) { |
| 238 |
$titleUrl .= $this->parserFactory->getMainInstance()->guessSectionNameFromWikiText( $matches[ 2 ] ); |
| 239 |
} |
| 240 |
|
| 241 |
$timestamp = $info['timestamp']; |
| 242 |
|
| 243 |
if ( isset( $info['user'] ) ) { |
| 244 |
$user = $info['user']; |
| 245 |
$completeText = "$comment ($user)"; |
| 246 |
} else { |
| 247 |
$user = ''; |
| 248 |
$completeText = (string)$comment; |
| 249 |
} |
| 250 |
|
| 251 |
return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user ); |
| 252 |
} |
| 253 |
|
| 254 |
private function getWatchlistModule() { |
| 255 |
$this->watchlistModule ??= $this->getMain()->getModuleManager()->getModule( 'query' ) |
| 256 |
->getModuleManager()->getModule( 'watchlist' ); |
| 257 |
|
| 258 |
return $this->watchlistModule; |
| 259 |
} |
| 260 |
|
| 261 |
public function getAllowedParams( $flags = 0 ) { |
| 262 |
$feedFormatNames = array_keys( $this->getConfig()->get( MainConfigNames::FeedClasses ) ); |
| 263 |
$ret = [ |
| 264 |
'feedformat' => [ |
| 265 |
ParamValidator::PARAM_DEFAULT => 'rss', |
| 266 |
ParamValidator::PARAM_TYPE => $feedFormatNames |
| 267 |
], |
| 268 |
'hours' => [ |
| 269 |
ParamValidator::PARAM_DEFAULT => 24, |
| 270 |
ParamValidator::PARAM_TYPE => 'integer', |
| 271 |
IntegerDef::PARAM_MIN => 1, |
| 272 |
IntegerDef::PARAM_MAX => 72, |
| 273 |
], |
| 274 |
'linktosections' => false, |
| 275 |
]; |
| 276 |
|
| 277 |
$copyParams = [ |
| 278 |
'allrev' => 'allrev', |
| 279 |
'owner' => 'wlowner', |
| 280 |
'token' => 'wltoken', |
| 281 |
'show' => 'wlshow', |
| 282 |
'type' => 'wltype', |
| 283 |
'excludeuser' => 'wlexcludeuser', |
| 284 |
]; |
| 285 |
|
| 286 |
$wlparams = $this->getWatchlistModule()->getAllowedParams( $flags ); |
| 287 |
foreach ( $copyParams as $from => $to ) { |
| 288 |
$p = $wlparams[$from]; |
| 289 |
if ( !is_array( $p ) ) { |
| 290 |
$p = [ ParamValidator::PARAM_DEFAULT => $p ]; |
| 291 |
} |
| 292 |
if ( !isset( $p[ApiBase::PARAM_HELP_MSG] ) ) { |
| 293 |
$p[ApiBase::PARAM_HELP_MSG] = "apihelp-query+watchlist-param-$from"; |
| 294 |
} |
| 295 |
if ( isset( $p[ParamValidator::PARAM_TYPE] ) && is_array( $p[ParamValidator::PARAM_TYPE] ) && |
| 296 |
isset( $p[ApiBase::PARAM_HELP_MSG_PER_VALUE] ) |
| 297 |
) { |
| 298 |
foreach ( $p[ParamValidator::PARAM_TYPE] as $v ) { |
| 299 |
if ( !isset( $p[ApiBase::PARAM_HELP_MSG_PER_VALUE][$v] ) ) { |
| 300 |
$p[ApiBase::PARAM_HELP_MSG_PER_VALUE][$v] = "apihelp-query+watchlist-paramvalue-$from-$v"; |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
$ret[$to] = $p; |
| 305 |
} |
| 306 |
|
| 307 |
return $ret; |
| 308 |
} |
| 309 |
|
| 310 |
protected function getExamplesMessages() { |
| 311 |
return [ |
| 312 |
'action=feedwatchlist' |
| 313 |
=> 'apihelp-feedwatchlist-example-default', |
| 314 |
'action=feedwatchlist&allrev=&hours=6' |
| 315 |
=> 'apihelp-feedwatchlist-example-all6hrs', |
| 316 |
]; |
| 317 |
} |
| 318 |
|
| 319 |
public function getHelpUrls() { |
| 320 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlist_feed'; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
class_alias( ApiFeedWatchlist::class, 'ApiFeedWatchlist' ); |