[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  final class PhabricatorFilesManagementRebuildWorkflow
   4    extends PhabricatorFilesManagementWorkflow {
   5  
   6    public function didConstruct() {
   7      $this
   8        ->setName('rebuild')
   9        ->setSynopsis('Rebuild metadata of old files.')
  10        ->setArguments(
  11          array(
  12            array(
  13              'name'      => 'all',
  14              'help'      => 'Update all files.',
  15            ),
  16            array(
  17              'name'      => 'dry-run',
  18              'help'      => 'Show what would be updated.',
  19            ),
  20            array(
  21              'name'      => 'rebuild-mime',
  22              'help'      => 'Rebuild MIME information.',
  23            ),
  24            array(
  25              'name'      => 'rebuild-dimensions',
  26              'help'      => 'Rebuild image dimension information.',
  27            ),
  28            array(
  29              'name'      => 'names',
  30              'wildcard'  => true,
  31            ),
  32          ));
  33    }
  34  
  35    public function execute(PhutilArgumentParser $args) {
  36      $console = PhutilConsole::getConsole();
  37  
  38      $iterator = $this->buildIterator($args);
  39      if (!$iterator) {
  40        throw new PhutilArgumentUsageException(
  41          'Either specify a list of files to update, or use `--all` '.
  42          'to update all files.');
  43      }
  44  
  45      $update = array(
  46        'mime'          => $args->getArg('rebuild-mime'),
  47        'dimensions'    => $args->getArg('rebuild-dimensions'),
  48      );
  49  
  50      // If the user didn't select anything, rebuild everything.
  51      if (!array_filter($update)) {
  52        foreach ($update as $key => $ignored) {
  53          $update[$key] = true;
  54        }
  55      }
  56  
  57      $is_dry_run = $args->getArg('dry-run');
  58  
  59      $failed = array();
  60  
  61      foreach ($iterator as $file) {
  62        $fid = 'F'.$file->getID();
  63  
  64        if ($update['mime']) {
  65          $tmp = new TempFile();
  66          Filesystem::writeFile($tmp, $file->loadFileData());
  67          $new_type = Filesystem::getMimeType($tmp);
  68  
  69          if ($new_type == $file->getMimeType()) {
  70            $console->writeOut(
  71              "%s: Mime type not changed (%s).\n",
  72              $fid,
  73              $new_type);
  74          } else {
  75            if ($is_dry_run) {
  76              $console->writeOut(
  77                "%s: Would update Mime type: '%s' -> '%s'.\n",
  78                $fid,
  79                $file->getMimeType(),
  80                $new_type);
  81            } else {
  82              $console->writeOut(
  83                "%s: Updating Mime type: '%s' -> '%s'.\n",
  84                $fid,
  85                $file->getMimeType(),
  86                $new_type);
  87              $file->setMimeType($new_type);
  88              $file->save();
  89            }
  90          }
  91        }
  92  
  93        if ($update['dimensions']) {
  94          if (!$file->isViewableImage()) {
  95            $console->writeOut(
  96              "%s: Not an image file.\n",
  97              $fid);
  98            continue;
  99          }
 100  
 101          $metadata = $file->getMetadata();
 102          $image_width = idx($metadata, PhabricatorFile::METADATA_IMAGE_WIDTH);
 103          $image_height = idx($metadata, PhabricatorFile::METADATA_IMAGE_HEIGHT);
 104          if ($image_width && $image_height) {
 105            $console->writeOut(
 106              "%s: Image dimensions already exist.\n",
 107              $fid);
 108            continue;
 109          }
 110  
 111          if ($is_dry_run) {
 112            $console->writeOut(
 113              "%s: Would update file dimensions (dry run)\n",
 114              $fid);
 115            continue;
 116          }
 117  
 118          $console->writeOut(
 119            '%s: Updating metadata... ',
 120            $fid);
 121  
 122          try {
 123            $file->updateDimensions();
 124            $console->writeOut("done.\n");
 125          } catch (Exception $ex) {
 126            $console->writeOut("failed!\n");
 127            $console->writeErr("%s\n", (string)$ex);
 128            $failed[] = $file;
 129          }
 130        }
 131      }
 132  
 133      if ($failed) {
 134        $console->writeOut("**Failures!**\n");
 135        $ids = array();
 136        foreach ($failed as $file) {
 137          $ids[] = 'F'.$file->getID();
 138        }
 139        $console->writeOut("%s\n", implode(', ', $ids));
 140  
 141        return 1;
 142      } else {
 143        $console->writeOut("**Success!**\n");
 144        return 0;
 145      }
 146  
 147      return 0;
 148    }
 149  }


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