0116cb51068c752c5e926140410ddc34e890d190
[shpub.git] / src / shpub / Command / Bookmark.php
1 <?php
2 namespace shpub;
3
4 class Command_Bookmark extends Command_AbstractProps
5 {
6     public static function opts(\Console_CommandLine $optParser)
7     {
8         $cmd = $optParser->addCommand('bookmark');
9         static::addOptHtml($cmd);
10         static::optsGeneric($cmd);
11         $cmd->addArgument(
12             'url',
13             [
14                 'optional'    => false,
15                 'description' => 'URL to bookmark',
16             ]
17         );
18         $cmd->addArgument(
19             'text',
20             [
21                 'optional'    => true,
22                 'multiple'    => false,
23                 'description' => 'Bookmark text',
24             ]
25         );
26     }
27
28     public function run(\Console_CommandLine_Result $cmdRes)
29     {
30         $url = Validator::url($cmdRes->args['url'], 'url');
31         if ($url === false) {
32             exit(10);
33         }
34
35         $req = new Request($this->cfg->host, $this->cfg);
36         $req->setType('entry');
37         $req->addProperty('bookmark-of', $url);
38
39         if ($cmdRes->args['text']) {
40             $req->addContent($cmdRes->args['text'], $cmdRes->options['html']);
41         }
42
43
44         $this->handleGenericOptions($cmdRes, $req);
45         $res = $req->send();
46
47         $postUrl = $res->getHeader('Location');
48         if ($postUrl === null) {
49             Log::err('Error: Server sent no "Location" header and said:');
50             Log::err($res->getBody());
51             exit(20);
52         } else {
53             Log::info('Bookmark created at server');
54             Log::msg($postUrl);
55         }
56     }
57 }
58 ?>