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;
27 public function __construct( Title $source, Title $target ) {
28 $this->map[$source->getNamespace()] = [
29 $target->getNamespace(),
34 $sourceTalkPage = $source->getTalkPageIfDefined();
35 $targetTalkPage = $target->getTalkPageIfDefined();
36 if ( $sourceTalkPage ) {
37 if ( !$targetTalkPage ) {
38 $this->map[$sourceTalkPage->getNamespace()] = [
44 $this->map[$sourceTalkPage->getNamespace()] = [
45 $targetTalkPage->getNamespace(),
52 $this->map[NS_TRANSLATIONS] = [
54 $source->getPrefixedText(),
55 $target->getPrefixedText(),
58 $this->map[NS_TRANSLATIONS_TALK] = [
60 $source->getPrefixedText(),
61 $target->getPrefixedText(),
65 public function getNewTitle( Title $title ): Title {
66 $instructions = $this->map[$title->getNamespace()] ??
null;
67 if ( $instructions ===
null ) {
69 'Trying to move a page which is not part of the translatable page', self::UNKNOWN_PAGE
73 [ $newNamespace, $search, $replace ] = $instructions;
75 if ( $newNamespace === self::IMPOSSIBLE ) {
77 'Trying to move a talk page to a namespace which does not have talk pages',
78 self::NS_TALK_UNSUPPORTED
82 $oldTitleText = $title->getText();
87 if ( $oldTitleText === $replace ) {
88 return Title::makeTitleSafe( $newNamespace, $replace );
91 $searchQuoted = preg_quote( $search,
'~' );
92 $newText = preg_replace(
"~^$searchQuoted~", $replace, $oldTitleText, 1 );
95 if ( $oldTitleText === $newText && $newNamespace === $title->getNamespace() ) {
99 $title = Title::makeTitleSafe( $newNamespace, $newText );
100 if ( $title ===
null ) {