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