5 * Abstract command class that handles generic properties
7 class Command_AbstractProps
14 public function __construct($cfg)
19 public static function optsGeneric(\Console_CommandLine_Command $cmd)
25 'long_name' => '--category',
26 'description' => 'Category names',
28 'action' => 'StoreArray',
36 'long_name' => '--file',
37 'description' => 'Files or URLs to upload',
38 'help_name' => 'PATH',
39 'action' => 'StoreArray',
46 'long_name' => '--direct-upload',
47 'description' => 'Ignore media endpoint at file upload',
48 'action' => 'StoreTrue',
56 'long_name' => '--name',
57 'description' => 'Post title',
58 'help_name' => 'TITLE',
59 'action' => 'StoreString',
66 'long_name' => '--published',
67 'description' => 'Publish date',
68 'help_name' => 'DATE',
69 'action' => 'StoreString',
76 'long_name' => '--updated',
77 'description' => 'Update date',
78 'help_name' => 'DATE',
79 'action' => 'StoreString',
87 'long_name' => '--slug',
88 'description' => 'URL path',
89 'help_name' => 'PATH',
90 'action' => 'StoreString',
98 'long_name' => '--syndication',
99 'description' => 'Syndication URL(s)',
100 'help_name' => 'URL',
101 'action' => 'StoreArray',
108 'short_name' => '-x',
109 'long_name' => '--xprop',
110 'description' => 'Additional property',
111 'help_name' => 'key=value',
112 'action' => 'StoreArray',
116 static::addOptJson($cmd);
119 protected static function addOptHtml(\Console_CommandLine_Command $cmd)
124 'short_name' => '-h',
125 'long_name' => '--html',
126 'description' => 'Text content is HTML',
127 'action' => 'StoreTrue',
133 protected static function addOptJson(\Console_CommandLine_Command $cmd)
138 'long_name' => '--json',
139 'description' => 'Send request data as JSON',
140 'action' => 'StoreTrue',
146 protected function handleGenericOptions(
147 \Console_CommandLine_Result $cmdRes, Request $req
149 $this->handleOptJson($cmdRes, $req);
151 if ($cmdRes->options['published'] !== null) {
152 $req->req->addPostParameter(
153 'published', $cmdRes->options['published']
156 if ($cmdRes->options['updated'] !== null) {
157 $req->req->addPostParameter(
158 'updated', $cmdRes->options['updated']
161 if (count($cmdRes->options['categories'])) {
162 $req->addPostParameter(
163 'category', $cmdRes->options['categories']
166 if ($cmdRes->options['name'] !== null) {
167 $req->req->addPostParameter(
168 'name', $cmdRes->options['name']
171 if ($cmdRes->options['slug'] !== null) {
172 $req->req->addPostParameter(
173 'slug', $cmdRes->options['slug']
176 if (count($cmdRes->options['syndication'])) {
177 $req->addPostParameter(
178 'syndication', $cmdRes->options['syndication']
182 $req->setDirectUpload($cmdRes->options['direct_upload']);
183 $this->handleFiles($cmdRes, $req);
185 if (count($cmdRes->options['x'])) {
187 foreach ($cmdRes->options['x'] as $xproperty) {
188 list($propkey, $propval) = explode('=', $xproperty, 2);
189 if (!isset($postParams[$propkey])) {
190 $postParams[$propkey] = [];
192 $postParams[$propkey][] = $propval;
194 foreach ($postParams as $propkey => $propvals) {
195 $req->addPostParameter($propkey, $propvals);
200 protected function handleOptJson(
201 \Console_CommandLine_Result $cmdRes, Request $req
203 $req->setSendAsJson($cmdRes->options['json']);
206 protected function handleFiles(
207 \Console_CommandLine_Result $cmdRes, Request $req
209 $files = $cmdRes->options['files'];
210 $fileList = $urlList = [
216 foreach ($files as $filePath) {
217 if (strpos($filePath, '://') !== false) {
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);
226 $urlList[$media][] = $filePath;
227 } else if (file_exists($filePath)) {
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);
235 $fileList[$media][] = $filePath;
237 Log::err('File does not exist: ' . $filePath);
241 foreach ($urlList as $type => $urls) {
242 if ($type == 'image') {
245 if (count($urls) > 0) {
246 $req->addProperty($type, $urls);
249 foreach ($fileList as $type => $filePaths) {
250 if ($type == 'image') {
253 if (count($filePaths) > 0) {
254 $req->addUpload($type, $filePaths);