5 * Abstract command class that handles generic properties
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
11 class Command_AbstractProps
18 public function __construct($cfg)
23 public static function optsGeneric(\Console_CommandLine_Command $cmd)
29 'long_name' => '--category',
30 'description' => 'Category names',
32 'action' => 'StoreArray',
40 'long_name' => '--file',
41 'description' => 'Files or URLs to upload',
42 'help_name' => 'PATH',
43 'action' => 'StoreArray',
50 'long_name' => '--direct-upload',
51 'description' => 'Ignore media endpoint at file upload',
52 'action' => 'StoreTrue',
60 'long_name' => '--name',
61 'description' => 'Post title',
62 'help_name' => 'TITLE',
63 'action' => 'StoreString',
70 'long_name' => '--published',
71 'description' => 'Publish date',
72 'help_name' => 'DATE',
73 'action' => 'StoreString',
80 'long_name' => '--updated',
81 'description' => 'Update date',
82 'help_name' => 'DATE',
83 'action' => 'StoreString',
91 'long_name' => '--slug',
92 'description' => 'URL path',
93 'help_name' => 'PATH',
94 'action' => 'StoreString',
101 'short_name' => '-u',
102 'long_name' => '--syndication',
103 'description' => 'Syndication URL(s)',
104 'help_name' => 'URL',
105 'action' => 'StoreArray',
112 'short_name' => '-x',
113 'long_name' => '--xprop',
114 'description' => 'Additional property',
115 'help_name' => 'key=value',
116 'action' => 'StoreArray',
120 static::addOptJson($cmd);
123 protected static function addOptHtml(\Console_CommandLine_Command $cmd)
128 'long_name' => '--html',
129 'description' => 'Text content is HTML',
130 'action' => 'StoreTrue',
136 protected static function addOptJson(\Console_CommandLine_Command $cmd)
141 'long_name' => '--json',
142 'description' => 'Send request data as JSON',
143 'action' => 'StoreTrue',
149 protected function handleGenericOptions(
150 \Console_CommandLine_Result $cmdRes, Request $req
152 $this->handleOptJson($cmdRes, $req);
154 if ($cmdRes->options['published'] !== null) {
156 'published', $cmdRes->options['published']
159 if ($cmdRes->options['updated'] !== null) {
161 'updated', $cmdRes->options['updated']
164 if (count($cmdRes->options['categories'])) {
166 'category', $cmdRes->options['categories']
169 if ($cmdRes->options['name'] !== null) {
171 'name', $cmdRes->options['name']
174 if ($cmdRes->options['slug'] !== null) {
176 'slug', $cmdRes->options['slug']
179 if (count($cmdRes->options['syndication'])) {
181 'syndication', $cmdRes->options['syndication']
185 $req->setDirectUpload($cmdRes->options['direct_upload']);
186 $this->handleFiles($cmdRes, $req);
188 if (count($cmdRes->options['x'])) {
190 foreach ($cmdRes->options['x'] as $xproperty) {
191 list($propkey, $propval) = explode('=', $xproperty, 2);
192 if (!isset($postParams[$propkey])) {
193 $postParams[$propkey] = [];
195 $postParams[$propkey][] = $propval;
197 foreach ($postParams as $propkey => $propvals) {
198 $req->addProperty($propkey, $propvals);
203 protected function handleOptJson(
204 \Console_CommandLine_Result $cmdRes, Request $req
206 $req->setSendAsJson($cmdRes->options['json']);
209 protected function handleFiles(
210 \Console_CommandLine_Result $cmdRes, Request $req
212 $files = $cmdRes->options['files'];
213 $fileList = $urlList = [
219 foreach ($files as $filePath) {
220 if (strpos($filePath, '://') !== false) {
222 $urlPath = parse_url($filePath, PHP_URL_PATH);
223 $mte = new \MIME_Type_Extension();
224 $mimetype = $mte->getMIMEType($urlPath);
225 $media = \MIME_Type::getMedia($mimetype);
226 if (!isset($urlList[$media])) {
227 Log::err('File type not allowed: ' . $mimetype);
230 $urlList[$media][] = $filePath;
231 } else if (file_exists($filePath)) {
233 $mimetype = \MIME_Type::autoDetect($filePath);
234 $media = \MIME_Type::getMedia($mimetype);
235 if (!isset($urlList[$media])) {
236 Log::err('File type not allowed: ' . $mimetype);
239 $fileList[$media][] = $filePath;
241 Log::err('File does not exist: ' . $filePath);
245 foreach ($urlList as $type => $urls) {
246 if ($type == 'image') {
249 if (count($urls) > 0) {
250 $req->addProperty($type, $urls);
253 foreach ($fileList as $type => $filePaths) {
254 if ($type == 'image') {
257 if (count($filePaths) > 0) {
258 $req->addUpload($type, $filePaths);