MediaWiki
REL1_37
cleanupTitles.php
Go to the documentation of this file.
1
<?php
28
use
MediaWiki\MediaWikiServices
;
29
30
require_once __DIR__ .
'/TableCleanup.php'
;
31
37
class
TitleCleanup
extends
TableCleanup
{
38
public
function
__construct
() {
39
parent::__construct();
40
$this->
addDescription
(
'Script to clean up broken, unparseable titles'
);
41
$this->
setBatchSize
( 1000 );
42
}
43
47
protected
function
processRow
( $row ) {
48
$display = Title::makeName( $row->page_namespace, $row->page_title );
49
$verified = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $display );
50
$title
= Title::newFromText( $verified );
51
52
if
(
$title
!==
null
53
&&
$title
->canExist()
54
&&
$title
->getNamespace() == $row->page_namespace
55
&&
$title
->getDBkey() === $row->page_title
56
) {
57
// all is fine
58
$this->
progress
( 0 );
59
60
return
;
61
}
62
63
if
( $row->page_namespace ==
NS_FILE
&& $this->fileExists( $row->page_title ) ) {
64
$this->
output
(
"file $row->page_title needs cleanup, please run cleanupImages.php.\n"
);
65
$this->
progress
( 0 );
66
} elseif (
$title
===
null
) {
67
$this->
output
(
"page $row->page_id ($display) is illegal.\n"
);
68
$this->
moveIllegalPage
( $row );
69
$this->
progress
( 1 );
70
}
else
{
71
$this->
output
(
"page $row->page_id ($display) doesn't match self.\n"
);
72
$this->
moveInconsistentPage
( $row,
$title
);
73
$this->
progress
( 1 );
74
}
75
}
76
81
protected
function
fileExists
( $name ) {
82
// XXX: Doesn't actually check for file existence, just presence of image record.
83
// This is reasonable, since cleanupImages.php only iterates over the image table.
84
$dbr
= $this->
getDB
(
DB_REPLICA
);
85
$row =
$dbr
->selectRow(
'image'
, [
'img_name'
], [
'img_name'
=> $name ], __METHOD__ );
86
87
return
$row !==
false
;
88
}
89
93
protected
function
moveIllegalPage
( $row ) {
94
$legal =
'A-Za-z0-9_/\\\\-'
;
95
$legalized = preg_replace_callback(
"!([^$legal])!"
,
96
[ $this,
'hexChar'
],
97
$row->page_title );
98
if
( $legalized ==
'.'
) {
99
$legalized =
'(dot)'
;
100
}
101
if
( $legalized ==
'_'
) {
102
$legalized =
'(space)'
;
103
}
104
$legalized =
'Broken/'
. $legalized;
105
106
$title
= Title::newFromText( $legalized );
107
if
(
$title
===
null
) {
108
$clean =
'Broken/id:'
. $row->page_id;
109
$this->
output
(
"Couldn't legalize; form '$legalized' still invalid; using '$clean'\n"
);
110
$title
= Title::newFromText( $clean );
111
} elseif (
$title
->exists() ) {
112
$clean =
'Broken/id:'
. $row->page_id;
113
$this->
output
(
"Legalized for '$legalized' exists; using '$clean'\n"
);
114
$title
= Title::newFromText( $clean );
115
}
116
117
$dest =
$title
->getDBkey();
118
if
( $this->dryrun ) {
119
$this->
output
(
"DRY RUN: would rename $row->page_id ($row->page_namespace,"
.
120
"'$row->page_title') to ($row->page_namespace,'$dest')\n"
);
121
}
else
{
122
$this->
output
(
"renaming $row->page_id ($row->page_namespace,"
.
123
"'$row->page_title') to ($row->page_namespace,'$dest')\n"
);
124
$dbw = $this->
getDB
(
DB_PRIMARY
);
125
$dbw->update(
'page'
,
126
[
'page_title'
=> $dest ],
127
[
'page_id'
=> $row->page_id ],
128
__METHOD__ );
129
}
130
}
131
136
protected
function
moveInconsistentPage
( $row,
Title
$title
) {
137
if
(
$title
->exists( Title::READ_LATEST )
138
||
$title
->getInterwiki()
139
|| !
$title
->canExist()
140
) {
141
$titleImpossible =
$title
->getInterwiki() || !
$title
->canExist();
142
if
( $titleImpossible ) {
143
$prior =
$title
->getPrefixedDBkey();
144
}
else
{
145
$prior =
$title
->getDBkey();
146
}
147
148
# Old cleanupTitles could move articles there. See T25147.
149
$ns = $row->page_namespace;
150
if
( $ns < 0 ) {
151
$ns = 0;
152
}
153
154
# Namespace which no longer exists. Put the page in the main namespace
155
# since we don't have any idea of the old namespace name. See T70501.
156
if
( !MediaWikiServices::getInstance()->getNamespaceInfo()->exists( $ns ) ) {
157
$ns = 0;
158
}
159
160
if
( !$titleImpossible && !
$title
->exists() ) {
161
// Looks like the current title, after cleaning it up, is valid and available
162
$clean = $prior;
163
}
else
{
164
$clean =
'Broken/'
. $prior;
165
}
166
$verified = Title::makeTitleSafe( $ns, $clean );
167
if
( !$verified || $verified->exists() ) {
168
$blah =
"Broken/id:"
. $row->page_id;
169
$this->
output
(
"Couldn't legalize; form '$clean' exists; using '$blah'\n"
);
170
$verified = Title::makeTitleSafe( $ns, $blah );
171
}
172
$title
= $verified;
173
}
174
if
(
$title
===
null
) {
175
$this->
fatalError
(
"Something awry; empty title."
);
176
}
177
$ns =
$title
->getNamespace();
178
$dest =
$title
->getDBkey();
179
180
if
( $this->dryrun ) {
181
$this->
output
(
"DRY RUN: would rename $row->page_id ($row->page_namespace,"
.
182
"'$row->page_title') to ($ns,'$dest')\n"
);
183
}
else
{
184
$this->
output
(
"renaming $row->page_id ($row->page_namespace,"
.
185
"'$row->page_title') to ($ns,'$dest')\n"
);
186
$dbw = $this->
getDB
(
DB_PRIMARY
);
187
$dbw->update(
'page'
,
188
[
189
'page_namespace'
=> $ns,
190
'page_title'
=> $dest
191
],
192
[
'page_id'
=> $row->page_id ],
193
__METHOD__ );
194
MediaWikiServices::getInstance()->getLinkCache()->clear();
195
}
196
}
197
}
198
199
$maintClass
= TitleCleanup::class;
200
require_once RUN_MAINTENANCE_IF_MAIN;
getDB
getDB()
NS_FILE
const NS_FILE
Definition
Defines.php:70
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition
Maintenance.php:435
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition
Maintenance.php:329
Maintenance\setBatchSize
setBatchSize( $s=0)
Definition
Maintenance.php:375
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition
Maintenance.php:489
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition
MediaWikiServices.php:190
TableCleanup
Generic class to cleanup a database table.
Definition
TableCleanup.php:31
TableCleanup\progress
progress( $updated)
Definition
TableCleanup.php:76
TitleCleanup
Maintenance script to clean up broken, unparseable titles.
Definition
cleanupTitles.php:37
TitleCleanup\moveIllegalPage
moveIllegalPage( $row)
Definition
cleanupTitles.php:93
TitleCleanup\processRow
processRow( $row)
Definition
cleanupTitles.php:47
TitleCleanup\moveInconsistentPage
moveInconsistentPage( $row, Title $title)
Definition
cleanupTitles.php:136
TitleCleanup\__construct
__construct()
Default constructor.
Definition
cleanupTitles.php:38
TitleCleanup\fileExists
fileExists( $name)
Definition
cleanupTitles.php:81
Title
Represents a title within MediaWiki.
Definition
Title.php:48
$maintClass
$maintClass
Definition
cleanupTitles.php:199
DB_REPLICA
const DB_REPLICA
Definition
defines.php:25
DB_PRIMARY
const DB_PRIMARY
Definition
defines.php:27
$dbr
$dbr
Definition
testCompression.php:54
$title
$title
Definition
testCompression.php:38
maintenance
cleanupTitles.php
Generated on Fri Apr 5 2024 23:41:19 for MediaWiki by
1.9.8