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