[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/scripts/util/ -> add_macro.php (source)

   1  #!/usr/bin/env php
   2  <?php
   3  
   4  $root = dirname(dirname(dirname(__FILE__)));
   5  require_once $root.'/scripts/__init_script__.php';
   6  
   7  $args = new PhutilArgumentParser($argv);
   8  $args->setTagline('load files as image macros');
   9  $args->setSynopsis(<<<EOHELP
  10  **add_macro.php** __image__ [--as __name__]
  11      Add an image macro. This can be useful for importing a large number
  12      of macros.
  13  EOHELP
  14  );
  15  $args->parseStandardArguments();
  16  
  17  $args->parse(
  18    array(
  19      array(
  20        'name'  => 'as',
  21        'param' => 'name',
  22        'help'  => 'Use a specific name instead of the first part of the image '.
  23                   'name.',
  24      ),
  25      array(
  26        'name'      => 'more',
  27        'wildcard'  => true,
  28      ),
  29    ));
  30  
  31  $more = $args->getArg('more');
  32  if (count($more) !== 1) {
  33    $args->printHelpAndExit();
  34  }
  35  
  36  $path = head($more);
  37  $data = Filesystem::readFile($path);
  38  
  39  $name = $args->getArg('as');
  40  if ($name === null) {
  41    $name = head(explode('.', basename($path)));
  42  }
  43  
  44  $existing = id(new PhabricatorFileImageMacro())->loadOneWhere(
  45    'name = %s',
  46    $name);
  47  if ($existing) {
  48    throw new Exception("A macro already exists with the name '{$name}'!");
  49  }
  50  
  51  $file = PhabricatorFile::newFromFileData(
  52    $data,
  53    array(
  54      'name' => basename($path),
  55      'canCDN' => true,
  56    ));
  57  
  58  $macro = id(new PhabricatorFileImageMacro())
  59    ->setFilePHID($file->getPHID())
  60    ->setName($name)
  61    ->save();
  62  
  63  $id = $file->getID();
  64  
  65  echo "Added macro '{$name}' (F{$id}).\n";


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