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