Make Phame blog policies non-nullable
[phabricator.git] / scripts / util / add_macro.php
blob03566cfb7689d34dce531238eb3c8450908d9d39
1 #!/usr/bin/env php
2 <?php
4 $root = dirname(dirname(dirname(__FILE__)));
5 require_once $root.'/scripts/__init_script__.php';
7 $args = new PhutilArgumentParser($argv);
8 $args->setTagline(pht('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
15 $args->parseStandardArguments();
17 $args->parse(
18 array(
19 array(
20 'name' => 'as',
21 'param' => 'name',
22 'help' => pht(
23 'Use a specific name instead of the first part of the image name.'),
25 array(
26 'name' => 'more',
27 'wildcard' => true,
29 ));
31 $more = $args->getArg('more');
32 if (count($more) !== 1) {
33 $args->printHelpAndExit();
36 $path = head($more);
37 $data = Filesystem::readFile($path);
39 $name = $args->getArg('as');
40 if ($name === null) {
41 $name = head(explode('.', basename($path)));
44 $existing = id(new PhabricatorFileImageMacro())->loadOneWhere(
45 'name = %s',
46 $name);
47 if ($existing) {
48 throw new Exception(pht("A macro already exists with the name '%s'!", $name));
51 $file = PhabricatorFile::newFromFileData(
52 $data,
53 array(
54 'name' => basename($path),
55 'canCDN' => true,
56 ));
58 $macro = id(new PhabricatorFileImageMacro())
59 ->setFilePHID($file->getPHID())
60 ->setName($name)
61 ->save();
63 $id = $file->getID();
65 echo pht("Added macro '%s' (%s).", $name, "F{$id}")."\n";