MediaWiki  1.29.1
fixDefaultJsonContentPages.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
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 );
38  }
39 
40  protected function getUpdateKey() {
41  return __CLASS__;
42  }
43 
44  protected function doDBUpdates() {
45  if ( !$this->getConfig()->get( 'ContentHandlerUseDB' ) ) {
46  $this->output( "\$wgContentHandlerUseDB is not enabled, nothing to do.\n" );
47  return true;
48  }
49 
50  $dbr = $this->getDB( DB_REPLICA );
51  $namespaces = [
52  NS_MEDIAWIKI => $dbr->buildLike( $dbr->anyString(), '.json' ),
53  NS_USER => $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString(), '.json' ),
54  ];
55  foreach ( $namespaces as $ns => $like ) {
56  $lastPage = 0;
57  do {
58  $rows = $dbr->select(
59  'page',
60  [ 'page_id', 'page_title', 'page_namespace', 'page_content_model' ],
61  [
62  'page_namespace' => $ns,
63  'page_title ' . $like,
64  'page_id > ' . $dbr->addQuotes( $lastPage )
65  ],
66  __METHOD__,
67  [ 'ORDER BY' => 'page_id', 'LIMIT' => $this->mBatchSize ]
68  );
69  foreach ( $rows as $row ) {
70  $this->handleRow( $row );
71  }
72  } while ( $rows->numRows() >= $this->mBatchSize );
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" );
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..." );
90  $dbw->update(
91  'page',
92  [ 'page_content_model' => CONTENT_MODEL_JSON ],
93  [ 'page_id' => $row->page_id ],
94  __METHOD__
95  );
96  $this->output( "done.\n" );
98  } else {
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(
106  'revision',
107  'rev_id',
108  [ 'rev_page' => $row->page_id ],
109  __METHOD__
110  );
111  foreach ( array_chunk( $ids, 50 ) as $chunk ) {
112  $dbw->update(
113  'revision',
114  [ 'rev_content_model' => CONTENT_MODEL_WIKITEXT ],
115  [ 'rev_page' => $row->page_id, 'rev_id' => $chunk ]
116  );
117  wfWaitForSlaves();
118  }
119  $this->output( "done.\n" );
120  }
121  } else {
122  $this->output( "not a JSON page? Skipping\n" );
123  }
124  }
125 }
126 
127 $maintClass = 'FixDefaultJsonContentPages';
128 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\$mBatchSize
int $mBatchSize
Batch size.
Definition: Maintenance.php:103
CONTENT_MODEL_JSON
const CONTENT_MODEL_JSON
Definition: Defines.php:237
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
$namespaces
namespace and then decline to actually register it & $namespaces
Definition: hooks.txt:934
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:233
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:3214
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
FixDefaultJsonContentPages\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: fixDefaultJsonContentPages.php:40
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:134
FixDefaultJsonContentPages\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: fixDefaultJsonContentPages.php:44
JsonContent
Represents the content of a JSON content.
Definition: JsonContent.php:15
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
Maintenance\getConfig
getConfig()
Definition: Maintenance.php:504
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1562
$content
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition: hooks.txt:1049
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:514
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
FixDefaultJsonContentPages\__construct
__construct()
Default constructor.
Definition: fixDefaultJsonContentPages.php:33
FixDefaultJsonContentPages\handleRow
handleRow(stdClass $row)
Definition: fixDefaultJsonContentPages.php:78
Revision\RAW
const RAW
Definition: Revision.php:100
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1741
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1251
NS_USER
const NS_USER
Definition: Defines.php:64
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:70
FixDefaultJsonContentPages
Usage: fixDefaultJsonContentPages.php.
Definition: fixDefaultJsonContentPages.php:32
$maintClass
$maintClass
Definition: fixDefaultJsonContentPages.php:127
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:314