MediaWiki  master
migrateArchiveText.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  'Migrates content from pre-1.5 ar_text and ar_flags columns to text storage'
37  );
38  $this->addOption(
39  'replace-missing',
40  "For rows with missing or unloadable data, throw away whatever is there and\n"
41  . "mark them as \"error\" in the database."
42  );
43  }
44 
49  public function setForce( $forced = true ) {
50  $this->mOptions['force'] = $forced;
51  }
52 
53  protected function getUpdateKey() {
54  return __CLASS__;
55  }
56 
57  protected function doDBUpdates() {
59 
60  $replaceMissing = $this->hasOption( 'replace-missing' );
61  $batchSize = $this->getBatchSize();
62 
63  $dbr = $this->getDB( DB_REPLICA, [ 'vslow' ] );
64  $dbw = $this->getDB( DB_MASTER );
65  if ( !$dbr->fieldExists( 'archive', 'ar_text', __METHOD__ ) ||
66  !$dbw->fieldExists( 'archive', 'ar_text', __METHOD__ )
67  ) {
68  $this->output( "No ar_text field, so nothing to migrate.\n" );
69  return true;
70  }
71 
72  $this->output( "Migrating ar_text to modern storage...\n" );
73  $last = 0;
74  $count = 0;
75  $errors = 0;
76  while ( true ) {
77  $res = $dbr->select(
78  'archive',
79  [ 'ar_id', 'ar_text', 'ar_flags' ],
80  [
81  'ar_text_id' => null,
82  "ar_id > $last",
83  ],
84  __METHOD__,
85  [ 'LIMIT' => $batchSize, 'ORDER BY' => [ 'ar_id' ] ]
86  );
87  $numRows = $res->numRows();
88 
89  foreach ( $res as $row ) {
90  $last = $row->ar_id;
91 
92  // Recompress the text (and store in external storage, if
93  // applicable) if it's not already in external storage.
94  if ( !in_array( 'external', explode( ',', $row->ar_flags ), true ) ) {
95  $data = Revision::getRevisionText( $row, 'ar_' );
96  if ( $data !== false ) {
97  $flags = Revision::compressRevisionText( $data );
98 
99  if ( $wgDefaultExternalStore ) {
100  $data = ExternalStore::insertToDefault( $data );
101  if ( $flags ) {
102  $flags .= ',';
103  }
104  $flags .= 'external';
105  }
106  } elseif ( $replaceMissing ) {
107  $this->error( "Replacing missing data for row ar_id=$row->ar_id" );
108  $data = 'Missing data in migrateArchiveText.php on ' . date( 'c' );
109  $flags = 'error';
110  } else {
111  $this->error( "No data for row ar_id=$row->ar_id" );
112  $errors++;
113  continue;
114  }
115  } else {
116  $flags = $row->ar_flags;
117  $data = $row->ar_text;
118  }
119 
120  $this->beginTransaction( $dbw, __METHOD__ );
121  $dbw->insert(
122  'text',
123  [ 'old_text' => $data, 'old_flags' => $flags ],
124  __METHOD__
125  );
126  $id = $dbw->insertId();
127  $dbw->update(
128  'archive',
129  [ 'ar_text_id' => $id, 'ar_text' => '', 'ar_flags' => '' ],
130  [ 'ar_id' => $row->ar_id, 'ar_text_id' => null ],
131  __METHOD__
132  );
133  $count += $dbw->affectedRows();
134  $this->commitTransaction( $dbw, __METHOD__ );
135  }
136 
137  if ( $numRows < $batchSize ) {
138  // We must have reached the end
139  break;
140  }
141 
142  $this->output( "... $last\n" );
143  // $this->commitTransaction() already waited for replication; no need to re-wait here
144  }
145 
146  $this->output( "Completed ar_text migration, $count rows updated, $errors missing data.\n" );
147  if ( $errors ) {
148  $this->output( "Run with --replace-missing to overwrite missing data with an error message.\n" );
149  }
150 
151  return $errors === 0;
152  }
153 }
154 
156 require_once RUN_MAINTENANCE_IF_MAIN;
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
array $wgDefaultExternalStore
The place to put new revisions, false to put them in the local text table.
error( $err, $die=0)
Throw an error to the user.
hasOption( $name)
Checks to see if a particular option exists.
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
static insertToDefault( $data, array $params=[])
Like insert() above, but does more of the work for us.
const DB_MASTER
Definition: defines.php:26
static getRevisionText( $row, $prefix='old_', $wiki=false)
Get revision text associated with an old or archive row.
Definition: Revision.php:1046
$last
$res
Definition: database.txt:21
addDescription( $text)
Set the description text.
static compressRevisionText(&$text)
If $wgCompressRevisions is enabled, we will compress data.
Definition: Revision.php:1122
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
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 script that migrates archive.ar_text and ar_flags to text storage.
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
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
setForce( $forced=true)
Sets whether a run of this maintenance script has the force parameter set.
getBatchSize()
Returns batch size.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
const DB_REPLICA
Definition: defines.php:25
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.