17 public const NO_ERROR = 0;
18 public const UNKNOWN_PAGE = 1;
19 public const NS_TALK_UNSUPPORTED = 2;
20 public const RENAME_FAILED = 3;
21 public const INVALID_TITLE = 4;
23 private const IMPOSSIBLE =
null;
26 public function __construct( Title $source, Title $target ) {
27 $this->map[$source->getNamespace()] = [
28 $target->getNamespace(),
33 $sourceTalkPage = $source->getTalkPageIfDefined();
34 $targetTalkPage = $target->getTalkPageIfDefined();
35 if ( $sourceTalkPage ) {
36 if ( !$targetTalkPage ) {
37 $this->map[$sourceTalkPage->getNamespace()] = [
43 $this->map[$sourceTalkPage->getNamespace()] = [
44 $targetTalkPage->getNamespace(),
51 $this->map[NS_TRANSLATIONS] = [
53 $source->getPrefixedText(),
54 $target->getPrefixedText(),
57 $this->map[NS_TRANSLATIONS_TALK] = [
59 $source->getPrefixedText(),
60 $target->getPrefixedText(),
64 public function getNewTitle( Title $title ): Title {
65 $instructions = $this->map[$title->getNamespace()] ??
null;
66 if ( $instructions ===
null ) {
68 'Trying to move a page which is not part of the translatable page', self::UNKNOWN_PAGE
72 [ $newNamespace, $search, $replace ] = $instructions;
74 if ( $newNamespace === self::IMPOSSIBLE ) {
76 'Trying to move a talk page to a namespace which does not have talk pages',
77 self::NS_TALK_UNSUPPORTED
81 $oldTitleText = $title->getText();
86 if ( $oldTitleText === $replace ) {
87 return Title::makeTitleSafe( $newNamespace, $replace );
90 $searchQuoted = preg_quote( $search,
'~' );
91 $newText = preg_replace(
"~^$searchQuoted~", $replace, $oldTitleText, 1 );
94 if ( $oldTitleText === $newText && $newNamespace === $title->getNamespace() ) {
98 $title = Title::makeTitleSafe( $newNamespace, $newText );
99 if ( $title ===
null ) {