Automatic installer.php lang files by installer_builder (20080120)
[moodle.git] / lib / setuplib.php
CommitLineData
d3f9f1f8 1<?php // $Id$
2 // These functions are required very early in the Moodle
3 // setup process, before any of the main libraries are
4 // loaded.
5
6
7/**
8 * Initializes our performance info early.
9 *
10 * Pairs up with get_performance_info() which is actually
11 * in moodlelib.php. This function is here so that we can
12 * call it before all the libs are pulled in.
13 *
14 * @uses $PERF
15 */
16function init_performance_info() {
17
6fc4ad72 18 global $PERF, $CFG, $USER;
b65567f4 19
d3f9f1f8 20 $PERF = new Object;
21 $PERF->dbqueries = 0;
22 $PERF->logwrites = 0;
23 if (function_exists('microtime')) {
24 $PERF->starttime = microtime();
25 }
26 if (function_exists('memory_get_usage')) {
27 $PERF->startmemory = memory_get_usage();
28 }
29 if (function_exists('posix_times')) {
30 $PERF->startposixtimes = posix_times();
31 }
b65567f4 32 if (function_exists('apd_set_pprof_trace')) {
33 // APD profiling
6fc4ad72 34 if ($USER->id > 0 && $CFG->perfdebug >= 15) {
35 $tempdir = $CFG->dataroot . '/temp/profile/' . $USER->id;
36 mkdir($tempdir);
37 apd_set_pprof_trace($tempdir);
38 $PERF->profiling = true;
39 }
b65567f4 40 }
d3f9f1f8 41}
42
76f3815b 43/**
44 * Function to raise the memory limit to a new value.
45 * Will respect the memory limit if it is higher, thus allowing
46 * settings in php.ini, apache conf or command line switches
47 * to override it
48 *
49 * The memory limit should be expressed with a string (eg:'64M')
50 *
51 * @param string $newlimit the new memory limit
52 * @return bool
53 */
54function raise_memory_limit ($newlimit) {
55
56 if (empty($newlimit)) {
57 return false;
58 }
59
60 $cur = @ini_get('memory_limit');
61 if (empty($cur)) {
62 // if php is compiled without --enable-memory-limits
63 // apparently memory_limit is set to ''
64 $cur=0;
65 } else {
66 if ($cur == -1){
67 return true; // unlimited mem!
68 }
69 $cur = get_real_size($cur);
70 }
71
72 $new = get_real_size($newlimit);
73 if ($new > $cur) {
74 ini_set('memory_limit', $newlimit);
75 return true;
76 }
77 return false;
78}
79
80/**
81 * Converts numbers like 10M into bytes.
82 *
83 * @param mixed $size The size to be converted
84 * @return mixed
85 */
86function get_real_size($size=0) {
87 if (!$size) {
88 return 0;
89 }
90 $scan['MB'] = 1048576;
91 $scan['Mb'] = 1048576;
92 $scan['M'] = 1048576;
93 $scan['m'] = 1048576;
94 $scan['KB'] = 1024;
95 $scan['Kb'] = 1024;
96 $scan['K'] = 1024;
97 $scan['k'] = 1024;
98
99 while (list($key) = each($scan)) {
100 if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) {
101 $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key];
102 break;
103 }
104 }
105 return $size;
106}
107
d3f9f1f8 108/**
109 * Create a directory.
110 *
111 * @uses $CFG
112 * @param string $directory a string of directory names under $CFG->dataroot eg stuff/assignment/1
113 * param bool $shownotices If true then notification messages will be printed out on error.
114 * @return string|false Returns full path to directory if successful, false if not
115 */
116function make_upload_directory($directory, $shownotices=true) {
117
118 global $CFG;
119
120 $currdir = $CFG->dataroot;
121
122 umask(0000);
123
124 if (!file_exists($currdir)) {
125 if (! mkdir($currdir, $CFG->directorypermissions)) {
126 if ($shownotices) {
127 echo '<div class="notifyproblem" align="center">ERROR: You need to create the directory '.
128 $currdir .' with web server write access</div>'."<br />\n";
129 }
130 return false;
131 }
d100b8a0 132 }
133
134 // Make sure a .htaccess file is here, JUST IN CASE the files area is in the open
135 if (!file_exists($currdir.'/.htaccess')) {
d3f9f1f8 136 if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety
d389b128 137 @fwrite($handle, "deny from all\r\nAllowOverride None\r\n");
d3f9f1f8 138 @fclose($handle);
139 }
140 }
141
142 $dirarray = explode('/', $directory);
143
144 foreach ($dirarray as $dir) {
145 $currdir = $currdir .'/'. $dir;
146 if (! file_exists($currdir)) {
147 if (! mkdir($currdir, $CFG->directorypermissions)) {
148 if ($shownotices) {
149 echo '<div class="notifyproblem" align="center">ERROR: Could not find or create a directory ('.
150 $currdir .')</div>'."<br />\n";
151 }
152 return false;
153 }
154 //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it
155 }
156 }
157
158 return $currdir;
159}
160
7104d80d 161/**
162 * This function will introspect inside DB to detect it it's a UTF-8 DB or no
163 * Used from setup.php to set correctly "set names" when the installation
164 * process is performed without the initial and beautiful installer
165 */
166function setup_is_unicodedb() {
167
7ddf8aa5 168 global $CFG, $db, $INSTALL;
7104d80d 169
170 $unicodedb = false;
7ddf8aa5 171
bb48a537 172 // Calculate $CFG->dbfamily
173 $dbfamily = set_dbfamily();
7104d80d 174
bb48a537 175 switch ($dbfamily) {
7104d80d 176 case 'mysql':
d4775d54 177 $rs = $db->Execute("SHOW LOCAL VARIABLES LIKE 'character_set_database'");
03cedd62 178 if ($rs && !$rs->EOF) { // rs_EOF() not available yet
7104d80d 179 $records = $rs->GetAssoc(true);
180 $encoding = $records['character_set_database']['Value'];
181 if (strtoupper($encoding) == 'UTF8') {
182 $unicodedb = true;
183 }
184 }
185 break;
bb48a537 186 case 'postgres':
7104d80d 187 /// Get PostgreSQL server_encoding value
188 $rs = $db->Execute("SHOW server_encoding");
03cedd62 189 if ($rs && !$rs->EOF) { // rs_EOF() not available yet
7104d80d 190 $encoding = $rs->fields['server_encoding'];
2544a80d 191 if (strtoupper($encoding) == 'UNICODE' || strtoupper($encoding) == 'UTF8') {
7104d80d 192 $unicodedb = true;
193 }
194 }
195 break;
bf052299 196 case 'mssql':
197 /// MSSQL only runs under UTF8 + the proper ODBTP driver (both for Unix and Win32)
198 $unicodedb = true;
010f2a39 199 break;
bb48a537 200 case 'oracle':
0700134d 201 /// Get Oracle DB character set value
202 $rs = $db->Execute("SELECT parameter, value FROM nls_database_parameters where parameter = 'NLS_CHARACTERSET'");
03cedd62 203 if ($rs && !$rs->EOF) { // rs_EOF() not available yet
0700134d 204 $encoding = $rs->fields['value'];
205 if (strtoupper($encoding) == 'AL32UTF8') {
206 $unicodedb = true;
207 }
208 }
209 break;
7104d80d 210 }
211 return $unicodedb;
212}
213
1d5c4004 214/**
215 * This internal function sets and returns the proper value for $CFG->dbfamily based on $CFG->dbtype
aaa291bc 216 * It's called by preconfigure_dbconnection() and at install time. Shouldn't be used
1d5c4004 217 * in other places. Code should rely on dbfamily to perform conditional execution
218 * instead of using dbtype directly. This allows quicker adoption of different
219 * drivers going against the same DB backend.
220 *
221 * This function must contain the init code needed for each dbtype supported.
222 *
223 * return string dbfamily value (mysql, postgres, oracle, mssql)
224 */
225function set_dbfamily() {
226
227 global $CFG, $INSTALL;
228
229 // Since this function is also used during installation process, i.e. during install.php before $CFG->dbtype is set.
230 // we need to get dbtype from the right variable
231 if (!empty($INSTALL['dbtype'])) {
232 $dbtype = $INSTALL['dbtype'];
233 } else {
234 $dbtype = $CFG->dbtype;
235 }
236
237 switch ($dbtype) {
238 case 'mysql':
239 case 'mysqli':
240 $CFG->dbfamily='mysql';
241 break;
242 case 'postgres7':
243 $CFG->dbfamily='postgres';
244 break;
245 case 'mssql':
246 case 'mssql_n':
247 case 'odbc_mssql':
248 $CFG->dbfamily='mssql';
249 break;
250 case 'oci8po':
251 $CFG->dbfamily='oracle';
252 break;
253 }
254
255 return $CFG->dbfamily;
256}
257
118e0116 258/**
259 * This internal function, called from setup.php BEFORE stabilishing the DB
260 * connection, defines the $CFG->dbfamily global -by calling set_dbfamily()-
261 * and predefines some constants needed by ADOdb to switch some default
262 * behaviours.
263 *
264 * This function must contain all the pre-connection code needed for each
265 * dbtype supported.
266 */
267function preconfigure_dbconnection() {
268
2f13f94c 269 if (defined('ADODB_ASSOC_CASE')) {
270 return; // when in cli mode, it's possible for this to be called twice (eg cli installer)
271 }
118e0116 272 global $CFG;
273
274/// Define dbfamily
275 set_dbfamily();
276
277/// Based on $CFG->dbfamily, set some ADOdb settings
278 switch ($CFG->dbfamily) {
279 /// list here family types where we know
280 /// the fieldnames will come in lowercase
281 /// so we can avoid expensive tolower()
282 case 'postgres':
283 case 'mysql':
284 case 'mssql':
285 define ('ADODB_ASSOC_CASE', 2);
286 break;
287 case 'oracle':
288 define ('ADODB_ASSOC_CASE', 0); /// Use lowercase fieldnames for ADODB_FETCH_ASSOC
289 /// (only meaningful for oci8po, it's the default
290 /// for other DB drivers so this won't affect them)
291 /// Row prefetching uses a bit of memory but saves a ton
292 /// of network latency. With current AdoDB and PHP, only
293 /// Oracle uses this setting.
294 define ('ADODB_PREFETCH_ROWS', 1000);
295 break;
296 default:
297 /// if we have to lowercase it, set to 0
298 /// - note that the lowercasing is very expensive
299 define ('ADODB_ASSOC_CASE', 0); //Use lowercase fieldnames for ADODB_FETCH_ASSOC
300 }
301}
302
419e1d93 303function init_memcached() {
304 global $CFG, $MCACHE;
305
f917d0ea 306 include_once($CFG->libdir . '/memcached.class.php');
307 $MCACHE = new memcached;
308 if ($MCACHE->status()) {
309 return true;
310 }
311 unset($MCACHE);
312 return false;
419e1d93 313}
314
2142d492 315function init_eaccelerator() {
316 global $CFG, $MCACHE;
317
318 include_once($CFG->libdir . '/eaccelerator.class.php');
319 $MCACHE = new eaccelerator;
f917d0ea 320 if ($MCACHE->status()) {
2142d492 321 return true;
322 }
323 unset($MCACHE);
324 return false;
325}
326
327
328
d3f9f1f8 329?>