Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
64.72% |
387 / 598 |
|
40.68% |
24 / 59 |
CRAP | |
0.00% |
0 / 1 |
| ApiPageSet | |
64.82% |
387 / 597 |
|
40.68% |
24 / 59 |
2367.07 | |
0.00% |
0 / 1 |
| addValues | |
70.00% |
7 / 10 |
|
0.00% |
0 / 1 |
5.68 | |||
| __construct | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
| executeDryRun | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| executeInternal | |
17.46% |
11 / 63 |
|
0.00% |
0 / 1 |
268.99 | |||
| isResolvingRedirects | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDataSource | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
42 | |||
| requestField | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCustomField | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPageTableFields | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
4.01 | |||
| getAllTitlesByNamespace | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTitleCount | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getGoodTitlesByNamespace | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getGoodPages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getGoodTitleCount | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMissingTitlesByNamespace | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMissingPages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getGoodAndMissingTitlesByNamespace | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getGoodAndMissingPages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getInvalidTitlesAndReasons | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMissingPageIDs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRedirectTargets | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRedirectTitlesAsResult | |
80.00% |
16 / 20 |
|
0.00% |
0 / 1 |
8.51 | |||
| getNormalizedTitles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getNormalizedTitlesAsResult | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
5.02 | |||
| getConvertedTitles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getConvertedTitlesAsResult | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
4.02 | |||
| getInterwikiTitles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getInterwikiTitlesAsResult | |
76.92% |
10 / 13 |
|
0.00% |
0 / 1 |
5.31 | |||
| getInvalidTitlesAndRevisions | |
92.86% |
26 / 28 |
|
0.00% |
0 / 1 |
11.04 | |||
| getRevisionIDs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLiveRevisionIDs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDeletedRevisionIDs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMissingRevisionIDs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMissingRevisionIDsAsResult | |
44.44% |
4 / 9 |
|
0.00% |
0 / 1 |
6.74 | |||
| getSpecialPages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRevisionCount | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| populateFromTitles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| populateFromPageIDs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| populateFromQueryResult | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| populateFromRevisionIDs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| processDbRow | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
4.01 | |||
| initFromTitles | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
2 | |||
| initFromPageIds | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| initFromQueryResult | |
80.65% |
25 / 31 |
|
0.00% |
0 / 1 |
15.42 | |||
| initFromRevIDs | |
0.00% |
0 / 53 |
|
0.00% |
0 / 1 |
132 | |||
| resolvePendingRedirects | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
6.01 | |||
| loadRedirectTargets | |
95.45% |
42 / 44 |
|
0.00% |
0 / 1 |
11 | |||
| getCacheMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| processTitlesArray | |
96.61% |
57 / 59 |
|
0.00% |
0 / 1 |
24 | |||
| setGeneratorData | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| setRedirectMergePolicy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| resolveRedirectTitleDest | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| populateGeneratorData | |
64.44% |
29 / 45 |
|
0.00% |
0 / 1 |
40.82 | |||
| getDB | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAllowedParams | |
92.50% |
37 / 40 |
|
0.00% |
0 / 1 |
4.01 | |||
| handleParamNormalization | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| getGenerators | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright © 2006, 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com" |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | */ |
| 8 | |
| 9 | namespace MediaWiki\Api; |
| 10 | |
| 11 | use MediaWiki\Api\Validator\SubmoduleDef; |
| 12 | use MediaWiki\Cache\GenderCache; |
| 13 | use MediaWiki\Context\DerivativeContext; |
| 14 | use MediaWiki\Language\ILanguageConverter; |
| 15 | use MediaWiki\Language\Language; |
| 16 | use MediaWiki\Language\LanguageConverter; |
| 17 | use MediaWiki\Linker\LinkTarget; |
| 18 | use MediaWiki\MainConfigNames; |
| 19 | use MediaWiki\MediaWikiServices; |
| 20 | use MediaWiki\Message\Message; |
| 21 | use MediaWiki\Page\LinkBatch; |
| 22 | use MediaWiki\Page\LinkBatchFactory; |
| 23 | use MediaWiki\Page\LinkCache; |
| 24 | use MediaWiki\Page\PageIdentity; |
| 25 | use MediaWiki\Page\PageReference; |
| 26 | use MediaWiki\Request\FauxRequest; |
| 27 | use MediaWiki\SpecialPage\RedirectSpecialArticle; |
| 28 | use MediaWiki\SpecialPage\SpecialPageFactory; |
| 29 | use MediaWiki\Title\MalformedTitleException; |
| 30 | use MediaWiki\Title\NamespaceInfo; |
| 31 | use MediaWiki\Title\Title; |
| 32 | use MediaWiki\Title\TitleFactory; |
| 33 | use stdClass; |
| 34 | use Wikimedia\Message\ListType; |
| 35 | use Wikimedia\ParamValidator\ParamValidator; |
| 36 | use Wikimedia\Rdbms\IReadableDatabase; |
| 37 | use Wikimedia\Rdbms\IResultWrapper; |
| 38 | |
| 39 | /** |
| 40 | * This class contains a list of pages that the client has requested. |
| 41 | * Initially, when the client passes in titles=, pageids=, or revisions= |
| 42 | * parameter, an instance of the ApiPageSet class will normalize titles, |
| 43 | * determine if the pages/revisions exist, and prefetch any additional page |
| 44 | * data requested. |
| 45 | * |
| 46 | * When a generator is used, the result of the generator will become the input |
| 47 | * for the second instance of this class, and all subsequent actions will use |
| 48 | * the second instance for all their work. |
| 49 | * |
| 50 | * @ingroup API |
| 51 | * @since 1.21 derives from ApiBase instead of ApiQueryBase |
| 52 | */ |
| 53 | class ApiPageSet extends ApiBase { |
| 54 | /** |
| 55 | * Constructor flag: The new instance of ApiPageSet will ignore the 'generator=' parameter |
| 56 | * @since 1.21 |
| 57 | */ |
| 58 | private const DISABLE_GENERATORS = 1; |
| 59 | |
| 60 | /** @var array */ |
| 61 | private $mParams; |
| 62 | |
| 63 | /** @var bool */ |
| 64 | private $mResolveRedirects; |
| 65 | |
| 66 | /** @var bool */ |
| 67 | private $mConvertTitles; |
| 68 | |
| 69 | /** @var bool */ |
| 70 | private $mAllowGenerator; |
| 71 | |
| 72 | /** @var array<int,array<string,int>> [ns][dbkey] => page_id or negative when missing */ |
| 73 | private $mAllPages = []; |
| 74 | |
| 75 | /** @var Title[] */ |
| 76 | private $mTitles = []; |
| 77 | |
| 78 | /** @var array<int,array<string,int>> [ns][dbkey] => page_id or negative when missing */ |
| 79 | private $mGoodAndMissingPages = []; |
| 80 | |
| 81 | /** @var array<int,array<string,int>> [ns][dbkey] => page_id */ |
| 82 | private $mGoodPages = []; |
| 83 | |
| 84 | /** @var array<int,Title> */ |
| 85 | private $mGoodTitles = []; |
| 86 | |
| 87 | /** @var array<int,array<string,int>> [ns][dbkey] => fake page_id */ |
| 88 | private $mMissingPages = []; |
| 89 | |
| 90 | /** @var array<int,Title> */ |
| 91 | private $mMissingTitles = []; |
| 92 | |
| 93 | /** @var array<int,array{title:string,invalidreason:array}> [fake_page_id] => [ 'title' => $title, 'invalidreason' => $reason ] */ |
| 94 | private $mInvalidTitles = []; |
| 95 | |
| 96 | /** @var int[] */ |
| 97 | private $mMissingPageIDs = []; |
| 98 | |
| 99 | /** @var array<string,Title> */ |
| 100 | private $mRedirectTitles = []; |
| 101 | |
| 102 | /** @var array<int,Title> */ |
| 103 | private $mSpecialTitles = []; |
| 104 | |
| 105 | /** @var array<int,array<string,int>> separate from mAllPages to avoid breaking getAllTitlesByNamespace() */ |
| 106 | private $mAllSpecials = []; |
| 107 | |
| 108 | /** @var array<string,string> */ |
| 109 | private $mNormalizedTitles = []; |
| 110 | |
| 111 | /** @var array<string,string> */ |
| 112 | private $mInterwikiTitles = []; |
| 113 | |
| 114 | /** @var array<int,Title> */ |
| 115 | private $mPendingRedirectIDs = []; |
| 116 | |
| 117 | /** @var array<string,array{Title,Title}> [dbkey] => [ Title $from, Title $to ] */ |
| 118 | private $mPendingRedirectSpecialPages = []; |
| 119 | |
| 120 | /** @var array<string,Title> */ |
| 121 | private $mResolvedRedirectTitles = []; |
| 122 | |
| 123 | /** @var array<string,string> */ |
| 124 | private $mConvertedTitles = []; |
| 125 | |
| 126 | /** @var array<int,int> Array of revID (int) => pageID (int) */ |
| 127 | private $mGoodRevIDs = []; |
| 128 | |
| 129 | /** @var array<int,int> Array of revID (int) => pageID (int) */ |
| 130 | private $mLiveRevIDs = []; |
| 131 | |
| 132 | /** @var array<int,int> Array of revID (int) => pageID (int) */ |
| 133 | private $mDeletedRevIDs = []; |
| 134 | |
| 135 | /** @var int[] */ |
| 136 | private $mMissingRevIDs = []; |
| 137 | |
| 138 | /** @var array<int,array<string,array>> [ns][dbkey] => data array */ |
| 139 | private $mGeneratorData = []; |
| 140 | |
| 141 | /** @var int */ |
| 142 | private $mFakePageId = -1; |
| 143 | |
| 144 | /** @var string */ |
| 145 | private $mCacheMode = 'public'; |
| 146 | |
| 147 | /** @var array<string,array<int,mixed>> [fieldName][pageId] => value */ |
| 148 | private $mRequestedPageFields = []; |
| 149 | |
| 150 | /** @var callable|null */ |
| 151 | private $mRedirectMergePolicy; |
| 152 | |
| 153 | /** @var array<string,string>|null see getGenerators() */ |
| 154 | private static $generators = null; |
| 155 | |
| 156 | private Language $contentLanguage; |
| 157 | private LinkCache $linkCache; |
| 158 | private NamespaceInfo $namespaceInfo; |
| 159 | private GenderCache $genderCache; |
| 160 | private LinkBatchFactory $linkBatchFactory; |
| 161 | private TitleFactory $titleFactory; |
| 162 | private ILanguageConverter $languageConverter; |
| 163 | private SpecialPageFactory $specialPageFactory; |
| 164 | |
| 165 | /** |
| 166 | * Add all items from $values into the result |
| 167 | * @param array &$result Output |
| 168 | * @param array $values Values to add |
| 169 | * @param string[] $flags The names of boolean flags to mark this element |
| 170 | * @param string|null $name If given, name of the value |
| 171 | */ |
| 172 | private static function addValues( array &$result, $values, $flags = [], $name = null ) { |
| 173 | foreach ( $values as $val ) { |
| 174 | if ( $val instanceof Title ) { |
| 175 | $v = []; |
| 176 | ApiQueryBase::addTitleInfo( $v, $val ); |
| 177 | } elseif ( $name !== null ) { |
| 178 | $v = [ $name => $val ]; |
| 179 | } else { |
| 180 | $v = $val; |
| 181 | } |
| 182 | foreach ( $flags as $flag ) { |
| 183 | $v[$flag] = true; |
| 184 | } |
| 185 | $result[] = $v; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @param ApiBase $mDbSource Module implementing getDB(). |
| 191 | * Allows PageSet to reuse existing db connection from the shared state like ApiQuery. |
| 192 | * @param int $flags Zero or more flags like DISABLE_GENERATORS |
| 193 | * @param int $mDefaultNamespace The namespace to use if none is specified by a prefix. |
| 194 | * @since 1.21 accepts $flags instead of two boolean values |
| 195 | */ |
| 196 | public function __construct( |
| 197 | private readonly ApiBase $mDbSource, |
| 198 | $flags = 0, |
| 199 | private readonly int $mDefaultNamespace = NS_MAIN, |
| 200 | ) { |
| 201 | parent::__construct( $mDbSource->getMain(), $mDbSource->getModuleName() ); |
| 202 | $this->mAllowGenerator = ( $flags & self::DISABLE_GENERATORS ) == 0; |
| 203 | |
| 204 | $this->mParams = $this->extractRequestParams(); |
| 205 | $this->mResolveRedirects = $this->mParams['redirects']; |
| 206 | $this->mConvertTitles = $this->mParams['converttitles']; |
| 207 | |
| 208 | // Needs service injection - T283314 |
| 209 | $services = MediaWikiServices::getInstance(); |
| 210 | $this->contentLanguage = $services->getContentLanguage(); |
| 211 | $this->linkCache = $services->getLinkCache(); |
| 212 | $this->namespaceInfo = $services->getNamespaceInfo(); |
| 213 | $this->genderCache = $services->getGenderCache(); |
| 214 | $this->linkBatchFactory = $services->getLinkBatchFactory(); |
| 215 | $this->titleFactory = $services->getTitleFactory(); |
| 216 | $this->languageConverter = $services->getLanguageConverterFactory() |
| 217 | ->getLanguageConverter( $this->contentLanguage ); |
| 218 | $this->specialPageFactory = $services->getSpecialPageFactory(); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * In case execute() is not called, call this method to mark all relevant parameters as used |
| 223 | * This prevents unused parameters from being reported as warnings |
| 224 | */ |
| 225 | public function executeDryRun() { |
| 226 | $this->executeInternal( true ); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Populate the PageSet from the request parameters. |
| 231 | */ |
| 232 | public function execute() { |
| 233 | $this->executeInternal( false ); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Populate the PageSet from the request parameters. |
| 238 | * @param bool $isDryRun If true, instantiates generator, but only to mark |
| 239 | * relevant parameters as used |
| 240 | */ |
| 241 | private function executeInternal( $isDryRun ) { |
| 242 | $generatorName = $this->mAllowGenerator ? $this->mParams['generator'] : null; |
| 243 | if ( $generatorName !== null ) { |
| 244 | $dbSource = $this->mDbSource; |
| 245 | if ( !$dbSource instanceof ApiQuery ) { |
| 246 | // If the parent container of this pageset is not ApiQuery, we must create it to run generator |
| 247 | $dbSource = $this->getMain()->getModuleManager()->getModule( 'query' ); |
| 248 | } |
| 249 | $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true ); |
| 250 | if ( $generator === null ) { |
| 251 | $this->dieWithError( [ 'apierror-badgenerator-unknown', $generatorName ], 'badgenerator' ); |
| 252 | } |
| 253 | if ( !$generator instanceof ApiQueryGeneratorBase ) { |
| 254 | $this->dieWithError( [ 'apierror-badgenerator-notgenerator', $generatorName ], 'badgenerator' ); |
| 255 | } |
| 256 | // Create a temporary pageset to store generator's output, |
| 257 | // add any additional fields generator may need, and execute pageset to populate titles/pageids |
| 258 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable |
| 259 | $tmpPageSet = new ApiPageSet( $dbSource, self::DISABLE_GENERATORS ); |
| 260 | $generator->setGeneratorMode( $tmpPageSet ); |
| 261 | $this->mCacheMode = $generator->getCacheMode( $generator->extractRequestParams() ); |
| 262 | |
| 263 | if ( !$isDryRun ) { |
| 264 | $generator->requestExtraData( $tmpPageSet ); |
| 265 | } |
| 266 | $tmpPageSet->executeInternal( $isDryRun ); |
| 267 | |
| 268 | // populate this pageset with the generator output |
| 269 | if ( !$isDryRun ) { |
| 270 | $generator->executeGenerator( $this ); |
| 271 | |
| 272 | $this->getHookRunner()->onAPIQueryGeneratorAfterExecute( $generator, $this ); |
| 273 | } else { |
| 274 | // Prevent warnings from being reported on these parameters |
| 275 | $main = $this->getMain(); |
| 276 | foreach ( $generator->extractRequestParams() as $paramName => $param ) { |
| 277 | $main->markParamsUsed( $generator->encodeParamName( $paramName ) ); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if ( !$isDryRun ) { |
| 282 | $this->resolvePendingRedirects(); |
| 283 | } |
| 284 | } else { |
| 285 | // Only one of the titles/pageids/revids is allowed at the same time |
| 286 | $dataSource = null; |
| 287 | if ( isset( $this->mParams['titles'] ) ) { |
| 288 | $dataSource = 'titles'; |
| 289 | } |
| 290 | if ( isset( $this->mParams['pageids'] ) ) { |
| 291 | if ( $dataSource !== null ) { |
| 292 | $this->dieWithError( |
| 293 | [ |
| 294 | 'apierror-invalidparammix-cannotusewith', |
| 295 | $this->encodeParamName( 'pageids' ), |
| 296 | $this->encodeParamName( $dataSource ) |
| 297 | ], |
| 298 | 'multisource' |
| 299 | ); |
| 300 | } |
| 301 | $dataSource = 'pageids'; |
| 302 | } |
| 303 | if ( isset( $this->mParams['revids'] ) ) { |
| 304 | if ( $dataSource !== null ) { |
| 305 | $this->dieWithError( |
| 306 | [ |
| 307 | 'apierror-invalidparammix-cannotusewith', |
| 308 | $this->encodeParamName( 'revids' ), |
| 309 | $this->encodeParamName( $dataSource ) |
| 310 | ], |
| 311 | 'multisource' |
| 312 | ); |
| 313 | } |
| 314 | $dataSource = 'revids'; |
| 315 | } |
| 316 | |
| 317 | if ( !$isDryRun ) { |
| 318 | // Populate page information with the original user input |
| 319 | switch ( $dataSource ) { |
| 320 | case 'titles': |
| 321 | $this->initFromTitles( $this->mParams['titles'] ); |
| 322 | break; |
| 323 | case 'pageids': |
| 324 | $this->initFromPageIds( $this->mParams['pageids'] ); |
| 325 | break; |
| 326 | case 'revids': |
| 327 | if ( $this->mResolveRedirects ) { |
| 328 | $this->addWarning( 'apiwarn-redirectsandrevids' ); |
| 329 | } |
| 330 | $this->mResolveRedirects = false; |
| 331 | $this->initFromRevIDs( $this->mParams['revids'] ); |
| 332 | break; |
| 333 | default: |
| 334 | // Do nothing - some queries do not need any of the data sources. |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Check whether this PageSet is resolving redirects |
| 343 | * @return bool |
| 344 | */ |
| 345 | public function isResolvingRedirects() { |
| 346 | return $this->mResolveRedirects; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Return the parameter name that is the source of data for this PageSet |
| 351 | * |
| 352 | * If multiple source parameters are specified (e.g. titles and pageids), |
| 353 | * one will be named arbitrarily. |
| 354 | * |
| 355 | * @return string|null |
| 356 | */ |
| 357 | public function getDataSource() { |
| 358 | if ( $this->mAllowGenerator && isset( $this->mParams['generator'] ) ) { |
| 359 | return 'generator'; |
| 360 | } |
| 361 | if ( isset( $this->mParams['titles'] ) ) { |
| 362 | return 'titles'; |
| 363 | } |
| 364 | if ( isset( $this->mParams['pageids'] ) ) { |
| 365 | return 'pageids'; |
| 366 | } |
| 367 | if ( isset( $this->mParams['revids'] ) ) { |
| 368 | return 'revids'; |
| 369 | } |
| 370 | |
| 371 | return null; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Request an additional field from the page table. |
| 376 | * Must be called before execute() |
| 377 | * @param string $fieldName A page table field, e.g. "page_touched" |
| 378 | */ |
| 379 | public function requestField( $fieldName ) { |
| 380 | $this->mRequestedPageFields[$fieldName] = []; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Get the values of one of the previously requested page table fields. Can only be used |
| 385 | * after execute() and only for fields previously requested through requestField(). |
| 386 | * @param string $fieldName A page table field, e.g. "page_touched" |
| 387 | * @return array<int,mixed> Field values per page id, initialized only after execute() |
| 388 | */ |
| 389 | public function getCustomField( $fieldName ) { |
| 390 | return $this->mRequestedPageFields[$fieldName]; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Get the fields that have to be queried from the page table: |
| 395 | * the ones requested through requestField() and a few basic ones |
| 396 | * we always need |
| 397 | * @return string[] Array of field names |
| 398 | */ |
| 399 | public function getPageTableFields() { |
| 400 | // Ensure we get minimum required fields |
| 401 | // DON'T change this order |
| 402 | $pageFlds = [ |
| 403 | 'page_namespace' => null, |
| 404 | 'page_title' => null, |
| 405 | 'page_id' => null, |
| 406 | ]; |
| 407 | |
| 408 | if ( $this->mResolveRedirects ) { |
| 409 | $pageFlds['page_is_redirect'] = null; |
| 410 | } |
| 411 | |
| 412 | $pageFlds['page_content_model'] = null; |
| 413 | |
| 414 | if ( $this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) { |
| 415 | $pageFlds['page_lang'] = null; |
| 416 | } |
| 417 | |
| 418 | foreach ( LinkCache::getSelectFields() as $field ) { |
| 419 | $pageFlds[$field] = null; |
| 420 | } |
| 421 | |
| 422 | $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields ); |
| 423 | |
| 424 | return array_keys( $pageFlds ); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Returns an array [ns][dbkey] => page_id for all requested titles. |
| 429 | * page_id is a unique negative number in case title was not found. |
| 430 | * Invalid titles will also have negative page IDs and will be in namespace 0 |
| 431 | * @return array<int,array<string,int>> |
| 432 | */ |
| 433 | public function getAllTitlesByNamespace() { |
| 434 | return $this->mAllPages; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * All existing and missing pages including redirects. |
| 439 | * Does not include special pages, interwiki links, and invalid titles. |
| 440 | * If redirects are resolved, both the redirect and the target will be included here. |
| 441 | * |
| 442 | * @since 1.37 |
| 443 | * @return PageIdentity[] |
| 444 | */ |
| 445 | public function getPages(): array { |
| 446 | return $this->mTitles; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Returns the number of unique pages (not revisions) in the set. |
| 451 | * @return int |
| 452 | */ |
| 453 | public function getTitleCount() { |
| 454 | return count( $this->mTitles ); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Returns an array [ns][dbkey] => page_id for all good titles. |
| 459 | * @return array<int,array<string,int>> |
| 460 | */ |
| 461 | public function getGoodTitlesByNamespace() { |
| 462 | return $this->mGoodPages; |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Pages that were found in the database, including redirects. |
| 467 | * If redirects are resolved, this will include existing redirect targets. |
| 468 | * @since 1.37 |
| 469 | * @return array<int,PageIdentity> Array page_id (int) => PageIdentity (obj) |
| 470 | */ |
| 471 | public function getGoodPages(): array { |
| 472 | return $this->mGoodTitles; |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Returns the number of found unique pages (not revisions) in the set. |
| 477 | * @return int |
| 478 | */ |
| 479 | public function getGoodTitleCount() { |
| 480 | return count( $this->mGoodTitles ); |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Returns an array [ns][dbkey] => fake_page_id for all missing titles. |
| 485 | * fake_page_id is a unique negative number. |
| 486 | * @return array<int,array<string,int>> |
| 487 | */ |
| 488 | public function getMissingTitlesByNamespace() { |
| 489 | return $this->mMissingPages; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Pages that were NOT found in the database. |
| 494 | * The array's index will be negative for each item. |
| 495 | * If redirects are resolved, this will include missing redirect targets. |
| 496 | * @since 1.37 |
| 497 | * @return PageIdentity[] |
| 498 | */ |
| 499 | public function getMissingPages(): array { |
| 500 | return $this->mMissingTitles; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Returns an array [ns][dbkey] => page_id for all good and missing titles. |
| 505 | * @return array<int,array<string,int>> |
| 506 | */ |
| 507 | public function getGoodAndMissingTitlesByNamespace() { |
| 508 | return $this->mGoodAndMissingPages; |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Pages for good and missing titles. |
| 513 | * @since 1.37 |
| 514 | * @return PageIdentity[] |
| 515 | */ |
| 516 | public function getGoodAndMissingPages(): array { |
| 517 | return $this->mGoodTitles + $this->mMissingTitles; |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Titles that were deemed invalid by Title::newFromText() |
| 522 | * The array's index will be unique and negative for each item |
| 523 | * @return array<int,array{title:string,invalidreason:array}> |
| 524 | */ |
| 525 | public function getInvalidTitlesAndReasons() { |
| 526 | return $this->mInvalidTitles; |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Page IDs that were not found in the database |
| 531 | * @return int[] Array of page IDs |
| 532 | */ |
| 533 | public function getMissingPageIDs() { |
| 534 | return $this->mMissingPageIDs; |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Get a list of redirect resolutions - maps a title to its redirect |
| 539 | * target. |
| 540 | * @since 1.37 |
| 541 | * @return LinkTarget[] |
| 542 | */ |
| 543 | public function getRedirectTargets(): array { |
| 544 | return $this->mRedirectTitles; |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Get a list of redirect resolutions - maps a title to its redirect |
| 549 | * target. Includes generator data for redirect source when available. |
| 550 | * @param ApiResult|null $result |
| 551 | * @return string[][] |
| 552 | * @since 1.21 |
| 553 | */ |
| 554 | public function getRedirectTitlesAsResult( $result = null ) { |
| 555 | $values = []; |
| 556 | foreach ( $this->mRedirectTitles as $titleStrFrom => $titleTo ) { |
| 557 | $r = [ |
| 558 | 'from' => strval( $titleStrFrom ), |
| 559 | 'to' => $titleTo->getPrefixedText(), |
| 560 | ]; |
| 561 | if ( $titleTo->hasFragment() ) { |
| 562 | $r['tofragment'] = $titleTo->getFragment(); |
| 563 | } |
| 564 | if ( $titleTo->isExternal() ) { |
| 565 | $r['tointerwiki'] = $titleTo->getInterwiki(); |
| 566 | } |
| 567 | if ( isset( $this->mResolvedRedirectTitles[$titleStrFrom] ) ) { |
| 568 | $titleFrom = $this->mResolvedRedirectTitles[$titleStrFrom]; |
| 569 | $ns = $titleFrom->getNamespace(); |
| 570 | $dbkey = $titleFrom->getDBkey(); |
| 571 | if ( isset( $this->mGeneratorData[$ns][$dbkey] ) ) { |
| 572 | $r = array_merge( $this->mGeneratorData[$ns][$dbkey], $r ); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | $values[] = $r; |
| 577 | } |
| 578 | if ( $values && $result ) { |
| 579 | ApiResult::setIndexedTagName( $values, 'r' ); |
| 580 | } |
| 581 | |
| 582 | return $values; |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Get a list of title normalizations - maps a title to its normalized |
| 587 | * version. |
| 588 | * @return array<string,string> Array of raw_prefixed_title (string) => prefixed_title (string) |
| 589 | */ |
| 590 | public function getNormalizedTitles() { |
| 591 | return $this->mNormalizedTitles; |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * Get a list of title normalizations - maps a title to its normalized |
| 596 | * version in the form of result array. |
| 597 | * @param ApiResult|null $result |
| 598 | * @return string[][] |
| 599 | * @since 1.21 |
| 600 | */ |
| 601 | public function getNormalizedTitlesAsResult( $result = null ) { |
| 602 | $values = []; |
| 603 | foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) { |
| 604 | $encode = $this->contentLanguage->normalize( $rawTitleStr ) !== $rawTitleStr; |
| 605 | $values[] = [ |
| 606 | 'fromencoded' => $encode, |
| 607 | 'from' => $encode ? rawurlencode( $rawTitleStr ) : $rawTitleStr, |
| 608 | 'to' => $titleStr |
| 609 | ]; |
| 610 | } |
| 611 | if ( $values && $result ) { |
| 612 | ApiResult::setIndexedTagName( $values, 'n' ); |
| 613 | } |
| 614 | |
| 615 | return $values; |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * Get a list of title conversions - maps a title to its converted |
| 620 | * version. |
| 621 | * @return string[] Array of raw_prefixed_title (string) => prefixed_title (string) |
| 622 | */ |
| 623 | public function getConvertedTitles() { |
| 624 | return $this->mConvertedTitles; |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Get a list of title conversions - maps a title to its converted |
| 629 | * version as a result array. |
| 630 | * @param ApiResult|null $result |
| 631 | * @return string[][] Array of (from, to) strings |
| 632 | * @since 1.21 |
| 633 | */ |
| 634 | public function getConvertedTitlesAsResult( $result = null ) { |
| 635 | $values = []; |
| 636 | foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) { |
| 637 | $values[] = [ |
| 638 | 'from' => $rawTitleStr, |
| 639 | 'to' => $titleStr |
| 640 | ]; |
| 641 | } |
| 642 | if ( $values && $result ) { |
| 643 | ApiResult::setIndexedTagName( $values, 'c' ); |
| 644 | } |
| 645 | |
| 646 | return $values; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * Get a list of interwiki titles - maps a title to its interwiki |
| 651 | * prefix. |
| 652 | * @return string[] Array of raw_prefixed_title (string) => interwiki_prefix (string) |
| 653 | */ |
| 654 | public function getInterwikiTitles() { |
| 655 | return $this->mInterwikiTitles; |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * Get a list of interwiki titles - maps a title to its interwiki |
| 660 | * prefix as result. |
| 661 | * @param ApiResult|null $result |
| 662 | * @param bool $iwUrl |
| 663 | * @return string[][] |
| 664 | * @since 1.21 |
| 665 | */ |
| 666 | public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) { |
| 667 | $values = []; |
| 668 | foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) { |
| 669 | $item = [ |
| 670 | 'title' => $rawTitleStr, |
| 671 | 'iw' => $interwikiStr, |
| 672 | ]; |
| 673 | if ( $iwUrl ) { |
| 674 | $title = $this->titleFactory->newFromText( $rawTitleStr ); |
| 675 | $item['url'] = $title->getFullURL( '', false, PROTO_CURRENT ); |
| 676 | } |
| 677 | $values[] = $item; |
| 678 | } |
| 679 | if ( $values && $result ) { |
| 680 | ApiResult::setIndexedTagName( $values, 'i' ); |
| 681 | } |
| 682 | |
| 683 | return $values; |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * Get an array of invalid/special/missing titles. |
| 688 | * |
| 689 | * @param string[] $invalidChecks List of types of invalid titles to include. |
| 690 | * Recognized values are: |
| 691 | * - invalidTitles: Titles and reasons from $this->getInvalidTitlesAndReasons() |
| 692 | * - special: Titles from $this->getSpecialPages() |
| 693 | * - missingIds: ids from $this->getMissingPageIDs() |
| 694 | * - missingRevIds: ids from $this->getMissingRevisionIDs() |
| 695 | * - missingTitles: Titles from $this->getMissingPages() |
| 696 | * - interwikiTitles: Titles from $this->getInterwikiTitlesAsResult() |
| 697 | * @return array Array suitable for inclusion in the response |
| 698 | * @since 1.23 |
| 699 | */ |
| 700 | public function getInvalidTitlesAndRevisions( $invalidChecks = [ 'invalidTitles', |
| 701 | 'special', 'missingIds', 'missingRevIds', 'missingTitles', 'interwikiTitles' ] |
| 702 | ) { |
| 703 | $result = []; |
| 704 | if ( in_array( 'invalidTitles', $invalidChecks ) ) { |
| 705 | self::addValues( $result, $this->getInvalidTitlesAndReasons(), [ 'invalid' ] ); |
| 706 | } |
| 707 | if ( in_array( 'special', $invalidChecks ) ) { |
| 708 | $known = []; |
| 709 | $unknown = []; |
| 710 | foreach ( $this->mSpecialTitles as $title ) { |
| 711 | if ( $title->isKnown() ) { |
| 712 | $known[] = $title; |
| 713 | } else { |
| 714 | $unknown[] = $title; |
| 715 | } |
| 716 | } |
| 717 | self::addValues( $result, $unknown, [ 'special', 'missing' ] ); |
| 718 | self::addValues( $result, $known, [ 'special' ] ); |
| 719 | } |
| 720 | if ( in_array( 'missingIds', $invalidChecks ) ) { |
| 721 | self::addValues( $result, $this->getMissingPageIDs(), [ 'missing' ], 'pageid' ); |
| 722 | } |
| 723 | if ( in_array( 'missingRevIds', $invalidChecks ) ) { |
| 724 | self::addValues( $result, $this->getMissingRevisionIDs(), [ 'missing' ], 'revid' ); |
| 725 | } |
| 726 | if ( in_array( 'missingTitles', $invalidChecks ) ) { |
| 727 | $known = []; |
| 728 | $unknown = []; |
| 729 | foreach ( $this->mMissingTitles as $title ) { |
| 730 | if ( $title->isKnown() ) { |
| 731 | $known[] = $title; |
| 732 | } else { |
| 733 | $unknown[] = $title; |
| 734 | } |
| 735 | } |
| 736 | self::addValues( $result, $unknown, [ 'missing' ] ); |
| 737 | self::addValues( $result, $known, [ 'missing', 'known' ] ); |
| 738 | } |
| 739 | if ( in_array( 'interwikiTitles', $invalidChecks ) ) { |
| 740 | self::addValues( $result, $this->getInterwikiTitlesAsResult() ); |
| 741 | } |
| 742 | |
| 743 | return $result; |
| 744 | } |
| 745 | |
| 746 | /** |
| 747 | * Get the list of valid revision IDs (requested with the revids= parameter) |
| 748 | * @return int[] Array of revID (int) => pageID (int) |
| 749 | */ |
| 750 | public function getRevisionIDs() { |
| 751 | return $this->mGoodRevIDs; |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * Get the list of non-deleted revision IDs (requested with the revids= parameter) |
| 756 | * @return int[] Array of revID (int) => pageID (int) |
| 757 | */ |
| 758 | public function getLiveRevisionIDs() { |
| 759 | return $this->mLiveRevIDs; |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * Get the list of revision IDs that were associated with deleted titles. |
| 764 | * @return int[] Array of revID (int) => pageID (int) |
| 765 | */ |
| 766 | public function getDeletedRevisionIDs() { |
| 767 | return $this->mDeletedRevIDs; |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * Revision IDs that were not found in the database |
| 772 | * @return int[] Array of revision IDs |
| 773 | */ |
| 774 | public function getMissingRevisionIDs() { |
| 775 | return $this->mMissingRevIDs; |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * Revision IDs that were not found in the database as result array. |
| 780 | * @param ApiResult|null $result |
| 781 | * @return array<int,array> |
| 782 | * @since 1.21 |
| 783 | */ |
| 784 | public function getMissingRevisionIDsAsResult( $result = null ) { |
| 785 | $values = []; |
| 786 | foreach ( $this->getMissingRevisionIDs() as $revid ) { |
| 787 | $values[$revid] = [ |
| 788 | 'revid' => $revid, |
| 789 | 'missing' => true, |
| 790 | ]; |
| 791 | } |
| 792 | if ( $values && $result ) { |
| 793 | ApiResult::setIndexedTagName( $values, 'rev' ); |
| 794 | } |
| 795 | |
| 796 | return $values; |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Get the list of pages with negative namespace |
| 801 | * @since 1.37 |
| 802 | * @return PageReference[] |
| 803 | */ |
| 804 | public function getSpecialPages(): array { |
| 805 | return $this->mSpecialTitles; |
| 806 | } |
| 807 | |
| 808 | /** |
| 809 | * Returns the number of revisions (requested with revids= parameter). |
| 810 | * @return int Number of revisions. |
| 811 | */ |
| 812 | public function getRevisionCount() { |
| 813 | return count( $this->getRevisionIDs() ); |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * Populate this PageSet |
| 818 | * @param string[]|LinkTarget[]|PageReference[] $titles |
| 819 | */ |
| 820 | public function populateFromTitles( $titles ) { |
| 821 | $this->initFromTitles( $titles ); |
| 822 | } |
| 823 | |
| 824 | /** |
| 825 | * Populate this PageSet from a list of page IDs |
| 826 | * @param int[] $pageIDs |
| 827 | */ |
| 828 | public function populateFromPageIDs( $pageIDs ) { |
| 829 | $this->initFromPageIds( $pageIDs ); |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * Populate this PageSet from a rowset returned from the database |
| 834 | * |
| 835 | * Note that the query result must include the columns returned by |
| 836 | * $this->getPageTableFields(). |
| 837 | * |
| 838 | * @param IReadableDatabase $db Unused since 2011 |
| 839 | * @param IResultWrapper $queryResult |
| 840 | */ |
| 841 | public function populateFromQueryResult( $db, $queryResult ) { |
| 842 | $this->initFromQueryResult( $queryResult ); |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * Populate this PageSet from a list of revision IDs |
| 847 | * @param int[] $revIDs Array of revision IDs |
| 848 | */ |
| 849 | public function populateFromRevisionIDs( $revIDs ) { |
| 850 | $this->initFromRevIDs( $revIDs ); |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * Extract all requested fields from the row received from the database |
| 855 | * @param stdClass $row Result row |
| 856 | */ |
| 857 | public function processDbRow( $row ) { |
| 858 | // Store Title object in various data structures |
| 859 | $title = $this->titleFactory->newFromRow( $row ); |
| 860 | |
| 861 | $this->linkCache->addGoodLinkObjFromRow( $title, $row ); |
| 862 | |
| 863 | $pageId = (int)$row->page_id; |
| 864 | $this->mAllPages[$row->page_namespace][$row->page_title] = $pageId; |
| 865 | $this->mTitles[] = $title; |
| 866 | |
| 867 | if ( $this->mResolveRedirects && $row->page_is_redirect == '1' ) { |
| 868 | $this->mPendingRedirectIDs[$pageId] = $title; |
| 869 | } else { |
| 870 | $this->mGoodPages[$row->page_namespace][$row->page_title] = $pageId; |
| 871 | $this->mGoodAndMissingPages[$row->page_namespace][$row->page_title] = $pageId; |
| 872 | $this->mGoodTitles[$pageId] = $title; |
| 873 | } |
| 874 | |
| 875 | foreach ( $this->mRequestedPageFields as $fieldName => &$fieldValues ) { |
| 876 | $fieldValues[$pageId] = $row->$fieldName; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | /** |
| 881 | * This method populates internal variables with page information |
| 882 | * based on the given array of title strings. |
| 883 | * |
| 884 | * Steps: |
| 885 | * #1 For each title, get data from `page` table |
| 886 | * #2 If page was not found in the DB, store it as missing |
| 887 | * |
| 888 | * Additionally, when resolving redirects: |
| 889 | * #3 If no more redirects left, stop. |
| 890 | * #4 For each redirect, get its target from the `redirect` table. |
| 891 | * #5 Substitute the original LinkBatch object with the new list |
| 892 | * #6 Repeat from step #1 |
| 893 | * |
| 894 | * @param string[]|LinkTarget[]|PageReference[] $titles |
| 895 | */ |
| 896 | private function initFromTitles( $titles ) { |
| 897 | // Get validated and normalized title objects |
| 898 | $linkBatch = $this->processTitlesArray( $titles ); |
| 899 | if ( $linkBatch->isEmpty() ) { |
| 900 | // There might be special-page redirects |
| 901 | $this->resolvePendingRedirects(); |
| 902 | return; |
| 903 | } |
| 904 | |
| 905 | $db = $this->getDB(); |
| 906 | |
| 907 | // Get pageIDs data from the `page` table |
| 908 | $res = $db->newSelectQueryBuilder() |
| 909 | ->select( $this->getPageTableFields() ) |
| 910 | ->from( 'page' ) |
| 911 | ->where( $linkBatch->constructSet( 'page', $db ) ) |
| 912 | ->caller( __METHOD__ ) |
| 913 | ->fetchResultSet(); |
| 914 | |
| 915 | // Hack: get the ns:titles stored in [ ns => [ titles ] ] format |
| 916 | $this->initFromQueryResult( $res, $linkBatch->data, true ); // process Titles |
| 917 | |
| 918 | // Resolve any found redirects |
| 919 | $this->resolvePendingRedirects(); |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * Does the same as initFromTitles(), but is based on page IDs instead |
| 924 | * @param int[] $pageids |
| 925 | * @param bool $filterIds Whether the IDs need filtering |
| 926 | */ |
| 927 | private function initFromPageIds( $pageids, $filterIds = true ) { |
| 928 | if ( !$pageids ) { |
| 929 | return; |
| 930 | } |
| 931 | |
| 932 | $pageids = array_map( 'intval', $pageids ); // paranoia |
| 933 | $remaining = array_fill_keys( $pageids, true ); |
| 934 | |
| 935 | if ( $filterIds ) { |
| 936 | $pageids = $this->filterIDs( [ [ 'page', 'page_id' ] ], $pageids ); |
| 937 | } |
| 938 | |
| 939 | $res = null; |
| 940 | if ( $pageids ) { |
| 941 | $db = $this->getDB(); |
| 942 | |
| 943 | // Get pageIDs data from the `page` table |
| 944 | $res = $db->newSelectQueryBuilder() |
| 945 | ->select( $this->getPageTableFields() ) |
| 946 | ->from( 'page' ) |
| 947 | ->where( [ 'page_id' => $pageids ] ) |
| 948 | ->caller( __METHOD__ ) |
| 949 | ->fetchResultSet(); |
| 950 | } |
| 951 | |
| 952 | $this->initFromQueryResult( $res, $remaining, false ); // process PageIDs |
| 953 | |
| 954 | // Resolve any found redirects |
| 955 | $this->resolvePendingRedirects(); |
| 956 | } |
| 957 | |
| 958 | /** |
| 959 | * Iterate through the result of the query on 'page' table, |
| 960 | * and for each row create and store title object and save any extra fields requested. |
| 961 | * @param IResultWrapper|null $res DB Query result |
| 962 | * @param array|null &$remaining Array of either pageID or ns/title elements (optional). |
| 963 | * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles |
| 964 | * @param bool|null $processTitles Must be provided together with $remaining. |
| 965 | * If true, treat $remaining as an array of [ns][title] |
| 966 | * If false, treat it as an array of [pageIDs] |
| 967 | */ |
| 968 | private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) { |
| 969 | if ( $remaining !== null && $processTitles === null ) { |
| 970 | ApiBase::dieDebug( __METHOD__, 'Missing $processTitles parameter when $remaining is provided' ); |
| 971 | } |
| 972 | |
| 973 | $usernames = []; |
| 974 | if ( $res ) { |
| 975 | foreach ( $res as $row ) { |
| 976 | $pageId = (int)$row->page_id; |
| 977 | |
| 978 | // Remove found page from the list of remaining items |
| 979 | if ( $remaining ) { |
| 980 | if ( $processTitles ) { |
| 981 | unset( $remaining[$row->page_namespace][$row->page_title] ); |
| 982 | } else { |
| 983 | unset( $remaining[$pageId] ); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | // Store any extra fields requested by modules |
| 988 | $this->processDbRow( $row ); |
| 989 | |
| 990 | // Need gender information |
| 991 | if ( $this->namespaceInfo->hasGenderDistinction( $row->page_namespace ) ) { |
| 992 | $usernames[] = $row->page_title; |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | if ( $remaining ) { |
| 998 | // Any items left in the $remaining list are added as missing |
| 999 | if ( $processTitles ) { |
| 1000 | // The remaining titles in $remaining are non-existent pages |
| 1001 | foreach ( $remaining as $ns => $dbkeys ) { |
| 1002 | foreach ( $dbkeys as $dbkey => $_ ) { |
| 1003 | $title = $this->titleFactory->makeTitle( $ns, $dbkey ); |
| 1004 | $this->linkCache->addBadLinkObj( $title ); |
| 1005 | $this->mAllPages[$ns][$dbkey] = $this->mFakePageId; |
| 1006 | $this->mMissingPages[$ns][$dbkey] = $this->mFakePageId; |
| 1007 | $this->mGoodAndMissingPages[$ns][$dbkey] = $this->mFakePageId; |
| 1008 | $this->mMissingTitles[$this->mFakePageId] = $title; |
| 1009 | $this->mFakePageId--; |
| 1010 | $this->mTitles[] = $title; |
| 1011 | |
| 1012 | // need gender information |
| 1013 | if ( $this->namespaceInfo->hasGenderDistinction( $ns ) ) { |
| 1014 | $usernames[] = $dbkey; |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | } else { |
| 1019 | // The remaining pageids do not exist |
| 1020 | if ( !$this->mMissingPageIDs ) { |
| 1021 | $this->mMissingPageIDs = array_keys( $remaining ); |
| 1022 | } else { |
| 1023 | $this->mMissingPageIDs = array_merge( $this->mMissingPageIDs, array_keys( $remaining ) ); |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | // Get gender information |
| 1029 | $this->genderCache->doQuery( $usernames, __METHOD__ ); |
| 1030 | } |
| 1031 | |
| 1032 | /** |
| 1033 | * Does the same as initFromTitles(), but is based on revision IDs |
| 1034 | * instead |
| 1035 | * @param int[] $revids Array of revision IDs |
| 1036 | */ |
| 1037 | private function initFromRevIDs( $revids ) { |
| 1038 | if ( !$revids ) { |
| 1039 | return; |
| 1040 | } |
| 1041 | |
| 1042 | $revids = array_map( 'intval', $revids ); // paranoia |
| 1043 | $db = $this->getDB(); |
| 1044 | $pageids = []; |
| 1045 | $remaining = array_fill_keys( $revids, true ); |
| 1046 | |
| 1047 | $revids = $this->filterIDs( [ [ 'revision', 'rev_id' ], [ 'archive', 'ar_rev_id' ] ], $revids ); |
| 1048 | $goodRemaining = array_fill_keys( $revids, true ); |
| 1049 | |
| 1050 | if ( $revids ) { |
| 1051 | $fields = [ 'rev_id', 'rev_page' ]; |
| 1052 | |
| 1053 | // Get pageIDs data from the `page` table |
| 1054 | $res = $db->newSelectQueryBuilder() |
| 1055 | ->select( $fields ) |
| 1056 | ->from( 'page' ) |
| 1057 | ->where( [ 'rev_id' => $revids ] ) |
| 1058 | ->join( 'revision', null, [ 'rev_page = page_id' ] ) |
| 1059 | ->caller( __METHOD__ ) |
| 1060 | ->fetchResultSet(); |
| 1061 | foreach ( $res as $row ) { |
| 1062 | $revid = (int)$row->rev_id; |
| 1063 | $pageid = (int)$row->rev_page; |
| 1064 | $this->mGoodRevIDs[$revid] = $pageid; |
| 1065 | $this->mLiveRevIDs[$revid] = $pageid; |
| 1066 | $pageids[$pageid] = ''; |
| 1067 | unset( $remaining[$revid] ); |
| 1068 | unset( $goodRemaining[$revid] ); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | // Populate all the page information |
| 1073 | $this->initFromPageIds( array_keys( $pageids ), false ); |
| 1074 | |
| 1075 | // If the user can see deleted revisions, pull out the corresponding |
| 1076 | // titles from the archive table and include them too. We ignore |
| 1077 | // ar_page_id because deleted revisions are tied by title, not page_id. |
| 1078 | if ( $goodRemaining && |
| 1079 | $this->getAuthority()->isAllowed( 'deletedhistory' ) ) { |
| 1080 | |
| 1081 | $res = $db->newSelectQueryBuilder() |
| 1082 | ->select( [ 'ar_rev_id', 'ar_namespace', 'ar_title' ] ) |
| 1083 | ->from( 'archive' ) |
| 1084 | ->where( [ 'ar_rev_id' => array_keys( $goodRemaining ) ] ) |
| 1085 | ->caller( __METHOD__ ) |
| 1086 | ->fetchResultSet(); |
| 1087 | |
| 1088 | $titles = []; |
| 1089 | foreach ( $res as $row ) { |
| 1090 | $revid = (int)$row->ar_rev_id; |
| 1091 | $titles[$revid] = $this->titleFactory->makeTitle( $row->ar_namespace, $row->ar_title ); |
| 1092 | unset( $remaining[$revid] ); |
| 1093 | } |
| 1094 | |
| 1095 | $this->initFromTitles( $titles ); |
| 1096 | |
| 1097 | foreach ( $titles as $revid => $title ) { |
| 1098 | $ns = $title->getNamespace(); |
| 1099 | $dbkey = $title->getDBkey(); |
| 1100 | |
| 1101 | // Handle converted titles |
| 1102 | if ( !isset( $this->mAllPages[$ns][$dbkey] ) && |
| 1103 | isset( $this->mConvertedTitles[$title->getPrefixedText()] ) |
| 1104 | ) { |
| 1105 | $title = $this->titleFactory->newFromText( $this->mConvertedTitles[$title->getPrefixedText()] ); |
| 1106 | $ns = $title->getNamespace(); |
| 1107 | $dbkey = $title->getDBkey(); |
| 1108 | } |
| 1109 | |
| 1110 | if ( isset( $this->mAllPages[$ns][$dbkey] ) ) { |
| 1111 | $this->mGoodRevIDs[$revid] = $this->mAllPages[$ns][$dbkey]; |
| 1112 | $this->mDeletedRevIDs[$revid] = $this->mAllPages[$ns][$dbkey]; |
| 1113 | } else { |
| 1114 | $remaining[$revid] = true; |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | $this->mMissingRevIDs = array_keys( $remaining ); |
| 1120 | } |
| 1121 | |
| 1122 | /** |
| 1123 | * Resolve any redirects in the result if redirect resolution was |
| 1124 | * requested. This function is called repeatedly until all redirects |
| 1125 | * have been resolved. |
| 1126 | */ |
| 1127 | private function resolvePendingRedirects() { |
| 1128 | if ( $this->mResolveRedirects ) { |
| 1129 | $db = $this->getDB(); |
| 1130 | |
| 1131 | // Repeat until all redirects have been resolved |
| 1132 | // The infinite loop is prevented by keeping all known pages in $this->mAllPages |
| 1133 | while ( $this->mPendingRedirectIDs || $this->mPendingRedirectSpecialPages ) { |
| 1134 | // Resolve redirects by querying the pagelinks table, and repeat the process |
| 1135 | // Create a new linkBatch object for the next pass |
| 1136 | $linkBatch = $this->loadRedirectTargets(); |
| 1137 | |
| 1138 | if ( $linkBatch->isEmpty() ) { |
| 1139 | break; |
| 1140 | } |
| 1141 | |
| 1142 | $set = $linkBatch->constructSet( 'page', $db ); |
| 1143 | if ( $set === false ) { |
| 1144 | break; |
| 1145 | } |
| 1146 | |
| 1147 | // Get pageIDs data from the `page` table |
| 1148 | $res = $db->newSelectQueryBuilder() |
| 1149 | ->select( $this->getPageTableFields() ) |
| 1150 | ->from( 'page' ) |
| 1151 | ->where( $set ) |
| 1152 | ->caller( __METHOD__ ) |
| 1153 | ->fetchResultSet(); |
| 1154 | |
| 1155 | // Hack: get the ns:titles stored in [ns => array(titles)] format |
| 1156 | $this->initFromQueryResult( $res, $linkBatch->data, true ); |
| 1157 | } |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | /** |
| 1162 | * Get the targets of the pending redirects from the database |
| 1163 | * |
| 1164 | * Also creates entries in the redirect table for redirects that don't |
| 1165 | * have one. |
| 1166 | * @return LinkBatch |
| 1167 | */ |
| 1168 | private function loadRedirectTargets() { |
| 1169 | $titlesToResolve = []; |
| 1170 | $db = $this->getDB(); |
| 1171 | |
| 1172 | if ( $this->mPendingRedirectIDs ) { |
| 1173 | $res = $db->newSelectQueryBuilder() |
| 1174 | ->select( [ |
| 1175 | 'rd_from', |
| 1176 | 'rd_namespace', |
| 1177 | 'rd_fragment', |
| 1178 | 'rd_interwiki', |
| 1179 | 'rd_title' |
| 1180 | ] ) |
| 1181 | ->from( 'redirect' ) |
| 1182 | ->where( [ 'rd_from' => array_keys( $this->mPendingRedirectIDs ) ] ) |
| 1183 | ->caller( __METHOD__ ) |
| 1184 | ->fetchResultSet(); |
| 1185 | |
| 1186 | foreach ( $res as $row ) { |
| 1187 | $rdfrom = (int)$row->rd_from; |
| 1188 | $from = $this->mPendingRedirectIDs[$rdfrom]->getPrefixedText(); |
| 1189 | $to = $this->titleFactory->makeTitle( |
| 1190 | $row->rd_namespace, |
| 1191 | $row->rd_title, |
| 1192 | $row->rd_fragment, |
| 1193 | $row->rd_interwiki |
| 1194 | ); |
| 1195 | $this->mResolvedRedirectTitles[$from] = $this->mPendingRedirectIDs[$rdfrom]; |
| 1196 | unset( $this->mPendingRedirectIDs[$rdfrom] ); |
| 1197 | if ( $to->isExternal() ) { |
| 1198 | $this->mInterwikiTitles[$to->getPrefixedText()] = $to->getInterwiki(); |
| 1199 | } elseif ( !isset( $this->mAllPages[$to->getNamespace()][$to->getDBkey()] ) |
| 1200 | && !( $this->mConvertTitles && isset( $this->mConvertedTitles[$to->getPrefixedText()] ) ) |
| 1201 | ) { |
| 1202 | $titlesToResolve[] = $to; |
| 1203 | } |
| 1204 | $this->mRedirectTitles[$from] = $to; |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | if ( $this->mPendingRedirectSpecialPages ) { |
| 1209 | foreach ( $this->mPendingRedirectSpecialPages as [ $from, $to ] ) { |
| 1210 | /** @var Title $from */ |
| 1211 | $fromKey = $from->getPrefixedText(); |
| 1212 | $this->mResolvedRedirectTitles[$fromKey] = $from; |
| 1213 | $this->mRedirectTitles[$fromKey] = $to; |
| 1214 | if ( $to->isExternal() ) { |
| 1215 | $this->mInterwikiTitles[$to->getPrefixedText()] = $to->getInterwiki(); |
| 1216 | } elseif ( !isset( $this->mAllPages[$to->getNamespace()][$to->getDBkey()] ) ) { |
| 1217 | $titlesToResolve[] = $to; |
| 1218 | } |
| 1219 | } |
| 1220 | $this->mPendingRedirectSpecialPages = []; |
| 1221 | |
| 1222 | // Set private caching since we don't know what criteria the |
| 1223 | // special pages used to decide on these redirects. |
| 1224 | $this->mCacheMode = 'private'; |
| 1225 | } |
| 1226 | |
| 1227 | return $this->processTitlesArray( $titlesToResolve ); |
| 1228 | } |
| 1229 | |
| 1230 | /** |
| 1231 | * Get the cache mode for the data generated by this module. |
| 1232 | * All PageSet users should take into account whether this returns a more-restrictive |
| 1233 | * cache mode than the using module itself. For possible return values and other |
| 1234 | * details about cache modes, see ApiMain::setCacheMode() |
| 1235 | * |
| 1236 | * Public caching will only be allowed if *all* the modules that supply |
| 1237 | * data for a given request return a cache mode of public. |
| 1238 | * |
| 1239 | * @param array|null $params |
| 1240 | * @return string |
| 1241 | * @since 1.21 |
| 1242 | */ |
| 1243 | public function getCacheMode( $params = null ) { |
| 1244 | return $this->mCacheMode; |
| 1245 | } |
| 1246 | |
| 1247 | /** |
| 1248 | * Given an array of title strings, convert them into Title objects. |
| 1249 | * Alternatively, an array of Title objects may be given. |
| 1250 | * This method validates access rights for the title, |
| 1251 | * and appends normalization values to the output. |
| 1252 | * |
| 1253 | * @param string[]|LinkTarget[]|PageReference[] $titles |
| 1254 | * @return LinkBatch |
| 1255 | */ |
| 1256 | private function processTitlesArray( $titles ) { |
| 1257 | $linkBatch = $this->linkBatchFactory->newLinkBatch(); |
| 1258 | |
| 1259 | /** @var Title[] $titleObjects */ |
| 1260 | $titleObjects = []; |
| 1261 | foreach ( $titles as $index => $title ) { |
| 1262 | if ( is_string( $title ) ) { |
| 1263 | try { |
| 1264 | /** @var Title $titleObj */ |
| 1265 | $titleObj = $this->titleFactory->newFromTextThrow( $title, $this->mDefaultNamespace ); |
| 1266 | } catch ( MalformedTitleException $ex ) { |
| 1267 | // Handle invalid titles gracefully |
| 1268 | if ( !isset( $this->mAllPages[0][$title] ) ) { |
| 1269 | $this->mAllPages[0][$title] = $this->mFakePageId; |
| 1270 | $this->mInvalidTitles[$this->mFakePageId] = [ |
| 1271 | 'title' => $title, |
| 1272 | 'invalidreason' => $this->getErrorFormatter()->formatException( $ex, [ 'bc' => true ] ), |
| 1273 | ]; |
| 1274 | $this->mFakePageId--; |
| 1275 | } |
| 1276 | continue; // There's nothing else we can do |
| 1277 | } |
| 1278 | } elseif ( $title instanceof LinkTarget ) { |
| 1279 | $titleObj = $this->titleFactory->newFromLinkTarget( $title ); |
| 1280 | } else { |
| 1281 | $titleObj = $this->titleFactory->newFromPageReference( $title ); |
| 1282 | } |
| 1283 | |
| 1284 | $titleObjects[$index] = $titleObj; |
| 1285 | } |
| 1286 | |
| 1287 | // Get gender information |
| 1288 | $this->genderCache->doTitlesArray( $titleObjects, __METHOD__ ); |
| 1289 | |
| 1290 | foreach ( $titleObjects as $index => $titleObj ) { |
| 1291 | $title = is_string( $titles[$index] ) ? $titles[$index] : false; |
| 1292 | $unconvertedTitle = $titleObj->getPrefixedText(); |
| 1293 | $titleWasConverted = false; |
| 1294 | if ( $titleObj->isExternal() ) { |
| 1295 | // This title is an interwiki link. |
| 1296 | $this->mInterwikiTitles[$unconvertedTitle] = $titleObj->getInterwiki(); |
| 1297 | } else { |
| 1298 | // Variants checking |
| 1299 | if ( |
| 1300 | $this->mConvertTitles |
| 1301 | && $this->languageConverter->hasVariants() |
| 1302 | && !$titleObj->exists() |
| 1303 | ) { |
| 1304 | // ILanguageConverter::findVariantLink will modify titleText and |
| 1305 | // titleObj into the canonical variant if possible |
| 1306 | $titleText = $title !== false ? $title : $titleObj->getPrefixedText(); |
| 1307 | $this->languageConverter->findVariantLink( $titleText, $titleObj ); |
| 1308 | $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText(); |
| 1309 | } |
| 1310 | |
| 1311 | if ( $titleObj->getNamespace() < 0 ) { |
| 1312 | // Handle Special and Media pages |
| 1313 | $titleObj = $titleObj->fixSpecialName(); |
| 1314 | $ns = $titleObj->getNamespace(); |
| 1315 | $dbkey = $titleObj->getDBkey(); |
| 1316 | if ( !isset( $this->mAllSpecials[$ns][$dbkey] ) ) { |
| 1317 | $this->mAllSpecials[$ns][$dbkey] = $this->mFakePageId; |
| 1318 | $target = null; |
| 1319 | if ( $ns === NS_SPECIAL && $this->mResolveRedirects ) { |
| 1320 | $special = $this->specialPageFactory->getPage( $dbkey ); |
| 1321 | if ( $special instanceof RedirectSpecialArticle ) { |
| 1322 | // Only RedirectSpecialArticle is intended to redirect to an article, other kinds of |
| 1323 | // RedirectSpecialPage are probably applying weird URL parameters we don't want to |
| 1324 | // handle. |
| 1325 | $context = new DerivativeContext( $this ); |
| 1326 | $context->setTitle( $titleObj ); |
| 1327 | $context->setRequest( new FauxRequest ); |
| 1328 | $special->setContext( $context ); |
| 1329 | [ /* $alias */, $subpage ] = $this->specialPageFactory->resolveAlias( $dbkey ); |
| 1330 | $target = $special->getRedirect( $subpage ); |
| 1331 | } |
| 1332 | } |
| 1333 | if ( $target ) { |
| 1334 | $this->mPendingRedirectSpecialPages[$dbkey] = [ $titleObj, $target ]; |
| 1335 | } else { |
| 1336 | $this->mSpecialTitles[$this->mFakePageId] = $titleObj; |
| 1337 | $this->mFakePageId--; |
| 1338 | } |
| 1339 | } |
| 1340 | } else { |
| 1341 | // Regular page |
| 1342 | $linkBatch->addObj( $titleObj ); |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | // Make sure we remember the original title that was |
| 1347 | // given to us. This way the caller can correlate new |
| 1348 | // titles with the originally requested when e.g. the |
| 1349 | // namespace is localized or the capitalization is |
| 1350 | // different |
| 1351 | if ( $titleWasConverted ) { |
| 1352 | $this->mConvertedTitles[$unconvertedTitle] = $titleObj->getPrefixedText(); |
| 1353 | // In this case the page can't be Special. |
| 1354 | if ( $title !== false && $title !== $unconvertedTitle ) { |
| 1355 | $this->mNormalizedTitles[$title] = $unconvertedTitle; |
| 1356 | } |
| 1357 | } elseif ( $title !== false && $title !== $titleObj->getPrefixedText() ) { |
| 1358 | $this->mNormalizedTitles[$title] = $titleObj->getPrefixedText(); |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | return $linkBatch; |
| 1363 | } |
| 1364 | |
| 1365 | /** |
| 1366 | * Set data for a title. |
| 1367 | * |
| 1368 | * This data may be extracted into an ApiResult using |
| 1369 | * self::populateGeneratorData. This should generally be limited to |
| 1370 | * data that is likely to be particularly useful to end users rather than |
| 1371 | * just being a dump of everything returned in non-generator mode. |
| 1372 | * |
| 1373 | * Redirects here will *not* be followed, even if 'redirects' was |
| 1374 | * specified, since in the case of multiple redirects we can't know which |
| 1375 | * source's data to use on the target. |
| 1376 | * |
| 1377 | * @param PageReference|LinkTarget $title |
| 1378 | * @param array $data |
| 1379 | */ |
| 1380 | public function setGeneratorData( $title, array $data ) { |
| 1381 | $ns = $title->getNamespace(); |
| 1382 | $dbkey = $title->getDBkey(); |
| 1383 | $this->mGeneratorData[$ns][$dbkey] = $data; |
| 1384 | } |
| 1385 | |
| 1386 | /** |
| 1387 | * Controls how generator data about a redirect source is merged into |
| 1388 | * the generator data for the redirect target. When not set no data |
| 1389 | * is merged. Note that if multiple titles redirect to the same target |
| 1390 | * the order of operations is undefined. |
| 1391 | * |
| 1392 | * Example to include generated data from redirect in target, preferring |
| 1393 | * the data generated for the destination when there is a collision: |
| 1394 | * @code |
| 1395 | * $pageSet->setRedirectMergePolicy( function( array $current, array $new ) { |
| 1396 | * return $current + $new; |
| 1397 | * } ); |
| 1398 | * @endcode |
| 1399 | * |
| 1400 | * @param callable|null $callable Receives two array arguments, first the |
| 1401 | * generator data for the redirect target and second the generator data |
| 1402 | * for the redirect source. Returns the resulting generator data to use |
| 1403 | * for the redirect target. |
| 1404 | */ |
| 1405 | public function setRedirectMergePolicy( $callable ) { |
| 1406 | $this->mRedirectMergePolicy = $callable; |
| 1407 | } |
| 1408 | |
| 1409 | /** |
| 1410 | * Resolve the title a redirect points to. |
| 1411 | * |
| 1412 | * Will follow sequential redirects to find the final page. In |
| 1413 | * the case of a redirect cycle the original page will be returned. |
| 1414 | * self::resolvePendingRedirects must be executed before calling |
| 1415 | * this method. |
| 1416 | * |
| 1417 | * @param Title $titleFrom A title from $this->mResolvedRedirectTitles |
| 1418 | * @return Title |
| 1419 | */ |
| 1420 | private function resolveRedirectTitleDest( Title $titleFrom ): Title { |
| 1421 | $seen = []; |
| 1422 | $dest = $titleFrom; |
| 1423 | while ( isset( $this->mRedirectTitles[$dest->getPrefixedText()] ) ) { |
| 1424 | $dest = $this->mRedirectTitles[$dest->getPrefixedText()]; |
| 1425 | if ( isset( $seen[$dest->getPrefixedText()] ) ) { |
| 1426 | return $titleFrom; |
| 1427 | } |
| 1428 | $seen[$dest->getPrefixedText()] = true; |
| 1429 | } |
| 1430 | return $dest; |
| 1431 | } |
| 1432 | |
| 1433 | /** |
| 1434 | * Populate the generator data for all titles in the result |
| 1435 | * |
| 1436 | * The page data may be inserted into an ApiResult object or into an |
| 1437 | * associative array. The $path parameter specifies the path within the |
| 1438 | * ApiResult or array to find the "pages" node. |
| 1439 | * |
| 1440 | * The "pages" node itself must be an associative array mapping the page ID |
| 1441 | * or fake page ID values returned by this pageset (see |
| 1442 | * self::getAllTitlesByNamespace() and self::getSpecialTitles()) to |
| 1443 | * associative arrays of page data. Each of those subarrays will have the |
| 1444 | * data from self::setGeneratorData() merged in. |
| 1445 | * |
| 1446 | * Data that was set by self::setGeneratorData() for pages not in the |
| 1447 | * "pages" node will be ignored. |
| 1448 | * |
| 1449 | * @param ApiResult|array &$result |
| 1450 | * @param array $path |
| 1451 | * @return bool Whether the data fit |
| 1452 | */ |
| 1453 | public function populateGeneratorData( &$result, array $path = [] ) { |
| 1454 | if ( $result instanceof ApiResult ) { |
| 1455 | $data = $result->getResultData( $path ); |
| 1456 | if ( $data === null ) { |
| 1457 | return true; |
| 1458 | } |
| 1459 | } else { |
| 1460 | $data = &$result; |
| 1461 | foreach ( $path as $key ) { |
| 1462 | if ( !isset( $data[$key] ) ) { |
| 1463 | // Path isn't in $result, so nothing to add, so everything |
| 1464 | // "fits" |
| 1465 | return true; |
| 1466 | } |
| 1467 | $data = &$data[$key]; |
| 1468 | } |
| 1469 | } |
| 1470 | foreach ( $this->mGeneratorData as $ns => $dbkeys ) { |
| 1471 | if ( $ns === NS_SPECIAL ) { |
| 1472 | $pages = []; |
| 1473 | foreach ( $this->mSpecialTitles as $id => $title ) { |
| 1474 | $pages[$title->getDBkey()] = $id; |
| 1475 | } |
| 1476 | } else { |
| 1477 | if ( !isset( $this->mAllPages[$ns] ) ) { |
| 1478 | // No known titles in the whole namespace. Skip it. |
| 1479 | continue; |
| 1480 | } |
| 1481 | $pages = $this->mAllPages[$ns]; |
| 1482 | } |
| 1483 | foreach ( $dbkeys as $dbkey => $genData ) { |
| 1484 | if ( !isset( $pages[$dbkey] ) ) { |
| 1485 | // Unknown title. Forget it. |
| 1486 | continue; |
| 1487 | } |
| 1488 | $pageId = $pages[$dbkey]; |
| 1489 | if ( !isset( $data[$pageId] ) ) { |
| 1490 | // $pageId didn't make it into the result. Ignore it. |
| 1491 | continue; |
| 1492 | } |
| 1493 | |
| 1494 | if ( $result instanceof ApiResult ) { |
| 1495 | $path2 = array_merge( $path, [ $pageId ] ); |
| 1496 | foreach ( $genData as $key => $value ) { |
| 1497 | if ( !$result->addValue( $path2, $key, $value ) ) { |
| 1498 | return false; |
| 1499 | } |
| 1500 | } |
| 1501 | } else { |
| 1502 | $data[$pageId] = array_merge( $data[$pageId], $genData ); |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | // Merge data generated about redirect titles into the redirect destination |
| 1508 | if ( $this->mRedirectMergePolicy ) { |
| 1509 | foreach ( $this->mResolvedRedirectTitles as $titleFrom ) { |
| 1510 | $dest = $this->resolveRedirectTitleDest( $titleFrom ); |
| 1511 | $fromNs = $titleFrom->getNamespace(); |
| 1512 | $fromDBkey = $titleFrom->getDBkey(); |
| 1513 | $toPageId = $dest->getArticleID(); |
| 1514 | if ( isset( $data[$toPageId] ) && |
| 1515 | isset( $this->mGeneratorData[$fromNs][$fromDBkey] ) |
| 1516 | ) { |
| 1517 | // It is necessary to set both $data and add to $result, if an ApiResult, |
| 1518 | // to ensure multiple redirects to the same destination are all merged. |
| 1519 | $data[$toPageId] = ( $this->mRedirectMergePolicy )( |
| 1520 | $data[$toPageId], |
| 1521 | $this->mGeneratorData[$fromNs][$fromDBkey] |
| 1522 | ); |
| 1523 | if ( $result instanceof ApiResult && |
| 1524 | !$result->addValue( $path, $toPageId, $data[$toPageId], ApiResult::OVERRIDE ) |
| 1525 | ) { |
| 1526 | return false; |
| 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | return true; |
| 1533 | } |
| 1534 | |
| 1535 | /** |
| 1536 | * Get the database connection (read-only) |
| 1537 | * @return \Wikimedia\Rdbms\IReadableDatabase |
| 1538 | */ |
| 1539 | protected function getDB() { |
| 1540 | return $this->mDbSource->getDB(); |
| 1541 | } |
| 1542 | |
| 1543 | /** @inheritDoc */ |
| 1544 | public function getAllowedParams( $flags = 0 ) { |
| 1545 | $result = [ |
| 1546 | 'titles' => [ |
| 1547 | ParamValidator::PARAM_ISMULTI => true, |
| 1548 | ApiBase::PARAM_HELP_MSG => 'api-pageset-param-titles', |
| 1549 | ], |
| 1550 | 'pageids' => [ |
| 1551 | ParamValidator::PARAM_TYPE => 'integer', |
| 1552 | ParamValidator::PARAM_ISMULTI => true, |
| 1553 | ApiBase::PARAM_HELP_MSG => 'api-pageset-param-pageids', |
| 1554 | ], |
| 1555 | 'revids' => [ |
| 1556 | ParamValidator::PARAM_TYPE => 'integer', |
| 1557 | ParamValidator::PARAM_ISMULTI => true, |
| 1558 | ApiBase::PARAM_HELP_MSG => 'api-pageset-param-revids', |
| 1559 | ], |
| 1560 | 'generator' => [ |
| 1561 | ParamValidator::PARAM_TYPE => null, |
| 1562 | ApiBase::PARAM_HELP_MSG => 'api-pageset-param-generator', |
| 1563 | SubmoduleDef::PARAM_SUBMODULE_PARAM_PREFIX => 'g', |
| 1564 | ], |
| 1565 | 'redirects' => [ |
| 1566 | ParamValidator::PARAM_DEFAULT => false, |
| 1567 | ApiBase::PARAM_HELP_MSG => $this->mAllowGenerator |
| 1568 | ? 'api-pageset-param-redirects-generator' |
| 1569 | : 'api-pageset-param-redirects-nogenerator', |
| 1570 | ], |
| 1571 | 'converttitles' => [ |
| 1572 | ParamValidator::PARAM_DEFAULT => false, |
| 1573 | ApiBase::PARAM_HELP_MSG => [ |
| 1574 | 'api-pageset-param-converttitles', |
| 1575 | Message::listParam( LanguageConverter::$languagesWithVariants, ListType::AND ), |
| 1576 | ], |
| 1577 | ], |
| 1578 | ]; |
| 1579 | |
| 1580 | if ( !$this->mAllowGenerator ) { |
| 1581 | unset( $result['generator'] ); |
| 1582 | } elseif ( $flags & ApiBase::GET_VALUES_FOR_HELP ) { |
| 1583 | $result['generator'][ParamValidator::PARAM_TYPE] = 'submodule'; |
| 1584 | $result['generator'][SubmoduleDef::PARAM_SUBMODULE_MAP] = $this->getGenerators(); |
| 1585 | } |
| 1586 | |
| 1587 | return $result; |
| 1588 | } |
| 1589 | |
| 1590 | /** @inheritDoc */ |
| 1591 | public function handleParamNormalization( $paramName, $value, $rawValue ) { |
| 1592 | parent::handleParamNormalization( $paramName, $value, $rawValue ); |
| 1593 | |
| 1594 | if ( $paramName === 'titles' ) { |
| 1595 | // For the 'titles' parameter, we want to split it like ApiBase would |
| 1596 | // and add any changed titles to $this->mNormalizedTitles |
| 1597 | $value = ParamValidator::explodeMultiValue( $value, self::LIMIT_SML2 + 1 ); |
| 1598 | $l = count( $value ); |
| 1599 | $rawValue = ParamValidator::explodeMultiValue( $rawValue, $l ); |
| 1600 | for ( $i = 0; $i < $l; $i++ ) { |
| 1601 | if ( $value[$i] !== $rawValue[$i] ) { |
| 1602 | $this->mNormalizedTitles[$rawValue[$i]] = $value[$i]; |
| 1603 | } |
| 1604 | } |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | /** |
| 1609 | * Get an array of all available generators |
| 1610 | * @return array<string,string> |
| 1611 | */ |
| 1612 | private function getGenerators() { |
| 1613 | if ( self::$generators === null ) { |
| 1614 | $query = $this->mDbSource; |
| 1615 | if ( !( $query instanceof ApiQuery ) ) { |
| 1616 | // If the parent container of this pageset is not ApiQuery, |
| 1617 | // we must create it to get module manager |
| 1618 | $query = $this->getMain()->getModuleManager()->getModule( 'query' ); |
| 1619 | } |
| 1620 | $gens = []; |
| 1621 | $prefix = $query->getModulePath() . '+'; |
| 1622 | $mgr = $query->getModuleManager(); |
| 1623 | foreach ( $mgr->getNamesWithClasses() as $name => $class ) { |
| 1624 | if ( is_subclass_of( $class, ApiQueryGeneratorBase::class ) ) { |
| 1625 | $gens[$name] = $prefix . $name; |
| 1626 | } |
| 1627 | } |
| 1628 | ksort( $gens ); |
| 1629 | self::$generators = $gens; |
| 1630 | } |
| 1631 | |
| 1632 | return self::$generators; |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | /** @deprecated class alias since 1.43 */ |
| 1637 | class_alias( ApiPageSet::class, 'ApiPageSet' ); |