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(),
67 $instructions = $this->map[$title->getNamespace()] ?? null;
68 if ( $instructions ===
null ) {
70 'Trying to move a page which is not part of the translatable page', self::UNKNOWN_PAGE
74 [ $newNamespace, $search, $replace ] = $instructions;
76 if ( $newNamespace === self::IMPOSSIBLE ) {
78 'Trying to move a talk page to a namespace which does not have talk pages',
79 self::NS_TALK_UNSUPPORTED
83 $oldTitleText = $title->getText();
88 if ( $oldTitleText === $replace ) {
89 return Title::makeTitleSafe( $newNamespace, $replace );
92 $searchQuoted = preg_quote( $search,
'~' );
93 $newText = preg_replace(
"~^$searchQuoted~", $replace, $oldTitleText, 1 );
96 if ( $oldTitleText === $newText && $newNamespace === $title->getNamespace() ) {
97 throw new InvalidPageTitleRename(
'Renaming failed', self::RENAME_FAILED );
100 $title = Title::makeTitleSafe( $newNamespace, $newText );
101 if ( $title ===
null ) {
102 throw new InvalidPageTitleRename(
'Invalid target title', self::INVALID_TITLE );