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