MediaWiki  1.33.0
migrateImageCommentTemp.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  'Merges image_comment_temp into the image table'
37  );
38  }
39 
44  public function setForce( $forced = true ) {
45  $this->mOptions['force'] = $forced;
46  }
47 
48  protected function getUpdateKey() {
49  return __CLASS__;
50  }
51 
52  protected function doDBUpdates() {
53  $batchSize = $this->getBatchSize();
54 
55  $dbw = $this->getDB( DB_MASTER );
56  if ( !$dbw->fieldExists( 'image', 'img_description_id', __METHOD__ ) ) {
57  $this->output( "Run update.php to create img_description_id.\n" );
58  return false;
59  }
60  if ( !$dbw->tableExists( 'image_comment_temp', __METHOD__ ) ) {
61  $this->output( "image_comment_temp does not exist, so nothing to do.\n" );
62  return true;
63  }
64 
65  $this->output( "Merging image_comment_temp into the image table...\n" );
66  $conds = [];
67  $updated = 0;
68  $deleted = 0;
69  while ( true ) {
70  $this->beginTransaction( $dbw, __METHOD__ );
71 
72  $res = $dbw->select(
73  [ 'image_comment_temp', 'image' ],
74  [
75  'name' => 'imgcomment_name',
76  'oldid' => 'imgcomment_description_id',
77  'newid' => 'img_description_id',
78  ],
79  $conds,
80  __METHOD__,
81  [ 'LIMIT' => $batchSize, 'ORDER BY' => [ 'name' ] ],
82  [ 'image' => [ 'JOIN', 'img_name = imgcomment_name' ] ]
83  );
84  $numRows = $res->numRows();
85 
86  $toDelete = [];
87  $last = null;
88  foreach ( $res as $row ) {
89  $last = $row->name;
90  $toDelete[] = $row->name;
91  if ( !$row->newid ) {
92  $dbw->update(
93  'image',
94  [ 'img_description_id' => $row->oldid ],
95  [ 'img_name' => $row->name ],
96  __METHOD__
97  );
98  $updated++;
99  } elseif ( $row->newid !== $row->oldid ) {
100  $this->error(
101  "Image \"$row->name\" has img_description_id = $row->newid and "
102  . "imgcomment_description_id = $row->oldid. Ignoring the latter."
103  );
104  }
105  }
106  if ( $toDelete ) {
107  $dbw->delete( 'image_comment_temp', [ 'imgcomment_name' => $toDelete ], __METHOD__ );
108  $deleted += count( $toDelete );
109  }
110 
111  $this->commitTransaction( $dbw, __METHOD__ );
112 
113  if ( $numRows < $batchSize ) {
114  // We must have reached the end
115  break;
116  }
117 
118  $this->output( "... $last, updated $updated, deleted $deleted\n" );
119  $conds = [ 'imgcomment_name > ' . $dbw->addQuotes( $last ) ];
120  }
121 
122  // This should be 0, so it should be very fast
123  $count = (int)$dbw->selectField( 'image_comment_temp', 'COUNT(*)', [], __METHOD__ );
124  if ( $count !== 0 ) {
125  $this->error( "Ignoring $count orphaned image_comment_temp row(s)." );
126  }
127 
128  $this->output(
129  "Completed merge of image_comment_temp into the image table, "
130  . "$updated image rows updated, $deleted image_comment_temp rows deleted.\n"
131  );
132 
133  return true;
134  }
135 }
136 
138 require_once RUN_MAINTENANCE_IF_MAIN;
captcha-old.count
count
Definition: captcha-old.py:249
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$res
$res
Definition: database.txt:21
$last
$last
Definition: profileinfo.php:416
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
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1399
MigrateImageCommentTemp\setForce
setForce( $forced=true)
Sets whether a run of this maintenance script has the force parameter set.
Definition: migrateImageCommentTemp.php:44
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1700
MigrateImageCommentTemp\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: migrateImageCommentTemp.php:52
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$maintClass
$maintClass
Definition: migrateImageCommentTemp.php:137
MigrateImageCommentTemp\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: migrateImageCommentTemp.php:48
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1414
MigrateImageCommentTemp\__construct
__construct()
Default constructor.
Definition: migrateImageCommentTemp.php:33
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:367
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:1373
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:462
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
class
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
MigrateImageCommentTemp
Maintenance script that merges image_comment_temp into the image table.
Definition: migrateImageCommentTemp.php:32