1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class Horde_Kolab_Cli_Module_Data
29: implements Horde_Kolab_Cli_Module
30: {
31: 32: 33: 34: 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: 57: 58: 59: 60:
61: public function getBaseOptions()
62: {
63: return array();
64: }
65:
66: 67: 68: 69: 70:
71: public function hasOptionGroup()
72: {
73: return false;
74: }
75:
76: 77: 78: 79: 80:
81: public function getOptionGroupTitle()
82: {
83: return '';
84: }
85:
86: 87: 88: 89: 90:
91: public function getOptionGroupDescription()
92: {
93: return '';
94: }
95:
96: 97: 98: 99: 100:
101: public function getOptionGroupOptions()
102: {
103: return array();
104: }
105:
106: 107: 108: 109: 110: 111: 112: 113: 114:
115: public function handleArguments(&$options, &$arguments, &$world)
116: {
117: }
118:
119: 120: 121: 122: 123: 124: 125: 126: 127: 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: }