MediaWiki  1.33.0
upgradeLogging.php
Go to the documentation of this file.
1 <?php
24 require __DIR__ . '/../commandLine.inc';
25 
27 
35 
39  public $dbw;
40  public $batchSize = 1000;
41  public $minTs = false;
42 
43  function execute() {
44  $this->dbw = $this->getDB( DB_MASTER );
45  $logging = $this->dbw->tableName( 'logging' );
46  $logging_1_10 = $this->dbw->tableName( 'logging_1_10' );
47  $logging_pre_1_10 = $this->dbw->tableName( 'logging_pre_1_10' );
48 
49  if ( $this->dbw->tableExists( 'logging_pre_1_10' ) && !$this->dbw->tableExists( 'logging' ) ) {
50  # Fix previous aborted run
51  echo "Cleaning up from previous aborted run\n";
52  $this->dbw->query( "RENAME TABLE $logging_pre_1_10 TO $logging", __METHOD__ );
53  }
54 
55  if ( $this->dbw->tableExists( 'logging_pre_1_10' ) ) {
56  echo "This script has already been run to completion\n";
57 
58  return;
59  }
60 
61  # Create the target table
62  if ( !$this->dbw->tableExists( 'logging_1_10' ) ) {
63  global $wgDBTableOptions;
64 
65  $sql = <<<EOT
66 CREATE TABLE $logging_1_10 (
67  -- Log ID, for referring to this specific log entry, probably for deletion and such.
68  log_id int unsigned NOT NULL auto_increment,
69 
70  -- Symbolic keys for the general log type and the action type
71  -- within the log. The output format will be controlled by the
72  -- action field, but only the type controls categorization.
73  log_type varbinary(10) NOT NULL default '',
74  log_action varbinary(10) NOT NULL default '',
75 
76  -- Timestamp. Duh.
77  log_timestamp binary(14) NOT NULL default '19700101000000',
78 
79  -- The user who performed this action; key to user_id
80  log_user int unsigned NOT NULL default 0,
81 
82  -- Key to the page affected. Where a user is the target,
83  -- this will point to the user page.
84  log_namespace int NOT NULL default 0,
85  log_title varchar(255) binary NOT NULL default '',
86 
87  -- Freeform text. Interpreted as edit history comments.
88  log_comment varchar(255) NOT NULL default '',
89 
90  -- LF separated list of miscellaneous parameters
91  log_params blob NOT NULL,
92 
93  -- rev_deleted for logs
94  log_deleted tinyint unsigned NOT NULL default '0',
95 
96  PRIMARY KEY log_id (log_id),
97  KEY type_time (log_type, log_timestamp),
98  KEY user_time (log_user, log_timestamp),
99  KEY page_time (log_namespace, log_title, log_timestamp),
100  KEY times (log_timestamp)
101 
103 EOT;
104  echo "Creating table logging_1_10\n";
105  $this->dbw->query( $sql, __METHOD__ );
106  }
107 
108  # Synchronise the tables
109  echo "Doing initial sync...\n";
110  $this->sync( 'logging', 'logging_1_10' );
111  echo "Sync done\n\n";
112 
113  # Rename the old table away
114  echo "Renaming the old table to $logging_pre_1_10\n";
115  $this->dbw->query( "RENAME TABLE $logging TO $logging_pre_1_10", __METHOD__ );
116 
117  # Copy remaining old rows
118  # Done before the new table is active so that $copyPos is accurate
119  echo "Doing final sync...\n";
120  $this->sync( 'logging_pre_1_10', 'logging_1_10' );
121 
122  # Move the new table in
123  echo "Moving the new table in...\n";
124  $this->dbw->query( "RENAME TABLE $logging_1_10 TO $logging", __METHOD__ );
125  echo "Finished.\n";
126  }
127 
133  function sync( $srcTable, $dstTable ) {
134  $batchSize = 1000;
135  $minTs = $this->dbw->selectField( $srcTable, 'MIN(log_timestamp)', '', __METHOD__ );
136  $minTsUnix = wfTimestamp( TS_UNIX, $minTs );
137  $numRowsCopied = 0;
138 
139  while ( true ) {
140  $maxTs = $this->dbw->selectField( $srcTable, 'MAX(log_timestamp)', '', __METHOD__ );
141  $copyPos = $this->dbw->selectField( $dstTable, 'MAX(log_timestamp)', '', __METHOD__ );
142  $maxTsUnix = wfTimestamp( TS_UNIX, $maxTs );
143  $copyPosUnix = wfTimestamp( TS_UNIX, $copyPos );
144 
145  if ( $copyPos === null ) {
146  $percent = 0;
147  } else {
148  $percent = ( $copyPosUnix - $minTsUnix ) / ( $maxTsUnix - $minTsUnix ) * 100;
149  }
150  printf( "%s %.2f%%\n", $copyPos, $percent );
151 
152  # Handle all entries with timestamp equal to $copyPos
153  if ( $copyPos !== null ) {
154  $numRowsCopied += $this->copyExactMatch( $srcTable, $dstTable, $copyPos );
155  }
156 
157  # Now copy a batch of rows
158  if ( $copyPos === null ) {
159  $conds = false;
160  } else {
161  $conds = [ 'log_timestamp > ' . $this->dbw->addQuotes( $copyPos ) ];
162  }
163  $srcRes = $this->dbw->select( $srcTable, '*', $conds, __METHOD__,
164  [ 'LIMIT' => $batchSize, 'ORDER BY' => 'log_timestamp' ] );
165 
166  if ( !$srcRes->numRows() ) {
167  # All done
168  break;
169  }
170 
171  $batch = [];
172  foreach ( $srcRes as $srcRow ) {
173  $batch[] = (array)$srcRow;
174  }
175  $this->dbw->insert( $dstTable, $batch, __METHOD__ );
176  $numRowsCopied += count( $batch );
177 
178  wfWaitForSlaves();
179  }
180  echo "Copied $numRowsCopied rows\n";
181  }
182 
183  function copyExactMatch( $srcTable, $dstTable, $copyPos ) {
184  $numRowsCopied = 0;
185  $srcRes = $this->dbw->select( $srcTable, '*', [ 'log_timestamp' => $copyPos ], __METHOD__ );
186  $dstRes = $this->dbw->select( $dstTable, '*', [ 'log_timestamp' => $copyPos ], __METHOD__ );
187 
188  if ( $srcRes->numRows() ) {
189  $srcRow = $srcRes->fetchObject();
190  $srcFields = array_keys( (array)$srcRow );
191  $srcRes->seek( 0 );
192  $dstRowsSeen = [];
193 
194  # Make a hashtable of rows that already exist in the destination
195  foreach ( $dstRes as $dstRow ) {
196  $reducedDstRow = [];
197  foreach ( $srcFields as $field ) {
198  $reducedDstRow[$field] = $dstRow->$field;
199  }
200  $hash = md5( serialize( $reducedDstRow ) );
201  $dstRowsSeen[$hash] = true;
202  }
203 
204  # Copy all the source rows that aren't already in the destination
205  foreach ( $srcRes as $srcRow ) {
206  $hash = md5( serialize( (array)$srcRow ) );
207  if ( !isset( $dstRowsSeen[$hash] ) ) {
208  $this->dbw->insert( $dstTable, (array)$srcRow, __METHOD__ );
209  $numRowsCopied++;
210  }
211  }
212  }
213 
214  return $numRowsCopied;
215  }
216 }
217 
218 $ul = new UpdateLogging;
219 $ul->execute();
captcha-old.count
count
Definition: captcha-old.py:249
logs
as see the revision history and logs
Definition: MIT-LICENSE.txt:5
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1912
a
</source > ! result< div class="mw-highlight mw-content-ltr" dir="ltr">< pre >< span ></span >< span class="kd"> var</span >< span class="nx"> a</span >< span class="p"></span ></pre ></div > ! end ! test Multiline< source/> in lists !input *< source > a b</source > *foo< source > a b</source > ! html< ul >< li >< div class="mw-highlight mw-content-ltr" dir="ltr">< pre > a b</pre ></div ></li ></ul >< ul >< li > foo< div class="mw-highlight mw-content-ltr" dir="ltr">< pre > a b</pre ></div ></li ></ul > ! html tidy< ul >< li >< div class="mw-highlight mw-content-ltr" dir="ltr">< pre > a b</pre ></div ></li ></ul >< ul >< li > foo< div class="mw-highlight mw-content-ltr" dir="ltr">< pre > a b</pre ></div ></li ></ul > ! end ! test Custom attributes !input< source lang="javascript" id="foo" class="bar" dir="rtl" style="font-size: larger;"> var a
Definition: parserTests.txt:85
page
target page
Definition: All_system_messages.txt:1267
UpdateLogging\$batchSize
$batchSize
Definition: upgradeLogging.php:40
serialize
serialize()
Definition: ApiMessageTrait.php:134
is
This document provides an overview of the usage of PageUpdater and that is
Definition: pageupdater.txt:3
performed
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 it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been performed
Definition: hooks.txt:780
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:2790
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
UpdateLogging\$minTs
$minTs
Definition: upgradeLogging.php:41
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 and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Definition: distributors.txt:9
UpdateLogging\execute
execute()
Definition: upgradeLogging.php:43
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
DB_MASTER
const DB_MASTER
Definition: defines.php:26
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
by
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable or merely the Work and Derivative Works thereof Contribution shall mean any work of including the original version of the Work and any modifications or additions to that Work or Derivative Works that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner For the purposes of this submitted means any form of or written communication sent to the Licensor or its including but not limited to communication on electronic mailing source code control and issue tracking systems that are managed by
Definition: APACHE-LICENSE-2.0.txt:49
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
captcha-old.action
action
Definition: captcha-old.py:212
will
</td >< td > &</td >< td > t want your writing to be edited mercilessly and redistributed at will
Definition: All_system_messages.txt:914
key
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message key
Definition: hooks.txt:2154
$ul
$ul
Definition: upgradeLogging.php:180
$wgDBTableOptions
$wgDBTableOptions
MySQL table options to use during installation or update.
Definition: DefaultSettings.php:1946
UpdateLogging\$dbw
IMaintainableDatabase $dbw
Definition: upgradeLogging.php:39
format
if the prop value should be in the metadata multi language array format
Definition: hooks.txt:1644
text
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
Definition: All_system_messages.txt:1267
and
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
UpdateLogging\sync
sync( $srcTable, $dstTable)
Copy all rows from $srcTable to $dstTable.
Definition: upgradeLogging.php:133
history
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc next in line in page history
Definition: hooks.txt:1769
UpdateLogging\copyExactMatch
copyExactMatch( $srcTable, $dstTable, $copyPos)
Definition: upgradeLogging.php:183
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
$batch
$batch
Definition: linkcache.txt:23
of
globals txt Globals are evil The original MediaWiki code relied on globals for processing context far too often MediaWiki development since then has been a story of slowly moving context out of global variables and into objects Storing processing context in object member variables allows those objects to be reused in a much more flexible way Consider the elegance of
Definition: globals.txt:10
UpdateLogging
Maintenance script that upgrade for log_id/log_deleted fields in a replication-safe way.
Definition: upgradeLogging.php:34
captcha-old.output
output
Definition: captcha-old.py:240
type
This document describes the state of Postgres support in and is fairly well maintained The main code is very well while extensions are very hit and miss it is probably the most supported database after MySQL Much of the work in making MediaWiki database agnostic came about through the work of creating Postgres as and are nearing end of but without copying over all the usage comments General notes on the but these can almost always be programmed around *Although Postgres has a true BOOLEAN type
Definition: postgres.txt:22
Wikimedia\Rdbms\IMaintainableDatabase
Advanced database interface for IDatabase handles that include maintenance methods.
Definition: IMaintainableDatabase.php:38