add description for all commands
[shpub.git] / src / shpub / Command / AbstractProps.php
1 <?php
2 namespace shpub;
3
4 /**
5  * Abstract command class that handles generic properties
6  */
7 class Command_AbstractProps
8 {
9     /**
10      * @var Config
11      */
12     protected $cfg;
13
14     public function __construct($cfg)
15     {
16         $this->cfg = $cfg;
17     }
18
19     public static function optsGeneric(\Console_CommandLine_Command $cmd)
20     {
21         $cmd->addOption(
22             'categories',
23             array(
24                 'short_name'  => '-c',
25                 'long_name'   => '--category',
26                 'description' => 'Category names',
27                 'help_name'   => 'CAT',
28                 'action'      => 'StoreArray',
29                 'default'     => [],
30             )
31         );
32         $cmd->addOption(
33             'files',
34             array(
35                 'short_name'  => '-f',
36                 'long_name'   => '--file',
37                 'description' => 'Files or URLs to upload',
38                 'help_name'   => 'PATH',
39                 'action'      => 'StoreArray',
40                 'default'     => [],
41             )
42         );
43         $cmd->addOption(
44             'direct_upload',
45             array(
46                 'long_name'   => '--direct-upload',
47                 'description' => 'Ignore media endpoint at file upload',
48                 'action'      => 'StoreTrue',
49                 'default'     => false,
50             )
51         );
52         $cmd->addOption(
53             'name',
54             array(
55                 'short_name'  => '-n',
56                 'long_name'   => '--name',
57                 'description' => 'Post title',
58                 'help_name'   => 'TITLE',
59                 'action'      => 'StoreString',
60                 'default'     => null,
61             )
62         );
63         $cmd->addOption(
64             'published',
65             array(
66                 'long_name'   => '--published',
67                 'description' => 'Publish date',
68                 'help_name'   => 'DATE',
69                 'action'      => 'StoreString',
70                 'default'     => null,
71             )
72         );
73         $cmd->addOption(
74             'updated',
75             array(
76                 'long_name'   => '--updated',
77                 'description' => 'Update date',
78                 'help_name'   => 'DATE',
79                 'action'      => 'StoreString',
80                 'default'     => null,
81             )
82         );
83         $cmd->addOption(
84             'slug',
85             array(
86                 'short_name'  => '-s',
87                 'long_name'   => '--slug',
88                 'description' => 'URL path',
89                 'help_name'   => 'PATH',
90                 'action'      => 'StoreString',
91                 'default'     => null,
92             )
93         );
94         $cmd->addOption(
95             'syndication',
96             array(
97                 'short_name'  => '-s',
98                 'long_name'   => '--syndication',
99                 'description' => 'Syndication URL(s)',
100                 'help_name'   => 'URL',
101                 'action'      => 'StoreArray',
102                 'default'     => [],
103             )
104         );
105         $cmd->addOption(
106             'x',
107             array(
108                 'short_name'  => '-x',
109                 'long_name'   => '--xprop',
110                 'description' => 'Additional property',
111                 'help_name'   => 'key=value',
112                 'action'      => 'StoreArray',
113                 'default'     => [],
114             )
115         );
116         static::addOptJson($cmd);
117     }
118
119     protected static function addOptHtml(\Console_CommandLine_Command $cmd)
120     {
121         $cmd->addOption(
122             'html',
123             array(
124                 'short_name'  => '-h',
125                 'long_name'   => '--html',
126                 'description' => 'Text content is HTML',
127                 'action'      => 'StoreTrue',
128                 'default'     => false,
129             )
130         );
131     }
132
133     protected static function addOptJson(\Console_CommandLine_Command $cmd)
134     {
135         $cmd->addOption(
136             'json',
137             array(
138                 'long_name'   => '--json',
139                 'description' => 'Send request data as JSON',
140                 'action'      => 'StoreTrue',
141                 'default'     => false,
142             )
143         );
144     }
145
146     protected function handleGenericOptions(
147         \Console_CommandLine_Result $cmdRes, Request $req
148     ) {
149         $this->handleOptJson($cmdRes, $req);
150
151         if ($cmdRes->options['published'] !== null) {
152             $req->req->addPostParameter(
153                 'published', $cmdRes->options['published']
154             );
155         }
156         if ($cmdRes->options['updated'] !== null) {
157             $req->req->addPostParameter(
158                 'updated', $cmdRes->options['updated']
159             );
160         }
161         if (count($cmdRes->options['categories'])) {
162             $req->addPostParameter(
163                 'category', $cmdRes->options['categories']
164             );
165         }
166         if ($cmdRes->options['name'] !== null) {
167             $req->req->addPostParameter(
168                 'name', $cmdRes->options['name']
169             );
170         }
171         if ($cmdRes->options['slug'] !== null) {
172             $req->req->addPostParameter(
173                 'slug', $cmdRes->options['slug']
174             );
175         }
176         if (count($cmdRes->options['syndication'])) {
177             $req->addPostParameter(
178                 'syndication', $cmdRes->options['syndication']
179             );
180         }
181
182         $req->setDirectUpload($cmdRes->options['direct_upload']);
183         $this->handleFiles($cmdRes, $req);
184
185         if (count($cmdRes->options['x'])) {
186             $postParams = [];
187             foreach ($cmdRes->options['x'] as $xproperty) {
188                 list($propkey, $propval) = explode('=', $xproperty, 2);
189                 if (!isset($postParams[$propkey])) {
190                     $postParams[$propkey] = [];
191                 }
192                 $postParams[$propkey][] = $propval;
193             }
194             foreach ($postParams as $propkey => $propvals) {
195                 $req->addPostParameter($propkey, $propvals);
196             }
197         }
198     }
199
200     protected function handleOptJson(
201         \Console_CommandLine_Result $cmdRes, Request $req
202     ) {
203         $req->setSendAsJson($cmdRes->options['json']);
204     }
205
206     protected function handleFiles(
207         \Console_CommandLine_Result $cmdRes, Request $req
208     ) {
209         $files = $cmdRes->options['files'];
210         $fileList = $urlList = [
211             'audio' => [],
212             'image' => [],
213             'video' => [],
214         ];
215
216         foreach ($files as $filePath) {
217             if (strpos($filePath, '://') !== false) {
218                 //url
219                 $mte      = new \MIME_Type_Extension();
220                 $mimetype = $mte->getMIMEType($filePath);
221                 $media    = \MIME_Type::getMedia($mimetype);
222                 if (!isset($urlList[$media])) {
223                     Log::err('File type not allowed: ' . $mimetype);
224                     exit(20);
225                 }
226                 $urlList[$media][] = $filePath;
227             } else if (file_exists($filePath)) {
228                 //file
229                 $mimetype = \MIME_Type::autoDetect($filePath);
230                 $media    = \MIME_Type::getMedia($mimetype);
231                 if (!isset($urlList[$media])) {
232                     Log::err('File type not allowed: ' . $mimetype);
233                     exit(20);
234                 }
235                 $fileList[$media][] = $filePath;
236             } else {
237                 Log::err('File does not exist: ' . $filePath);
238                 exit(20);
239             }
240         }
241         foreach ($urlList as $type => $urls) {
242             if ($type == 'image') {
243                 $type = 'photo';
244             }
245             if (count($urls) > 0) {
246                 $req->addProperty($type, $urls);
247             }
248         }
249         foreach ($fileList as $type => $filePaths) {
250             if ($type == 'image') {
251                 $type = 'photo';
252             }
253             if (count($filePaths) > 0) {
254                 $req->addUpload($type, $filePaths);
255             }
256         }
257     }
258 }
259 ?>