Example #1
0
 public function execute()
 {
     $dbw = $this->getDB(DB_MASTER);
     $updater = DatabaseUpdater::newForDB($dbw, true, $this);
     foreach ($this->mArgs as $arg) {
         $files = [$arg, $updater->patchPath($dbw, $arg), $updater->patchPath($dbw, "patch-{$arg}.sql")];
         foreach ($files as $file) {
             if (file_exists($file)) {
                 $this->output("{$file} ...\n");
                 $dbw->sourceFile($file);
                 continue 2;
             }
         }
         $this->error("Could not find {$arg}\n");
     }
     $this->output("done.\n");
 }
Example #2
0
 private function prepareDB($version)
 {
     static $maint = null;
     if ($maint === null) {
         $maint = new FakeMaintenance();
         $maint->loadParamsAndArgs(null, array('quiet' => 1));
     }
     global $IP;
     $db = new DatabaseSqliteStandalone(':memory:');
     $db->sourceFile("{$IP}/tests/phpunit/data/db/sqlite/tables-{$version}.sql");
     $updater = DatabaseUpdater::newForDB($db, false, $maint);
     $updater->doUpdates(array('core'));
     return $db;
 }
 /**
  * Perform database upgrades
  *
  * @return Boolean
  */
 public function doUpgrade()
 {
     $this->setupSchemaVars();
     $this->enableLB();
     $ret = true;
     ob_start(array($this, 'outputHandler'));
     $up = DatabaseUpdater::newForDB($this->db);
     try {
         $up->doUpdates();
     } catch (MWException $e) {
         echo "\nAn error occurred:\n";
         echo $e->getText();
         $ret = false;
     }
     $up->purgeCache();
     ob_end_flush();
     return $ret;
 }
Example #4
0
 /**
  * maybeUpgrade
  *
  * look for existence of some columns in database. If they are not exist
  * run database upgrade  on first request. Not very efficient for regular
  * usage but good for transition time
  */
 private function maybeUpgrade()
 {
     wfProfileIn(__METHOD__ . "-upgradedb");
     $dbr = $this->getDB();
     /**
      * look for rev_sha1 in revision table
      */
     if (!$dbr->fieldExists("revision", "rev_sha1", __METHOD__)) {
         $ret = true;
         ob_start(array($this, 'outputHandler'));
         try {
             $up = DatabaseUpdater::newForDB($this->db);
             $up->doUpdates();
         } catch (MWException $e) {
             $this->debug("An error occured: " . $e->getText());
             $ret = false;
         }
         ob_end_flush();
         wfProfileOut(__METHOD__ . "-upgradedb");
         return $ret;
     }
     wfProfileOut(__METHOD__ . "-upgradedb");
 }
Example #5
0
 function execute()
 {
     global $wgVersion, $wgLang, $wgAllowSchemaUpdates;
     if (!$wgAllowSchemaUpdates && !($this->hasOption('force') || $this->hasOption('schema') || $this->hasOption('noschema'))) {
         $this->error("Do not run update.php on this wiki. If you're seeing this you should\n" . "probably ask for some help in performing your schema updates or use\n" . "the --noschema and --schema options to get an SQL file for someone\n" . "else to inspect and run.\n\n" . "If you know what you are doing, you can continue with --force\n", true);
     }
     $this->fileHandle = null;
     if (substr($this->getOption('schema'), 0, 2) === "--") {
         $this->error("The --schema option requires a file as an argument.\n", true);
     } elseif ($this->hasOption('schema')) {
         $file = $this->getOption('schema');
         $this->fileHandle = fopen($file, "w");
         if ($this->fileHandle === false) {
             $err = error_get_last();
             $this->error("Problem opening the schema file for writing: {$file}\n\t{$err['message']}", true);
         }
     }
     $lang = Language::factory('en');
     // Set global language to ensure localised errors are in English (bug 20633)
     RequestContext::getMain()->setLanguage($lang);
     $wgLang = $lang;
     // BackCompat
     define('MW_UPDATER', true);
     $this->output("MediaWiki {$wgVersion} Updater\n\n");
     wfWaitForSlaves();
     if (!$this->hasOption('skip-compat-checks')) {
         $this->compatChecks();
     } else {
         $this->output("Skipping compatibility checks, proceed at your own risk (Ctrl+C to abort)\n");
         wfCountDown(5);
     }
     // Check external dependencies are up to date
     if (!$this->hasOption('skip-external-dependencies')) {
         $composerLockUpToDate = $this->runChild('CheckComposerLockUpToDate');
         $composerLockUpToDate->execute();
     } else {
         $this->output("Skipping checking whether external dependencies are up to date, proceed at your own risk\n");
     }
     # Attempt to connect to the database as a privileged user
     # This will vomit up an error if there are permissions problems
     $db = $this->getDB(DB_MASTER);
     $this->output("Going to run database updates for " . wfWikiID() . "\n");
     if ($db->getType() === 'sqlite') {
         $this->output("Using SQLite file: '{$db->getDbFilePath()}'\n");
     }
     $this->output("Depending on the size of your database this may take a while!\n");
     if (!$this->hasOption('quick')) {
         $this->output("Abort with control-c in the next five seconds " . "(skip this countdown with --quick) ... ");
         wfCountDown(5);
     }
     $time1 = microtime(true);
     $shared = $this->hasOption('doshared');
     $updates = ['core', 'extensions'];
     if (!$this->hasOption('schema')) {
         if ($this->hasOption('noschema')) {
             $updates[] = 'noschema';
         }
         $updates[] = 'stats';
     }
     $updater = DatabaseUpdater::newForDB($db, $shared, $this);
     $updater->doUpdates($updates);
     foreach ($updater->getPostDatabaseUpdateMaintenance() as $maint) {
         $child = $this->runChild($maint);
         // LoggedUpdateMaintenance is checking the updatelog itself
         $isLoggedUpdate = $child instanceof LoggedUpdateMaintenance;
         if (!$isLoggedUpdate && $updater->updateRowExists($maint)) {
             continue;
         }
         $child->execute();
         if (!$isLoggedUpdate) {
             $updater->insertUpdateRow($maint);
         }
     }
     $updater->setFileAccess();
     if (!$this->hasOption('nopurge')) {
         $updater->purgeCache();
     }
     $time2 = microtime(true);
     $timeDiff = $lang->formatTimePeriod($time2 - $time1);
     $this->output("\nDone in {$timeDiff}.\n");
 }
