| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Api; |
| 23 |
|
| 24 |
use ChangesFeed; |
| 25 |
use MediaWiki\Context\DerivativeContext; |
| 26 |
use MediaWiki\Feed\ChannelFeed; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\Request\DerivativeRequest; |
| 29 |
use MediaWiki\SpecialPage\SpecialPage; |
| 30 |
use MediaWiki\SpecialPage\SpecialPageFactory; |
| 31 |
use MediaWiki\Title\Title; |
| 32 |
use MediaWiki\User\TempUser\TempUserConfig; |
| 33 |
use RuntimeException; |
| 34 |
use Wikimedia\ParamValidator\ParamValidator; |
| 35 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class ApiFeedRecentChanges extends ApiBase { |
| 44 |
|
| 45 |
|
| 46 |
private $params; |
| 47 |
|
| 48 |
private SpecialPageFactory $specialPageFactory; |
| 49 |
private TempUserConfig $tempUserConfig; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function __construct( |
| 58 |
ApiMain $mainModule, |
| 59 |
string $moduleName, |
| 60 |
SpecialPageFactory $specialPageFactory, |
| 61 |
TempUserConfig $tempUserConfig |
| 62 |
) { |
| 63 |
parent::__construct( $mainModule, $moduleName ); |
| 64 |
$this->specialPageFactory = $specialPageFactory; |
| 65 |
$this->tempUserConfig = $tempUserConfig; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function getCustomPrinter() { |
| 74 |
return new ApiFormatFeedWrapper( $this->getMain() ); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function execute() { |
| 82 |
$config = $this->getConfig(); |
| 83 |
|
| 84 |
$this->params = $this->extractRequestParams(); |
| 85 |
|
| 86 |
if ( !$config->get( MainConfigNames::Feed ) ) { |
| 87 |
$this->dieWithError( 'feed-unavailable' ); |
| 88 |
} |
| 89 |
|
| 90 |
$feedClasses = $config->get( MainConfigNames::FeedClasses ); |
| 91 |
if ( !isset( $feedClasses[$this->params['feedformat']] ) ) { |
| 92 |
$this->dieWithError( 'feed-invalid' ); |
| 93 |
} |
| 94 |
|
| 95 |
$this->getMain()->setCacheMode( 'public' ); |
| 96 |
if ( !$this->getMain()->getParameter( 'smaxage' ) ) { |
| 97 |
|
| 98 |
$this->getMain()->setCacheMaxAge( 15 ); |
| 99 |
} |
| 100 |
|
| 101 |
$feedFormat = $this->params['feedformat']; |
| 102 |
$specialPageName = $this->params['target'] !== null |
| 103 |
? 'Recentchangeslinked' |
| 104 |
: 'Recentchanges'; |
| 105 |
|
| 106 |
$formatter = $this->getFeedObject( $feedFormat, $specialPageName ); |
| 107 |
|
| 108 |
:( |
| 109 |
$context = new DerivativeContext( $this ); |
| 110 |
$context->setRequest( new DerivativeRequest( |
| 111 |
$this->getRequest(), |
| 112 |
$this->params, |
| 113 |
$this->getRequest()->wasPosted() |
| 114 |
) ); |
| 115 |
|
| 116 |
… |
| 117 |
$rc = $this->specialPageFactory->getPage( $specialPageName ); |
| 118 |
if ( $rc === null ) { |
| 119 |
throw new RuntimeException( __METHOD__ . ' not able to instance special page ' . $specialPageName ); |
| 120 |
} |
| 121 |
'@phan-var \MediaWiki\SpecialPage\ChangesListSpecialPage $rc'; |
| 122 |
$rc->setContext( $context ); |
| 123 |
$rows = $rc->getRows(); |
| 124 |
|
| 125 |
$feedItems = $rows ? ChangesFeed::buildItems( $rows ) : []; |
| 126 |
|
| 127 |
ApiFormatFeedWrapper::setResult( $this->getResult(), $formatter, $feedItems ); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
private function getFeedObject( $feedFormat, $specialPageName ) { |
| 139 |
if ( $specialPageName === 'Recentchangeslinked' ) { |
| 140 |
$title = Title::newFromText( $this->params['target'] ); |
| 141 |
if ( !$title || $title->isExternal() ) { |
| 142 |
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['target'] ) ] ); |
| 143 |
} |
| 144 |
|
| 145 |
$feed = new ChangesFeed( $feedFormat ); |
| 146 |
$feedObj = $feed->getFeedObject( |
| 147 |
$this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) |
| 148 |
->inContentLanguage()->text(), |
| 149 |
$this->msg( 'recentchangeslinked-feed' )->inContentLanguage()->text(), |
| 150 |
SpecialPage::getTitleFor( 'Recentchangeslinked' )->getFullURL() |
| 151 |
); |
| 152 |
} else { |
| 153 |
$feed = new ChangesFeed( $feedFormat ); |
| 154 |
$feedObj = $feed->getFeedObject( |
| 155 |
$this->msg( 'recentchanges' )->inContentLanguage()->text(), |
| 156 |
$this->msg( 'recentchanges-feed-description' )->inContentLanguage()->text(), |
| 157 |
SpecialPage::getTitleFor( 'Recentchanges' )->getFullURL() |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
return $feedObj; |
| 162 |
} |
| 163 |
|
| 164 |
public function getAllowedParams() { |
| 165 |
$config = $this->getConfig(); |
| 166 |
$feedFormatNames = array_keys( $config->get( MainConfigNames::FeedClasses ) ); |
| 167 |
|
| 168 |
return [ |
| 169 |
'feedformat' => [ |
| 170 |
ParamValidator::PARAM_DEFAULT => 'rss', |
| 171 |
ParamValidator::PARAM_TYPE => $feedFormatNames, |
| 172 |
], |
| 173 |
|
| 174 |
'namespace' => [ |
| 175 |
ParamValidator::PARAM_TYPE => 'namespace', |
| 176 |
], |
| 177 |
|
| 178 |
'invert' => false, |
| 179 |
'associated' => false, |
| 180 |
|
| 181 |
'days' => [ |
| 182 |
ParamValidator::PARAM_DEFAULT => 7, |
| 183 |
IntegerDef::PARAM_MIN => 1, |
| 184 |
ParamValidator::PARAM_TYPE => 'integer', |
| 185 |
], |
| 186 |
'limit' => [ |
| 187 |
ParamValidator::PARAM_DEFAULT => 50, |
| 188 |
IntegerDef::PARAM_MIN => 1, |
| 189 |
IntegerDef::PARAM_MAX => $config->get( MainConfigNames::FeedLimit ), |
| 190 |
ParamValidator::PARAM_TYPE => 'integer', |
| 191 |
], |
| 192 |
'from' => [ |
| 193 |
ParamValidator::PARAM_TYPE => 'timestamp', |
| 194 |
], |
| 195 |
|
| 196 |
'hideminor' => false, |
| 197 |
'hidebots' => false, |
| 198 |
'hideanons' => [ |
| 199 |
ParamValidator::PARAM_DEFAULT => false, |
| 200 |
ApiBase::PARAM_HELP_MSG => $this->tempUserConfig->isKnown() ? |
| 201 |
'apihelp-feedrecentchanges-param-hideanons-temp' : |
| 202 |
'apihelp-feedrecentchanges-param-hideanons', |
| 203 |
], |
| 204 |
'hideliu' => false, |
| 205 |
'hidepatrolled' => false, |
| 206 |
'hidemyself' => false, |
| 207 |
'hidecategorization' => false, |
| 208 |
|
| 209 |
'tagfilter' => [ |
| 210 |
ParamValidator::PARAM_TYPE => 'string', |
| 211 |
], |
| 212 |
'inverttags' => false, |
| 213 |
|
| 214 |
'target' => [ |
| 215 |
ParamValidator::PARAM_TYPE => 'string', |
| 216 |
], |
| 217 |
'showlinkedto' => false, |
| 218 |
]; |
| 219 |
} |
| 220 |
|
| 221 |
protected function getExamplesMessages() { |
| 222 |
return [ |
| 223 |
'action=feedrecentchanges' |
| 224 |
=> 'apihelp-feedrecentchanges-example-simple', |
| 225 |
'action=feedrecentchanges&days=30' |
| 226 |
=> 'apihelp-feedrecentchanges-example-30days', |
| 227 |
]; |
| 228 |
} |
| 229 |
|
| 230 |
public function getHelpUrls() { |
| 231 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Feedrecentchanges'; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
class_alias( ApiFeedRecentChanges::class, 'ApiFeedRecentChanges' ); |