Replace deprecated ApiPageSet::getGoodTitles

PageIdentity does not have inNamespace() (it is from LinkTarget)
PageIdentity does not have getContentModel(), use the WikiPage instead
Inject a TitleFormatter to get the prefixed title text

Bug: T339384
Change-Id: I0029e718f20ca01ee3cd13ada8be04a16480d51d
This commit is contained in:
Umherirrender 2024-06-30 13:30:52 +02:00
parent e623c552d2
commit 290be2e8de
3 changed files with 20 additions and 13 deletions

View file

@ -20,7 +20,8 @@
"ConfigFactory", "ConfigFactory",
"MainWANObjectCache", "MainWANObjectCache",
"LanguageConverterFactory", "LanguageConverterFactory",
"WikiPageFactory" "WikiPageFactory",
"TitleFormatter"
] ]
} }
}, },

View file

@ -10,8 +10,9 @@ use MediaWiki\Config\ConfigFactory;
use MediaWiki\Languages\LanguageConverterFactory; use MediaWiki\Languages\LanguageConverterFactory;
use MediaWiki\Logger\LoggerFactory; use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Page\WikiPageFactory; use MediaWiki\Page\WikiPageFactory;
use MediaWiki\Title\Title; use MediaWiki\Title\TitleFormatter;
use ParserOptions; use ParserOptions;
use WANObjectCache; use WANObjectCache;
use Wikimedia\ParamValidator\ParamValidator; use Wikimedia\ParamValidator\ParamValidator;
@ -50,6 +51,7 @@ class ApiQueryExtracts extends ApiQueryBase {
* @var WikiPageFactory * @var WikiPageFactory
*/ */
private $wikiPageFactory; private $wikiPageFactory;
private TitleFormatter $titleFormatter;
// TODO: Allow extensions to hook into this to opt-in. // TODO: Allow extensions to hook into this to opt-in.
// This is partly for security reasons; see T107170. // This is partly for security reasons; see T107170.
@ -65,6 +67,7 @@ class ApiQueryExtracts extends ApiQueryBase {
* @param WANObjectCache $cache * @param WANObjectCache $cache
* @param LanguageConverterFactory $langConvFactory * @param LanguageConverterFactory $langConvFactory
* @param WikiPageFactory $wikiPageFactory * @param WikiPageFactory $wikiPageFactory
* @param TitleFormatter $titleFormatter
*/ */
public function __construct( public function __construct(
$query, $query,
@ -72,13 +75,15 @@ class ApiQueryExtracts extends ApiQueryBase {
ConfigFactory $configFactory, ConfigFactory $configFactory,
WANObjectCache $cache, WANObjectCache $cache,
LanguageConverterFactory $langConvFactory, LanguageConverterFactory $langConvFactory,
WikiPageFactory $wikiPageFactory WikiPageFactory $wikiPageFactory,
TitleFormatter $titleFormatter
) { ) {
parent::__construct( $query, $moduleName, self::PREFIX ); parent::__construct( $query, $moduleName, self::PREFIX );
$this->config = $configFactory->makeConfig( 'textextracts' ); $this->config = $configFactory->makeConfig( 'textextracts' );
$this->cache = $cache; $this->cache = $cache;
$this->langConvFactory = $langConvFactory; $this->langConvFactory = $langConvFactory;
$this->wikiPageFactory = $wikiPageFactory; $this->wikiPageFactory = $wikiPageFactory;
$this->titleFormatter = $titleFormatter;
} }
/** /**
@ -86,7 +91,7 @@ class ApiQueryExtracts extends ApiQueryBase {
* and sets up the result * and sets up the result
*/ */
public function execute() { public function execute() {
$titles = $this->getPageSet()->getGoodTitles(); $titles = $this->getPageSet()->getGoodPages();
if ( $titles === [] ) { if ( $titles === [] ) {
return; return;
} }
@ -108,14 +113,14 @@ class ApiQueryExtracts extends ApiQueryBase {
} }
$count = 0; $count = 0;
$titleInFileNamespace = false; $titleInFileNamespace = false;
/** @var Title $t */ /** @var PageIdentity $t */
foreach ( $titles as $id => $t ) { foreach ( $titles as $id => $t ) {
if ( ++$count > $limit ) { if ( ++$count > $limit ) {
$this->setContinueEnumParameter( 'continue', $continue + $count - 1 ); $this->setContinueEnumParameter( 'continue', $continue + $count - 1 );
break; break;
} }
if ( $t->inNamespace( NS_FILE ) ) { if ( $t->getNamespace() === NS_FILE ) {
$text = ''; $text = '';
$titleInFileNamespace = true; $titleInFileNamespace = true;
} else { } else {
@ -157,22 +162,22 @@ class ApiQueryExtracts extends ApiQueryBase {
/** /**
* Returns a processed, but not trimmed extract * Returns a processed, but not trimmed extract
* @param Title $title * @param PageIdentity $title
* @return string * @return string
*/ */
private function getExtract( Title $title ) { private function getExtract( PageIdentity $title ) {
$contentModel = $title->getContentModel(); $page = $this->wikiPageFactory->newFromTitle( $title );
$contentModel = $page->getContentModel();
if ( !in_array( $contentModel, $this->supportedContentModels, true ) ) { if ( !in_array( $contentModel, $this->supportedContentModels, true ) ) {
$this->addWarning( [ $this->addWarning( [
'apiwarn-textextracts-unsupportedmodel', 'apiwarn-textextracts-unsupportedmodel',
wfEscapeWikiText( $title->getPrefixedText() ), wfEscapeWikiText( $this->titleFormatter->getPrefixedText( $title ) ),
$contentModel $contentModel
] ); ] );
return ''; return '';
} }
$page = $this->wikiPageFactory->newFromTitle( $title );
$introOnly = $this->params['intro']; $introOnly = $this->params['intro'];
$text = $this->getFromCache( $page, $introOnly ); $text = $this->getFromCache( $page, $introOnly );
// if we need just first section, try retrieving full page and getting first section out of it // if we need just first section, try retrieving full page and getting first section out of it

View file

@ -68,7 +68,8 @@ class ApiQueryExtractsTest extends \MediaWikiIntegrationTestCase {
$configFactory, $configFactory,
$cache, $cache,
$langConvFactory, $langConvFactory,
$this->getServiceContainer()->getWikiPageFactory() $this->getServiceContainer()->getWikiPageFactory(),
$this->getServiceContainer()->getTitleFormatter()
); );
} }