PageRenderTime 64ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/maintenance/upgrade1_5.php

https://github.com/spenser-roark/OOUG-Wiki
PHP | 1337 lines | 1001 code | 156 blank | 180 comment | 53 complexity | 59de7d3f654ccf389b46d8dff592a744 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * Alternate 1.4 -> 1.5 schema upgrade.
  4. * This does only the main tables + UTF-8 and is designed to allow upgrades to
  5. * interleave with other updates on the replication stream so that large wikis
  6. * can be upgraded without disrupting other services.
  7. *
  8. * Note: this script DOES NOT apply every update, nor will it probably handle
  9. * much older versions, etc.
  10. * Run this, FOLLOWED BY update.php, for upgrading from 1.4.5 release to 1.5.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25. * http://www.gnu.org/copyleft/gpl.html
  26. *
  27. * @file
  28. * @ingroup Maintenance
  29. */
  30. require_once( dirname( __FILE__ ) . '/Maintenance.php' );
  31. define( 'MW_UPGRADE_COPY', false );
  32. define( 'MW_UPGRADE_ENCODE', true );
  33. define( 'MW_UPGRADE_NULL', null );
  34. define( 'MW_UPGRADE_CALLBACK', null ); // for self-documentation only
  35. /**
  36. * @ingroup Maintenance
  37. */
  38. class FiveUpgrade extends Maintenance {
  39. /**
  40. * @var DatabaseBase
  41. */
  42. protected $db;
  43. function __construct() {
  44. parent::__construct();
  45. $this->mDescription = 'Script for upgrades from 1.4 to 1.5 (NOT 1.15) in very special cases.';
  46. $this->addOption( 'upgrade', 'Really run the script' );
  47. $this->addOption( 'noimage', '' );
  48. $this->addOption( 'step', 'Only do a specific step', false, true );
  49. }
  50. public function getDbType() {
  51. return Maintenance::DB_ADMIN;
  52. }
  53. public function execute() {
  54. $this->output( "ATTENTION: This script is for upgrades from 1.4 to 1.5 (NOT 1.15) in very special cases.\n" );
  55. $this->output( "Use update.php for usual updates.\n" );
  56. if ( !$this->hasOption( 'upgrade' ) ) {
  57. $this->output( "Please run this script with --upgrade key to actually run the updater.\n" );
  58. return;
  59. }
  60. $this->setMembers();
  61. $tables = array(
  62. 'page',
  63. 'links',
  64. 'user',
  65. 'image',
  66. 'oldimage',
  67. 'watchlist',
  68. 'logging',
  69. 'archive',
  70. 'imagelinks',
  71. 'categorylinks',
  72. 'ipblocks',
  73. 'recentchanges',
  74. 'querycache'
  75. );
  76. foreach ( $tables as $table ) {
  77. if ( $this->doing( $table ) ) {
  78. $method = 'upgrade' . ucfirst( $table );
  79. $this->$method();
  80. }
  81. }
  82. if ( $this->doing( 'cleanup' ) ) {
  83. $this->upgradeCleanup();
  84. }
  85. }
  86. protected function setMembers() {
  87. $this->conversionTables = $this->prepareWindows1252();
  88. $this->loadBalancers = array();
  89. $this->dbw = wfGetDB( DB_MASTER );
  90. $this->dbr = $this->streamConnection();
  91. $this->cleanupSwaps = array();
  92. $this->emailAuth = false; # don't preauthenticate emails
  93. $this->step = $this->getOption( 'step', null );
  94. }
  95. function doing( $step ) {
  96. return is_null( $this->step ) || $step == $this->step;
  97. }
  98. /**
  99. * Open a connection to the master server with the admin rights.
  100. * @return Database
  101. * @access private
  102. */
  103. function newConnection() {
  104. $lb = wfGetLBFactory()->newMainLB();
  105. $db = $lb->getConnection( DB_MASTER );
  106. $this->loadBalancers[] = $lb;
  107. return $db;
  108. }
  109. /**
  110. * Commit transactions and close the connections when we're done...
  111. */
  112. function close() {
  113. foreach ( $this->loadBalancers as $lb ) {
  114. $lb->commitMasterChanges();
  115. $lb->closeAll();
  116. }
  117. }
  118. /**
  119. * Open a second connection to the master server, with buffering off.
  120. * This will let us stream large datasets in and write in chunks on the
  121. * other end.
  122. * @return Database
  123. * @access private
  124. */
  125. function streamConnection() {
  126. $timeout = 3600 * 24;
  127. $db = $this->newConnection();
  128. $db->bufferResults( false );
  129. if ( $db->getType() == 'mysql' ) {
  130. $db->query( "SET net_read_timeout=$timeout" );
  131. $db->query( "SET net_write_timeout=$timeout" );
  132. }
  133. return $db;
  134. }
  135. /**
  136. * Prepare a conversion array for converting Windows Code Page 1252 to
  137. * UTF-8. This should provide proper conversion of text that was miscoded
  138. * as Windows-1252 by naughty user-agents, and doesn't rely on an outside
  139. * iconv library.
  140. *
  141. * @return array
  142. * @access private
  143. */
  144. function prepareWindows1252() {
  145. # Mappings from:
  146. # http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
  147. static $cp1252 = array(
  148. 0x80 => 0x20AC, # EURO SIGN
  149. 0x81 => 0xFFFD, # REPLACEMENT CHARACTER (no mapping)
  150. 0x82 => 0x201A, # SINGLE LOW-9 QUOTATION MARK
  151. 0x83 => 0x0192, # LATIN SMALL LETTER F WITH HOOK
  152. 0x84 => 0x201E, # DOUBLE LOW-9 QUOTATION MARK
  153. 0x85 => 0x2026, # HORIZONTAL ELLIPSIS
  154. 0x86 => 0x2020, # DAGGER
  155. 0x87 => 0x2021, # DOUBLE DAGGER
  156. 0x88 => 0x02C6, # MODIFIER LETTER CIRCUMFLEX ACCENT
  157. 0x89 => 0x2030, # PER MILLE SIGN
  158. 0x8A => 0x0160, # LATIN CAPITAL LETTER S WITH CARON
  159. 0x8B => 0x2039, # SINGLE LEFT-POINTING ANGLE QUOTATION MARK
  160. 0x8C => 0x0152, # LATIN CAPITAL LIGATURE OE
  161. 0x8D => 0xFFFD, # REPLACEMENT CHARACTER (no mapping)
  162. 0x8E => 0x017D, # LATIN CAPITAL LETTER Z WITH CARON
  163. 0x8F => 0xFFFD, # REPLACEMENT CHARACTER (no mapping)
  164. 0x90 => 0xFFFD, # REPLACEMENT CHARACTER (no mapping)
  165. 0x91 => 0x2018, # LEFT SINGLE QUOTATION MARK
  166. 0x92 => 0x2019, # RIGHT SINGLE QUOTATION MARK
  167. 0x93 => 0x201C, # LEFT DOUBLE QUOTATION MARK
  168. 0x94 => 0x201D, # RIGHT DOUBLE QUOTATION MARK
  169. 0x95 => 0x2022, # BULLET
  170. 0x96 => 0x2013, # EN DASH
  171. 0x97 => 0x2014, # EM DASH
  172. 0x98 => 0x02DC, # SMALL TILDE
  173. 0x99 => 0x2122, # TRADE MARK SIGN
  174. 0x9A => 0x0161, # LATIN SMALL LETTER S WITH CARON
  175. 0x9B => 0x203A, # SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
  176. 0x9C => 0x0153, # LATIN SMALL LIGATURE OE
  177. 0x9D => 0xFFFD, # REPLACEMENT CHARACTER (no mapping)
  178. 0x9E => 0x017E, # LATIN SMALL LETTER Z WITH CARON
  179. 0x9F => 0x0178, # LATIN CAPITAL LETTER Y WITH DIAERESIS
  180. );
  181. $pairs = array();
  182. for ( $i = 0; $i < 0x100; $i++ ) {
  183. $unicode = isset( $cp1252[$i] ) ? $cp1252[$i] : $i;
  184. $pairs[chr( $i )] = codepointToUtf8( $unicode );
  185. }
  186. return $pairs;
  187. }
  188. /**
  189. * Convert from 8-bit Windows-1252 to UTF-8 if necessary.
  190. * @param string $text
  191. * @return string
  192. * @access private
  193. */
  194. function conv( $text ) {
  195. global $wgUseLatin1;
  196. return is_null( $text )
  197. ? null
  198. : ( $wgUseLatin1
  199. ? strtr( $text, $this->conversionTables )
  200. : $text );
  201. }
  202. /**
  203. * Dump timestamp and message to output
  204. * @param $message String
  205. * @access private
  206. */
  207. function log( $message ) {
  208. $this->output( wfWikiID() . ' ' . wfTimestamp( TS_DB ) . ': ' . $message . "\n" );
  209. }
  210. /**
  211. * Initialize the chunked-insert system.
  212. * Rows will be inserted in chunks of the given number, rather
  213. * than in a giant INSERT...SELECT query, to keep the serialized
  214. * MySQL database replication from getting hung up. This way other
  215. * things can be going on during conversion without waiting for
  216. * slaves to catch up as badly.
  217. *
  218. * @param int $chunksize Number of rows to insert at once
  219. * @param int $final Total expected number of rows / id of last row,
  220. * used for progress reports.
  221. * @param string $table to insert on
  222. * @param string $fname function name to report in SQL
  223. * @access private
  224. */
  225. function setChunkScale( $chunksize, $final, $table, $fname ) {
  226. $this->chunkSize = $chunksize;
  227. $this->chunkFinal = $final;
  228. $this->chunkCount = 0;
  229. $this->chunkStartTime = wfTime();
  230. $this->chunkOptions = array( 'IGNORE' );
  231. $this->chunkTable = $table;
  232. $this->chunkFunction = $fname;
  233. }
  234. /**
  235. * Chunked inserts: perform an insert if we've reached the chunk limit.
  236. * Prints a progress report with estimated completion time.
  237. * @param array &$chunk -- This will be emptied if an insert is done.
  238. * @param int $key A key identifier to use in progress estimation in
  239. * place of the number of rows inserted. Use this if
  240. * you provided a max key number instead of a count
  241. * as the final chunk number in setChunkScale()
  242. * @access private
  243. */
  244. function addChunk( &$chunk, $key = null ) {
  245. if ( count( $chunk ) >= $this->chunkSize ) {
  246. $this->insertChunk( $chunk );
  247. $this->chunkCount += count( $chunk );
  248. $now = wfTime();
  249. $delta = $now - $this->chunkStartTime;
  250. $rate = $this->chunkCount / $delta;
  251. if ( is_null( $key ) ) {
  252. $completed = $this->chunkCount;
  253. } else {
  254. $completed = $key;
  255. }
  256. $portion = $completed / $this->chunkFinal;
  257. $estimatedTotalTime = $delta / $portion;
  258. $eta = $this->chunkStartTime + $estimatedTotalTime;
  259. printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec\n",
  260. wfTimestamp( TS_DB, intval( $now ) ),
  261. $portion * 100.0,
  262. $this->chunkTable,
  263. wfTimestamp( TS_DB, intval( $eta ) ),
  264. $completed,
  265. $this->chunkFinal,
  266. $rate );
  267. flush();
  268. $chunk = array();
  269. }
  270. }
  271. /**
  272. * Chunked inserts: perform an insert unconditionally, at the end, and log.
  273. * @param array &$chunk -- This will be emptied if an insert is done.
  274. * @access private
  275. */
  276. function lastChunk( &$chunk ) {
  277. $n = count( $chunk );
  278. if ( $n > 0 ) {
  279. $this->insertChunk( $chunk );
  280. }
  281. $this->log( "100.00% done on $this->chunkTable (last chunk $n rows)." );
  282. }
  283. /**
  284. * Chunked inserts: perform an insert.
  285. * @param array &$chunk -- This will be emptied if an insert is done.
  286. * @access private
  287. */
  288. function insertChunk( &$chunk ) {
  289. // Give slaves a chance to catch up
  290. wfWaitForSlaves();
  291. $this->dbw->insert( $this->chunkTable, $chunk, $this->chunkFunction, $this->chunkOptions );
  292. }
  293. /**
  294. * Helper function for copyTable array_filter
  295. * @param $x
  296. * @return bool
  297. */
  298. static private function notUpgradeNull( $x ) {
  299. return $x !== MW_UPGRADE_NULL;
  300. }
  301. /**
  302. * Copy and transcode a table to table_temp.
  303. * @param string $name Base name of the source table
  304. * @param string $tabledef CREATE TABLE definition, w/ $1 for the name
  305. * @param array $fields set of destination fields to these constants:
  306. * MW_UPGRADE_COPY - straight copy
  307. * MW_UPGRADE_ENCODE - for old Latin1 wikis, conv to UTF-8
  308. * MW_UPGRADE_NULL - just put NULL
  309. * @param callable $callback An optional callback to modify the data
  310. * or perform other processing. Func should be
  311. * ( object $row, array $copy ) and return $copy
  312. * @access private
  313. */
  314. function copyTable( $name, $tabledef, $fields, $callback = null ) {
  315. $name_temp = $name . '_temp';
  316. $this->log( "Migrating $name table to $name_temp..." );
  317. $table_temp = $this->dbw->tableName( $name_temp );
  318. // Create temporary table; we're going to copy everything in there,
  319. // then at the end rename the final tables into place.
  320. $def = str_replace( '$1', $table_temp, $tabledef );
  321. $this->dbw->query( $def, __METHOD__ );
  322. $numRecords = $this->dbw->selectField( $name, 'COUNT(*)', '', __METHOD__ );
  323. $this->setChunkScale( 100, $numRecords, $name_temp, __METHOD__ );
  324. // Pull all records from the second, streaming database connection.
  325. $sourceFields = array_keys( array_filter( $fields, 'FiveUpgrade::notUpgradeNull' ) );
  326. $result = $this->dbr->select( $name,
  327. $sourceFields,
  328. '',
  329. __METHOD__ );
  330. $add = array();
  331. foreach ( $result as $row ) {
  332. $copy = array();
  333. foreach ( $fields as $field => $source ) {
  334. if ( $source === MW_UPGRADE_COPY ) {
  335. $copy[$field] = $row->$field;
  336. } elseif ( $source === MW_UPGRADE_ENCODE ) {
  337. $copy[$field] = $this->conv( $row->$field );
  338. } elseif ( $source === MW_UPGRADE_NULL ) {
  339. $copy[$field] = null;
  340. } else {
  341. $this->log( "Unknown field copy type: $field => $source" );
  342. }
  343. }
  344. if ( is_callable( $callback ) ) {
  345. $copy = call_user_func( $callback, $row, $copy );
  346. }
  347. $add[] = $copy;
  348. $this->addChunk( $add );
  349. }
  350. $this->lastChunk( $add );
  351. $this->log( "Done converting $name." );
  352. $this->cleanupSwaps[] = $name;
  353. }
  354. function upgradePage() {
  355. $chunksize = 100;
  356. if ( $this->dbw->tableExists( 'page' ) ) {
  357. $this->error( 'Page table already exists.', true );
  358. }
  359. $this->log( "Checking cur table for unique title index and applying if necessary" );
  360. $this->checkDupes();
  361. $this->log( "...converting from cur/old to page/revision/text DB structure." );
  362. list ( $cur, $old, $page, $revision, $text ) = $this->dbw->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
  363. $this->log( "Creating page and revision tables..." );
  364. $this->dbw->query( "CREATE TABLE $page (
  365. page_id int(8) unsigned NOT NULL auto_increment,
  366. page_namespace int NOT NULL,
  367. page_title varchar(255) binary NOT NULL,
  368. page_restrictions tinyblob NOT NULL default '',
  369. page_counter bigint(20) unsigned NOT NULL default '0',
  370. page_is_redirect tinyint(1) unsigned NOT NULL default '0',
  371. page_is_new tinyint(1) unsigned NOT NULL default '0',
  372. page_random real unsigned NOT NULL,
  373. page_touched char(14) binary NOT NULL default '',
  374. page_latest int(8) unsigned NOT NULL,
  375. page_len int(8) unsigned NOT NULL,
  376. PRIMARY KEY page_id (page_id),
  377. UNIQUE INDEX name_title (page_namespace,page_title),
  378. INDEX (page_random),
  379. INDEX (page_len)
  380. ) TYPE=InnoDB", __METHOD__ );
  381. $this->dbw->query( "CREATE TABLE $revision (
  382. rev_id int(8) unsigned NOT NULL auto_increment,
  383. rev_page int(8) unsigned NOT NULL,
  384. rev_text_id int(8) unsigned NOT NULL,
  385. rev_comment tinyblob NOT NULL default '',
  386. rev_user int(5) unsigned NOT NULL default '0',
  387. rev_user_text varchar(255) binary NOT NULL default '',
  388. rev_timestamp char(14) binary NOT NULL default '',
  389. rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
  390. rev_deleted tinyint(1) unsigned NOT NULL default '0',
  391. PRIMARY KEY rev_page_id (rev_page, rev_id),
  392. UNIQUE INDEX rev_id (rev_id),
  393. INDEX rev_timestamp (rev_timestamp),
  394. INDEX page_timestamp (rev_page,rev_timestamp),
  395. INDEX user_timestamp (rev_user,rev_timestamp),
  396. INDEX usertext_timestamp (rev_user_text,rev_timestamp)
  397. ) TYPE=InnoDB", __METHOD__ );
  398. $maxold = intval( $this->dbw->selectField( 'old', 'max(old_id)', '', __METHOD__ ) );
  399. $this->log( "Last old record is {$maxold}" );
  400. global $wgLegacySchemaConversion;
  401. if ( $wgLegacySchemaConversion ) {
  402. // Create HistoryBlobCurStub entries.
  403. // Text will be pulled from the leftover 'cur' table at runtime.
  404. echo "......Moving metadata from cur; using blob references to text in cur table.\n";
  405. $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
  406. $cur_flags = "'object'";
  407. } else {
  408. // Copy all cur text in immediately: this may take longer but avoids
  409. // having to keep an extra table around.
  410. echo "......Moving text from cur.\n";
  411. $cur_text = 'cur_text';
  412. $cur_flags = "''";
  413. }
  414. $maxcur = $this->dbw->selectField( 'cur', 'max(cur_id)', '', __METHOD__ );
  415. $this->log( "Last cur entry is $maxcur" );
  416. /**
  417. * Copy placeholder records for each page's current version into old
  418. * Don't do any conversion here; text records are converted at runtime
  419. * based on the flags (and may be originally binary!) while the meta
  420. * fields will be converted in the old -> rev and cur -> page steps.
  421. */
  422. $this->setChunkScale( $chunksize, $maxcur, 'old', __METHOD__ );
  423. $result = $this->dbr->query(
  424. "SELECT cur_id, cur_namespace, cur_title, $cur_text AS text, cur_comment,
  425. cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags AS flags
  426. FROM $cur
  427. ORDER BY cur_id", __METHOD__ );
  428. $add = array();
  429. foreach ( $result as $row ) {
  430. $add[] = array(
  431. 'old_namespace' => $row->cur_namespace,
  432. 'old_title' => $row->cur_title,
  433. 'old_text' => $row->text,
  434. 'old_comment' => $row->cur_comment,
  435. 'old_user' => $row->cur_user,
  436. 'old_user_text' => $row->cur_user_text,
  437. 'old_timestamp' => $row->cur_timestamp,
  438. 'old_minor_edit' => $row->cur_minor_edit,
  439. 'old_flags' => $row->flags );
  440. $this->addChunk( $add, $row->cur_id );
  441. }
  442. $this->lastChunk( $add );
  443. /**
  444. * Copy revision metadata from old into revision.
  445. * We'll also do UTF-8 conversion of usernames and comments.
  446. */
  447. # $newmaxold = $this->dbw->selectField( 'old', 'max(old_id)', '', __METHOD__ );
  448. # $this->setChunkScale( $chunksize, $newmaxold, 'revision', __METHOD__ );
  449. # $countold = $this->dbw->selectField( 'old', 'count(old_id)', '', __METHOD__ );
  450. $countold = $this->dbw->selectField( 'old', 'max(old_id)', '', __METHOD__ );
  451. $this->setChunkScale( $chunksize, $countold, 'revision', __METHOD__ );
  452. $this->log( "......Setting up revision table." );
  453. $result = $this->dbr->query(
  454. "SELECT old_id, cur_id, old_comment, old_user, old_user_text,
  455. old_timestamp, old_minor_edit
  456. FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title",
  457. __METHOD__ );
  458. $add = array();
  459. foreach ( $result as $row ) {
  460. $add[] = array(
  461. 'rev_id' => $row->old_id,
  462. 'rev_page' => $row->cur_id,
  463. 'rev_text_id' => $row->old_id,
  464. 'rev_comment' => $this->conv( $row->old_comment ),
  465. 'rev_user' => $row->old_user,
  466. 'rev_user_text' => $this->conv( $row->old_user_text ),
  467. 'rev_timestamp' => $row->old_timestamp,
  468. 'rev_minor_edit' => $row->old_minor_edit );
  469. $this->addChunk( $add );
  470. }
  471. $this->lastChunk( $add );
  472. /**
  473. * Copy page metadata from cur into page.
  474. * We'll also do UTF-8 conversion of titles.
  475. */
  476. $this->log( "......Setting up page table." );
  477. $this->setChunkScale( $chunksize, $maxcur, 'page', __METHOD__ );
  478. $result = $this->dbr->query( "
  479. SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
  480. cur_random, cur_touched, rev_id, LENGTH(cur_text) AS len
  481. FROM $cur,$revision
  482. WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
  483. ORDER BY cur_id", __METHOD__ );
  484. $add = array();
  485. foreach ( $result as $row ) {
  486. $add[] = array(
  487. 'page_id' => $row->cur_id,
  488. 'page_namespace' => $row->cur_namespace,
  489. 'page_title' => $this->conv( $row->cur_title ),
  490. 'page_restrictions' => $row->cur_restrictions,
  491. 'page_counter' => $row->cur_counter,
  492. 'page_is_redirect' => $row->cur_is_redirect,
  493. 'page_is_new' => $row->cur_is_new,
  494. 'page_random' => $row->cur_random,
  495. 'page_touched' => $this->dbw->timestamp(),
  496. 'page_latest' => $row->rev_id,
  497. 'page_len' => $row->len );
  498. # $this->addChunk( $add, $row->cur_id );
  499. $this->addChunk( $add );
  500. }
  501. $this->lastChunk( $add );
  502. $this->log( "...done with cur/old -> page/revision." );
  503. }
  504. function upgradeLinks() {
  505. $chunksize = 200;
  506. list ( $links, $brokenlinks, $pagelinks, $cur ) = $this->dbw->tableNamesN( 'links', 'brokenlinks', 'pagelinks', 'cur' );
  507. $this->log( 'Checking for interwiki table change in case of bogus items...' );
  508. if ( $this->dbw->fieldExists( 'interwiki', 'iw_trans' ) ) {
  509. $this->log( 'interwiki has iw_trans.' );
  510. } else {
  511. global $IP;
  512. $this->log( 'adding iw_trans...' );
  513. $this->dbw->sourceFile( $IP . '/maintenance/archives/patch-interwiki-trans.sql' );
  514. $this->log( 'added iw_trans.' );
  515. }
  516. $this->log( 'Creating pagelinks table...' );
  517. $this->dbw->query( "
  518. CREATE TABLE $pagelinks (
  519. -- Key to the page_id of the page containing the link.
  520. pl_from int(8) unsigned NOT NULL default '0',
  521. -- Key to page_namespace/page_title of the target page.
  522. -- The target page may or may not exist, and due to renames
  523. -- and deletions may refer to different page records as time
  524. -- goes by.
  525. pl_namespace int NOT NULL default '0',
  526. pl_title varchar(255) binary NOT NULL default '',
  527. UNIQUE KEY pl_from(pl_from,pl_namespace,pl_title),
  528. KEY (pl_namespace,pl_title)
  529. ) TYPE=InnoDB" );
  530. $this->log( 'Importing live links -> pagelinks' );
  531. $nlinks = $this->dbw->selectField( 'links', 'count(*)', '', __METHOD__ );
  532. if ( $nlinks ) {
  533. $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', __METHOD__ );
  534. $result = $this->dbr->query( "
  535. SELECT l_from,cur_namespace,cur_title
  536. FROM $links, $cur
  537. WHERE l_to=cur_id", __METHOD__ );
  538. $add = array();
  539. foreach ( $result as $row ) {
  540. $add[] = array(
  541. 'pl_from' => $row->l_from,
  542. 'pl_namespace' => $row->cur_namespace,
  543. 'pl_title' => $this->conv( $row->cur_title ) );
  544. $this->addChunk( $add );
  545. }
  546. $this->lastChunk( $add );
  547. } else {
  548. $this->log( 'no links!' );
  549. }
  550. $this->log( 'Importing brokenlinks -> pagelinks' );
  551. $nbrokenlinks = $this->dbw->selectField( 'brokenlinks', 'count(*)', '', __METHOD__ );
  552. if ( $nbrokenlinks ) {
  553. $this->setChunkScale( $chunksize, $nbrokenlinks, 'pagelinks', __METHOD__ );
  554. $result = $this->dbr->query(
  555. "SELECT bl_from, bl_to FROM $brokenlinks",
  556. __METHOD__ );
  557. $add = array();
  558. foreach ( $result as $row ) {
  559. $pagename = $this->conv( $row->bl_to );
  560. $title = Title::newFromText( $pagename );
  561. if ( is_null( $title ) ) {
  562. $this->log( "** invalid brokenlink: $row->bl_from -> '$pagename' (converted from '$row->bl_to')" );
  563. } else {
  564. $add[] = array(
  565. 'pl_from' => $row->bl_from,
  566. 'pl_namespace' => $title->getNamespace(),
  567. 'pl_title' => $title->getDBkey() );
  568. $this->addChunk( $add );
  569. }
  570. }
  571. $this->lastChunk( $add );
  572. } else {
  573. $this->log( 'no brokenlinks!' );
  574. }
  575. $this->log( 'Done with links.' );
  576. }
  577. function userDupeCallback( $str ) {
  578. echo $str;
  579. }
  580. function upgradeUser() {
  581. // Apply unique index, if necessary:
  582. $duper = new UserDupes( $this->dbw, array( $this, 'userDupeCallback' ) );
  583. if ( $duper->hasUniqueIndex() ) {
  584. $this->log( "Already have unique user_name index." );
  585. } else {
  586. $this->log( "Clearing user duplicates..." );
  587. if ( !$duper->clearDupes() ) {
  588. $this->log( "WARNING: Duplicate user accounts, may explode!" );
  589. }
  590. }
  591. $tabledef = <<<END
  592. CREATE TABLE $1 (
  593. user_id int(5) unsigned NOT NULL auto_increment,
  594. user_name varchar(255) binary NOT NULL default '',
  595. user_real_name varchar(255) binary NOT NULL default '',
  596. user_password tinyblob NOT NULL default '',
  597. user_newpassword tinyblob NOT NULL default '',
  598. user_email tinytext NOT NULL default '',
  599. user_options blob NOT NULL default '',
  600. user_touched char(14) binary NOT NULL default '',
  601. user_token char(32) binary NOT NULL default '',
  602. user_email_authenticated CHAR(14) BINARY,
  603. user_email_token CHAR(32) BINARY,
  604. user_email_token_expires CHAR(14) BINARY,
  605. PRIMARY KEY user_id (user_id),
  606. UNIQUE INDEX user_name (user_name),
  607. INDEX (user_email_token)
  608. ) TYPE=InnoDB
  609. END;
  610. $fields = array(
  611. 'user_id' => MW_UPGRADE_COPY,
  612. 'user_name' => MW_UPGRADE_ENCODE,
  613. 'user_real_name' => MW_UPGRADE_ENCODE,
  614. 'user_password' => MW_UPGRADE_COPY,
  615. 'user_newpassword' => MW_UPGRADE_COPY,
  616. 'user_email' => MW_UPGRADE_ENCODE,
  617. 'user_options' => MW_UPGRADE_ENCODE,
  618. 'user_touched' => MW_UPGRADE_CALLBACK,
  619. 'user_token' => MW_UPGRADE_COPY,
  620. 'user_email_authenticated' => MW_UPGRADE_CALLBACK,
  621. 'user_email_token' => MW_UPGRADE_NULL,
  622. 'user_email_token_expires' => MW_UPGRADE_NULL );
  623. $this->copyTable( 'user', $tabledef, $fields,
  624. array( &$this, 'userCallback' ) );
  625. }
  626. function userCallback( $row, $copy ) {
  627. $now = $this->dbw->timestamp();
  628. $copy['user_touched'] = $now;
  629. $copy['user_email_authenticated'] = $this->emailAuth ? $now : null;
  630. return $copy;
  631. }
  632. function upgradeImage() {
  633. $tabledef = <<<END
  634. CREATE TABLE $1 (
  635. img_name varchar(255) binary NOT NULL default '',
  636. img_size int(8) unsigned NOT NULL default '0',
  637. img_width int(5) NOT NULL default '0',
  638. img_height int(5) NOT NULL default '0',
  639. img_metadata mediumblob NOT NULL,
  640. img_bits int(3) NOT NULL default '0',
  641. img_media_type ENUM("UNKNOWN", "BITMAP", "DRAWING", "AUDIO", "VIDEO", "MULTIMEDIA", "OFFICE", "TEXT", "EXECUTABLE", "ARCHIVE") default NULL,
  642. img_major_mime ENUM("unknown", "application", "audio", "image", "text", "video", "message", "model", "multipart") NOT NULL default "unknown",
  643. img_minor_mime varchar(32) NOT NULL default "unknown",
  644. img_description tinyblob NOT NULL default '',
  645. img_user int(5) unsigned NOT NULL default '0',
  646. img_user_text varchar(255) binary NOT NULL default '',
  647. img_timestamp char(14) binary NOT NULL default '',
  648. PRIMARY KEY img_name (img_name),
  649. INDEX img_size (img_size),
  650. INDEX img_timestamp (img_timestamp)
  651. ) TYPE=InnoDB
  652. END;
  653. $fields = array(
  654. 'img_name' => MW_UPGRADE_ENCODE,
  655. 'img_size' => MW_UPGRADE_COPY,
  656. 'img_width' => MW_UPGRADE_CALLBACK,
  657. 'img_height' => MW_UPGRADE_CALLBACK,
  658. 'img_metadata' => MW_UPGRADE_CALLBACK,
  659. 'img_bits' => MW_UPGRADE_CALLBACK,
  660. 'img_media_type' => MW_UPGRADE_CALLBACK,
  661. 'img_major_mime' => MW_UPGRADE_CALLBACK,
  662. 'img_minor_mime' => MW_UPGRADE_CALLBACK,
  663. 'img_description' => MW_UPGRADE_ENCODE,
  664. 'img_user' => MW_UPGRADE_COPY,
  665. 'img_user_text' => MW_UPGRADE_ENCODE,
  666. 'img_timestamp' => MW_UPGRADE_COPY );
  667. $this->copyTable( 'image', $tabledef, $fields,
  668. array( &$this, 'imageCallback' ) );
  669. }
  670. function imageCallback( $row, $copy ) {
  671. if ( !$this->hasOption( 'noimage' ) ) {
  672. // Fill in the new image info fields
  673. $info = $this->imageInfo( $row->img_name );
  674. $copy['img_width' ] = $info['width'];
  675. $copy['img_height' ] = $info['height'];
  676. $copy['img_metadata' ] = ""; // loaded on-demand
  677. $copy['img_bits' ] = $info['bits'];
  678. $copy['img_media_type'] = $info['media'];
  679. $copy['img_major_mime'] = $info['major'];
  680. $copy['img_minor_mime'] = $info['minor'];
  681. }
  682. // If doing UTF8 conversion the file must be renamed
  683. $this->renameFile( $row->img_name, 'wfImageDir' );
  684. return $copy;
  685. }
  686. function imageInfo( $filename ) {
  687. $info = array(
  688. 'width' => 0,
  689. 'height' => 0,
  690. 'bits' => 0,
  691. 'media' => '',
  692. 'major' => '',
  693. 'minor' => '' );
  694. $magic = MimeMagic::singleton();
  695. $mime = $magic->guessMimeType( $filename, true );
  696. list( $info['major'], $info['minor'] ) = explode( '/', $mime );
  697. $info['media'] = $magic->getMediaType( $filename, $mime );
  698. $image = UnregisteredLocalFile::newFromPath( $filename, $mime );
  699. $info['width'] = $image->getWidth();
  700. $info['height'] = $image->getHeight();
  701. $gis = $image->getImageSize( $filename );
  702. if ( isset( $gis['bits'] ) ) {
  703. $info['bits'] = $gis['bits'];
  704. }
  705. return $info;
  706. }
  707. /**
  708. * Truncate a table.
  709. * @param string $table The table name to be truncated
  710. */
  711. function clearTable( $table ) {
  712. print "Clearing $table...\n";
  713. $tableName = $this->db->tableName( $table );
  714. $this->db->query( "TRUNCATE $tableName" );
  715. }
  716. /**
  717. * Rename a given image or archived image file to the converted filename,
  718. * leaving a symlink for URL compatibility.
  719. *
  720. * @param $oldname string pre-conversion filename
  721. * @param $subdirCallback string
  722. * @param $basename string pre-conversion base filename for dir hashing, if an archive
  723. * @return bool|string
  724. * @access private
  725. */
  726. function renameFile( $oldname, $subdirCallback = 'wfImageDir', $basename = null ) {
  727. $newname = $this->conv( $oldname );
  728. if ( $newname == $oldname ) {
  729. // No need to rename; another field triggered this row.
  730. return false;
  731. }
  732. if ( is_null( $basename ) ) $basename = $oldname;
  733. $ubasename = $this->conv( $basename );
  734. $oldpath = call_user_func( $subdirCallback, $basename ) . '/' . $oldname;
  735. $newpath = call_user_func( $subdirCallback, $ubasename ) . '/' . $newname;
  736. $this->log( "$oldpath -> $newpath" );
  737. if ( rename( $oldpath, $newpath ) ) {
  738. $relpath = wfRelativePath( $newpath, dirname( $oldpath ) );
  739. if ( !symlink( $relpath, $oldpath ) ) {
  740. $this->log( "... symlink failed!" );
  741. }
  742. return $newname;
  743. } else {
  744. $this->log( "... rename failed!" );
  745. return false;
  746. }
  747. }
  748. function upgradeOldImage() {
  749. $tabledef = <<<END
  750. CREATE TABLE $1 (
  751. -- Base filename: key to image.img_name
  752. oi_name varchar(255) binary NOT NULL default '',
  753. -- Filename of the archived file.
  754. -- This is generally a timestamp and '!' prepended to the base name.
  755. oi_archive_name varchar(255) binary NOT NULL default '',
  756. -- Other fields as in image...
  757. oi_size int(8) unsigned NOT NULL default 0,
  758. oi_width int(5) NOT NULL default 0,
  759. oi_height int(5) NOT NULL default 0,
  760. oi_bits int(3) NOT NULL default 0,
  761. oi_description tinyblob NOT NULL default '',
  762. oi_user int(5) unsigned NOT NULL default '0',
  763. oi_user_text varchar(255) binary NOT NULL default '',
  764. oi_timestamp char(14) binary NOT NULL default '',
  765. INDEX oi_name (oi_name(10))
  766. ) TYPE=InnoDB;
  767. END;
  768. $fields = array(
  769. 'oi_name' => MW_UPGRADE_ENCODE,
  770. 'oi_archive_name' => MW_UPGRADE_ENCODE,
  771. 'oi_size' => MW_UPGRADE_COPY,
  772. 'oi_width' => MW_UPGRADE_CALLBACK,
  773. 'oi_height' => MW_UPGRADE_CALLBACK,
  774. 'oi_bits' => MW_UPGRADE_CALLBACK,
  775. 'oi_description' => MW_UPGRADE_ENCODE,
  776. 'oi_user' => MW_UPGRADE_COPY,
  777. 'oi_user_text' => MW_UPGRADE_ENCODE,
  778. 'oi_timestamp' => MW_UPGRADE_COPY );
  779. $this->copyTable( 'oldimage', $tabledef, $fields,
  780. array( &$this, 'oldimageCallback' ) );
  781. }
  782. function oldimageCallback( $row, $copy ) {
  783. global $options;
  784. if ( !isset( $options['noimage'] ) ) {
  785. // Fill in the new image info fields
  786. $info = $this->imageInfo( $row->oi_archive_name, 'wfImageArchiveDir', $row->oi_name );
  787. $copy['oi_width' ] = $info['width' ];
  788. $copy['oi_height'] = $info['height'];
  789. $copy['oi_bits' ] = $info['bits' ];
  790. }
  791. // If doing UTF8 conversion the file must be renamed
  792. $this->renameFile( $row->oi_archive_name, 'wfImageArchiveDir', $row->oi_name );
  793. return $copy;
  794. }
  795. function upgradeWatchlist() {
  796. $chunksize = 100;
  797. list ( $watchlist, $watchlist_temp ) = $this->dbw->tableNamesN( 'watchlist', 'watchlist_temp' );
  798. $this->log( 'Migrating watchlist table to watchlist_temp...' );
  799. $this->dbw->query(
  800. "CREATE TABLE $watchlist_temp (
  801. -- Key to user_id
  802. wl_user int(5) unsigned NOT NULL,
  803. -- Key to page_namespace/page_title
  804. -- Note that users may watch patches which do not exist yet,
  805. -- or existed in the past but have been deleted.
  806. wl_namespace int NOT NULL default '0',
  807. wl_title varchar(255) binary NOT NULL default '',
  808. -- Timestamp when user was last sent a notification e-mail;
  809. -- cleared when the user visits the page.
  810. -- FIXME: add proper null support etc
  811. wl_notificationtimestamp varchar(14) binary NOT NULL default '0',
  812. UNIQUE KEY (wl_user, wl_namespace, wl_title),
  813. KEY namespace_title (wl_namespace,wl_title)
  814. ) TYPE=InnoDB;", __METHOD__ );
  815. // Fix encoding for Latin-1 upgrades, add some fields,
  816. // and double article to article+talk pairs
  817. $numwatched = $this->dbw->selectField( 'watchlist', 'count(*)', '', __METHOD__ );
  818. $this->setChunkScale( $chunksize, $numwatched * 2, 'watchlist_temp', __METHOD__ );
  819. $result = $this->dbr->select( 'watchlist',
  820. array(
  821. 'wl_user',
  822. 'wl_namespace',
  823. 'wl_title' ),
  824. '',
  825. __METHOD__ );
  826. $add = array();
  827. foreach ( $result as $row ) {
  828. $add[] = array(
  829. 'wl_user' => $row->wl_user,
  830. 'wl_namespace' => MWNamespace::getSubject( $row->wl_namespace ),
  831. 'wl_title' => $this->conv( $row->wl_title ),
  832. 'wl_notificationtimestamp' => '0' );
  833. $this->addChunk( $add );
  834. $add[] = array(
  835. 'wl_user' => $row->wl_user,
  836. 'wl_namespace' => MWNamespace::getTalk( $row->wl_namespace ),
  837. 'wl_title' => $this->conv( $row->wl_title ),
  838. 'wl_notificationtimestamp' => '0' );
  839. $this->addChunk( $add );
  840. }
  841. $this->lastChunk( $add );
  842. $this->log( 'Done converting watchlist.' );
  843. $this->cleanupSwaps[] = 'watchlist';
  844. }
  845. function upgradeLogging() {
  846. $tabledef = <<<ENDS
  847. CREATE TABLE $1 (
  848. -- Symbolic keys for the general log type and the action type
  849. -- within the log. The output format will be controlled by the
  850. -- action field, but only the type controls categorization.
  851. log_type char(10) NOT NULL default '',
  852. log_action char(10) NOT NULL default '',
  853. -- Timestamp. Duh.
  854. log_timestamp char(14) NOT NULL default '19700101000000',
  855. -- The user who performed this action; key to user_id
  856. log_user int unsigned NOT NULL default 0,
  857. -- Key to the page affected. Where a user is the target,
  858. -- this will point to the user page.
  859. log_namespace int NOT NULL default 0,
  860. log_title varchar(255) binary NOT NULL default '',
  861. -- Freeform text. Interpreted as edit history comments.
  862. log_comment varchar(255) NOT NULL default '',
  863. -- LF separated list of miscellaneous parameters
  864. log_params blob NOT NULL default '',
  865. KEY type_time (log_type, log_timestamp),
  866. KEY user_time (log_user, log_timestamp),
  867. KEY page_time (log_namespace, log_title, log_timestamp)
  868. ) TYPE=InnoDB
  869. ENDS;
  870. $fields = array(
  871. 'log_type' => MW_UPGRADE_COPY,
  872. 'log_action' => MW_UPGRADE_COPY,
  873. 'log_timestamp' => MW_UPGRADE_COPY,
  874. 'log_user' => MW_UPGRADE_COPY,
  875. 'log_namespace' => MW_UPGRADE_COPY,
  876. 'log_title' => MW_UPGRADE_ENCODE,
  877. 'log_comment' => MW_UPGRADE_ENCODE,
  878. 'log_params' => MW_UPGRADE_ENCODE );
  879. $this->copyTable( 'logging', $tabledef, $fields );
  880. }
  881. function upgradeArchive() {
  882. $tabledef = <<<ENDS
  883. CREATE TABLE $1 (
  884. ar_namespace int NOT NULL default '0',
  885. ar_title varchar(255) binary NOT NULL default '',
  886. ar_text mediumblob NOT NULL default '',
  887. ar_comment tinyblob NOT NULL default '',
  888. ar_user int(5) unsigned NOT NULL default '0',
  889. ar_user_text varchar(255) binary NOT NULL,
  890. ar_timestamp char(14) binary NOT NULL default '',
  891. ar_minor_edit tinyint(1) NOT NULL default '0',
  892. ar_flags tinyblob NOT NULL default '',
  893. ar_rev_id int(8) unsigned,
  894. ar_text_id int(8) unsigned,
  895. KEY name_title_timestamp (ar_namespace,ar_title,ar_timestamp)
  896. ) TYPE=InnoDB
  897. ENDS;
  898. $fields = array(
  899. 'ar_namespace' => MW_UPGRADE_COPY,
  900. 'ar_title' => MW_UPGRADE_ENCODE,
  901. 'ar_text' => MW_UPGRADE_COPY,
  902. 'ar_comment' => MW_UPGRADE_ENCODE,
  903. 'ar_user' => MW_UPGRADE_COPY,
  904. 'ar_user_text' => MW_UPGRADE_ENCODE,
  905. 'ar_timestamp' => MW_UPGRADE_COPY,
  906. 'ar_minor_edit' => MW_UPGRADE_COPY,
  907. 'ar_flags' => MW_UPGRADE_COPY,
  908. 'ar_rev_id' => MW_UPGRADE_NULL,
  909. 'ar_text_id' => MW_UPGRADE_NULL );
  910. $this->copyTable( 'archive', $tabledef, $fields );
  911. }
  912. function upgradeImagelinks() {
  913. global $wgUseLatin1;
  914. if ( $wgUseLatin1 ) {
  915. $tabledef = <<<ENDS
  916. CREATE TABLE $1 (
  917. -- Key to page_id of the page containing the image / media link.
  918. il_from int(8) unsigned NOT NULL default '0',
  919. -- Filename of target image.
  920. -- This is also the page_title of the file's description page;
  921. -- all such pages are in namespace 6 (NS_FILE).
  922. il_to varchar(255) binary NOT NULL default '',
  923. UNIQUE KEY il_from(il_from,il_to),
  924. KEY (il_to)
  925. ) TYPE=InnoDB
  926. ENDS;
  927. $fields = array(
  928. 'il_from' => MW_UPGRADE_COPY,
  929. 'il_to' => MW_UPGRADE_ENCODE );
  930. $this->copyTable( 'imagelinks', $tabledef, $fields );
  931. }
  932. }
  933. function upgradeCategorylinks() {
  934. global $wgUseLatin1;
  935. if ( $wgUseLatin1 ) {
  936. $tabledef = <<<ENDS
  937. CREATE TABLE $1 (
  938. cl_from int(8) unsigned NOT NULL default '0',
  939. cl_to varchar(255) binary NOT NULL default '',
  940. cl_sortkey varchar(86) binary NOT NULL default '',
  941. cl_timestamp timestamp NOT NULL,
  942. UNIQUE KEY cl_from(cl_from,cl_to),
  943. KEY cl_sortkey(cl_to,cl_sortkey),
  944. KEY cl_timestamp(cl_to,cl_timestamp)
  945. ) TYPE=InnoDB
  946. ENDS;
  947. $fields = array(
  948. 'cl_from' => MW_UPGRADE_COPY,
  949. 'cl_to' => MW_UPGRADE_ENCODE,
  950. 'cl_sortkey' => MW_UPGRADE_ENCODE,
  951. 'cl_timestamp' => MW_UPGRADE_COPY );
  952. $this->copyTable( 'categorylinks', $tabledef, $fields );
  953. }
  954. }
  955. function upgradeIpblocks() {
  956. global $wgUseLatin1;
  957. if ( $wgUseLatin1 ) {
  958. $tabledef = <<<ENDS
  959. CREATE TABLE $1 (
  960. ipb_id int(8) NOT NULL auto_increment,
  961. ipb_address varchar(40) binary NOT NULL default '',
  962. ipb_user int(8) unsigned NOT NULL default '0',
  963. ipb_by int(8) unsigned NOT NULL default '0',
  964. ipb_reason tinyblob NOT NULL default '',
  965. ipb_timestamp char(14) binary NOT NULL default '',
  966. ipb_auto tinyint(1) NOT NULL default '0',
  967. ipb_expiry char(14) binary NOT NULL default '',
  968. PRIMARY KEY ipb_id (ipb_id),
  969. INDEX ipb_address (ipb_address),
  970. INDEX ipb_user (ipb_user)
  971. ) TYPE=InnoDB
  972. ENDS;
  973. $fields = array(
  974. 'ipb_id' => MW_UPGRADE_COPY,
  975. 'ipb_address' => MW_UPGRADE_COPY,
  976. 'ipb_user' => MW_UPGRADE_COPY,
  977. 'ipb_by' => MW_UPGRADE_COPY,
  978. 'ipb_reason' => MW_UPGRADE_ENCODE,
  979. 'ipb_timestamp' => MW_UPGRADE_COPY,
  980. 'ipb_auto' => MW_UPGRADE_COPY,
  981. 'ipb_expiry' => MW_UPGRADE_COPY );
  982. $this->copyTable( 'ipblocks', $tabledef, $fields );
  983. }
  984. }
  985. function upgradeRecentchanges() {
  986. // There's a format change in the namespace field
  987. $tabledef = <<<ENDS
  988. CREATE TABLE $1 (
  989. rc_id int(8) NOT NULL auto_increment,
  990. rc_timestamp varchar(14) binary NOT NULL default '',
  991. rc_cur_time varchar(14) binary NOT NULL default '',
  992. rc_user int(10) unsigned NOT NULL default '0',
  993. rc_user_text varchar(255) binary NOT NULL default '',
  994. rc_namespace int NOT NULL default '0',
  995. rc_title varchar(255) binary NOT NULL default '',
  996. rc_comment varchar(255) binary NOT NULL default '',
  997. rc_minor tinyint(3) unsigned NOT NULL default '0',
  998. rc_bot tinyint(3) unsigned NOT NULL default '0',
  999. rc_new tinyint(3) unsigned NOT NULL default '0',
  1000. rc_cur_id int(10) unsigned NOT NULL default '0',
  1001. rc_this_oldid int(10) unsigned NOT NULL default '0',
  1002. rc_last_oldid int(10) unsigned NOT NULL default '0',
  1003. rc_type tinyint(3) unsigned NOT NULL default '0',
  1004. rc_moved_to_ns tinyint(3) unsigned NOT NULL default '0',
  1005. rc_moved_to_title varchar(255) binary NOT NULL default '',
  1006. rc_patrolled tinyint(3) unsigned NOT NULL default '0',
  1007. rc_ip char(15) NOT NULL default '',
  1008. PRIMARY KEY rc_id (rc_id),
  1009. INDEX rc_timestamp (rc_timestamp),
  1010. INDEX rc_namespace_title (rc_namespace, rc_title),
  1011. INDEX rc_cur_id (rc_cur_id),
  1012. INDEX new_name_timestamp(rc_new,rc_namespace,rc_timestamp),
  1013. INDEX rc_ip (rc_ip)
  1014. ) TYPE=InnoDB
  1015. ENDS;
  1016. $fields = array(
  1017. 'rc_id' => MW_UPGRADE_COPY,
  1018. 'rc_timestamp' => MW_UPGRADE_COPY,
  1019. 'rc_cur_time' => MW_UPGRADE_COPY,
  1020. 'rc_user' => MW_UPGRADE_COPY,
  1021. 'rc_user_text' => MW_UPGRADE_ENCODE,
  1022. 'rc_namespace' => MW_UPGRADE_COPY,
  1023. 'rc_title' => MW_UPGRADE_ENCODE,
  1024. 'rc_comment' => MW_UPGRADE_ENCODE,
  1025. 'rc_minor' => MW_UPGRADE_COPY,
  1026. 'rc_bot' => MW_UPGRADE_COPY,
  1027. 'rc_new' => MW_UPGRADE_COPY,
  1028. 'rc_cur_id' => MW_UPGRADE_COPY,
  1029. 'rc_this_oldid' => MW_UPGRADE_COPY,
  1030. 'rc_last_oldid' => MW_UPGRADE_COPY,
  1031. 'rc_type' => MW_UPGRADE_COPY,
  1032. 'rc_moved_to_ns' => MW_UPGRADE_COPY,
  1033. 'rc_moved_to_title' => MW_UPGRADE_ENCODE,
  1034. 'rc_patrolled' => MW_UPGRADE_COPY,
  1035. 'rc_ip' => MW_UPGRADE_COPY );
  1036. $this->copyTable( 'recentchanges', $tabledef, $fields );
  1037. }
  1038. function upgradeQuerycache() {
  1039. // There's a format change in the namespace field
  1040. $tabledef = <<<ENDS
  1041. CREATE TABLE $1 (
  1042. -- A key name, generally the base name of of the special page.
  1043. qc_type char(32) NOT NULL,
  1044. -- Some sort of stored value. Sizes, counts...
  1045. qc_value int(5) unsigned NOT NULL default '0',
  1046. -- Target namespace+title
  1047. qc_namespace int NOT NULL default '0',
  1048. qc_title char(255) binary NOT NULL default '',
  1049. KEY (qc_type,qc_value)
  1050. ) TYPE=InnoDB
  1051. ENDS;
  1052. $fields = array(
  1053. 'qc_type' => MW_UPGRADE_COPY,
  1054. 'qc_value' => MW_UPGRADE_COPY,
  1055. 'qc_namespace' => MW_UPGRADE_COPY,
  1056. 'qc_title' => MW_UPGRADE_ENCODE );
  1057. $this->copyTable( 'querycache', $tabledef, $fields );
  1058. }
  1059. /**
  1060. * Check for duplicate rows in "cur" table and move duplicates entries in
  1061. * "old" table.
  1062. *
  1063. * This was in cleanupDupes.inc before.
  1064. */
  1065. function checkDupes() {
  1066. $dbw = wfGetDB( DB_MASTER );
  1067. if ( $dbw->indexExists( 'cur', 'name_title' ) &&
  1068. $dbw->indexUnique( 'cur', 'name_title' ) ) {
  1069. echo wfWikiID() . ": cur table has the current unique index; no duplicate entries.\n";
  1070. return;
  1071. } elseif ( $dbw->indexExists( 'cur', 'name_title_dup_prevention' ) ) {
  1072. echo wfWikiID() . ": cur table has a temporary name_title_dup_prevention unique index; no duplicate entries.\n";
  1073. return;
  1074. }
  1075. echo wfWikiID() . ": cur table has the old non-unique index and may have duplicate entries.\n";
  1076. $dbw = wfGetDB( DB_MASTER );
  1077. $cur = $dbw->tableName( 'cur' );
  1078. $old = $dbw->tableName( 'old' );
  1079. $dbw->query( "LOCK TABLES $cur WRITE, $old WRITE" );
  1080. echo "Checking for duplicate cur table entries... (this may take a while on a large wiki)\n";
  1081. $res = $dbw->query( <<<END
  1082. SELECT cur_namespace,cur_title,count(*) as c,min(cur_id) as id
  1083. FROM $cur
  1084. GROUP BY cur_namespace,cur_title
  1085. HAVING c > 1
  1086. END
  1087. );
  1088. $n = $dbw->numRows( $res );
  1089. echo "Found $n titles with duplicate entries.\n";
  1090. if ( $n > 0 ) {
  1091. echo "Correcting...\n";
  1092. foreach ( $res as $row ) {
  1093. $ns = intval( $row->cur_namespace );
  1094. $title = $dbw->addQuotes( $row->cur_title );
  1095. # Get the first responding ID; that'll be the one we keep.
  1096. $id = $dbw->selectField( 'cur', 'cur_id', array(
  1097. 'cur_namespace' => $row->cur_namespace,
  1098. 'cur_title' => $row->cur_title ) );
  1099. echo "$ns:$row->cur_title (canonical ID $id)\n";
  1100. if ( $id != $row->id ) {
  1101. echo " ** minimum ID $row->id; ";
  1102. $timeMin = $dbw->selectField( 'cur', 'cur_timestamp', array(
  1103. 'cur_id' => $row->id ) );
  1104. $timeFirst = $dbw->selectField( 'cur', 'cur_timestamp', array(
  1105. 'cur_id' => $id ) );
  1106. if ( $timeMin == $timeFirst ) {
  1107. echo "timestamps match at $timeFirst; ok\n";
  1108. } else {
  1109. echo "timestamps don't match! min: $timeMin, first: $timeFirst; ";
  1110. if ( $timeMin > $timeFirst ) {
  1111. $id = $row->id;
  1112. echo "keeping minimum: $id\n";
  1113. } else {
  1114. echo "keeping first: $id\n";
  1115. }
  1116. }
  1117. }
  1118. $dbw->query( <<<END
  1119. INSERT
  1120. INTO $old
  1121. (old_namespace, old_title, old_text,
  1122. old_comment, old_user, old_user_text,
  1123. old_timestamp, old_minor_edit, old_flags,
  1124. inverse_timestamp)
  1125. SELECT cur_namespace, cur_title, cur_text,
  1126. cur_comment, cur_user, cur_user_text,
  1127. cur_timestamp, cur_minor_edit, '',
  1128. inverse_timestamp
  1129. FROM $cur
  1130. WHERE cur_namespace=$ns
  1131. AND cur_title=$title
  1132. AND cur_id != $id
  1133. END
  1134. );
  1135. $dbw->query( <<<END
  1136. DELETE
  1137. FROM $cur
  1138. WHERE cur_namespace=$ns
  1139. AND cur_title=$title
  1140. AND cur_id != $id
  1141. END
  1142. );
  1143. }
  1144. }
  1145. $dbw->query( 'UNLOCK TABLES' );
  1146. echo "Done.\n";
  1147. }
  1148. /**
  1149. * Rename all our temporary tables into final place.
  1150. * We've left things in place so a read-only wiki can continue running
  1151. * on the old code during all this.
  1152. */
  1153. function upgradeCleanup() {
  1154. $this->renameTable( 'old', 'text' );
  1155. foreach ( $this->cleanupSwaps as $table ) {
  1156. $this->swap( $table );
  1157. }
  1158. }
  1159. function renameTable( $from, $to ) {
  1160. $this->log( "Renaming $from to $to..." );
  1161. $fromtable = $this->dbw->tableName( $from );
  1162. $totable = $this->dbw->tableName( $to );
  1163. $this->dbw->query( "ALTER TABLE $fromtable RENAME TO $totable" );
  1164. }
  1165. function swap( $base ) {
  1166. $this->renameTable( $base, "{$base}_old" );
  1167. $this->renameTable( "{$base}_temp", $base );
  1168. }
  1169. }
  1170. $maintClass = 'FiveUpgrade';
  1171. require_once( RUN_MAINTENANCE_IF_MAIN );