1: <?php
2: /**
3: * The Horde_Kolab_Cli_Module_Format:: handles the Kolab format.
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_Format:: handles the Kolab format.
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_Format
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(" format - Handle the Kolab format (the default action is \"read\")
39:
40: - read TYPE [FILE|FOLDER UID PART]: Read a Kolab format file of the specified
41: type. Specify either a direct file name
42: or a combination of an IMAP folder, a UID
43: within that folder and the specific part
44: that should be parsed.
45:
46:
47: ");
48: }
49:
50: /**
51: * Get a set of base options that this module adds to the CLI argument
52: * parser.
53: *
54: * @return array The options.
55: */
56: public function getBaseOptions()
57: {
58: return array();
59: }
60:
61: /**
62: * Indicate if the module provides an option group.
63: *
64: * @return boolean True if an option group should be added.
65: */
66: public function hasOptionGroup()
67: {
68: return false;
69: }
70:
71: /**
72: * Return the title for the option group representing this module.
73: *
74: * @return string The group title.
75: */
76: public function getOptionGroupTitle()
77: {
78: return '';
79: }
80:
81: /**
82: * Return the description for the option group representing this module.
83: *
84: * @return string The group description.
85: */
86: public function getOptionGroupDescription()
87: {
88: return '';
89: }
90:
91: /**
92: * Return the options for this module.
93: *
94: * @return array The group options.
95: */
96: public function getOptionGroupOptions()
97: {
98: return array();
99: }
100:
101: /**
102: * Handle the options and arguments.
103: *
104: * @param mixed &$options An array of options.
105: * @param mixed &$arguments An array of arguments.
106: * @param array &$world A list of initialized dependencies.
107: *
108: * @return NULL
109: */
110: public function handleArguments(&$options, &$arguments, &$world)
111: {
112: }
113:
114: /**
115: * Run the module.
116: *
117: * @param Horde_Cli $cli The CLI handler.
118: * @param mixed $options An array of options.
119: * @param mixed $arguments An array of arguments.
120: * @param array &$world A list of initialized dependencies.
121: *
122: * @return NULL
123: */
124: public function run($cli, $options, $arguments, &$world)
125: {
126: if (!isset($arguments[1])) {
127: $action = 'read';
128: } else {
129: $action = $arguments[1];
130: }
131: switch ($action) {
132: case 'read':
133: $parser = $world['format']->create('Xml', $arguments[2]);
134: if (empty($arguments[4])) {
135: if (file_exists($arguments[3])) {
136: $contents = file_get_contents($arguments[3]);
137: $data = $parser->load($contents);
138: $id = $arguments[3];
139: } else {
140: $cli->message(
141: sprintf(
142: Horde_Kolab_Cli_Translation::t('%s is no local file!'),
143: $arguments[3]
144: ),
145: 'cli.error'
146: );
147: }
148: } else {
149: $ks_data = $world['storage']->getData($arguments[3]);
150: $part = $ks_data->fetchPart($arguments[4], $arguments[5]);
151: rewind($part);
152: $xml = quoted_printable_decode(stream_get_contents($part));
153: $data = $parser->load($xml);
154: $id = $arguments[3] . ':' . $arguments[4] . '[' . $arguments[5] . ']';
155: }
156: if (class_exists('Horde_Yaml')) {
157: $this->_formatOutput($cli, $id, Horde_Yaml::dump($data));
158: } else {
159: $this->_formatOutput($cli, $id, print_r($data, true));
160: }
161: break;
162: default:
163: $cli->message(
164: sprintf(
165: Horde_Kolab_Cli_Translation::t('Action %s not supported!'),
166: $action
167: ),
168: 'cli.error'
169: );
170: break;
171: }
172: }
173:
174: private function _formatOutput($cli, $id, $output)
175: {
176: $cli->writeln('Kolab XML [' . $id . ']');
177: $cli->writeln('================================================================================');
178: $cli->writeln();
179: $cli->writeln($output);
180: $cli->writeln();
181: $cli->writeln('================================================================================');
182: $cli->writeln();
183: }
184:
185: }