MDL-15249 improving cli installer
[moodle.git] / lib / clilib.php
CommitLineData
9d068cd6 1<?php //$Id$
2/**
3 * CLI support functions
4 * @author Dilan
5 */
6
7//include PEAR Console libraries
8set_include_path($CFG->libdir . PATH_SEPARATOR . $CFG->libdir . '/pear/');
9require_once('Console/Getopt.php');
10
11/**
12 * Check the validity of the language
13 * return true or false
14 *
15 * @param string $lang (short code for language)
16 * @return true/false
17 */
18function valid_language($lang) {
19 global $DEFAULT;
20 $langdir = dir($DEFAULT['dirroot'].'/install/lang');
21 $i=0;
22 $validllangs = array();
23
24 while (false !== ($file=$langdir->read())) {
25 if ($file[0] != '.' ) {
26 $validllangs[$i++]=$file;
27 }
28 }
29 if (in_array($lang,$validllangs)) {
30 return true;
31 } else {
32 return false;
33 }
34}
35//========================================================================================//
36/**
37 * Read from array of language strings and return a array of string elements in which
38 * both values and keys are set to input array's key
39 *
40 * @param array $lang string elements
41 * @return array of string element
42 */
43function get_short_codes ($lang = array()) {
44 $short_codes = array();
45
46 foreach ($lang as $key => $value) {
47 $short_codes[$key] = $key;
48 }
49 return $short_codes;
50}
51//========================================================================================//
52/**
53 * Check value for valid yes/no argument
54 * Return true or false
55 *
56 * @param string $value
57 * @return true/false
58 */
59function valid_yes_no($value){
60 $valid=array('yes','y','n','no');
61 $value=strtolower($value);
62
63 if (in_array($value,$valid)) {
64 if ($value[0]=='y') {
65 return true;
66 } else if ($value[0]=='n') {
67 return true;
68 }
69 } else {
70 return false;
71 }
72}
73//========================================================================================//
74/**
75 * Can value have a valid integer in the given range
76 * Return true or false
77 * @link valid_param()
78 *
79 *
80 * @param mixedtype $value
81 * @param int $start
82 * @param int $end
83 * @return true/false
84 */
85function valid_int_range($value,$start,$end) {
86 if (valid_param($value,PARAM_INT)) {
87 if ($value < $end && $value > $start) {
88 return true;
89 } else {
90 return false;
91 }
92 }
93}
94
95/**
96 * Take a value and and check it with the given set of values
97 * If value if found in the set return true. False otherwise
98 *
99 * @param mixed type $value
100 * @param array $set of valid elements
101 * @return boolean
102 */
103
104function valid_element($value,$set) {
105 if(!empty($set)) {
106 //convert all the elements from set to lower case
107 foreach ($set as $key=>$opt) {
108 $set[$key]=strtolower($opt);
109 }
110 $value=strtolower($value);
111 if (in_array($value,$set)) {
112 return true;
113 } else {
114 return false;
115 }
116 }
117}
118
119/**
120 * Take a value and Type of the value
121 * If value match the type return true, false otherwise
122 * uses {@link clean_param()} in moodlelib.php
123 * @param mixedtype $value
124 * @param int $type
125 * @return boolean
126 */
127function valid_param($value,$type){
128 $clean_val = clean_param($value,$type);
129 if ($clean_val == $value) {
130 return true;
131 }else {
132 return false;
133 }
134}
135//========================================================================================//
136/**
137 * Creat long arument list for PEAR method invocation using LONGOPTOIN array
138 *
139 * @param long option array $long_opt
140 * @return PEAR method compatible long option array
141 */
142function create_long_options($long_opt) {
143 $opt=array();
144 $i=0;
145 if (is_array($long_opt)) {
146 foreach ($long_opt as $key=>$value) {
147 if ($value == CLI_VAL_REQ) {
148 $opt[$i++]=$key.'=';
149 } else if ($value == CLI_VAL_OPT) {
150 $opt[$i++]=$key.'==';
151 }
152
153 }
154 }
155 return $opt;
156}
157//========================================================================================//
158/**
159 * This funtion return an array of options with option as key containing the value of
160 * respective option
161 *
162 * @param array of option arguments as defined by PEAR GetOpt calss $opt
163 * @return return a options arguments with options as keys and values as respective value for key
164 */
165function get_options($opt=array()) {
166
167 global $LONG_OPTIONS;
168 $ret_arr=array();
169
170 //get the options from the defined list of arguments
171 if (!empty($opt[0]) && is_array($opt[0])) {
172
173 foreach ($opt[0] as $key=>$value) {
174
175 if (substr($value[0],0,2)=='--') { //if the argument is a long option
176 $input_option=substr($value[0],2);
177 } else if (substr($value[0],0,1)=='-'){ //if the argument is a short option
178 $input_option=substr($value[0],1);
179 }
180
181 //check with valid set of options
182 if (in_array($input_option,$LONG_OPTIONS)) {
183 $ret_arr[$input_option]=$value[1];
184 }
185 }
186
187 }
188 //return array
189 return $ret_arr;
190
191}
192
193//========================================================================================//
194
195//=========================================================================//
196/**
197 * Validate options values
198 *
199 * @param array $options
200 */
201function validate_option_values($options){
202 $values=array();
203 $i=0;
204 foreach ($options as $val) {
205 $values[$i++]=$val;
206 }
207 if (isset($values['lang'])) {
208 if (!valid_language($values['lang'])) {
209 console_write(STDERR,'invalidvalueforlanguage');
210 console_write(STDOUT, "\n", '', false);
211 }
212 }
213 if (isset($values['webdir'])) {
214 /**
215 * @todo check valid directory path
216 */
217 }
218 if (isset($values['webaddr'])) {
219 /**
220 * @todo check valid http url
221 */
222 }
223 if (isset($values['moodledir'])) {
224 /**
225 * @todo check valid directory path
226 */
227 }
228 if (isset($values['datadir'])) {
229 /**
230 * @todo check valid directory path
231 */
232 }
233 if (isset($values['dbtype'])) {
234 $dbtypes=array('mysql','oci8po','postgres7','mssql','mssql_n','odbc_mssql');
235 if (!in_array($values['dbtype'],$dbtypes)) {
236 console_write(STDERR,'invaliddbtype');
237 }
238 }
239 if (isset($values['dbhost'])) {
240 /**
241 * @todo check host?
242 */
243 }
244 if (isset($values['dbname'])) {
245 /**
246 * @todo check name for valid ones if required
247 */
248 }
249 if (isset($values['dbuser'])) {
250 /**
251 * @todo check validity of db user if required
252 */
253 }
254 if (isset($values['dbpass'])) {
255 /**
256 * @todo check validity of database password if required
257 */
258 }
259 if (isset($values['prefix'])) {
260 /**
261 * @todo check for valid prefix
262 */
263 }
264 if (isset($values['sitefullname'])) {
265 /**
266 * @todo check for valid fullname for site
267 */
268 }
269 if (isset($values['siteshortname'])) {
270 /**
271 * @todo check for valid short name for site
272 */
273 }
274 if (isset($values['sitesummary'])) {
275 /**
276 * @todo check for valid summary
277 */
278 }
279 if (isset($values['sitenewsitems'])) {
280 /**
281 * @todo check for valid news items
282 */
283 }
284 if (isset($values['adminfirstname'])) {
285 /**
286 * @todo check for valid admin first name
287 */
288 }
289 if (isset($values['adminlastname'])) {
290 /**
291 * @todo check for valid last name
292 */
293 }
294 if (isset($values['adminusername'])) {
295 /**
296 * @todo check for valid username
297 */
298 }
299 if (isset($values['adminpassword'])) {
300 /**
301 * @todo check for valid password
302 */
303 }
304 if (isset($values['adminemail'])) {
305 /**
306 * @todo check for valid email
307 */
308 }
309 if (isset($values['verbose'])) {
310 if(!valid_int_range($values['verbose'],CLI_NO,CLI_FULL)){
311 console_write(STDERR,'invalidverbosevalue');
312 }
313 }
314 if (isset($values['interactivelevel'])) {
315 if(!valid_int_range($values['verbose'],CLI_NO,CLI_FULL)){
316 console_write(STDERR,'invalidinteractivevalue');
317 }
318 }
319
320 if (isset($values['help'])) {
321 /**
322 * @todo nothing really
323 */
324 }
325}
326//=========================================================================//
327/**
328 * Write to standard out and error with exit in error
329 *
330 * @param standard out/err $stream
331 * @param string $identifier
332 * @param name of module $module
333 */
334function console_write($stream,$identifier,$module='install',$use_string_lib=true) {
335 if ($use_string_lib) {
336 fwrite($stream,get_string($identifier,$module));
337 } else {
338 fwrite($stream,$identifier);
339 }
340 if ($stream == STDERR) {
341 fwrite($stream, "\n\n" . get_string('aborting',$module) . "\n\n");
342 die;
343 }
344}
345//=========================================================================//
346/**
347 * Read a mixed type
348 *
349 * @param stream $from
350 * @param int $size
351 * @return mixed type
352 */
353function read($from=STDIN,$size=1024) {
354 $input= trim(fread($from,$size));
355 return $input;
356}
357/**
358 * Read an integer
359 *
360 * @return integer
361 */
362function read_int() {
363 $input=read();
364 if (valid_param($input,PARAM_INT)) {
365 return $input;
366 } else {
367 console_write(STDERR,'invalidint');
368 console_write(STDOUT, "\n", '', false);
369 }
370}
371//=========================================================================//
372/**
373 * Read and integer value within range
374 *
375 * @param int $start
376 * @param int $end
377 * @return int
378 */
379function read_int_range($start,$end) {
380 $input=read_int();
381 if (valid_int_range($input,$start,$end)) {
382 return $input;
383 } else {
384 console_write(STDERR,'invalidintrange');
385 console_write(STDOUT, "\n", '', false);
386 }
387
388}
389//=========================================================================//
390/**
391 * Read yes/no argument
392 *
393 * @return string yes/no
394 */
395function read_yes_no() {
396 $input=strtolower(read());
397 if (valid_yes_no($input)) {
398 if ($input[0]=='y') {
399 return 'yes';
400 } else if($input[0]=='n') {
401 return 'no';
402 }
403 } else {
404 console_write(STDERR,'invalidyesno');
405 console_write(STDOUT, "\n", '', false);
406 }
407}
408
409//=========================================================================//
410/**
411 * Read a boolean parameter from the input
412 *
413 * @return boolean
414 */
415function read_boolean(){
416 $input=read_yes_no();
417 return clean_param($input,PARAM_BOOL);
418
419}
420//=========================================================================//
421/**
422 * Reading an element from a given set
423 *
424 * @param mixed type array $set
425 * @return mixed type
426 */
427function read_element($set=array()) {
428 $input=read();
429 if (valid_element($input,$set)) {
430 return $input;
431 } else {
432 console_write(STDERR,'invalidsetelement');
433 console_write(STDOUT, "\n", '', false);
434 }
435}
436//=========================================================================//
437function read_url() {
438 $input = read();
439 $localhost = false;
440 if ( strpos($input,'localhost') !== false) {
441 $input = str_replace('localhost','127.0.0.1',$input);
442 $localhost=true;
443 }
444 if (valid_param($input,PARAM_URL)) {
445 if ($localhost) {
446 return str_replace('127.0.0.1','localhost',$input);
447 } else {
448 return $input;
449 }
450 } else {
451 console_write(STDERR,'invalidurl');
452 }
453
454}
455//=========================================================================//
456/**
457 * Enter description here...
458 *
459 * @return string
460 */
461function read_dir() {
462 $input = read();
463 return $input;
464}
465//===========================================================================//
466/**
467 * Print compatibility message to standard out, and errors to standard error
468 *
469 * @param boolean $success
470 * @param string $testtext
471 * @param string $errormessage
472 * @param boolean $caution
473 * @param boolean $silent
474 * @return boolean
475 */
476function check_compatibility($success, $testtext,$errormessage,$caution=false,$silent=false) {
477 if ($success) {
478 if (!$silent) {
479 console_write(STDOUT,get_string('pass', 'install'),'',false);
480 }
481 } else {
482 if ($caution) {
483 if (!$silent) {
484 console_write(STDOUT,get_string('caution', 'install'),'',false);
485 }
486 } else {
487 console_write(STDOUT,get_string('fail', 'install'),'',false);
488 console_write(STDERR,$errormessage,'',false);
489 }
490 }
491 if (!$silent) {
492 console_write(STDOUT,"\t\t",'',false);
493 console_write(STDOUT,$testtext,'',false);
494 console_write(STDOUT,"\n",'',false);
495 }
496 return $success;
497}
498
499//==========================================================================//
500/**
501 * Print environment status to standard out
502 *
503 * @param array $env, of type object
504 */
505function print_environment_status($env = array()) {
506 console_write(STDOUT, get_string('name') . "\t\t\t" . get_string('info') . "\t" . get_string('status') . "\n\r", '', false);
507 //console_write(STDOUT,"Status\t\tInfo\t\tPart\n\r",'',false);
508 foreach ( $env as $object) {
509 console_write(STDOUT,$object->part,'',false);
510 console_write(STDOUT,"\t\t",'',false);
511 if (!empty($object->info)) {
512 console_write(STDOUT, $object->info, '', false);
513 } else {
514 console_write(STDOUT, "\t", '', false);
515 }
516 console_write(STDOUT, "\t\t", '', false);
517 if ($object->status == 1 ) {
518 console_write(STDOUT,'ok','',false);
519 } else {
520 console_write(STDOUT,'fail','',false);
521 }
522 console_write(STDOUT,"\n\r",'',false);
523 }
524}
525
526/**
527 * Print environment status to standard out
528 *
529 * @param array $env, of type object
530 */
531function print_environment_status_detailed($env = array()) {
532 console_write(STDOUT,"Status\t\tLevel\t\tCurrent ver\tRequired ver\t\tPart\t\tInfo\n\r",'',false);
533 foreach ( $env as $object) {
534
535 if ($object->status == 1 ) {
536 console_write(STDOUT,'ok ','',false);
537 } else if ($object->errorcode != 0) {
538 console_write(STDOUT,'fail ','',false);
539 } else {
540 console_write(STDOUT,'----','',false);
541 }
542 console_write(STDOUT,"\t\t",'',false);
543 console_write(STDOUT,$object->level,'',false);
544 console_write(STDOUT,"\t\t",'',false);
545 console_write(STDOUT,$object->current_version,'',false);
546 console_write(STDOUT,"\t",'',false);
547 console_write(STDOUT,$object->needed_version,'',false);
548 console_write(STDOUT,"\t\t",'',false);
549 console_write(STDOUT,$object->part,'',false);
550 console_write(STDOUT,"\t\t",'',false);
551 console_write(STDOUT,$object->info,'',false);
552 console_write(STDOUT,"\n\r",'',false);
553 }
554}
555/**
556 * Print a new line in the standard output
557 *
558 */
559
560function print_newline() {
561 console_write(STDOUT, "\n", '', false);
562}
563?>