]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/commandline.inc
minor fixes and cleanups in the scripts directory
[quix0rs-gnu-social.git] / scripts / commandline.inc
1 <?php
2 /*
3  * StatusNet - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 // -*- mode: php -*-
21
22 # Abort if called from a web server
23
24 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
25     print "This script must be run from the command line\n";
26     exit();
27 }
28
29 define('STATUSNET', true);
30
31 // Set various flags so we don't time out on long-running processes
32
33 ini_set("max_execution_time", "0");
34 ini_set("max_input_time", "0");
35 set_time_limit(0);
36 mb_internal_encoding('UTF-8');
37
38 // Add extlib to our path so we can get Console_Getopt
39
40 $_extra_path = array(INSTALLDIR.'/extlib/');
41
42 set_include_path(implode(PATH_SEPARATOR, $_extra_path) . PATH_SEPARATOR . get_include_path());
43
44 require_once 'Console/Getopt.php';
45
46 // Note: $shortoptions and $longoptions should be pre-defined!
47
48 $_default_shortoptions = 'qvhc:s:p:';
49
50 $_default_longoptions = array('quiet', 'verbose', 'help', 'conf=', 'server=', 'path=');
51
52 if (isset($shortoptions)) {
53     $shortoptions .= $_default_shortoptions;
54 } else {
55     $shortoptions = $_default_shortoptions;
56 }
57
58 if (isset($longoptions)) {
59     $longoptions = array_merge($longoptions, $_default_longoptions);
60 } else {
61     $longoptions = $_default_longoptions;
62 }
63
64 $parser = new Console_Getopt();
65
66 $result = $parser->getopt($argv, $shortoptions, $longoptions);
67
68 if (PEAR::isError($result)) {
69     print $result->getMessage()."\n";
70     exit(1);
71 } else {
72     list($options, $args) = $result;
73 }
74
75 function show_help()
76 {
77     global $helptext;
78
79     $_default_help_text = <<<END_OF_DEFAULT
80       General options:
81
82     -q --quiet           Quiet (little output)
83     -v --verbose         Verbose (lots of output)
84     -c --conf=<filename> Use <filename> as config file
85     -s --server=<name>   Use <name> as server name
86     -p --path=<path>     Use <path> as path name
87     -h --help            Show this message and quit.
88
89 END_OF_DEFAULT;
90     if (isset($helptext)) {
91         print $helptext;
92     }
93     print $_default_help_text;
94     exit(0);
95 }
96
97 foreach ($options as $option) {
98
99     switch ($option[0]) {
100      case '--server':
101      case 's':
102         $server = $option[1];
103         break;
104
105      case '--path':
106      case 'p':
107         $path = $option[1];
108         break;
109
110      case '--conf':
111      case 'c':
112         $conffile = $option[1];
113         break;
114
115      case '--help':
116      case 'h':
117         show_help();
118     }
119 }
120
121 require_once INSTALLDIR . '/lib/common.php';
122
123 set_error_handler('common_error_handler');
124
125 // Set up the language infrastructure so we can localize anything that
126 // needs to be sent out to users, such as mail notifications.
127 common_init_language();
128
129 function _make_matches($opt, $alt)
130 {
131     $matches = array();
132
133     if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) {
134         $matches[] = '--'.$opt;
135     } else {
136         $matches[] = $opt;
137     }
138
139     if (!empty($alt)) {
140         if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) {
141             $matches[] = '--'.$alt;
142         } else {
143             $matches[] = $alt;
144         }
145     }
146
147     return $matches;
148 }
149
150 function have_option($opt, $alt=null)
151 {
152     global $options;
153
154     $matches = _make_matches($opt, $alt);
155
156     foreach ($options as $option) {
157         if (in_array($option[0], $matches)) {
158             return true;
159         }
160     }
161
162     return false;
163 }
164
165 function get_option_value($opt, $alt=null)
166 {
167     global $options;
168
169     $matches = _make_matches($opt, $alt);
170
171     foreach ($options as $option) {
172         if (in_array($option[0], $matches)) {
173             return $option[1];
174         }
175     }
176
177     return null;
178 }
179
180 class NoUserArgumentException extends Exception
181 {
182 }
183
184 function getUser()
185 {
186     $user = null;
187
188     if (have_option('i', 'id')) {
189         $id = get_option_value('i', 'id');
190         $user = User::getKV('id', $id);
191         if (empty($user)) {
192             throw new Exception("Can't find user with id '$id'.");
193         }
194     } else if (have_option('n', 'nickname')) {
195         $nickname = get_option_value('n', 'nickname');
196         $user = User::getKV('nickname', $nickname);
197         if (empty($user)) {
198             throw new Exception("Can't find user with nickname '$nickname'");
199         }
200     } else {
201         throw new NoUserArgumentException("No user argument specified.");
202     }
203
204     return $user;
205 }
206
207 /** "Printf not quiet" */
208
209 function printfnq()
210 {
211     if (have_option('q', 'quiet')) {
212         return null;
213     }
214
215     $cargs  = func_num_args();
216
217     if ($cargs == 0) {
218         return 0;
219     }
220
221     $args   = func_get_args();
222     $format = array_shift($args);
223
224     return vprintf($format, $args);
225 }
226
227 /** "Print when verbose" */
228
229 function printfv()
230 {
231     if (!have_option('v', 'verbose')) {
232         return null;
233     }
234
235     $cargs  = func_num_args();
236
237     if ($cargs == 0) {
238         return 0;
239     }
240
241     $args   = func_get_args();
242     $format = array_shift($args);
243
244     return vprintf($format, $args);
245 }