| 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 |
use MediaWiki\Title\Title; |
| 29 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 30 |
|
| 31 |
|
| 32 |
require_once __DIR__ . '/TableCleanup.php'; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class TitleCleanup extends TableCleanup { |
| 41 |
|
| 42 |
private string $prefix; |
| 43 |
|
| 44 |
public function __construct() { |
| 45 |
parent::__construct(); |
| 46 |
$this->addDescription( 'Script to clean up broken, unparseable titles' ); |
| 47 |
$this->addOption( 'prefix', "Broken pages will be renamed to titles with " . |
| 48 |
"<prefix> prepended before the article name. Defaults to 'Broken'", false, true ); |
| 49 |
$this->setBatchSize( 1000 ); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function execute() { |
| 56 |
$this->prefix = $this->getOption( 'prefix', 'Broken' ) . "/"; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
$title = Title::newFromText( $this->prefix ); |
| 63 |
if ( !$title || !$title->canExist() || $title->getInterwiki() || $title->getNamespace() !== 0 ) { |
| 64 |
$this->fatalError( "Invalid prefix {$this->prefix}. Must be a valid mainspace title." ); |
| 65 |
} |
| 66 |
parent::execute(); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
protected function processRow( $row ) { |
| 73 |
$display = Title::makeName( $row->page_namespace, $row->page_title ); |
| 74 |
$verified = $this->getServiceContainer()->getContentLanguage()->normalize( $display ); |
| 75 |
$title = Title::newFromText( $verified ); |
| 76 |
|
| 77 |
if ( $title !== null |
| 78 |
&& $title->canExist() |
| 79 |
&& $title->getNamespace() == $row->page_namespace |
| 80 |
&& $title->getDBkey() === $row->page_title |
| 81 |
) { |
| 82 |
|
| 83 |
$this->progress( 0 ); |
| 84 |
|
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
if ( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) { |
| 89 |
$this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" ); |
| 90 |
$this->progress( 0 ); |
| 91 |
} elseif ( $title === null ) { |
| 92 |
$this->output( "page $row->page_id ($display) is illegal.\n" ); |
| 93 |
$this->moveIllegalPage( $row ); |
| 94 |
$this->progress( 1 ); |
| 95 |
} else { |
| 96 |
$this->output( "page $row->page_id ($display) doesn't match self.\n" ); |
| 97 |
$this->moveInconsistentPage( $row, $title ); |
| 98 |
$this->progress( 1 ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
protected function fileExists( $name ) { |
| 107 |
|
| 108 |
|
| 109 |
$dbr = $this->getReplicaDB(); |
| 110 |
$row = $dbr->newSelectQueryBuilder() |
| 111 |
->select( '*' ) |
| 112 |
->from( 'image' ) |
| 113 |
->where( [ 'img_name' => $name ] ) |
| 114 |
->caller( __METHOD__ ) |
| 115 |
->fetchRow(); |
| 116 |
|
| 117 |
return $row !== false; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
protected function moveIllegalPage( $row ) { |
| 124 |
$legalChars = Title::legalChars(); |
| 125 |
$legalizedUnprefixed = preg_replace_callback( "/([^$legalChars])/", |
| 126 |
[ $this, 'hexChar' ], |
| 127 |
$row->page_title ); |
| 128 |
if ( $legalizedUnprefixed == '.' ) { |
| 129 |
$legalizedUnprefixed = '(dot)'; |
| 130 |
} |
| 131 |
if ( $legalizedUnprefixed == '_' ) { |
| 132 |
$legalizedUnprefixed = '(space)'; |
| 133 |
} |
| 134 |
$ns = (int)$row->page_namespace; |
| 135 |
|
| 136 |
$title = null; |
| 137 |
|
| 138 |
if ( $ns === 1 ) { |
| 139 |
$subjectTitle = Title::newFromText( $legalizedUnprefixed ); |
| 140 |
if ( $subjectTitle && !$subjectTitle->isTalkPage() ) { |
| 141 |
$talkTitle = $subjectTitle->getTalkPageIfDefined(); |
| 142 |
if ( $talkTitle !== null && !$talkTitle->exists() ) { |
| 143 |
$ns = $talkTitle->getNamespace(); |
| 144 |
$title = $talkTitle; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
if ( $title === null ) { |
| 150 |
|
| 151 |
|
| 152 |
if ( $ns !== 0 ) { |
| 153 |
$namespaceInfo = $this->getServiceContainer()->getNamespaceInfo(); |
| 154 |
$namespaceName = $namespaceInfo->getCanonicalName( $ns ); |
| 155 |
if ( $namespaceName === false ) { |
| 156 |
$namespaceName = "NS$ns"; |
| 157 |
} |
| 158 |
$ns = 0; |
| 159 |
$legalizedUnprefixed = "$namespaceName:$legalizedUnprefixed"; |
| 160 |
} |
| 161 |
$title = Title::newFromText( $this->prefix . $legalizedUnprefixed ); |
| 162 |
} |
| 163 |
|
| 164 |
if ( $title === null ) { |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
$legalizedUnprefixed = preg_replace_callback( '!([^A-Za-z0-9_:\\-])!', |
| 170 |
[ $this, 'hexChar' ], |
| 171 |
$legalizedUnprefixed |
| 172 |
); |
| 173 |
$title = Title::newFromText( $this->prefix . $legalizedUnprefixed ); |
| 174 |
} |
| 175 |
|
| 176 |
if ( $title === null ) { |
| 177 |
|
| 178 |
$clean = $this->prefix . 'id:' . $row->page_id; |
| 179 |
$legalized = $this->prefix . $legalizedUnprefixed; |
| 180 |
$this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" ); |
| 181 |
$title = Title::newFromText( $clean ); |
| 182 |
} elseif ( $title->exists( IDBAccessObject::READ_LATEST ) ) { |
| 183 |
$clean = $this->prefix . 'id:' . $row->page_id; |
| 184 |
$conflict = $title->getDBKey(); |
| 185 |
$this->output( "Legalized for '$conflict' exists; using '$clean'\n" ); |
| 186 |
$title = Title::newFromText( $clean ); |
| 187 |
} |
| 188 |
|
| 189 |
if ( !$title || $title->exists( IDBAccessObject::READ_LATEST ) ) { |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
$this->error( "Destination page {$title->getText()} is invalid or already exists, skipping." ); |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
$dest = $title->getDBkey(); |
| 198 |
if ( $this->dryrun ) { |
| 199 |
$this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," . |
| 200 |
"'$row->page_title') to ($ns,'$dest')\n" ); |
| 201 |
} else { |
| 202 |
$this->output( "renaming $row->page_id ($row->page_namespace," . |
| 203 |
"'$row->page_title') to ($ns,'$dest')\n" ); |
| 204 |
$this->getPrimaryDB() |
| 205 |
->newUpdateQueryBuilder() |
| 206 |
->update( 'page' ) |
| 207 |
->set( [ 'page_title' => $dest, 'page_namespace' => $ns ] ) |
| 208 |
->where( [ 'page_id' => $row->page_id ] ) |
| 209 |
->caller( __METHOD__ )->execute(); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
protected function moveInconsistentPage( $row, Title $title ) { |
| 218 |
$titleImpossible = $title->getInterwiki() || !$title->canExist(); |
| 219 |
if ( $title->exists( IDBAccessObject::READ_LATEST ) || $titleImpossible ) { |
| 220 |
if ( $titleImpossible ) { |
| 221 |
$prior = $title->getPrefixedDBkey(); |
| 222 |
} else { |
| 223 |
$prior = $title->getDBkey(); |
| 224 |
} |
| 225 |
|
| 226 |
$ns = (int)$row->page_namespace; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
if ( $ns === 0 ) { |
| 232 |
$ns = $title->getNamespace(); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
if ( $ns < 0 ) { |
| 240 |
$ns = 0; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
$namespaceInfo = $this->getServiceContainer()->getNamespaceInfo(); |
| 248 |
if ( !$namespaceInfo->exists( $ns ) ) { |
| 249 |
$clean = "{$this->prefix}NS$ns:$row->page_title"; |
| 250 |
$ns = 0; |
| 251 |
} elseif ( !$titleImpossible && !$title->exists( IDBAccessObject::READ_LATEST ) ) { |
| 252 |
|
| 253 |
$clean = $prior; |
| 254 |
} elseif ( $ns !== 0 ) { |
| 255 |
|
| 256 |
$nsName = $namespaceInfo->getCanonicalName( $ns ); |
| 257 |
$clean = "{$this->prefix}$nsName:{$prior}"; |
| 258 |
$ns = 0; |
| 259 |
} else { |
| 260 |
$clean = $this->prefix . $prior; |
| 261 |
} |
| 262 |
$verified = Title::makeTitleSafe( $ns, $clean ); |
| 263 |
if ( !$verified || $verified->exists( IDBAccessObject::READ_LATEST ) ) { |
| 264 |
$lastResort = "{$this->prefix}id: {$row->page_id}"; |
| 265 |
$this->output( "Couldn't legalize; form '$clean' exists; using '$lastResort'\n" ); |
| 266 |
$verified = Title::makeTitleSafe( $ns, $lastResort ); |
| 267 |
if ( !$verified || $verified->exists( IDBAccessObject::READ_LATEST ) ) { |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
$this->error( "Destination page $lastResort invalid or already exists." ); |
| 272 |
return; |
| 273 |
} |
| 274 |
} |
| 275 |
$title = $verified; |
| 276 |
} |
| 277 |
|
| 278 |
$ns = $title->getNamespace(); |
| 279 |
$dest = $title->getDBkey(); |
| 280 |
|
| 281 |
if ( $this->dryrun ) { |
| 282 |
$this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," . |
| 283 |
"'$row->page_title') to ($ns,'$dest')\n" ); |
| 284 |
} else { |
| 285 |
$this->output( "renaming $row->page_id ($row->page_namespace," . |
| 286 |
"'$row->page_title') to ($ns,'$dest')\n" ); |
| 287 |
$this->getPrimaryDB() |
| 288 |
->newUpdateQueryBuilder() |
| 289 |
->update( 'page' ) |
| 290 |
->set( [ |
| 291 |
'page_namespace' => $ns, |
| 292 |
'page_title' => $dest |
| 293 |
] ) |
| 294 |
->where( [ 'page_id' => $row->page_id ] ) |
| 295 |
->caller( __METHOD__ )->execute(); |
| 296 |
$this->getServiceContainer()->getLinkCache()->clear(); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
$maintClass = TitleCleanup::class; |
| 303 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 304 |
|