]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/commandline.inc.php
Merge branch 'master' of gitorious.org:social/mainline
[quix0rs-gnu-social.git] / scripts / commandline.inc.php
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('GNUSOCIAL', true);
30 define('STATUSNET', true); //compatibility
31
32 // Set various flags so we don't time out on long-running processes
33
34 ini_set("max_execution_time", "0");
35 ini_set("max_input_time", "0");
36 set_time_limit(0);
37 mb_internal_encoding('UTF-8');
38 error_reporting(0);
39 # DEBUG: error_reporting(E_ALL & ~E_STRICT & ~E_DEPRECATED);
40
41 // Add extlib to our path so we can get Console_Getopt
42
43 $_extra_path = array(INSTALLDIR.'/extlib/');
44
45 set_include_path(implode(PATH_SEPARATOR, $_extra_path) . PATH_SEPARATOR . get_include_path());
46
47 require_once 'Console/Getopt.php';
48
49 // Note: $shortoptions and $longoptions should be pre-defined!
50
51 $_default_shortoptions = 'qvhc:s:p:';
52
53 $_default_longoptions = array('quiet', 'verbose', 'help', 'conf=', 'server=', 'path=');
54
55 if (isset($shortoptions)) {
56     $shortoptions .= $_default_shortoptions;
57 } else {
58     $shortoptions = $_default_shortoptions;
59 }
60
61 if (isset($longoptions)) {
62     $longoptions = array_merge($longoptions, $_default_longoptions);
63 } else {
64     $longoptions = $_default_longoptions;
65 }
66
67 $parser = new Console_Getopt();
68
69 $result = $parser->getopt($argv, $shortoptions, $longoptions);
70
71 if (PEAR::isError($result)) {
72     print $result->getMessage()."\n";
73     exit(1);
74 } else {
75     list($options, $args) = $result;
76 }
77
78 function show_help()
79 {
80     global $helptext;
81
82     $_default_help_text = <<<END_OF_DEFAULT
83       General options:
84
85     -q --quiet           Quiet (little output)
86     -v --verbose         Verbose (lots of output)
87     -c --conf=<filename> Use <filename> as config file
88     -s --server=<name>   Use <name> as server name
89     -p --path=<path>     Use <path> as path name
90     -h --help            Show this message and quit.
91
92 END_OF_DEFAULT;
93     if (isset($helptext)) {
94         print $helptext;
95     }
96     print $_default_help_text;
97     exit(0);
98 }
99
100 foreach ($options as $option) {
101
102     switch ($option[0]) {
103      case '--server':
104      case 's':
105         $server = $option[1];
106         break;
107
108      case '--path':
109      case 'p':
110         $path = $option[1];
111         break;
112
113      case '--conf':
114      case 'c':
115         $conffile = $option[1];
116         break;
117
118      case '--help':
119      case 'h':
120         show_help();
121     }
122 }
123
124 require_once INSTALLDIR . '/lib/common.php';
125
126 set_error_handler('common_error_handler');
127
128 // Set up the language infrastructure so we can localize anything that
129 // needs to be sent out to users, such as mail notifications.
130 common_init_language();
131
132 function _make_matches($opt, $alt)
133 {
134     $matches = array();
135
136     if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) {
137         $matches[] = '--'.$opt;
138     } else {
139         $matches[] = $opt;
140     }
141
142     if (!empty($alt)) {
143         if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) {
144             $matches[] = '--'.$alt;
145         } else {
146             $matches[] = $alt;
147         }
148     }
149
150     return $matches;
151 }
152
153 function have_option($opt, $alt=null)
154 {
155     global $options;
156
157     $matches = _make_matches($opt, $alt);
158
159     foreach ($options as $option) {
160         if (in_array($option[0], $matches)) {
161             return true;
162         }
163     }
164
165     return false;
166 }
167
168 function get_option_value($opt, $alt=null)
169 {
170     global $options;
171
172     $matches = _make_matches($opt, $alt);
173
174     foreach ($options as $option) {
175         if (in_array($option[0], $matches)) {
176             return $option[1];
177         }
178     }
179
180     return null;
181 }
182
183 class NoUserArgumentException extends Exception
184 {
185 }
186
187 function getUser()
188 {
189     $user = null;
190
191     if (have_option('i', 'id')) {
192         $id = get_option_value('i', 'id');
193         $user = User::getKV('id', $id);
194         if (empty($user)) {
195             throw new Exception("Can't find user with id '$id'.");
196         }
197     } else if (have_option('n', 'nickname')) {
198         $nickname = get_option_value('n', 'nickname');
199         $user = User::getKV('nickname', $nickname);
200         if (empty($user)) {
201             throw new Exception("Can't find user with nickname '$nickname'");
202         }
203     } else {
204         throw new NoUserArgumentException("No user argument specified.");
205     }
206
207     return $user;
208 }
209
210 /** "Printf not quiet" */
211
212 function printfnq()
213 {
214     if (have_option('q', 'quiet')) {
215         return null;
216     }
217
218     $cargs  = func_num_args();
219
220     if ($cargs == 0) {
221         return 0;
222     }
223
224     $args   = func_get_args();
225     $format = array_shift($args);
226
227     return vprintf($format, $args);
228 }
229
230 /** "Print when verbose" */
231
232 function printfv()
233 {
234     if (!have_option('v', 'verbose')) {
235         return null;
236     }
237
238     $cargs  = func_num_args();
239
240     if ($cargs == 0) {
241         return 0;
242     }
243
244     $args   = func_get_args();
245     $format = array_shift($args);
246
247     return vprintf($format, $args);
248 }