-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveDisplayTitle.php
More file actions
69 lines (56 loc) · 2.18 KB
/
removeDisplayTitle.php
File metadata and controls
69 lines (56 loc) · 2.18 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
<?php
/**
* This maintenance script removes the "title" parameter from {{Page data}} when it is identical to the real title of the page
*/
$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";
use MediaWiki\MediaWikiServices;
class RemoveDisplayTitle extends Maintenance {
public function execute() {
$services = MediaWikiServices::getInstance();
$provider = $services->getConnectionProvider();
$dbr = $provider->getReplicaDatabase();
$ids = $dbr->newSelectQueryBuilder()
->field( 'page_id' )
->from( 'page' )
->where( [ 'page_is_redirect' => 0, 'page_namespace' => 0 ] )
->fetchFieldValues();
$total = count( $ids );
$factory = $services->getWikiPageFactory();
foreach ( $ids as $count => $id ) {
// Check if the page has a display title set
$title = Title::newFromID( $id );
$page = $factory->newFromTitle( $title );
$revision = $page->getRevisionRecord();
$content = $revision->getContent( 'main' );
$wikitext = $content->getText();
if ( !preg_match( '/{{Page data[^}]*\| *title *= *(.+) */', $wikitext, $matches ) ) {
continue;
}
// Check if the display title is equal to the real title
$displayTitle = $matches[1];
$subpageText = $title->getSubpageText();
if ( strtolower( $displayTitle ) != strtolower( $subpageText ) ) {
continue;
}
// Edit the wikitext
$wikitext = preg_replace( '/{{Page data([^}]*)\| *title *= *(.+)\n/', '{{Page data$1', $wikitext );
// Output the progress
$url = $title->getFullURL();
$percent = round( $count / $total * 100, 2 );
$this->output( "$percent% $url" . PHP_EOL );
// Save the page
$content = ContentHandler::makeContent( $wikitext, $title );
$user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
$updater = $page->newPageUpdater( $user );
$updater->setContent( 'main', $content );
$comment = CommentStoreComment::newUnsavedComment( 'Remove redundant display title' );
$updater->saveRevision( $comment, EDIT_SUPPRESS_RC | EDIT_FORCE_BOT | EDIT_MINOR | EDIT_INTERNAL );
}
}
}
$maintClass = RemoveDisplayTitle::class;
require_once RUN_MAINTENANCE_IF_MAIN;