2023-05-19 12:58:45 +00:00
|
|
|
<?php
|
|
|
|
declare( strict_types = 1 );
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\ReplaceText;
|
|
|
|
|
2023-08-15 20:40:53 +00:00
|
|
|
use MediaWiki\Extension\ReplaceText\Hooks\HookRunner;
|
2023-05-19 12:58:45 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
2023-08-19 18:18:31 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-08-19 22:06:05 +00:00
|
|
|
use MediaWiki\Title\TitleArrayFromResult;
|
2023-05-19 12:58:45 +00:00
|
|
|
use Wikimedia\Rdbms\IResultWrapper;
|
|
|
|
|
2023-08-15 20:40:53 +00:00
|
|
|
class HookHelper {
|
|
|
|
private HookRunner $hookRunner;
|
2023-05-19 12:58:45 +00:00
|
|
|
|
2023-09-21 15:28:13 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
* @param HookContainer $hookContainer
|
|
|
|
*/
|
2023-05-19 12:58:45 +00:00
|
|
|
public function __construct( HookContainer $hookContainer ) {
|
2023-08-15 20:40:53 +00:00
|
|
|
$this->hookRunner = new HookRunner( $hookContainer );
|
2023-05-19 12:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 );
|
2023-08-15 20:40:53 +00:00
|
|
|
$this->hookRunner->onReplaceTextFilterPageTitlesForEdit( $filteredTitles );
|
2023-05-19 12:58:45 +00:00
|
|
|
|
2023-05-24 08:27:17 +00:00
|
|
|
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 );
|
2023-08-15 20:40:53 +00:00
|
|
|
$this->hookRunner->onReplaceTextFilterPageTitlesForRename( $filteredTitles );
|
2023-05-24 08:27:17 +00:00
|
|
|
|
|
|
|
return $this->normalizeTitlesToProcess( $filteredTitles, $titles );
|
|
|
|
}
|
|
|
|
|
|
|
|
private function normalizeTitlesToProcess( array $filteredTitles, TitleArrayFromResult $titles ): array {
|
2023-05-19 12:58:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|