Add support for update queries
authorChristian Weiske <cweiske@cweiske.de>
Wed, 14 Sep 2016 21:20:42 +0000 (23:20 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 14 Sep 2016 21:20:42 +0000 (23:20 +0200)
src/shpub/Cli.php
src/shpub/Command/Update.php [new file with mode: 0644]
src/shpub/Request.php

index aa9dcaa0236a31a938b4d0baef34e493fadfadf8..9daa59bd681edd2bf92474e1a144f23bc208b122 100644 (file)
@@ -168,6 +168,7 @@ class Cli
 
         Command_Delete::opts($optParser);
         Command_Undelete::opts($optParser);
 
         Command_Delete::opts($optParser);
         Command_Undelete::opts($optParser);
+        Command_Update::opts($optParser);
 
         return $optParser;
     }
 
         return $optParser;
     }
diff --git a/src/shpub/Command/Update.php b/src/shpub/Command/Update.php
new file mode 100644 (file)
index 0000000..d942f5e
--- /dev/null
@@ -0,0 +1,99 @@
+<?php
+namespace shpub;
+
+class Command_Update extends Command_AbstractProps
+{
+    public static function opts(\Console_CommandLine $optParser)
+    {
+        $cmd = $optParser->addCommand('update');
+        $cmd->addOption(
+            'add',
+            array(
+                'short_name'  => '-a',
+                'long_name'   => '--add',
+                'description' => 'Property to add',
+                'help_name'   => 'PROP=VAL',
+                'action'      => 'StoreArray',
+                'default'     => [],
+            )
+        );
+        $cmd->addOption(
+            'replace',
+            array(
+                'short_name'  => '-r',
+                'long_name'   => '--replace',
+                'description' => 'Property to remove',
+                'help_name'   => 'PROP=VAL',
+                'action'      => 'StoreArray',
+                'default'     => [],
+            )
+        );
+        $cmd->addOption(
+            'delete',
+            array(
+                'short_name'  => '-d',
+                'long_name'   => '--delete',
+                'description' => 'Property to delete',
+                'help_name'   => 'PROP=VAL',
+                'action'      => 'StoreArray',
+                'default'     => [],
+            )
+        );
+        $cmd->addArgument(
+            'url',
+            [
+                'optional'    => false,
+                'multiple'    => false,
+                'description' => 'Post URL',
+            ]
+        );
+    }
+
+    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);
+
+        $json = [
+            'action' => 'update',
+            'url'    => $url,
+        ];
+
+        $parts = [
+            'add'     => [],
+            'delete'  => [],
+            'replace' => [],
+        ];
+        foreach (['add', 'delete', 'replace'] as $part) {
+            if (!count($cmdRes->options[$part])) {
+                continue;
+            }
+            foreach ($cmdRes->options[$part] as $kvpair) {
+                list($prop, $val) = explode('=', $kvpair, 2);
+                if (!isset($parts[$part][$prop])) {
+                    $parts[$part][$prop] = [];
+                }
+                $parts[$part][$prop][] = $val;
+            }
+        }
+        foreach ($parts as $part => $changes) {
+            if (count($changes)) {
+                $json[$part] = $changes;
+            }
+        }
+
+        $req->req->setHeader('Content-Type: application/json');
+        $res = $req->send(json_encode($json));
+        $newPostUrl = $res->getHeader('Location');
+        Log::info('Post updated at server');
+        if ($newPostUrl) {
+            Log::info('Post has a new URL:');
+            Log::msg($newPostUrl);
+        }
+    }
+}
+?>
index 8ee7c5a25f15b37ace9527b15b15195a157267f0..cbcc57e0e4a8e4fe348139ab850c78899dc53667 100644 (file)
@@ -7,6 +7,7 @@ class Request
     public $cfg;
 
     protected $uploadsInfo = [];
     public $cfg;
 
     protected $uploadsInfo = [];
+    protected $dedicatedBody = false;
 
     public function __construct($host, $cfg)
     {
 
     public function __construct($host, $cfg)
     {
@@ -25,6 +26,7 @@ class Request
     public function send($body = null)
     {
         if ($body !== null) {
     public function send($body = null)
     {
         if ($body !== null) {
+            $this->dedicatedBody = true;
             $this->req->setBody($body);
         }
         if ($this->cfg->debug) {
             $this->req->setBody($body);
         }
         if ($this->cfg->debug) {
@@ -112,6 +114,10 @@ class Request
             }
         }
 
             }
         }
 
+        if ($this->dedicatedBody) {
+            $command .= ' --data ' . escapeshellarg($this->req->getBody());
+        }
+
         $command .= ' ' . escapeshellarg((string) $this->req->getUrl());
 
         Log::msg($command);
         $command .= ' ' . escapeshellarg((string) $this->req->getUrl());
 
         Log::msg($command);