add support for articles (with HTML)
[shpub.git] / src / shpub / Command / Article.php
1 <?php
2 namespace shpub;
3
4 class Command_Article extends Command_AbstractProps
5 {
6     /**
7      * @var Config
8      */
9     protected $cfg;
10
11     public function __construct($cfg)
12     {
13         $this->cfg = $cfg;
14     }
15
16     public static function opts(\Console_CommandLine $optParser)
17     {
18         $cmd = $optParser->addCommand('article');
19         $cmd->addOption(
20             'html',
21             array(
22                 'short_name'  => '-h',
23                 'long_name'   => '--html',
24                 'description' => 'Text content is HTML',
25                 'action'      => 'StoreTrue',
26                 'default'     => false,
27             )
28         );
29         static::optsGeneric($cmd);
30         $cmd->addArgument(
31             'title',
32             [
33                 'optional'    => false,
34                 'multiple'    => false,
35                 'description' => 'Title/Name',
36             ]
37         );
38         $cmd->addArgument(
39             'text',
40             [
41                 'optional'    => false,
42                 'multiple'    => false,
43                 'description' => 'Text content',
44             ]
45         );
46     }
47
48     public function run(\Console_CommandLine_Result $cmdRes)
49     {
50         $req = new Request($this->cfg->host, $this->cfg);
51         $req->req->addPostParameter('h', 'entry');
52         $req->req->addPostParameter('name', $cmdRes->args['title']);
53         if ($cmdRes->options['html']) {
54             $req->req->addPostParameter(
55                 'content[html]', $cmdRes->args['text']
56             );
57         } else {
58             $req->req->addPostParameter('content', $cmdRes->args['text']);
59         }
60         $this->handleGenericOptions($cmdRes, $req);
61
62         $res = $req->send();
63         $postUrl = $res->getHeader('Location');
64         Log::info('Article created at server');
65         Log::msg($postUrl);
66     }
67 }
68 ?>