MediaWiki 1.41.2
fixDefaultJsonContentPages.php
Go to the documentation of this file.
1<?php
27
28require_once __DIR__ . '/Maintenance.php';
29
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription(
40 'Fix instances of JSON pages prior to them being the ContentHandler default' );
41 $this->setBatchSize( 100 );
42 }
43
44 protected function getUpdateKey() {
45 return __CLASS__;
46 }
47
48 protected function doDBUpdates() {
49 $dbr = $this->getDB( DB_REPLICA );
50 $namespaces = [
51 NS_MEDIAWIKI => $dbr->buildLike( $dbr->anyString(), '.json' ),
52 NS_USER => $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString(), '.json' ),
53 ];
54 foreach ( $namespaces as $ns => $like ) {
55 $lastPage = 0;
56 do {
57 $rows = $dbr->newSelectQueryBuilder()
58 ->select( [ 'page_id', 'page_title', 'page_namespace', 'page_content_model' ] )
59 ->from( 'page' )
60 ->where( [
61 'page_namespace' => $ns,
62 'page_title ' . $like,
63 'page_id > ' . $dbr->addQuotes( $lastPage )
64 ] )
65 ->orderBy( 'page_id' )
66 ->limit( $this->getBatchSize() )
67 ->caller( __METHOD__ )->fetchResultSet();
68 foreach ( $rows as $row ) {
69 $this->handleRow( $row );
70 $lastPage = $row->page_id;
71 }
72 } while ( $rows->numRows() >= $this->getBatchSize() );
73 }
74
75 return true;
76 }
77
78 protected function handleRow( stdClass $row ) {
79 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
80 $this->output( "Processing {$title} ({$row->page_id})...\n" );
81 $rev = $this->getServiceContainer()
82 ->getRevisionLookup()
83 ->getRevisionByTitle( $title );
84 $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
85 $dbw = $this->getDB( DB_PRIMARY );
86 if ( $content instanceof JsonContent ) {
87 if ( $content->isValid() ) {
88 // Yay, actually JSON. We need to just change the
89 // page_content_model because revision will automatically
90 // use the default, which is *now* JSON.
91 $this->output( "Setting page_content_model to json..." );
92 $dbw->update(
93 'page',
94 [ 'page_content_model' => CONTENT_MODEL_JSON ],
95 [ 'page_id' => $row->page_id ],
96 __METHOD__
97 );
98 $this->output( "done.\n" );
99 $this->waitForReplication();
100 } else {
101 // Not JSON...force it to wikitext. We need to update the
102 // revision table so that these revisions are always processed
103 // as wikitext in the future. page_content_model is already
104 // set to "wikitext".
105 $this->output( "Setting rev_content_model to wikitext..." );
106 // Grab all the ids for batching
107 $ids = $dbw->newSelectQueryBuilder()
108 ->select( 'rev_id' )
109 ->from( 'revision' )
110 ->where( [ 'rev_page' => $row->page_id ] )
111 ->caller( __METHOD__ )->fetchFieldValues();
112 foreach ( array_chunk( $ids, 50 ) as $chunk ) {
113 $dbw->update(
114 'revision',
115 [ 'rev_content_model' => CONTENT_MODEL_WIKITEXT ],
116 [ 'rev_page' => $row->page_id, 'rev_id' => $chunk ],
117 __METHOD__
118 );
119 $this->waitForReplication();
120 }
121 $this->output( "done.\n" );
122 }
123 } else {
124 $this->output( "not a JSON page? Skipping\n" );
125 }
126 }
127}
128
129$maintClass = FixDefaultJsonContentPages::class;
130require_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:209
const CONTENT_MODEL_JSON
Definition Defines.php:213
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.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
Page revision base class.
Value object representing a content slot associated with a page revision.
Represents a title within MediaWiki.
Definition Title.php:76
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28
$content
Definition router.php:76