MediaWiki  master
fixDefaultJsonContentPages.php
Go to the documentation of this file.
1 <?php
27 
28 require_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->select(
58  'page',
59  [ 'page_id', 'page_title', 'page_namespace', 'page_content_model' ],
60  [
61  'page_namespace' => $ns,
62  'page_title ' . $like,
63  'page_id > ' . $dbr->addQuotes( $lastPage )
64  ],
65  __METHOD__,
66  [ 'ORDER BY' => 'page_id', 'LIMIT' => $this->getBatchSize() ]
67  );
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 = MediaWikiServices::getInstance()
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  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
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  $lbFactory->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  $lbFactory->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;
132 require_once RUN_MAINTENANCE_IF_MAIN;
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:72
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
$dbr
$dbr
Definition: testCompression.php:54
FixDefaultJsonContentPages\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: fixDefaultJsonContentPages.php:44
CONTENT_MODEL_JSON
const CONTENT_MODEL_JSON
Definition: Defines.php:212
FixDefaultJsonContentPages\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: fixDefaultJsonContentPages.php:48
JsonContent
Represents the content of a JSON content.
Definition: JsonContent.php:15
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: LoggedUpdateMaintenance.php:26
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:651
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
$content
$content
Definition: router.php:76
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
FixDefaultJsonContentPages\__construct
__construct()
Default constructor.
Definition: fixDefaultJsonContentPages.php:37
FixDefaultJsonContentPages\handleRow
handleRow(stdClass $row)
Definition: fixDefaultJsonContentPages.php:78
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1362
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:208
NS_USER
const NS_USER
Definition: Defines.php:66
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
FixDefaultJsonContentPages
Usage: fixDefaultJsonContentPages.php.
Definition: fixDefaultJsonContentPages.php:36
$maintClass
$maintClass
Definition: fixDefaultJsonContentPages.php:131
MediaWiki\Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
Maintenance\setBatchSize
setBatchSize( $s=0)
Definition: Maintenance.php:375