MediaWiki  1.34.0
fixDefaultJsonContentPages.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
35  public function __construct() {
36  parent::__construct();
37  $this->addDescription(
38  'Fix instances of JSON pages prior to them being the ContentHandler default' );
39  $this->setBatchSize( 100 );
40  }
41 
42  protected function getUpdateKey() {
43  return __CLASS__;
44  }
45 
46  protected function doDBUpdates() {
47  if ( !$this->getConfig()->get( 'ContentHandlerUseDB' ) ) {
48  $this->output( "\$wgContentHandlerUseDB is not enabled, nothing to do.\n" );
49  return true;
50  }
51 
52  $dbr = $this->getDB( DB_REPLICA );
53  $namespaces = [
54  NS_MEDIAWIKI => $dbr->buildLike( $dbr->anyString(), '.json' ),
55  NS_USER => $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString(), '.json' ),
56  ];
57  foreach ( $namespaces as $ns => $like ) {
58  $lastPage = 0;
59  do {
60  $rows = $dbr->select(
61  'page',
62  [ 'page_id', 'page_title', 'page_namespace', 'page_content_model' ],
63  [
64  'page_namespace' => $ns,
65  'page_title ' . $like,
66  'page_id > ' . $dbr->addQuotes( $lastPage )
67  ],
68  __METHOD__,
69  [ 'ORDER BY' => 'page_id', 'LIMIT' => $this->getBatchSize() ]
70  );
71  foreach ( $rows as $row ) {
72  $this->handleRow( $row );
73  }
74  } while ( $rows->numRows() >= $this->getBatchSize() );
75  }
76 
77  return true;
78  }
79 
80  protected function handleRow( stdClass $row ) {
81  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
82  $this->output( "Processing {$title} ({$row->page_id})...\n" );
84  $content = $rev->getContent( RevisionRecord::RAW );
85  $dbw = $this->getDB( DB_MASTER );
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" );
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->selectFieldValues(
108  'revision',
109  'rev_id',
110  [ 'rev_page' => $row->page_id ],
111  __METHOD__
112  );
113  foreach ( array_chunk( $ids, 50 ) as $chunk ) {
114  $dbw->update(
115  'revision',
116  [ 'rev_content_model' => CONTENT_MODEL_WIKITEXT ],
117  [ 'rev_page' => $row->page_id, 'rev_id' => $chunk ]
118  );
119  wfWaitForSlaves();
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;
130 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
CONTENT_MODEL_JSON
const CONTENT_MODEL_JSON
Definition: Defines.php:219
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:215
wfWaitForSlaves
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Definition: GlobalFunctions.php:2718
$dbr
$dbr
Definition: testCompression.php:50
FixDefaultJsonContentPages\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: fixDefaultJsonContentPages.php:42
Revision\newFromTitle
static newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target.
Definition: Revision.php:138
FixDefaultJsonContentPages\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: fixDefaultJsonContentPages.php:46
JsonContent
Represents the content of a JSON content.
Definition: JsonContent.php:15
Maintenance\getConfig
getConfig()
Definition: Maintenance.php:613
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1727
$title
$title
Definition: testCompression.php:34
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$content
$content
Definition: router.php:78
FixDefaultJsonContentPages\__construct
__construct()
Default constructor.
Definition: fixDefaultJsonContentPages.php:35
FixDefaultJsonContentPages\handleRow
handleRow(stdClass $row)
Definition: fixDefaultJsonContentPages.php:80
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:386
NS_USER
const NS_USER
Definition: Defines.php:62
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:68
FixDefaultJsonContentPages
Usage: fixDefaultJsonContentPages.php.
Definition: fixDefaultJsonContentPages.php:34
$maintClass
$maintClass
Definition: fixDefaultJsonContentPages.php:129
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:394