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