From 6f748120fbb71c4a7b3d9fd762f40933b8cb4e95 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 14 Sep 2016 23:20:42 +0200 Subject: [PATCH] Add support for update queries --- src/shpub/Cli.php | 1 + src/shpub/Command/Update.php | 99 ++++++++++++++++++++++++++++++++++++ src/shpub/Request.php | 6 +++ 3 files changed, 106 insertions(+) create mode 100644 src/shpub/Command/Update.php diff --git a/src/shpub/Cli.php b/src/shpub/Cli.php index aa9dcaa..9daa59b 100644 --- a/src/shpub/Cli.php +++ b/src/shpub/Cli.php @@ -168,6 +168,7 @@ class Cli Command_Delete::opts($optParser); Command_Undelete::opts($optParser); + Command_Update::opts($optParser); return $optParser; } diff --git a/src/shpub/Command/Update.php b/src/shpub/Command/Update.php new file mode 100644 index 0000000..d942f5e --- /dev/null +++ b/src/shpub/Command/Update.php @@ -0,0 +1,99 @@ +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); + } + } +} +?> diff --git a/src/shpub/Request.php b/src/shpub/Request.php index 8ee7c5a..cbcc57e 100644 --- a/src/shpub/Request.php +++ b/src/shpub/Request.php @@ -7,6 +7,7 @@ class Request public $cfg; protected $uploadsInfo = []; + protected $dedicatedBody = false; public function __construct($host, $cfg) { @@ -25,6 +26,7 @@ class Request public function send($body = null) { if ($body !== null) { + $this->dedicatedBody = true; $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); -- 2.30.2