mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/ReplaceText
synced 2024-11-23 22:33:48 +00:00
Create HookRunner class and the hook handler interfaces
Rename existing HookRunner to HookHelper and create a new class HookRunner using the name described in core's Hooks.md Bug: T271025 Change-Id: I85e828226d3d259f93ab2a4eb29dd1fe535abc30
This commit is contained in:
parent
203564d43d
commit
2185037fc9
12
hooks.txt
12
hooks.txt
|
@ -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
|
|
@ -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 ) {
|
||||
|
|
|
@ -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 );
|
||||
}
|
22
src/Hooks/FilterPageTitlesForEditHook.php
Normal file
22
src/Hooks/FilterPageTitlesForEditHook.php
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\ReplaceText\Hooks;
|
||||
|
||||
use MediaWiki\Title\Title;
|
||||
|
||||
/**
|
||||
* This is a hook handler interface, see docs/Hooks.md in core.
|
||||
* Use the hook name "ReplaceTextFilterPageTitlesForEdit" to register handlers implementing this interface.
|
||||
*
|
||||
* @stable to implement
|
||||
* @ingroup Hooks
|
||||
*/
|
||||
interface FilterPageTitlesForEditHook {
|
||||
/**
|
||||
* Provides other extension the ability to avoid editing content on pages based on their titles.
|
||||
*
|
||||
* @param Title[] &$filteredTitles Array of page titles whose content will be edited
|
||||
* @return bool|void True or no return value to continue or false to abort
|
||||
*/
|
||||
public function onReplaceTextFilterPageTitlesForEdit( array &$filteredTitles );
|
||||
}
|
22
src/Hooks/FilterPageTitlesForRenameHook.php
Normal file
22
src/Hooks/FilterPageTitlesForRenameHook.php
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\ReplaceText\Hooks;
|
||||
|
||||
use MediaWiki\Title\Title;
|
||||
|
||||
/**
|
||||
* This is a hook handler interface, see docs/Hooks.md in core.
|
||||
* Use the hook name "ReplaceTextFilterPageTitlesForRename" to register handlers implementing this interface.
|
||||
*
|
||||
* @stable to implement
|
||||
* @ingroup Hooks
|
||||
*/
|
||||
interface FilterPageTitlesForRenameHook {
|
||||
/**
|
||||
* Provides other extension the ability to avoid renaming pages based on their titles.
|
||||
*
|
||||
* @param Title[] &$filteredTitles Array of page titles being renamed
|
||||
* @return bool|void True or no return value to continue or false to abort
|
||||
*/
|
||||
public function onReplaceTextFilterPageTitlesForRename( array &$filteredTitles );
|
||||
}
|
40
src/Hooks/HookRunner.php
Normal file
40
src/Hooks/HookRunner.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\ReplaceText\Hooks;
|
||||
|
||||
use MediaWiki\HookContainer\HookContainer;
|
||||
|
||||
/**
|
||||
* This is a hook runner class, see docs/Hooks.md in core.
|
||||
* @internal
|
||||
*/
|
||||
class HookRunner implements
|
||||
FilterPageTitlesForEditHook,
|
||||
FilterPageTitlesForRenameHook
|
||||
{
|
||||
private HookContainer $hookContainer;
|
||||
|
||||
public function __construct( HookContainer $hookContainer ) {
|
||||
$this->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 ]
|
||||
);
|
||||
}
|
||||
}
|
|
@ -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 )
|
||||
|
|
16
tests/phpunit/unit/HookRunnerTest.php
Normal file
16
tests/phpunit/unit/HookRunnerTest.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\ReplaceText\Tests\Unit;
|
||||
|
||||
use MediaWiki\Extension\ReplaceText\Hooks\HookRunner;
|
||||
use MediaWiki\Tests\HookContainer\HookRunnerTestBase;
|
||||
|
||||
/**
|
||||
* @covers \MediaWiki\Extension\ReplaceText\Hooks\HookRunner
|
||||
*/
|
||||
class HookRunnerTest extends HookRunnerTestBase {
|
||||
|
||||
public static function provideHookRunners() {
|
||||
yield HookRunner::class => [ HookRunner::class ];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue