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 require_once __DIR__
. '/Maintenance.php';
28 * fixDefaultJsonContentPages.php
30 * It is automatically run by update.php
32 class FixDefaultJsonContentPages
extends LoggedUpdateMaintenance
{
33 public function __construct() {
34 parent
::__construct();
35 $this->addDescription(
36 'Fix instances of JSON pages prior to them being the ContentHandler default' );
37 $this->setBatchSize( 100 );
40 protected function getUpdateKey() {
44 protected function doDBUpdates() {
45 if ( !$this->getConfig()->get( 'ContentHandlerUseDB' ) ) {
46 $this->output( "\$wgContentHandlerUseDB is not enabled, nothing to do.\n" );
50 $dbr = $this->getDB( DB_REPLICA
);
52 NS_MEDIAWIKI
=> $dbr->buildLike( $dbr->anyString(), '.json' ),
53 NS_USER
=> $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString(), '.json' ),
55 foreach ( $namespaces as $ns => $like ) {
60 [ 'page_id', 'page_title', 'page_namespace', 'page_content_model' ],
62 'page_namespace' => $ns,
63 'page_title ' . $like,
64 'page_id > ' . $dbr->addQuotes( $lastPage )
67 [ 'ORDER BY' => 'page_id', 'LIMIT' => $this->mBatchSize
]
69 foreach ( $rows as $row ) {
70 $this->handleRow( $row );
72 } while ( $rows->numRows() >= $this->mBatchSize
);
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 = Revision
::newFromTitle( $title );
82 $content = $rev->getContent( Revision
::RAW
);
83 $dbw = $this->getDB( DB_MASTER
);
84 if ( $content instanceof JsonContent
) {
85 if ( $content->isValid() ) {
86 // Yay, actually JSON. We need to just change the
87 // page_content_model because revision will automatically
88 // use the default, which is *now* JSON.
89 $this->output( "Setting page_content_model to json..." );
92 [ 'page_content_model' => CONTENT_MODEL_JSON
],
93 [ 'page_id' => $row->page_id
],
96 $this->output( "done.\n" );
99 // Not JSON...force it to wikitext. We need to update the
100 // revision table so that these revisions are always processed
101 // as wikitext in the future. page_content_model is already
102 // set to "wikitext".
103 $this->output( "Setting rev_content_model to wikitext..." );
104 // Grab all the ids for batching
105 $ids = $dbw->selectFieldValues(
108 [ 'rev_page' => $row->page_id
],
111 foreach ( array_chunk( $ids, 50 ) as $chunk ) {
114 [ 'rev_content_model' => CONTENT_MODEL_WIKITEXT
],
115 [ 'rev_page' => $row->page_id
, 'rev_id' => $chunk ]
119 $this->output( "done.\n" );
122 $this->output( "not a JSON page? Skipping\n" );
127 $maintClass = 'FixDefaultJsonContentPages';
128 require_once RUN_MAINTENANCE_IF_MAIN
;