From 290be2e8dec4ab927d8969f82f0036979abdd0c8 Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Sun, 30 Jun 2024 13:30:52 +0200 Subject: [PATCH] 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 --- extension.json | 3 ++- includes/ApiQueryExtracts.php | 27 +++++++++++++++----------- tests/phpunit/ApiQueryExtractsTest.php | 3 ++- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/extension.json b/extension.json index 0a9f21b..abfb0ab 100644 --- a/extension.json +++ b/extension.json @@ -20,7 +20,8 @@ "ConfigFactory", "MainWANObjectCache", "LanguageConverterFactory", - "WikiPageFactory" + "WikiPageFactory", + "TitleFormatter" ] } }, diff --git a/includes/ApiQueryExtracts.php b/includes/ApiQueryExtracts.php index e0550d1..1508eff 100644 --- a/includes/ApiQueryExtracts.php +++ b/includes/ApiQueryExtracts.php @@ -10,8 +10,9 @@ use MediaWiki\Config\ConfigFactory; use MediaWiki\Languages\LanguageConverterFactory; use MediaWiki\Logger\LoggerFactory; use MediaWiki\MediaWikiServices; +use MediaWiki\Page\PageIdentity; use MediaWiki\Page\WikiPageFactory; -use MediaWiki\Title\Title; +use MediaWiki\Title\TitleFormatter; use ParserOptions; use WANObjectCache; use Wikimedia\ParamValidator\ParamValidator; @@ -50,6 +51,7 @@ class ApiQueryExtracts extends ApiQueryBase { * @var WikiPageFactory */ private $wikiPageFactory; + private TitleFormatter $titleFormatter; // TODO: Allow extensions to hook into this to opt-in. // This is partly for security reasons; see T107170. @@ -65,6 +67,7 @@ class ApiQueryExtracts extends ApiQueryBase { * @param WANObjectCache $cache * @param LanguageConverterFactory $langConvFactory * @param WikiPageFactory $wikiPageFactory + * @param TitleFormatter $titleFormatter */ public function __construct( $query, @@ -72,13 +75,15 @@ class ApiQueryExtracts extends ApiQueryBase { ConfigFactory $configFactory, WANObjectCache $cache, LanguageConverterFactory $langConvFactory, - WikiPageFactory $wikiPageFactory + WikiPageFactory $wikiPageFactory, + TitleFormatter $titleFormatter ) { parent::__construct( $query, $moduleName, self::PREFIX ); $this->config = $configFactory->makeConfig( 'textextracts' ); $this->cache = $cache; $this->langConvFactory = $langConvFactory; $this->wikiPageFactory = $wikiPageFactory; + $this->titleFormatter = $titleFormatter; } /** @@ -86,7 +91,7 @@ class ApiQueryExtracts extends ApiQueryBase { * and sets up the result */ public function execute() { - $titles = $this->getPageSet()->getGoodTitles(); + $titles = $this->getPageSet()->getGoodPages(); if ( $titles === [] ) { return; } @@ -108,14 +113,14 @@ class ApiQueryExtracts extends ApiQueryBase { } $count = 0; $titleInFileNamespace = false; - /** @var Title $t */ + /** @var PageIdentity $t */ foreach ( $titles as $id => $t ) { if ( ++$count > $limit ) { $this->setContinueEnumParameter( 'continue', $continue + $count - 1 ); break; } - if ( $t->inNamespace( NS_FILE ) ) { + if ( $t->getNamespace() === NS_FILE ) { $text = ''; $titleInFileNamespace = true; } else { @@ -157,22 +162,22 @@ class ApiQueryExtracts extends ApiQueryBase { /** * Returns a processed, but not trimmed extract - * @param Title $title + * @param PageIdentity $title * @return string */ - private function getExtract( Title $title ) { - $contentModel = $title->getContentModel(); + private function getExtract( PageIdentity $title ) { + $page = $this->wikiPageFactory->newFromTitle( $title ); + + $contentModel = $page->getContentModel(); if ( !in_array( $contentModel, $this->supportedContentModels, true ) ) { $this->addWarning( [ 'apiwarn-textextracts-unsupportedmodel', - wfEscapeWikiText( $title->getPrefixedText() ), + wfEscapeWikiText( $this->titleFormatter->getPrefixedText( $title ) ), $contentModel ] ); return ''; } - $page = $this->wikiPageFactory->newFromTitle( $title ); - $introOnly = $this->params['intro']; $text = $this->getFromCache( $page, $introOnly ); // if we need just first section, try retrieving full page and getting first section out of it diff --git a/tests/phpunit/ApiQueryExtractsTest.php b/tests/phpunit/ApiQueryExtractsTest.php index e223f42..39d862f 100644 --- a/tests/phpunit/ApiQueryExtractsTest.php +++ b/tests/phpunit/ApiQueryExtractsTest.php @@ -68,7 +68,8 @@ class ApiQueryExtractsTest extends \MediaWikiIntegrationTestCase { $configFactory, $cache, $langConvFactory, - $this->getServiceContainer()->getWikiPageFactory() + $this->getServiceContainer()->getWikiPageFactory(), + $this->getServiceContainer()->getTitleFormatter() ); }