MediaWiki REL1_40
fixDefaultJsonContentPages.php
Go to the documentation of this file.
1<?php
28
29require_once __DIR__ . '/Maintenance.php';
30
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription(
41 'Fix instances of JSON pages prior to them being the ContentHandler default' );
42 $this->setBatchSize( 100 );
43 }
44
45 protected function getUpdateKey() {
46 return __CLASS__;
47 }
48
49 protected function doDBUpdates() {
50 $dbr = $this->getDB( DB_REPLICA );
51 $namespaces = [
52 NS_MEDIAWIKI => $dbr->buildLike( $dbr->anyString(), '.json' ),
53 NS_USER => $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString(), '.json' ),
54 ];
55 foreach ( $namespaces as $ns => $like ) {
56 $lastPage = 0;
57 do {
58 $rows = $dbr->select(
59 'page',
60 [ 'page_id', 'page_title', 'page_namespace', 'page_content_model' ],
61 [
62 'page_namespace' => $ns,
63 'page_title ' . $like,
64 'page_id > ' . $dbr->addQuotes( $lastPage )
65 ],
66 __METHOD__,
67 [ 'ORDER BY' => 'page_id', 'LIMIT' => $this->getBatchSize() ]
68 );
69 foreach ( $rows as $row ) {
70 $this->handleRow( $row );
71 $lastPage = $row->page_id;
72 }
73 } while ( $rows->numRows() >= $this->getBatchSize() );
74 }
75
76 return true;
77 }
78
79 protected function handleRow( stdClass $row ) {
80 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
81 $this->output( "Processing {$title} ({$row->page_id})...\n" );
82 $rev = MediaWikiServices::getInstance()
83 ->getRevisionLookup()
84 ->getRevisionByTitle( $title );
85 $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
86 $dbw = $this->getDB( DB_PRIMARY );
87 if ( $content instanceof JsonContent ) {
88 if ( $content->isValid() ) {
89 // Yay, actually JSON. We need to just change the
90 // page_content_model because revision will automatically
91 // use the default, which is *now* JSON.
92 $this->output( "Setting page_content_model to json..." );
93 $dbw->update(
94 'page',
95 [ 'page_content_model' => CONTENT_MODEL_JSON ],
96 [ 'page_id' => $row->page_id ],
97 __METHOD__
98 );
99 $this->output( "done.\n" );
100 $this->waitForReplication();
101 } else {
102 // Not JSON...force it to wikitext. We need to update the
103 // revision table so that these revisions are always processed
104 // as wikitext in the future. page_content_model is already
105 // set to "wikitext".
106 $this->output( "Setting rev_content_model to wikitext..." );
107 // Grab all the ids for batching
108 $ids = $dbw->selectFieldValues(
109 'revision',
110 'rev_id',
111 [ 'rev_page' => $row->page_id ],
112 __METHOD__
113 );
114 foreach ( array_chunk( $ids, 50 ) as $chunk ) {
115 $dbw->update(
116 'revision',
117 [ 'rev_content_model' => CONTENT_MODEL_WIKITEXT ],
118 [ 'rev_page' => $row->page_id, 'rev_id' => $chunk ],
119 __METHOD__
120 );
121 $this->waitForReplication();
122 }
123 $this->output( "done.\n" );
124 }
125 } else {
126 $this->output( "not a JSON page? Skipping\n" );
127 }
128 }
129}
130
131$maintClass = FixDefaultJsonContentPages::class;
132require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const NS_USER
Definition Defines.php:66
const NS_MEDIAWIKI
Definition Defines.php:72
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:211
const CONTENT_MODEL_JSON
Definition Defines.php:215
Usage: fixDefaultJsonContentPages.php.
getUpdateKey()
Get the update key name to go in the update log table.
JSON text content that can be viewed and edit directly by users.
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
Service locator for MediaWiki core services.
Page revision base class.
Value object representing a content slot associated with a page revision.
Represents a title within MediaWiki.
Definition Title.php:82
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28
$content
Definition router.php:76