3 * Fix instances of pre-existing JSON pages
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 use MediaWiki\MediaWikiServices
;
25 use MediaWiki\Revision\RevisionRecord
;
26 use MediaWiki\Revision\SlotRecord
;
28 require_once __DIR__
. '/Maintenance.php';
32 * fixDefaultJsonContentPages.php
34 * It is automatically run by update.php
36 class FixDefaultJsonContentPages
extends LoggedUpdateMaintenance
{
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 );
44 protected function getUpdateKey() {
48 protected function doDBUpdates() {
49 $dbr = $this->getDB( DB_REPLICA
);
51 NS_MEDIAWIKI
=> $dbr->buildLike( $dbr->anyString(), '.json' ),
52 NS_USER
=> $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString(), '.json' ),
54 foreach ( $namespaces as $ns => $like ) {
59 [ 'page_id', 'page_title', 'page_namespace', 'page_content_model' ],
61 'page_namespace' => $ns,
62 'page_title ' . $like,
63 'page_id > ' . $dbr->addQuotes( $lastPage )
66 [ 'ORDER BY' => 'page_id', 'LIMIT' => $this->getBatchSize() ]
68 foreach ( $rows as $row ) {
69 $this->handleRow( $row );
71 } while ( $rows->numRows() >= $this->getBatchSize() );
77 protected function handleRow( stdClass
$row ) {
78 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
79 $this->output( "Processing {$title} ({$row->page_id})...\n" );
80 $rev = MediaWikiServices
::getInstance()
82 ->getRevisionByTitle( $title );
83 $content = $rev->getContent( SlotRecord
::MAIN
, RevisionRecord
::RAW
);
84 $dbw = $this->getDB( DB_MASTER
);
85 if ( $content instanceof JsonContent
) {
86 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
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..." );
94 [ 'page_content_model' => CONTENT_MODEL_JSON
],
95 [ 'page_id' => $row->page_id
],
98 $this->output( "done.\n" );
99 $lbFactory->waitForReplication();
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->selectFieldValues(
110 [ 'rev_page' => $row->page_id
],
113 foreach ( array_chunk( $ids, 50 ) as $chunk ) {
116 [ 'rev_content_model' => CONTENT_MODEL_WIKITEXT
],
117 [ 'rev_page' => $row->page_id
, 'rev_id' => $chunk ],
120 $lbFactory->waitForReplication();
122 $this->output( "done.\n" );
125 $this->output( "not a JSON page? Skipping\n" );
130 $maintClass = FixDefaultJsonContentPages
::class;
131 require_once RUN_MAINTENANCE_IF_MAIN
;