Browse Source

Feature #272 - Installation scripts: The code for the)language selection box has been reworked. It is about language during installation procedure. Some other minor reworks.

Ivan Tcholakov 15 years ago
parent
commit
e3e823d8f6
3 changed files with 83 additions and 112 deletions
  1. 30 59
      main/install/install_functions.inc.php
  2. 53 30
      main/install/install_upgrade.lib.php
  3. 0 23
      main/install/upgrade.php

+ 30 - 59
main/install/install_functions.inc.php

@@ -231,50 +231,17 @@ function get_config_param($param, $updatePath = '') {
  */
 function get_config_param_from_db($host, $login, $pass, $db_name, $param = '') {
 
-	$mydb = mysql_connect($host, $login, $pass);
-	@mysql_query("set session sql_mode='';"); // Disabling special SQL modes (MySQL 5)
-
-	$myconnect = mysql_select_db($db_name);
-
-	$sql = "SELECT * FROM settings_current WHERE variable = '$param'";
-	$res = mysql_query($sql);
-	if ($res === false) {
-		return null;
-	}
-
-	if (mysql_num_rows($res) > 0) {
-		$row = mysql_fetch_array($res);
-		$value = $row['selected_value'];
-		return $value;
-	}
-	return null;
-}
-
-/**
- * TODO: The main API is accessible here. Then we could use a function for this purpose from there?
- *
- *	Return a list of language directories.
- *	@todo function does not belong here, move to code library,
- *	also see infocours.php which contains similar function
- */
-function get_language_folder_list($dirname) {
-	if ($dirname[strlen($dirname) - 1] != '/') {
-		$dirname .= '/';
-	}
-	$handle = opendir($dirname);
-	$language_list = array();
-
-	while ($entries = readdir($handle)) {
-		if ($entries == '.' || $entries == '..' || $entries=='CVS'  || $entries == '.svn') {
-			continue;
-		}
-		if (is_dir($dirname.$entries)) {
-			$language_list[] = $entries;
+	Database::connect(array('server' => $host, 'username' => $login, 'password' => $pass));
+	Database::query("set session sql_mode='';"); // Disabling special SQL modes (MySQL 5)
+	Database::select_db($db_name);
+
+	if (($res = Database::query("SELECT * FROM settings_current WHERE variable = '$param'")) !== false) {
+		if (Database::num_rows($res) > 0) {
+			$row = Database::fetch_array($res);
+			return $row['selected_value'];
 		}
 	}
-
-	closedir($handle);
-	return $language_list;
+	return null;
 }
 
 /*
@@ -284,34 +251,38 @@ function get_language_folder_list($dirname) {
 */
 
 /**
- *	Displays a form (drop down menu) so the user can select
- *	his/her preferred language.
+ *	Displays a drop down box for selection the preferred language.
  */
 function display_language_selection_box() {
-	//get language list
-	$language_list = get_language_folder_list(api_get_path(SYS_LANG_PATH));
-	sort($language_list);
-	//Reduce the number of languages shown to only show those with higher than 90% translation in DLTT
-	//This option can be easily removed later on. The aim is to test people response to less choice
-	//$language_to_display = $language_list;
-	$language_to_display = array('asturian', 'bulgarian', 'english', 'italian', 'french', 'slovenian', 'slovenian_unicode', 'spanish');
+	// Reading language list.
+	$language_list = get_language_folder_list();
 
-	//display
-	echo "\t\t<select name=\"language_list\">\n";
+	/*
+	// Reduction of the number of languages shown. Enable this fragment of code for customization purposes.
+	// Modify the language list according to your preference. Don't exclude the 'english' item.
+	$language_to_display = array('asturian', 'bulgarian', 'english', 'italian', 'french', 'slovenian', 'slovenian_unicode', 'spanish');
+	foreach ($language_list as $key => & $value) {
+		if (!in_array($key, $language_to_display)) {
+			unset($language_list[$key]);
+		}
+	}
+	*/
 
+	// The default selection, it may be customized too.
 	$default_language = 'english';
-	foreach ($language_to_display as $key => $value) {
-		if ($value == $default_language) {
+
+	// Displaying the box.
+	echo "\t\t<select name=\"language_list\">\n";
+	foreach ($language_list as $key => $value) {
+		if ($key == $default_language) {
 			$option_end = ' selected="selected">';
 		} else {
 			$option_end = '>';
 		}
-		echo "\t\t\t<option value=\"$value\"$option_end";
-
-		echo api_ucfirst($value);
+		echo "\t\t\t<option value=\"$key\"$option_end";
+		echo $value;
 		echo "</option>\n";
 	}
-
 	echo "\t\t</select>\n";
 }
 

+ 53 - 30
main/install/install_upgrade.lib.php

@@ -46,8 +46,8 @@ function set_file_folder_permissions() {
  */
 function fill_track_countries_table($track_countries_table) {
 	$file_path = dirname(__FILE__).'/'.COUNTRY_DATA_FILENAME;
-	$add_country_sql = "LOAD DATA INFILE '".mysql_real_escape_string($file_path)."' INTO TABLE $track_countries_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\'';";
-	@ mysql_query($add_country_sql);
+	$add_country_sql = "LOAD DATA INFILE '".Database::escape_string($file_path)."' INTO TABLE $track_countries_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\'';";
+	@ Database::query($add_country_sql);
 }
 
 /**
@@ -165,7 +165,7 @@ function load_main_database($installation_settings, $db_script = '') {
 
 	//replace symbolic parameters with user-specified values
 	foreach ($installation_settings as $key => $value) {
-		$dokeos_main_sql_file_string = str_replace($key, mysql_real_escape_string($value), $dokeos_main_sql_file_string);
+		$dokeos_main_sql_file_string = str_replace($key, Database::escape_string($value), $dokeos_main_sql_file_string);
 	}
 
 	//split in array of sql strings
@@ -176,7 +176,7 @@ function load_main_database($installation_settings, $db_script = '') {
 	$count = count($sql_instructions);
 	for ($i = 0; $i < $count; $i++) {
 		$this_sql_query = $sql_instructions[$i]['query'];
-		mysql_query($this_sql_query);
+		Database::query($this_sql_query);
 	}
 }
 
@@ -195,7 +195,7 @@ function load_database_script($db_script) {
 	$count = count($sql_instructions);
 	for ($i = 0; $i < $count; $i++) {
 		$this_sql_query = $sql_instructions[$i]['query'];
-		mysql_query($this_sql_query);
+		Database::query($this_sql_query);
 	}
 }
 
@@ -222,8 +222,8 @@ function split_sql_file(&$ret, $sql) {
     $sql_len      = strlen($sql);
     $char         = '';
     $string_start = '';
-    $in_string    = FALSE;
-    $nothing      = TRUE;
+    $in_string    = false;
+    $nothing      = true;
     $time0        = time();
 
     for ($i = 0; $i < $sql_len; ++$i) {
@@ -238,21 +238,21 @@ function split_sql_file(&$ret, $sql) {
                 // returned array
                 if (!$i) {
                     $ret[] = $sql;
-                    return TRUE;
+                    return true;
                 }
                 // Backquotes or no backslashes before quotes: it's indeed the
                 // end of the string -> exit the loop
-                else if ($string_start == '`' || $sql[$i-1] != '\\') {
+                elseif ($string_start == '`' || $sql[$i - 1] != '\\') {
                     $string_start      = '';
-                    $in_string         = FALSE;
+                    $in_string         = false;
                     break;
                 }
                 // one or more Backslashes before the presumed end of string...
                 else {
                     // ... first checks for escaped backslashes
                     $j                     = 2;
-                    $escaped_backslash     = FALSE;
-                    while ($i-$j > 0 && $sql[$i-$j] == '\\') {
+                    $escaped_backslash     = false;
+                    while ($i - $j > 0 && $sql[$i - $j] == '\\') {
                         $escaped_backslash = !$escaped_backslash;
                         $j++;
                     }
@@ -260,7 +260,7 @@ function split_sql_file(&$ret, $sql) {
                     // string -> exit the loop
                     if ($escaped_backslash) {
                         $string_start  = '';
-                        $in_string     = FALSE;
+                        $in_string     = false;
                         break;
                     }
                     // ... else loop
@@ -272,39 +272,39 @@ function split_sql_file(&$ret, $sql) {
         } // end if (in string)
 
         // lets skip comments (/*, -- and #)
-        else if (($char == '-' && $sql_len > $i + 2 && $sql[$i + 1] == '-' && $sql[$i + 2] <= ' ') || $char == '#' || ($char == '/' && $sql_len > $i + 1 && $sql[$i + 1] == '*')) {
+        elseif (($char == '-' && $sql_len > $i + 2 && $sql[$i + 1] == '-' && $sql[$i + 2] <= ' ') || $char == '#' || ($char == '/' && $sql_len > $i + 1 && $sql[$i + 1] == '*')) {
             $i = strpos($sql, $char == '/' ? '*/' : "\n", $i);
             // didn't we hit end of string?
-            if ($i === FALSE) {
+            if ($i === false) {
                 break;
             }
             if ($char == '/') $i++;
         }
 
         // We are not in a string, first check for delimiter...
-        else if ($char == ';') {
+        elseif ($char == ';') {
             // if delimiter found, add the parsed part to the returned array
             $ret[]      = array('query' => substr($sql, 0, $i), 'empty' => $nothing);
-            $nothing    = TRUE;
+            $nothing    = true;
             $sql        = ltrim(substr($sql, min($i + 1, $sql_len)));
             $sql_len    = strlen($sql);
             if ($sql_len) {
                 $i      = -1;
             } else {
                 // The submited statement(s) end(s) here
-                return TRUE;
+                return true;
             }
-        } // end else if (is delimiter)
+        } // end elseif (is delimiter)
 
         // ... then check for start of a string,...
-        else if (($char == '"') || ($char == '\'') || ($char == '`')) {
-            $in_string    = TRUE;
-            $nothing      = FALSE;
+        elseif (($char == '"') || ($char == '\'') || ($char == '`')) {
+            $in_string    = true;
+            $nothing      = false;
             $string_start = $char;
-        } // end else if (is start of string)
+        } // end elseif (is start of string)
 
         elseif ($nothing) {
-            $nothing = FALSE;
+            $nothing = false;
         }
 
         // loic1: send a fake header each 30 sec. to bypass browser timeout
@@ -320,8 +320,8 @@ function split_sql_file(&$ret, $sql) {
         $ret[] = array('query' => $sql, 'empty' => $nothing);
     }
 
-    return TRUE;
-} // end of the 'PMA_splitSqlFile()' function
+    return true;
+} // end of the 'split_sql_file()' function
 
 /**
  * Get an SQL file's contents
@@ -367,7 +367,7 @@ function get_sql_file_contents($file, $section, $print_errors = true) {
 		if (substr($line, 0, 2) == '--') {
 			//This is a comment. Check if section name, otherwise ignore
 			$result = array();
-			if (preg_match('/^-- xx([A-Z]*)xx/', $line,$result)) {	//we got a section name here
+			if (preg_match('/^-- xx([A-Z]*)xx/', $line, $result)) {	//we got a section name here
 				if ($result[1] == strtoupper($section)) {
 					//we have the section we are looking for, start recording
 					$record = true;
@@ -391,6 +391,29 @@ function get_sql_file_contents($file, $section, $print_errors = true) {
 	return $section_contents;
 }
 
+/**
+ *	Returns a list of language directories.
+ */
+function get_language_folder_list() {
+	$result = array();
+	$exceptions = array('.', '..', 'CVS', '.svn');
+	$search       = array('_latin',   '_unicode',   '_corporate',   '_org'  , '_KM',   '_');
+	$replace_with = array(' (Latin)', ' (unicode)', ' (corporate)', ' (org)', ' (KM)', ' ');
+	$dirname = api_get_path(SYS_LANG_PATH);
+	$handle = opendir($dirname);
+	while ($entries = readdir($handle)) {
+		if (in_array($entries, $exceptions)) {
+			continue;
+		}
+		if (is_dir($dirname.$entries)) {
+			$result[$entries] = ucwords(str_replace($search, $replace_with, $entries));
+		}
+	}
+	closedir($handle);
+	asort($result);
+	return $result;
+}
+
 // TODO: Maybe within the main API there is already a suitable function?
 function my_directory_to_array($directory) {
 	$array_items = array();
@@ -426,10 +449,10 @@ function add_document_180($_course, $path, $filetype, $filesize, $title, $commen
     VALUES ('$path','$filetype','$filesize','".
     Database::escape_string($title)."', '$comment')";
     if (Database::query($sql)) {
-        //display_message("Added to database (id ".mysql_insert_id().")!");
-        return mysql_insert_id();
+        //display_message("Added to database (id ".Database::insert_id().")!");
+        return Database::insert_id();
     } else {
-        //display_error("The uploaded file could not be added to the database (".mysql_error().")!");
+        //display_error("The uploaded file could not be added to the database (".Database::error().")!");
         return false;
     }
 }

+ 0 - 23
main/install/upgrade.php

@@ -589,29 +589,6 @@ function display_upgrade_header($text_dir, $dokeos_version, $install_type, $upda
 	<?php
 }
 
-/**
- *	TODO: Copy/paste again. Find an existing function for this job.
- *	Return a list of language directories.
- *	@todo function does not belong here, move to code library,
- *	also see infocours.php which contains similar function
- */
-function get_language_folder_list() {
-	$dirname = dirname(__FILE__).'/../lang';
-	if ($dirname[strlen($dirname) - 1] != '/')
-		$dirname .= '/';
-	$handle = opendir($dirname);
-	while ($entries = readdir($handle)) {
-		if ($entries == '.' || $entries == '..' || $entries == '.svn')
-			continue;
-		if (is_dir($dirname.$entries)) {
-			$language_list[$entries] = api_ucfirst($entries);
-		}
-	}
-	closedir($handle);
-	asort($language_list);
-	return $language_list;
-}
-
 function display_installation_overview() {
 	echo '<div id="installation_steps">';
 	echo '<img src="../img/bluelogo.gif" hspace="10" vspace="10" alt="Dokeos logo" />';