blocklib.php MDL-19236 added phpdocs and copyrights
[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'])) {
27bd819b 209 console_write_error('invalidvalueforlanguage');
210 console_write( "\n", '', false);
9d068cd6 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)) {
27bd819b 236 console_write_error('invaliddbtype');
9d068cd6 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)){
27bd819b 311 console_write_error('invalidverbosevalue');
9d068cd6 312 }
313 }
314 if (isset($values['interactivelevel'])) {
315 if(!valid_int_range($values['verbose'],CLI_NO,CLI_FULL)){
27bd819b 316 console_write_error('invalidinteractivevalue');
9d068cd6 317 }
318 }
319
320 if (isset($values['help'])) {
321 /**
322 * @todo nothing really
323 */
324 }
325}
326//=========================================================================//
9d068cd6 327/**
328 * Read a mixed type
329 *
330 * @param stream $from
331 * @param int $size
332 * @return mixed type
333 */
334function read($from=STDIN,$size=1024) {
335 $input= trim(fread($from,$size));
336 return $input;
337}
338/**
339 * Read an integer
340 *
341 * @return integer
342 */
343function read_int() {
344 $input=read();
345 if (valid_param($input,PARAM_INT)) {
346 return $input;
347 } else {
27bd819b 348 console_write_error('invalidint');
349 console_write( "\n", '', false);
9d068cd6 350 }
351}
352//=========================================================================//
353/**
354 * Read and integer value within range
355 *
356 * @param int $start
357 * @param int $end
358 * @return int
359 */
360function read_int_range($start,$end) {
361 $input=read_int();
362 if (valid_int_range($input,$start,$end)) {
363 return $input;
364 } else {
27bd819b 365 console_write_error('invalidintrange');
366 console_write( "\n", '', false);
9d068cd6 367 }
368
369}
370//=========================================================================//
371/**
372 * Read yes/no argument
373 *
374 * @return string yes/no
375 */
376function read_yes_no() {
377 $input=strtolower(read());
378 if (valid_yes_no($input)) {
379 if ($input[0]=='y') {
380 return 'yes';
381 } else if($input[0]=='n') {
382 return 'no';
383 }
384 } else {
27bd819b 385 console_write_error('invalidyesno');
386 console_write( "\n", '', false);
9d068cd6 387 }
388}
389
390//=========================================================================//
391/**
392 * Read a boolean parameter from the input
393 *
394 * @return boolean
395 */
396function read_boolean(){
397 $input=read_yes_no();
398 return clean_param($input,PARAM_BOOL);
399
400}
401//=========================================================================//
402/**
403 * Reading an element from a given set
404 *
405 * @param mixed type array $set
406 * @return mixed type
407 */
408function read_element($set=array()) {
409 $input=read();
410 if (valid_element($input,$set)) {
411 return $input;
412 } else {
27bd819b 413 console_write_error('invalidsetelement');
414 console_write( "\n", '', false);
9d068cd6 415 }
416}
417//=========================================================================//
418function read_url() {
419 $input = read();
420 $localhost = false;
421 if ( strpos($input,'localhost') !== false) {
422 $input = str_replace('localhost','127.0.0.1',$input);
423 $localhost=true;
424 }
425 if (valid_param($input,PARAM_URL)) {
426 if ($localhost) {
427 return str_replace('127.0.0.1','localhost',$input);
428 } else {
429 return $input;
430 }
431 } else {
27bd819b 432 console_write_error('invalidurl');
9d068cd6 433 }
434
435}
436//=========================================================================//
437/**
438 * Enter description here...
439 *
440 * @return string
441 */
442function read_dir() {
443 $input = read();
444 return $input;
445}
446//===========================================================================//
447/**
448 * Print compatibility message to standard out, and errors to standard error
449 *
450 * @param boolean $success
451 * @param string $testtext
452 * @param string $errormessage
453 * @param boolean $caution
454 * @param boolean $silent
455 * @return boolean
456 */
457function check_compatibility($success, $testtext,$errormessage,$caution=false,$silent=false) {
458 if ($success) {
459 if (!$silent) {
27bd819b 460 console_write(get_string('pass', 'install'),'',false);
9d068cd6 461 }
462 } else {
463 if ($caution) {
464 if (!$silent) {
27bd819b 465 console_write(get_string('caution', 'install'),'',false);
9d068cd6 466 }
467 } else {
27bd819b 468 console_write(get_string('fail', 'install'),'',false);
469 console_write_error($errormessage,'',false);
9d068cd6 470 }
471 }
472 if (!$silent) {
27bd819b 473 console_write("\t\t",'',false);
474 console_write($testtext,'',false);
475 console_write("\n",'',false);
9d068cd6 476 }
477 return $success;
478}
479
480//==========================================================================//
481/**
482 * Print environment status to standard out
483 *
484 * @param array $env, of type object
485 */
486function print_environment_status($env = array()) {
27bd819b 487 console_write( get_string('name') . "\t\t\t" . get_string('info') . "\t" . get_string('status') . "\n\r", '', false);
488 //console_write("Status\t\tInfo\t\tPart\n\r",'',false);
9d068cd6 489 foreach ( $env as $object) {
27bd819b 490 console_write($object->part,'',false);
491 console_write("\t\t",'',false);
9d068cd6 492 if (!empty($object->info)) {
27bd819b 493 console_write( $object->info, '', false);
9d068cd6 494 } else {
27bd819b 495 console_write( "\t", '', false);
9d068cd6 496 }
27bd819b 497 console_write( "\t\t", '', false);
9d068cd6 498 if ($object->status == 1 ) {
27bd819b 499 console_write('ok','',false);
9d068cd6 500 } else {
27bd819b 501 console_write('fail','',false);
9d068cd6 502 }
27bd819b 503 console_write("\n\r",'',false);
9d068cd6 504 }
505}
506
507/**
508 * Print environment status to standard out
509 *
510 * @param array $env, of type object
511 */
512function print_environment_status_detailed($env = array()) {
27bd819b 513 console_write("Status\t\tLevel\t\tCurrent ver\tRequired ver\t\tPart\t\tInfo\n\r",'',false);
9d068cd6 514 foreach ( $env as $object) {
515
516 if ($object->status == 1 ) {
27bd819b 517 console_write('ok ','',false);
9d068cd6 518 } else if ($object->errorcode != 0) {
27bd819b 519 console_write('fail ','',false);
9d068cd6 520 } else {
27bd819b 521 console_write('----','',false);
9d068cd6 522 }
27bd819b 523 console_write("\t\t",'',false);
524 console_write($object->level,'',false);
525 console_write("\t\t",'',false);
526 console_write($object->current_version,'',false);
527 console_write("\t",'',false);
528 console_write($object->needed_version,'',false);
529 console_write("\t\t",'',false);
530 console_write($object->part,'',false);
531 console_write("\t\t",'',false);
532 console_write($object->info,'',false);
533 console_write("\n\r",'',false);
9d068cd6 534 }
535}
536/**
537 * Print a new line in the standard output
538 *
539 */
540
541function print_newline() {
27bd819b 542 console_write( "\n", '', false);
9d068cd6 543}
544?>