Example #6
0
 public function execute()
 {
     global $IP;
     // We wan't to allow "" for the wikidb, meaning don't call select_db()
     $wiki = $this->hasOption('wikidb') ? $this->getOption('wikidb') : false;
     // Get the appropriate load balancer (for this wiki)
     if ($this->hasOption('cluster')) {
         $lb = wfGetLBFactory()->getExternalLB($this->getOption('cluster'), $wiki);
     } else {
         $lb = wfGetLB($wiki);
     }
     // Figure out which server to use
     $replicaDB = $this->getOption('replicadb', $this->getOption('slave', ''));
     if ($replicaDB === 'any') {
         $index = DB_REPLICA;
     } elseif ($replicaDB != '') {
         $index = null;
         $serverCount = $lb->getServerCount();
         for ($i = 0; $i < $serverCount; ++$i) {
             if ($lb->getServerName($i) === $replicaDB) {
                 $index = $i;
                 break;
             }
         }
         if ($index === null) {
             $this->error("No replica DB server configured with the name '{$replicaDB}'.", 1);
         }
     } else {
         $index = DB_MASTER;
     }
     /** @var Database $db DB handle for the appropriate cluster/wiki */
     $db = $lb->getConnection($index, [], $wiki);
     if ($replicaDB != '' && $db->getLBInfo('master') !== null) {
         $this->error("The server selected ({$db->getServer()}) is not a replica DB.", 1);
     }
     if ($index === DB_MASTER) {
         $updater = DatabaseUpdater::newForDB($db, true, $this);
         $db->setSchemaVars($updater->getSchemaVars());
     }
     if ($this->hasArg(0)) {
         $file = fopen($this->getArg(0), 'r');
         if (!$file) {
             $this->error("Unable to open input file", true);
         }
         $error = $db->sourceStream($file, null, [$this, 'sqlPrintResult']);
         if ($error !== true) {
             $this->error($error, true);
         } else {
             exit(0);
         }
     }
     if ($this->hasOption('query')) {
         $query = $this->getOption('query');
         $this->sqlDoQuery($db, $query, true);
         wfWaitForSlaves();
         return;
     }
     if (function_exists('readline_add_history') && Maintenance::posix_isatty(0)) {
         $historyFile = isset($_ENV['HOME']) ? "{$_ENV['HOME']}/.mwsql_history" : "{$IP}/maintenance/.mwsql_history";
         readline_read_history($historyFile);
     } else {
         $historyFile = null;
     }
     $wholeLine = '';
     $newPrompt = '> ';
     $prompt = $newPrompt;
     $doDie = !Maintenance::posix_isatty(0);
     while (($line = Maintenance::readconsole($prompt)) !== false) {
         if (!$line) {
             # User simply pressed return key
             continue;
         }
         $done = $db->streamStatementEnd($wholeLine, $line);
         $wholeLine .= $line;
         if (!$done) {
             $wholeLine .= ' ';
             $prompt = '    -> ';
             continue;
         }
         if ($historyFile) {
             # Delimiter is eated by streamStatementEnd, we add it
             # up in the history (bug 37020)
             readline_add_history($wholeLine . ';');
             readline_write_history($historyFile);
         }
         $this->sqlDoQuery($db, $wholeLine, $doDie);
         $prompt = $newPrompt;
         $wholeLine = '';
     }
     wfWaitForSlaves();
 }