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_Push_Cli
29: {
30: 31: 32: 33: 34:
35: static public function main(array $parameters = array())
36: {
37: $parser = new Horde_Argv_Parser(
38: array('usage' => '%prog [OPTIONS] [SOURCE://ID]')
39: );
40: $parser->addOptions(
41: array(
42: new Horde_Argv_Option(
43: '-c',
44: '--config',
45: array(
46: 'action' => 'store',
47: 'help' => Horde_Push_Translation::t('Path to the configuration file.'),
48: )
49: ),
50: new Horde_Argv_Option(
51: '-S',
52: '--summary',
53: array(
54: 'action' => 'store',
55: 'help' => Horde_Push_Translation::t('A summary replacing the value provided by the source.'),
56: )
57: ),
58: new Horde_Argv_Option(
59: '-R',
60: '--recipients',
61: array(
62: 'action' => 'store',
63: 'help' => Horde_Push_Translation::t('A comma delimited list of recipients.'),
64: )
65: ),
66: new Horde_Argv_Option(
67: '-T',
68: '--tags',
69: array(
70: 'action' => 'store',
71: 'help' => Horde_Push_Translation::t('A comma delimited list of tags.'),
72: )
73: ),
74: new Horde_Argv_Option(
75: '-L',
76: '--links',
77: array(
78: 'action' => 'store',
79: 'help' => Horde_Push_Translation::t('A comma delimited list of links.'),
80: )
81: ),
82: new Horde_Argv_Option(
83: '-p',
84: '--pretend',
85: array(
86: 'action' => 'store_true',
87: 'help' => Horde_Push_Translation::t('Do not push the content but display what would be done.'),
88: )
89: ),
90: )
91: );
92: list($options, $arguments) = $parser->parseArgs();
93:
94: global $conf;
95:
96: if (isset($options['config'])) {
97: if (!file_exists($options['config'])) {
98: throw new Horde_Push_Exception(
99: sprintf(
100: 'The specified config file %s does not exist!',
101: $options['config']
102: )
103: );
104: }
105: include $options['config'];
106: } else {
107: $conf = array('recipients' => array('mock'));
108: }
109:
110: if (empty($arguments)) {
111: $arguments = explode(' ', trim(file_get_contents('php://stdin')));
112: }
113: $push_factory = new Horde_Push_Factory_Push();
114: $pushes = $push_factory->create($arguments, $options, $conf);
115: $fail = false;
116: foreach ($pushes as $push) {
117: if (isset($options['summary'])) {
118: $push->setSummary($options['summary']);
119: }
120: if (isset($options['tags'])) {
121: foreach (explode(',', $options['tags']) as $tag) {
122: $push->addTag($tag);
123: }
124: }
125: if (isset($options['links'])) {
126: foreach (explode(',', $options['links']) as $reference) {
127: $push->addReference($reference);
128: }
129: }
130:
131: $recipient_factory = new Horde_Push_Factory_Recipients();
132: $recipients = $recipient_factory->create($options, $conf);
133:
134: foreach ($recipients as $recipient) {
135: $push->addRecipient($recipient);
136: }
137:
138: $results = $push->push(array('pretend' => !empty($options['pretend'])));
139:
140: $cli = Horde_Cli::init();
141: foreach ($results as $result) {
142: if ($result instanceOf Exception) {
143: $cli->message($result->getMessage(), 'cli.error');
144: $fail = true;
145: } else {
146: $cli->message((string)$result, 'cli.success');
147: }
148: }
149: }
150: if ($fail === true) {
151: $status = 1;
152: } else {
153: $status = 0;
154: }
155: if (empty($parameters['no_exit'])) {
156: exit($status);
157: } else {
158: return $status;
159: }
160: }
161: }