add support for reposts
[shpub.git] / src / shpub / Command / Repost.php
diff --git a/src/shpub/Command/Repost.php b/src/shpub/Command/Repost.php
new file mode 100644 (file)
index 0000000..2f4b46f
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+namespace shpub;
+
+class Command_Repost extends Command_AbstractProps
+{
+    /**
+     * @var Config
+     */
+    protected $cfg;
+
+    public function __construct($cfg)
+    {
+        $this->cfg = $cfg;
+    }
+
+    public static function opts(\Console_CommandLine $optParser)
+    {
+        $cmd = $optParser->addCommand('repost');
+        static::optsGeneric($cmd);
+        $cmd->addArgument(
+            'url',
+            [
+                'optional'    => false,
+                'description' => 'URL that shall be reposted',
+            ]
+        );
+    }
+
+    public function run(\Console_CommandLine_Result $cmdRes)
+    {
+        $url = Validator::url($cmdRes->args['url'], 'url');
+        if ($url === false) {
+            exit(10);
+        }
+
+        $req = new Request($this->cfg->host, $this->cfg);
+        $req->req->addPostParameter('h', 'entry');
+        $req->req->addPostParameter('repost-of', $url);
+
+        $this->handleGenericOptions($cmdRes, $req);
+        $res = $req->send();
+
+        $postUrl = $res->getHeader('Location');
+        if ($postUrl === null) {
+            Log::err('Error: Server sent no "Location" header and said:');
+            Log::err($res->getBody());
+            exit(20);
+        } else {
+            Log::info('Repost created at server');
+            Log::msg($postUrl);
+        }
+    }
+}
+?>