| 1 |
<?php |
| 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 |
use MediaWiki\Title\Title; |
| 33 |
use MediaWiki\User\User; |
| 34 |
|
| 35 |
|
| 36 |
require_once __DIR__ . '/TableCleanup.php'; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class CleanupCaps extends TableCleanup { |
| 46 |
|
| 47 |
|
| 48 |
private $user; |
| 49 |
|
| 50 |
private $namespace; |
| 51 |
|
| 52 |
public function __construct() { |
| 53 |
parent::__construct(); |
| 54 |
$this->addDescription( 'Script to cleanup capitalization' ); |
| 55 |
$this->addOption( 'namespace', 'Namespace number to run caps cleanup on', false, true ); |
| 56 |
} |
| 57 |
|
| 58 |
public function execute() { |
| 59 |
$this->user = User::newSystemUser( 'Conversion script', [ 'steal' => true ] ); |
| 60 |
|
| 61 |
$this->namespace = intval( $this->getOption( 'namespace', 0 ) ); |
| 62 |
|
| 63 |
if ( |
| 64 |
$this->getServiceContainer()->getNamespaceInfo()-> |
| 65 |
isCapitalized( $this->namespace ) |
| 66 |
) { |
| 67 |
$this->output( "Will be moving pages to first letter capitalized titles" ); |
| 68 |
$callback = 'processRowToUppercase'; |
| 69 |
} else { |
| 70 |
$this->output( "Will be moving pages to first letter lowercase titles" ); |
| 71 |
$callback = 'processRowToLowercase'; |
| 72 |
} |
| 73 |
|
| 74 |
$this->dryrun = $this->hasOption( 'dry-run' ); |
| 75 |
|
| 76 |
$this->runTable( [ |
| 77 |
'table' => 'page', |
| 78 |
'conds' => [ 'page_namespace' => $this->namespace ], |
| 79 |
'index' => 'page_id', |
| 80 |
'callback' => $callback ] ); |
| 81 |
} |
| 82 |
|
| 83 |
protected function processRowToUppercase( $row ) { |
| 84 |
$current = Title::makeTitle( $row->page_namespace, $row->page_title ); |
| 85 |
$display = $current->getPrefixedText(); |
| 86 |
$lower = $row->page_title; |
| 87 |
$upper = $this->getServiceContainer()->getContentLanguage()->ucfirst( $row->page_title ); |
| 88 |
if ( $upper == $lower ) { |
| 89 |
$this->output( "\"$display\" already uppercase.\n" ); |
| 90 |
|
| 91 |
$this->progress( 0 ); |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
$target = Title::makeTitle( $row->page_namespace, $upper ); |
| 96 |
if ( $target->exists() ) { |
| 97 |
|
| 98 |
$target = Title::newFromText( 'CapsCleanup/' . $display ); |
| 99 |
} |
| 100 |
$ok = $this->movePage( |
| 101 |
$current, |
| 102 |
|
| 103 |
$target, |
| 104 |
'Converting page title to first-letter uppercase', |
| 105 |
false |
| 106 |
); |
| 107 |
if ( $ok ) { |
| 108 |
$this->progress( 1 ); |
| 109 |
if ( $row->page_namespace == $this->namespace ) { |
| 110 |
$talk = $target->getTalkPage(); |
| 111 |
$row->page_namespace = $talk->getNamespace(); |
| 112 |
if ( $talk->exists() ) { |
| 113 |
$this->processRowToUppercase( $row ); |
| 114 |
return; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
$this->progress( 0 ); |
| 120 |
} |
| 121 |
|
| 122 |
protected function processRowToLowercase( $row ) { |
| 123 |
$current = Title::makeTitle( $row->page_namespace, $row->page_title ); |
| 124 |
$display = $current->getPrefixedText(); |
| 125 |
$upper = $row->page_title; |
| 126 |
$lower = $this->getServiceContainer()->getContentLanguage()->lcfirst( $row->page_title ); |
| 127 |
if ( $upper == $lower ) { |
| 128 |
$this->output( "\"$display\" already lowercase.\n" ); |
| 129 |
|
| 130 |
$this->progress( 0 ); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
$target = Title::makeTitle( $row->page_namespace, $lower ); |
| 135 |
if ( $target->exists() ) { |
| 136 |
$targetDisplay = $target->getPrefixedText(); |
| 137 |
$this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" ); |
| 138 |
|
| 139 |
$this->progress( 0 ); |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
$ok = $this->movePage( $current, $target, 'Converting page titles to lowercase', true ); |
| 144 |
if ( $ok === true ) { |
| 145 |
$this->progress( 1 ); |
| 146 |
if ( $row->page_namespace == $this->namespace ) { |
| 147 |
$talk = $target->getTalkPage(); |
| 148 |
$row->page_namespace = $talk->getNamespace(); |
| 149 |
if ( $talk->exists() ) { |
| 150 |
$this->processRowToLowercase( $row ); |
| 151 |
return; |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
$this->progress( 0 ); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private function movePage( Title $current, Title $target, $reason, $createRedirect ) { |
| 167 |
$display = $current->getPrefixedText(); |
| 168 |
$targetDisplay = $target->getPrefixedText(); |
| 169 |
|
| 170 |
if ( $this->dryrun ) { |
| 171 |
$this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" ); |
| 172 |
$ok = 'OK'; |
| 173 |
} else { |
| 174 |
$mp = $this->getServiceContainer()->getMovePageFactory() |
| 175 |
->newMovePage( $current, $target ); |
| 176 |
$status = $mp->move( $this->user, $reason, $createRedirect ); |
| 177 |
$ok = $status->isOK() ? 'OK' : 'FAILED'; |
| 178 |
$this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" ); |
| 179 |
if ( !$status->isOK() ) { |
| 180 |
$this->error( $status ); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return $ok === 'OK'; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
$maintClass = CleanupCaps::class; |
| 190 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 191 |
|