-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeRedirect.php
More file actions
76 lines (71 loc) · 2.46 KB
/
Copy pathmakeRedirect.php
File metadata and controls
76 lines (71 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
require_once( dirname( __FILE__ ) . '/Maintenance.php' );
class MakeRedirect extends Maintenance {
public function __construct() {
parent::__construct();
$this->addArg( 'from', 'Redirect from', true );
$this->addArg( 'to', 'Redirect to', true );
$this->addOption( 'bot', 'Mark edits as bot', false );
$this->addOption( 'no-edit', 'Do not edit existing pages', false );
$this->addOption( 'no-self', 'Do not create self redirects', false );
$this->addOption( 'no-red', 'Do not create redirects to red titles', false );
$this->addOption( 'text', 'Extra text to add, if supported', false );
$this->addOption( 'summary', 'Edit summary to use', false );
$this->addOption( 'force', 'Force a re-edit when no change of redirect target is made', false );
}
public function doMake( $fromText, $toText ) {
$fromTitle = Title::newFromText( $fromText );
$toTitle = Title::newFromText( $toText );
if ( !$fromTitle || !$toTitle ) {
return array( 1, 'invalid-title' );
}
if ( $fromTitle->equals( $toTitle ) && $this->hasOption( 'no-self' ) ) {
return array( 2, 'no-self' );
}
if ( !$toTitle->isKnown() && $this->hasOption( 'no-red' ) ) {
return array( 2, 'no-red' );
}
try {
$fromWikiPage = WikiPage::factory( $fromTitle );
} catch ( MWException $e ) {
return array( 1, 'invalid-page' );
}
if ( $fromWikiPage->exists() ) {
$currentRedir = $fromWikiPage->getRedirectTarget();
if ( $currentRedir && $currentRedir->equals( $toTitle ) ) {
if ( !$this->hasOption( 'force' ) ) {
return array( 0, 'no-change' );
}
} elseif ( $this->getOption( 'no-edit' ) ) {
return array( 2, 'no-edit' );
}
}
$contentHandler = ContentHandler::getForTitle( $fromTitle );
$redirectContent = $contentHandler->makeRedirectContent( $toTitle, $this->getOption( 'text', '' ) );
if ( $fromWikiPage->doEditContent( $redirectContent,
$this->getOption( 'summary', '' ),
$this->hasOption( 'bot' ) ? EDIT_SUPPRESS_RC : 0
)->isOK() ) {
return array( 0, 'success' );
} else {
return array( 4, 'failure' );
}
}
public function execute() {
$fromText = null;
$retVal = 0;
foreach ( $this->mArgs as $arg ) {
if ( $fromText === null ) {
$fromText = $arg;
} else {
list( $thisRetVal, $message ) = $this->doMake( $fromText, $arg );
$retVal = max( $retVal, $thisRetVal );
$this->output( "$message\n" );
$fromText = null;
}
}
die( $retVal );
}
}
$maintClass = "MakeRedirect";
require_once( RUN_MAINTENANCE_IF_MAIN );