c9ea8bb1a5c323876da182ebe2c1f455afdd2ff7
[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             'name',
45             array(
46                 'short_name'  => '-n',
47                 'long_name'   => '--name',
48                 'description' => 'Post title',
49                 'help_name'   => 'TITLE',
50                 'action'      => 'StoreString',
51                 'default'     => null,
52             )
53         );
54         $cmd->addOption(
55             'published',
56             array(
57                 'long_name'   => '--published',
58                 'description' => 'Publish date',
59                 'help_name'   => 'DATE',
60                 'action'      => 'StoreString',
61                 'default'     => null,
62             )
63         );
64         $cmd->addOption(
65             'updated',
66             array(
67                 'long_name'   => '--updated',
68                 'description' => 'Update date',
69                 'help_name'   => 'DATE',
70                 'action'      => 'StoreString',
71                 'default'     => null,
72             )
73         );
74         $cmd->addOption(
75             'slug',
76             array(
77                 'short_name'  => '-s',
78                 'long_name'   => '--slug',
79                 'description' => 'URL path',
80                 'help_name'   => 'PATH',
81                 'action'      => 'StoreString',
82                 'default'     => null,
83             )
84         );
85         $cmd->addOption(
86             'syndication',
87             array(
88                 'short_name'  => '-s',
89                 'long_name'   => '--syndication',
90                 'description' => 'Syndication URL(s)',
91                 'help_name'   => 'URL',
92                 'action'      => 'StoreArray',
93                 'default'     => [],
94             )
95         );
96         $cmd->addOption(
97             'x',
98             array(
99                 'short_name'  => '-x',
100                 'long_name'   => '--xprop',
101                 'description' => 'Additional property',
102                 'help_name'   => 'key=value',
103                 'action'      => 'StoreArray',
104                 'default'     => [],
105             )
106         );
107         static::addOptJson($cmd);
108     }
109
110     protected static function addOptJson(\Console_CommandLine_Command $cmd)
111     {
112         $cmd->addOption(
113             'json',
114             array(
115                 'long_name'   => '--json',
116                 'description' => 'Send request data as JSON',
117                 'action'      => 'StoreTrue',
118                 'default'     => false,
119             )
120         );
121     }
122
123     protected function handleGenericOptions(
124         \Console_CommandLine_Result $cmdRes, Request $req
125     ) {
126         $this->handleOptJson($cmdRes, $req);
127
128         if ($cmdRes->options['published'] !== null) {
129             $req->req->addPostParameter(
130                 'published', $cmdRes->options['published']
131             );
132         }
133         if ($cmdRes->options['updated'] !== null) {
134             $req->req->addPostParameter(
135                 'updated', $cmdRes->options['updated']
136             );
137         }
138         if (count($cmdRes->options['categories'])) {
139             $req->addPostParameter(
140                 'category', $cmdRes->options['categories']
141             );
142         }
143         if ($cmdRes->options['name'] !== null) {
144             $req->req->addPostParameter(
145                 'name', $cmdRes->options['name']
146             );
147         }
148         if ($cmdRes->options['slug'] !== null) {
149             $req->req->addPostParameter(
150                 'slug', $cmdRes->options['slug']
151             );
152         }
153         if (count($cmdRes->options['syndication'])) {
154             $req->addPostParameter(
155                 'syndication', $cmdRes->options['syndication']
156             );
157         }
158
159         $this->handleFiles($cmdRes, $req);
160
161         if (count($cmdRes->options['x'])) {
162             $postParams = [];
163             foreach ($cmdRes->options['x'] as $xproperty) {
164                 list($propkey, $propval) = explode('=', $xproperty, 2);
165                 if (!isset($postParams[$propkey])) {
166                     $postParams[$propkey] = [];
167                 }
168                 $postParams[$propkey][] = $propval;
169             }
170             foreach ($postParams as $propkey => $propvals) {
171                 $req->addPostParameter($propkey, $propvals);
172             }
173         }
174     }
175
176     protected function handleOptJson(
177         \Console_CommandLine_Result $cmdRes, Request $req
178     ) {
179         $req->setSendAsJson($cmdRes->options['json']);
180     }
181
182     protected function handleFiles(
183         \Console_CommandLine_Result $cmdRes, Request $req
184     ) {
185         $files = $cmdRes->options['files'];
186         $fileList = $urlList = [
187             'audio' => [],
188             'image' => [],
189             'video' => [],
190         ];
191
192         foreach ($files as $filePath) {
193             if (strpos($filePath, '://') !== false) {
194                 //url
195                 $mte      = new \MIME_Type_Extension();
196                 $mimetype = $mte->getMIMEType($filePath);
197                 $media    = \MIME_Type::getMedia($mimetype);
198                 if (!isset($urlList[$media])) {
199                     Log::err('File type not allowed: ' . $mimetype);
200                     exit(20);
201                 }
202                 $urlList[$media][] = $filePath;
203             } else if (file_exists($filePath)) {
204                 //file
205                 $mimetype = \MIME_Type::autoDetect($filePath);
206                 $media    = \MIME_Type::getMedia($mimetype);
207                 if (!isset($urlList[$media])) {
208                     Log::err('File type not allowed: ' . $mimetype);
209                     exit(20);
210                 }
211                 $fileList[$media][] = $filePath;
212             } else {
213                 Log::err('File does not exist: ' . $filePath);
214                 exit(20);
215             }
216         }
217         foreach ($urlList as $type => $urls) {
218             if ($type == 'image') {
219                 $type = 'photo';
220             }
221             if (count($urls) == 1) {
222                 $req->req->addPostParameter($type, reset($urls));
223             } else if (count($urls) > 1) {
224                 $n = 0;
225                 foreach ($urls as $url) {
226                     $req->req->addPostParameter(
227                         $type . '[' . $n++ . ']', $url
228                     );
229                 }
230             }
231         }
232         foreach ($fileList as $type => $filePaths) {
233             if ($type == 'image') {
234                 $type = 'photo';
235             }
236             if (count($filePaths) == 1) {
237                 $req->addUpload($type, reset($filePaths));
238             } else if (count($filePaths) > 0) {
239                 $req->addUpload($type, $filePaths);
240             }
241         }
242     }
243 }
244 ?>