PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/clilib.php

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