diff --git a/hooks.txt b/hooks.txt deleted file mode 100644 index b696fc93..00000000 --- a/hooks.txt +++ /dev/null @@ -1,12 +0,0 @@ -This document describes how event hooks work in the ReplaceText extension; - -For a more comprehensive guide to hooks, navigate to your root MediaWiki -directory, and then find /docs/Hooks.md. - -=== PHP events === - -;ReplaceTextFilterPageTitlesForEdit: Provides other extension the ability to avoid editing content on pages based on their titles. -\TitleArrayFromResult &$titles: Array of page titles whose content will be edited - -;ReplaceTextFilterPageTitlesForRename: Provides other extension the ability to avoid renaming pages based on their titles. -\TitleArrayFromResult &$titles: Array of page titles being renamed diff --git a/maintenance/replaceAll.php b/maintenance/replaceAll.php index 705e20f4..7a52e3df 100755 --- a/maintenance/replaceAll.php +++ b/maintenance/replaceAll.php @@ -375,6 +375,7 @@ EOF; $this->fatalError( 'No matching namespaces.' ); } + $hookHelper = new HookHelper( MediaWikiServices::getInstance()->getHookContainer() ); foreach ( $this->target as $index => $target ) { $replacement = $this->replacement[$index]; $useRegex = $this->useRegex[$index]; @@ -387,7 +388,6 @@ EOF; $this->output( ".\n" ); } - $hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ); if ( $this->rename ) { $res = Search::getMatchingTitles( $target, @@ -396,7 +396,7 @@ EOF; $this->prefix, $useRegex ); - $titlesToProcess = $hookRunner->filterPageTitlesForRename( $res ); + $titlesToProcess = $hookHelper->filterPageTitlesForRename( $res ); } else { $res = Search::doSearchQuery( $target, @@ -405,7 +405,7 @@ EOF; $this->prefix, $useRegex ); - $titlesToProcess = $hookRunner->filterPageTitlesForEdit( $res ); + $titlesToProcess = $hookHelper->filterPageTitlesForEdit( $res ); } if ( count( $titlesToProcess ) === 0 ) { diff --git a/src/HookRunner.php b/src/HookHelper.php similarity index 83% rename from src/HookRunner.php rename to src/HookHelper.php index bd99ac20..f21a4bc8 100644 --- a/src/HookRunner.php +++ b/src/HookHelper.php @@ -3,16 +3,17 @@ declare( strict_types = 1 ); namespace MediaWiki\Extension\ReplaceText; +use MediaWiki\Extension\ReplaceText\Hooks\HookRunner; use MediaWiki\HookContainer\HookContainer; use Title; use TitleArrayFromResult; use Wikimedia\Rdbms\IResultWrapper; -class HookRunner { - private HookContainer $hookContainer; +class HookHelper { + private HookRunner $hookRunner; public function __construct( HookContainer $hookContainer ) { - $this->hookContainer = $hookContainer; + $this->hookRunner = new HookRunner( $hookContainer ); } /** @@ -23,7 +24,7 @@ class HookRunner { public function filterPageTitlesForEdit( IResultWrapper $resultWrapper ): array { $titles = new TitleArrayFromResult( $resultWrapper ); $filteredTitles = iterator_to_array( $titles ); - $this->hookContainer->run( 'ReplaceTextFilterPageTitlesForEdit', [ &$filteredTitles ] ); + $this->hookRunner->onReplaceTextFilterPageTitlesForEdit( $filteredTitles ); return $this->normalizeTitlesToProcess( $filteredTitles, $titles ); } @@ -36,7 +37,7 @@ class HookRunner { public function filterPageTitlesForRename( IResultWrapper $resultWrapper ): array { $titles = new TitleArrayFromResult( $resultWrapper ); $filteredTitles = iterator_to_array( $titles ); - $this->hookContainer->run( 'ReplaceTextFilterPageTitlesForRename', [ &$filteredTitles ] ); + $this->hookRunner->onReplaceTextFilterPageTitlesForRename( $filteredTitles ); return $this->normalizeTitlesToProcess( $filteredTitles, $titles ); } diff --git a/src/Hooks/FilterPageTitlesForEditHook.php b/src/Hooks/FilterPageTitlesForEditHook.php new file mode 100644 index 00000000..cf8c0b27 --- /dev/null +++ b/src/Hooks/FilterPageTitlesForEditHook.php @@ -0,0 +1,22 @@ +hookContainer = $hookContainer; + } + + /** + * @inheritDoc + */ + public function onReplaceTextFilterPageTitlesForEdit( array &$filteredTitles ) { + return $this->hookContainer->run( + 'ReplaceTextFilterPageTitlesForEdit', + [ &$filteredTitles ] + ); + } + + /** + * @inheritDoc + */ + public function onReplaceTextFilterPageTitlesForRename( array &$filteredTitles ) { + return $this->hookContainer->run( + 'ReplaceTextFilterPageTitlesForRename', + [ &$filteredTitles ] + ); + } +} diff --git a/src/SpecialReplaceText.php b/src/SpecialReplaceText.php index 540dcf71..2ffd8074 100644 --- a/src/SpecialReplaceText.php +++ b/src/SpecialReplaceText.php @@ -52,8 +52,8 @@ class SpecialReplaceText extends SpecialPage { private $selected_namespaces; private $botEdit; - /** @var HookRunner */ - private $hookRunner; + /** @var HookHelper */ + private $hookHelper; /** @var Language */ private $contentLanguage; @@ -112,7 +112,7 @@ class SpecialReplaceText extends SpecialPage { UserOptionsLookup $userOptionsLookup ) { parent::__construct( 'ReplaceText', 'replacetext' ); - $this->hookRunner = new HookRunner( $hookContainer ); + $this->hookHelper = new HookHelper( $hookContainer ); $this->contentLanguage = $contentLanguage; $this->jobQueueGroup = $jobQueueGroup; $this->linkRenderer = $linkRenderer; @@ -399,7 +399,7 @@ class SpecialReplaceText extends SpecialPage { $this->use_regex ); - $titles_to_process = $this->hookRunner->filterPageTitlesForEdit( $res ); + $titles_to_process = $this->hookHelper->filterPageTitlesForEdit( $res ); $titles_to_skip = []; foreach ( $res as $row ) { @@ -443,7 +443,7 @@ class SpecialReplaceText extends SpecialPage { $this->use_regex ); - $titles_to_process = $this->hookRunner->filterPageTitlesForRename( $res ); + $titles_to_process = $this->hookHelper->filterPageTitlesForRename( $res ); foreach ( $res as $row ) { $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title ); @@ -507,7 +507,7 @@ class SpecialReplaceText extends SpecialPage { $this->prefix, $this->use_regex ); - $titles = $this->hookRunner->filterPageTitlesForEdit( $res ); + $titles = $this->hookHelper->filterPageTitlesForEdit( $res ); $count = count( $titles ); if ( $count > 0 ) { return $this->msg( 'replacetext_warning' )->numParams( $count ) @@ -521,7 +521,7 @@ class SpecialReplaceText extends SpecialPage { $this->prefix, $this->use_regex ); - $titles = $this->hookRunner->filterPageTitlesForRename( $res ); + $titles = $this->hookHelper->filterPageTitlesForRename( $res ); $count = count( $titles ); if ( $count > 0 ) { return $this->msg( 'replacetext_warning' )->numParams( $count ) diff --git a/tests/phpunit/unit/HookRunnerTest.php b/tests/phpunit/unit/HookRunnerTest.php new file mode 100644 index 00000000..0d9cd179 --- /dev/null +++ b/tests/phpunit/unit/HookRunnerTest.php @@ -0,0 +1,16 @@ + [ HookRunner::class ]; + } +}