Overview

Packages

  • Kolab
    • Cli

Classes

  • Horde_Kolab_Cli
  • Horde_Kolab_Cli_Data_Ledger
  • Horde_Kolab_Cli_Exception
  • Horde_Kolab_Cli_Module_Account
  • Horde_Kolab_Cli_Module_Base
  • Horde_Kolab_Cli_Module_Data
  • Horde_Kolab_Cli_Module_Folder
  • Horde_Kolab_Cli_Module_Format
  • Horde_Kolab_Cli_Module_Ledger
  • Horde_Kolab_Cli_Module_List
  • Horde_Kolab_Cli_Translation

Interfaces

  • Horde_Kolab_Cli_Module
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * The Horde_Kolab_Cli_Module_Data:: class handles Kolab data.
  4:  *
  5:  * PHP version 5
  6:  *
  7:  * @category Horde
  8:  * @package  Kolab_Cli
  9:  * @author   Gunnar Wrobel <wrobel@pardus.de>
 10:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 11:  * @link     http://pear.horde.org/index.php?package=Kolab_Cli
 12:  */
 13: 
 14: /**
 15:  * The Horde_Kolab_Cli_Module_Data:: class handles Kolab data.
 16:  *
 17:  * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
 18:  *
 19:  * See the enclosed file COPYING for license information (LGPL). If you
 20:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 21:  *
 22:  * @category Horde
 23:  * @package  Kolab_Cli
 24:  * @author   Gunnar Wrobel <wrobel@pardus.de>
 25:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 26:  * @link     http://pear.horde.org/index.php?package=Kolab_Cli
 27:  */
 28: class Horde_Kolab_Cli_Module_Data
 29: implements Horde_Kolab_Cli_Module
 30: {
 31:     /**
 32:      * Get the usage description for this module.
 33:      *
 34:      * @return string The description.
 35:      */
 36:     public function getUsage()
 37:     {
 38:         return Horde_Kolab_Cli_Translation::t("  data - Handle Kolab data (the default action is \"info\"). PATH refers to the path of the folder that holds the data and the optional TYPE argument indicates which data type should be read. This is usually already defined by the folder setting.
 39: 
 40:   - info      PATH               : Display general information.
 41:   - stamp     PATH               : Display the folder status information.
 42:   - ids       PATH TYPE          : Display all object ids in the folder PATH of
 43:                                    type TYPE.
 44:   - complete  PATH BACKENDID     : Return the complete message from folder PATH
 45:                                    for the given BACKENDID.
 46:   - create    PATH TYPE yaml PATH: Create an object as defined in the specified
 47:                                    YAML data
 48:   - backendid PATH TYPE OBJECTID : Return the backend ID for the object with ID
 49:                                    OBJECTID.
 50:   - delete    PATH TYPE ID,ID,.. : Delete the given object id's.
 51: 
 52: ");
 53:     }
 54: 
 55:     /**
 56:      * Get a set of base options that this module adds to the CLI argument
 57:      * parser.
 58:      *
 59:      * @return array The options.
 60:      */
 61:     public function getBaseOptions()
 62:     {
 63:         return array();
 64:     }
 65: 
 66:     /**
 67:      * Indicate if the module provides an option group.
 68:      *
 69:      * @return boolean True if an option group should be added.
 70:      */
 71:     public function hasOptionGroup()
 72:     {
 73:         return false;
 74:     }
 75: 
 76:     /**
 77:      * Return the title for the option group representing this module.
 78:      *
 79:      * @return string The group title.
 80:      */
 81:     public function getOptionGroupTitle()
 82:     {
 83:         return '';
 84:     }
 85: 
 86:     /**
 87:      * Return the description for the option group representing this module.
 88:      *
 89:      * @return string The group description.
 90:      */
 91:     public function getOptionGroupDescription()
 92:     {
 93:         return '';
 94:     }
 95: 
 96:     /**
 97:      * Return the options for this module.
 98:      *
 99:      * @return array The group options.
100:      */
101:     public function getOptionGroupOptions()
102:     {
103:         return array();
104:     }
105: 
106:     /**
107:      * Handle the options and arguments.
108:      *
109:      * @param mixed &$options   An array of options.
110:      * @param mixed &$arguments An array of arguments.
111:      * @param array &$world     A list of initialized dependencies.
112:      *
113:      * @return NULL
114:      */
115:     public function handleArguments(&$options, &$arguments, &$world)
116:     {
117:     }
118: 
119:     /**
120:      * Run the module.
121:      *
122:      * @param Horde_Cli $cli       The CLI handler.
123:      * @param mixed     $options   An array of options.
124:      * @param mixed     $arguments An array of arguments.
125:      * @param array     &$world    A list of initialized dependencies.
126:      *
127:      * @return NULL
128:      */
129:     public function run($cli, $options, $arguments, &$world)
130:     {
131:         if (!isset($arguments[1])) {
132:             $action = 'info';
133:         } else {
134:             $action = $arguments[1];
135:         }
136:         if (!isset($arguments[2])) {
137:             $folder_name = 'INBOX';
138:         } else {
139:             $folder_name = $arguments[2];
140:         }
141:         switch ($action) {
142:         case 'info':
143:             break;
144:         case 'synchronize':
145:             $world['storage']->getData($folder_name, $arguments[3])->synchronize();
146:             break;
147:         case 'stamp':
148:             $cli->writeln(
149:                 (string)$world['storage']->getData($folder_name)->getStamp()
150:             );
151:             break;
152:         case 'complete':
153:             $data = $world['storage']->getData($folder_name);
154:             $complete = $data->fetchComplete($arguments[3]);
155:             $cli->writeln($complete[1]->toString(array('headers' => $complete[0])));
156:             break;
157:         case 'part':
158:             $data = $world['storage']->getData($folder_name);
159:             $part = $data->fetchPart($arguments[3], $arguments[4]);
160:             rewind($part);
161:             $cli->writeln(quoted_printable_decode(stream_get_contents($part)));
162:             break;
163:         case 'fetch':
164:             $data = $world['storage']->getData($folder_name, $arguments[3]);
165:             $objects = $data->fetch(explode(',', $arguments[4]));
166:             foreach ($objects as $uid => $message) {
167:                 $this->_yamlOutput($cli, $uid, $message);
168:             }
169:             break;
170:         case 'ids':
171:             $data = $world['storage']->getData($folder_name, $arguments[3]);
172:             foreach ($data->getObjectIds() as $id) {
173:                 $cli->writeln((string)$id);
174:             }
175:             break;
176:         case 'objects':
177:             $data = $world['storage']->getData($folder_name, $arguments[3]);
178:             foreach ($data->getObjects() as $id => $object) {
179:                 $this->_yamlOutput($cli, $id, $object);
180:             }
181:             break;
182:         case 'backendobjects':
183:             $data = $world['storage']->getData($folder_name, $arguments[3]);
184:             foreach ($data->getObjectsByBackendId() as $id => $object) {
185:                 $this->_yamlOutput($cli, $id, $object);
186:             }
187:             break;
188:         case 'object':
189:             $data = $world['storage']->getData($folder_name, $arguments[3]);
190:             $object = $data->getObject($arguments[4]);
191:             $this->_yamlOutput($cli, $arguments[4], $object);
192:             break;
193:         case 'backendobject':
194:             $data = $world['storage']->getData($folder_name, $arguments[3]);
195:             $object = $data->getObjectByBackendId($arguments[4]);
196:             $this->_yamlOutput($cli, $arguments[4], $object);
197:             break;
198:         case 'create':
199:             $data = $world['storage']->getData($folder_name, $arguments[3]);
200:             switch (strtolower($arguments[4])) {
201:             case 'yaml':
202:                 if (class_exists('Horde_Yaml')) {
203:                     $object = Horde_Yaml::loadFile($arguments[5]);
204:                 } else {
205:                     throw new Horde_Kolab_Cli_Exception(
206:                         'The Horde_Yaml package is missing!'
207:                     );
208:                 }
209:             }
210:             $data->create($object);
211:             $cli->writeln($object['uid']);
212:             break;
213:         case 'move':
214:             $data = $world['storage']->getData($folder_name, $arguments[3]);
215:             $objects = $data->move($arguments[4], $arguments[5]);
216:             break;
217:         case 'delete':
218:             $data = $world['storage']->getData($folder_name, $arguments[3]);
219:             $objects = $data->delete(explode(',', $arguments[4]));
220:             break;
221:         case 'deleteall':
222:             $world['storage']->getData($folder_name, $arguments[3])->deleteAll();
223:             break;
224:         case 'deleteuids':
225:             $data = $world['storage']->getData($folder_name, $arguments[3]);
226:             $objects = $data->deleteBackendIds(explode(',', $arguments[4]));
227:             break;
228:         case 'backendid':
229:             $data = $world['storage']->getData($folder_name, $arguments[3]);
230:             $cli->writeln((string)$data->getBackendId($arguments[4]));
231:             break;
232:         default:
233:             $cli->message(
234:                 sprintf(
235:                     Horde_Kolab_Cli_Translation::t('Action %s not supported!'),
236:                     $action
237:                 ),
238:                 'cli.error'
239:             );
240:             break;
241:         }
242:     }
243: 
244:     private function _messageOutput($cli, $id, $output)
245:     {
246:         $cli->writeln('Message UID [' . $id . ']');
247:         $cli->writeln('================================================================================');
248:         $cli->writeln();
249:         $cli->writeln($output);
250:         $cli->writeln();
251:         $cli->writeln('================================================================================');
252:         $cli->writeln();
253:     }
254: 
255:     private function _yamlOutput($cli, $id, $output)
256:     {
257:         $output = $this->_convertDates($output);
258:         if (class_exists('Horde_Yaml')) {
259:             $this->_messageOutput($cli, $id, Horde_Yaml::dump($output));
260:         } else {
261:             $this->_messageOutput($cli, $id, print_r($output, true));
262:         }
263:     }
264: 
265: 
266:     private function _convertDates($output)
267:     {
268:         $result = array();
269:         foreach ($output as $name => $element) {
270:             if (is_array($element)) {
271:                 $result[$name] = $this->_convertDates($element);
272:             } else if ($element instanceOf DateTime) {
273:                 $result[$name] = $element->format('c');
274:             } else {
275:                 $result[$name] = $element;
276:             }
277:         }
278:         return $result;
279:     }
280: }
API documentation generated by ApiGen