mediawiki-extensions-Replac.../src/HookHelper.php
TechieNK bc85753b87 Fix documentation problem
Change-Id: I5015da83b84b89cd965d97f418a31b0e6116abd3
2023-09-21 20:58:30 +05:30

66 lines
2 KiB
PHP

<?php
declare( strict_types = 1 );
namespace MediaWiki\Extension\ReplaceText;
use MediaWiki\Extension\ReplaceText\Hooks\HookRunner;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleArrayFromResult;
use Wikimedia\Rdbms\IResultWrapper;
class HookHelper {
private HookRunner $hookRunner;
/**
* Constructor.
* @param HookContainer $hookContainer
*/
public function __construct( HookContainer $hookContainer ) {
$this->hookRunner = new HookRunner( $hookContainer );
}
/**
* Runs the ReplaceTextFilterPageTitlesForEdit hook and returns titles to be edited
* @param IResultWrapper $resultWrapper
* @return Title[]
*/
public function filterPageTitlesForEdit( IResultWrapper $resultWrapper ): array {
$titles = new TitleArrayFromResult( $resultWrapper );
$filteredTitles = iterator_to_array( $titles );
$this->hookRunner->onReplaceTextFilterPageTitlesForEdit( $filteredTitles );
return $this->normalizeTitlesToProcess( $filteredTitles, $titles );
}
/**
* Runs the ReplaceTextFilterPageTitlesForRename hook and returns titles to be edited
* @param IResultWrapper $resultWrapper
* @return Title[]
*/
public function filterPageTitlesForRename( IResultWrapper $resultWrapper ): array {
$titles = new TitleArrayFromResult( $resultWrapper );
$filteredTitles = iterator_to_array( $titles );
$this->hookRunner->onReplaceTextFilterPageTitlesForRename( $filteredTitles );
return $this->normalizeTitlesToProcess( $filteredTitles, $titles );
}
private function normalizeTitlesToProcess( array $filteredTitles, TitleArrayFromResult $titles ): array {
foreach ( $filteredTitles as $title ) {
$filteredTitles[ $title->getPrefixedText() ] = $title;
}
$titlesToEdit = [];
foreach ( $titles as $title ) {
if ( isset( $filteredTitles[ $title->getPrefixedText() ] ) ) {
$titlesToEdit[ $title->getPrefixedText() ] = $title;
} else {
$titlesToEdit[ $title->getPrefixedText() ] = null;
}
}
return $titlesToEdit;
}
}