[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/files/management/ -> PhabricatorFilesManagementMigrateWorkflow.php (source)

   1  <?php
   2  
   3  final class PhabricatorFilesManagementMigrateWorkflow
   4    extends PhabricatorFilesManagementWorkflow {
   5  
   6    public function didConstruct() {
   7      $this
   8        ->setName('migrate')
   9        ->setSynopsis('Migrate files between storage engines.')
  10        ->setArguments(
  11          array(
  12            array(
  13              'name'      => 'engine',
  14              'param'     => 'storage_engine',
  15              'help'      => 'Migrate to the named storage engine.',
  16            ),
  17            array(
  18              'name'      => 'dry-run',
  19              'help'      => 'Show what would be migrated.',
  20            ),
  21            array(
  22              'name'      => 'all',
  23              'help'      => 'Migrate all files.',
  24            ),
  25            array(
  26              'name'      => 'names',
  27              'wildcard'  => true,
  28            ),
  29          ));
  30    }
  31  
  32    public function execute(PhutilArgumentParser $args) {
  33      $console = PhutilConsole::getConsole();
  34  
  35      $engine_id = $args->getArg('engine');
  36      if (!$engine_id) {
  37        throw new PhutilArgumentUsageException(
  38          'Specify an engine to migrate to with `--engine`. '.
  39          'Use `files engines` to get a list of engines.');
  40      }
  41  
  42      $engine = PhabricatorFile::buildEngine($engine_id);
  43  
  44      $iterator = $this->buildIterator($args);
  45      if (!$iterator) {
  46        throw new PhutilArgumentUsageException(
  47          'Either specify a list of files to migrate, or use `--all` '.
  48          'to migrate all files.');
  49      }
  50  
  51      $is_dry_run = $args->getArg('dry-run');
  52  
  53      $failed = array();
  54  
  55      foreach ($iterator as $file) {
  56        $fid = 'F'.$file->getID();
  57  
  58        if ($file->getStorageEngine() == $engine_id) {
  59          $console->writeOut(
  60            "%s: Already stored on '%s'\n",
  61            $fid,
  62            $engine_id);
  63          continue;
  64        }
  65  
  66        if ($is_dry_run) {
  67          $console->writeOut(
  68            "%s: Would migrate from '%s' to '%s' (dry run)\n",
  69            $fid,
  70            $file->getStorageEngine(),
  71            $engine_id);
  72          continue;
  73        }
  74  
  75        $console->writeOut(
  76          "%s: Migrating from '%s' to '%s'...",
  77          $fid,
  78          $file->getStorageEngine(),
  79          $engine_id);
  80  
  81        try {
  82          $file->migrateToEngine($engine);
  83          $console->writeOut("done.\n");
  84        } catch (Exception $ex) {
  85          $console->writeOut("failed!\n");
  86          $console->writeErr("%s\n", (string)$ex);
  87          $failed[] = $file;
  88        }
  89      }
  90  
  91      if ($failed) {
  92        $console->writeOut("**Failures!**\n");
  93        $ids = array();
  94        foreach ($failed as $file) {
  95          $ids[] = 'F'.$file->getID();
  96        }
  97        $console->writeOut("%s\n", implode(', ', $ids));
  98  
  99        return 1;
 100      } else {
 101        $console->writeOut("**Success!**\n");
 102        return 0;
 103      }
 104    }
 105  
 106  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1