From 619d62310481dd36f751fb5c77d2368f20e4cabe Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Wed, 21 Jun 2023 23:03:44 +0200 Subject: [PATCH] ParserFileProcessing: Inject TitleFactory Check the parsed title if valid and can exists on the database Change-Id: I32805de6ab4b9dca7d56faf982762f6949fc3203 --- extension.json | 2 +- .../Hooks/ParserFileProcessingHookHandlers.php | 17 +++++++++++++---- .../ParserFileProcessingHookHandlersTest.php | 6 +++++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/extension.json b/extension.json index 4fca4c3..ee376ce 100644 --- a/extension.json +++ b/extension.json @@ -40,7 +40,7 @@ }, "parser": { "class": "PageImages\\Hooks\\ParserFileProcessingHookHandlers", - "services": [ "RepoGroup", "MainWANObjectCache", "HttpRequestFactory", "DBLoadBalancerFactory" ] + "services": [ "RepoGroup", "MainWANObjectCache", "HttpRequestFactory", "DBLoadBalancerFactory", "TitleFactory" ] }, "search": { "class": "PageImages\\Hooks\\SearchResultProvideThumbnailHookHandler", diff --git a/includes/Hooks/ParserFileProcessingHookHandlers.php b/includes/Hooks/ParserFileProcessingHookHandlers.php index 62597b4..fc401b6 100644 --- a/includes/Hooks/ParserFileProcessingHookHandlers.php +++ b/includes/Hooks/ParserFileProcessingHookHandlers.php @@ -17,7 +17,7 @@ use Parser; use ParserOutput; use RepoGroup; use RuntimeException; -use Title; +use TitleFactory; use WANObjectCache; use Wikimedia\Rdbms\IConnectionProvider; @@ -57,22 +57,28 @@ class ParserFileProcessingHookHandlers implements /** @var IConnectionProvider */ private $connectionProvider; + /** @var TitleFactory */ + private $titleFactory; + /** * @param RepoGroup $repoGroup * @param WANObjectCache $mainWANObjectCache * @param HttpRequestFactory $httpRequestFactory * @param IConnectionProvider $connectionProvider + * @param TitleFactory $titleFactory */ public function __construct( RepoGroup $repoGroup, WANObjectCache $mainWANObjectCache, HttpRequestFactory $httpRequestFactory, - IConnectionProvider $connectionProvider + IConnectionProvider $connectionProvider, + TitleFactory $titleFactory ) { $this->repoGroup = $repoGroup; $this->mainWANObjectCache = $mainWANObjectCache; $this->httpRequestFactory = $httpRequestFactory; $this->connectionProvider = $connectionProvider; + $this->titleFactory = $titleFactory; } /** @@ -457,7 +463,10 @@ class ParserFileProcessingHookHandlers implements * @return string[] */ private function getDbDenylist( $dbName, $page ) { - $title = Title::newFromText( $page ); + $title = $this->titleFactory->newFromText( $page ); + if ( !$title || !$title->canExist() ) { + return []; + } $dbr = $this->connectionProvider->getReplicaDatabase( $dbName ); $id = $dbr->newSelectQueryBuilder() @@ -494,7 +503,7 @@ class ParserFileProcessingHookHandlers implements if ( $text && preg_match_all( $regex, $text, $matches ) ) { foreach ( $matches[1] as $s ) { - $t = Title::makeTitleSafe( NS_FILE, $s ); + $t = $this->titleFactory->makeTitleSafe( NS_FILE, $s ); if ( $t ) { $list[] = $t->getDBkey(); diff --git a/tests/phpunit/Hooks/ParserFileProcessingHookHandlersTest.php b/tests/phpunit/Hooks/ParserFileProcessingHookHandlersTest.php index 28852e5..1c85ff3 100644 --- a/tests/phpunit/Hooks/ParserFileProcessingHookHandlersTest.php +++ b/tests/phpunit/Hooks/ParserFileProcessingHookHandlersTest.php @@ -12,6 +12,7 @@ use Parser; use ParserOptions; use RepoGroup; use Title; +use TitleFactory; use WANObjectCache; use Wikimedia\Rdbms\IConnectionProvider; use Wikimedia\TestingAccessWrapper; @@ -219,6 +220,7 @@ class ParserFileProcessingHookHandlersTest extends MediaWikiIntegrationTestCase $this->createMock( WANObjectCache::class ), $this->createMock( HttpRequestFactory::class ), $this->createMock( IConnectionProvider::class ), + $this->createMock( TitleFactory::class ), ] ) ->onlyMethods( [ 'scoreFromTable', 'fetchFileMetadata', 'getRatio', 'getDenylist' ] ) ->getMock() @@ -291,7 +293,8 @@ class ParserFileProcessingHookHandlersTest extends MediaWikiIntegrationTestCase $this->getRepoGroup(), $this->createMock( WANObjectCache::class ), $this->createMock( HttpRequestFactory::class ), - $this->createMock( IConnectionProvider::class ) + $this->createMock( IConnectionProvider::class ), + $this->createMock( TitleFactory::class ) ) ); @@ -356,6 +359,7 @@ class ParserFileProcessingHookHandlersTest extends MediaWikiIntegrationTestCase $this->createMock( WANObjectCache::class ), $this->createMock( HttpRequestFactory::class ), $this->createMock( IConnectionProvider::class ), + $this->createMock( TitleFactory::class ), ] ) ->onlyMethods( [ 'fetchFileMetadata' ] ) ->getMock()