Support --html everywhere
[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 addOptHtml(\Console_CommandLine_Command $cmd)
111     {
112         $cmd->addOption(
113             'html',
114             array(
115                 'short_name'  => '-h',
116                 'long_name'   => '--html',
117                 'description' => 'Text content is HTML',
118                 'action'      => 'StoreTrue',
119                 'default'     => false,
120             )
121         );
122     }
123
124     protected static function addOptJson(\Console_CommandLine_Command $cmd)
125     {
126         $cmd->addOption(
127             'json',
128             array(
129                 'long_name'   => '--json',
130                 'description' => 'Send request data as JSON',
131                 'action'      => 'StoreTrue',
132                 'default'     => false,
133             )
134         );
135     }
136
137     protected function handleGenericOptions(
138         \Console_CommandLine_Result $cmdRes, Request $req
139     ) {
140         $this->handleOptJson($cmdRes, $req);
141
142         if ($cmdRes->options['published'] !== null) {
143             $req->req->addPostParameter(
144                 'published', $cmdRes->options['published']
145             );
146         }
147         if ($cmdRes->options['updated'] !== null) {
148             $req->req->addPostParameter(
149                 'updated', $cmdRes->options['updated']
150             );
151         }
152         if (count($cmdRes->options['categories'])) {
153             $req->addPostParameter(
154                 'category', $cmdRes->options['categories']
155             );
156         }
157         if ($cmdRes->options['name'] !== null) {
158             $req->req->addPostParameter(
159                 'name', $cmdRes->options['name']
160             );
161         }
162         if ($cmdRes->options['slug'] !== null) {
163             $req->req->addPostParameter(
164                 'slug', $cmdRes->options['slug']
165             );
166         }
167         if (count($cmdRes->options['syndication'])) {
168             $req->addPostParameter(
169                 'syndication', $cmdRes->options['syndication']
170             );
171         }
172
173         $this->handleFiles($cmdRes, $req);
174
175         if (count($cmdRes->options['x'])) {
176             $postParams = [];
177             foreach ($cmdRes->options['x'] as $xproperty) {
178                 list($propkey, $propval) = explode('=', $xproperty, 2);
179                 if (!isset($postParams[$propkey])) {
180                     $postParams[$propkey] = [];
181                 }
182                 $postParams[$propkey][] = $propval;
183             }
184             foreach ($postParams as $propkey => $propvals) {
185                 $req->addPostParameter($propkey, $propvals);
186             }
187         }
188     }
189
190     protected function handleOptJson(
191         \Console_CommandLine_Result $cmdRes, Request $req
192     ) {
193         $req->setSendAsJson($cmdRes->options['json']);
194     }
195
196     protected function handleFiles(
197         \Console_CommandLine_Result $cmdRes, Request $req
198     ) {
199         $files = $cmdRes->options['files'];
200         $fileList = $urlList = [
201             'audio' => [],
202             'image' => [],
203             'video' => [],
204         ];
205
206         foreach ($files as $filePath) {
207             if (strpos($filePath, '://') !== false) {
208                 //url
209                 $mte      = new \MIME_Type_Extension();
210                 $mimetype = $mte->getMIMEType($filePath);
211                 $media    = \MIME_Type::getMedia($mimetype);
212                 if (!isset($urlList[$media])) {
213                     Log::err('File type not allowed: ' . $mimetype);
214                     exit(20);
215                 }
216                 $urlList[$media][] = $filePath;
217             } else if (file_exists($filePath)) {
218                 //file
219                 $mimetype = \MIME_Type::autoDetect($filePath);
220                 $media    = \MIME_Type::getMedia($mimetype);
221                 if (!isset($urlList[$media])) {
222                     Log::err('File type not allowed: ' . $mimetype);
223                     exit(20);
224                 }
225                 $fileList[$media][] = $filePath;
226             } else {
227                 Log::err('File does not exist: ' . $filePath);
228                 exit(20);
229             }
230         }
231         foreach ($urlList as $type => $urls) {
232             if ($type == 'image') {
233                 $type = 'photo';
234             }
235             if (count($urls) == 1) {
236                 $req->req->addPostParameter($type, reset($urls));
237             } else if (count($urls) > 1) {
238                 $n = 0;
239                 foreach ($urls as $url) {
240                     $req->req->addPostParameter(
241                         $type . '[' . $n++ . ']', $url
242                     );
243                 }
244             }
245         }
246         foreach ($fileList as $type => $filePaths) {
247             if ($type == 'image') {
248                 $type = 'photo';
249             }
250             if (count($filePaths) == 1) {
251                 $req->addUpload($type, reset($filePaths));
252             } else if (count($filePaths) > 0) {
253                 $req->addUpload($type, $filePaths);
254             }
255         }
256     }
257 }
258 ?>