summaryrefslogtreecommitdiff
path: root/maintenance/fixDefaultJsonContentPages.php
blob: 12658910ec162830ae3da7b346e06d7265c923d9 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
 * Fix instances of pre-existing JSON pages
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Maintenance
 */

require_once __DIR__ . '/Maintenance.php';

/**
 * Usage:
 *  fixDefaultJsonContentPages.php
 *
 * It is automatically run by update.php
 */
class FixDefaultJsonContentPages extends LoggedUpdateMaintenance {
	public function __construct() {
		parent::__construct();
		$this->mDescription =
				'Fix instances of JSON pages prior to them being the ContentHandler default';
		$this->setBatchSize( 100 );
	}

	protected function getUpdateKey() {
		return __CLASS__;
	}

	protected function doDBUpdates() {
		if ( !$this->getConfig()->get( 'ContentHandlerUseDB' ) ) {
			$this->output( "\$wgContentHandlerUseDB is not enabled, nothing to do.\n" );
			return true;
		}

		$dbr = wfGetDB( DB_SLAVE );
		$namespaces = array(
			NS_MEDIAWIKI => $dbr->buildLike( $dbr->anyString(), '.json' ),
			NS_USER => $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString(), '.json' ),
		);
		foreach ( $namespaces as $ns => $like ) {
			$lastPage = 0;
			do {
				$rows = $dbr->select(
						'page',
						array( 'page_id', 'page_title', 'page_namespace', 'page_content_model' ),
						array(
								'page_namespace' => $ns,
								'page_title ' . $like,
								'page_id > ' . $dbr->addQuotes( $lastPage )
						),
						__METHOD__,
						array( 'ORDER BY' => 'page_id', 'LIMIT' => $this->mBatchSize )
				);
				foreach ( $rows as $row ) {
					$this->handleRow( $row );
				}
			} while ( $rows->numRows() >= $this->mBatchSize );
		}

		return true;
	}

	protected function handleRow( stdClass $row ) {
		$title = Title::makeTitle( $row->page_namespace, $row->page_title );
		$this->output( "Processing {$title} ({$row->page_id})...\n" );
		$rev = Revision::newFromTitle( $title );
		$content = $rev->getContent( Revision::RAW );
		$dbw = wfGetDB( DB_MASTER );
		if ( $content instanceof JsonContent ) {
			if ( $content->isValid() ) {
				// Yay, actually JSON. We need to just change the
				// page_content_model because revision will automatically
				// use the default, which is *now* JSON.
				$this->output( "Setting page_content_model to json..." );
				$dbw->update(
					'page',
					array( 'page_content_model' => CONTENT_MODEL_JSON ),
					array( 'page_id' => $row->page_id ),
					__METHOD__
				);
				$this->output( "done.\n" );
				wfWaitForSlaves();
			} else {
				// Not JSON...force it to wikitext. We need to update the
				// revision table so that these revisions are always processed
				// as wikitext in the future. page_content_model is already
				// set to "wikitext".
				$this->output( "Setting rev_content_model to wikitext..." );
				// Grab all the ids for batching
				$ids = $dbw->selectFieldValues(
					'revision',
					'rev_id',
					array( 'rev_page' => $row->page_id ),
					__METHOD__
				);
				foreach ( array_chunk( $ids, 50 ) as $chunk ) {
					$dbw->update(
						'revision',
						array( 'rev_content_model' => CONTENT_MODEL_WIKITEXT ),
						array( 'rev_page' => $row->page_id, 'rev_id' => $chunk )
					);
					wfWaitForSlaves();
				}
				$this->output( "done.\n" );
			}
		} else {
			$this->output( "not a JSON page? Skipping\n" );
		}
	}
}

$maintClass = 'FixDefaultJsonContentPages';
require_once RUN_MAINTENANCE_IF_MAIN;