205
+ − 1
<?php
+ − 2
+ − 3
/*
+ − 4
* Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
536
+ − 5
* Version 1.1.4 (Caoineag alpha 4)
+ − 6
* Copyright (C) 2006-2008 Dan Fuhry
205
+ − 7
*
+ − 8
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
+ − 9
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
+ − 10
*
+ − 11
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
+ − 12
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
+ − 13
*/
+ − 14
+ − 15
/**
+ − 16
* Language class - processes, stores, and retrieves language strings.
+ − 17
* @package Enano
+ − 18
* @subpackage Localization
+ − 19
* @copyright 2007 Dan Fuhry
+ − 20
* @license GNU General Public License
+ − 21
*/
+ − 22
+ − 23
class Language
+ − 24
{
+ − 25
+ − 26
/**
+ − 27
* The numerical ID of the loaded language.
+ − 28
* @var int
+ − 29
*/
+ − 30
+ − 31
var $lang_id;
+ − 32
+ − 33
/**
+ − 34
* The ISO-639-3 code for the loaded language. This should be grabbed directly from the database.
+ − 35
* @var string
+ − 36
*/
+ − 37
+ − 38
var $lang_code;
210
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 39
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 40
/**
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 41
* Used to track when a language was last changed, to allow browsers to cache language data
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 42
* @var int
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 43
*/
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 44
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 45
var $lang_timestamp;
205
+ − 46
+ − 47
/**
+ − 48
* Will be an object that holds an instance of the class configured with the site's default language. Only instanciated when needed.
+ − 49
* @var object
+ − 50
*/
+ − 51
+ − 52
var $default;
+ − 53
+ − 54
/**
+ − 55
* The list of loaded strings.
+ − 56
* @var array
+ − 57
* @access private
+ − 58
*/
+ − 59
+ − 60
var $strings = array();
+ − 61
+ − 62
/**
504
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 63
* Switch for debug mode. If true, will show an asterisk after localized strings. This
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 64
* can be useful if you're localizing a component and need to see what's already done.
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 65
* @var bool
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 66
*/
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 67
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 68
var $debug = false;
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 69
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 70
/**
205
+ − 71
* Constructor.
+ − 72
* @param int|string Language ID or code to load.
+ − 73
*/
+ − 74
+ − 75
function __construct($lang)
+ − 76
{
+ − 77
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 78
616
e311f5e6f904
Got the installer working. Fixed a few bugs including a nasty-to-debug issue where the lang_id was being hardcoded during installation, resulting in strings being inserted with the wrong lang_id causing an infinfinite loop with fetch() throwing a "no strings" error and using template (which calls fetch()) to complain
Dan
diff
changeset
+ − 79
if ( defined('IN_ENANO_INSTALL') && !defined('ENANO_CONFIG_FETCHED') )
205
+ − 80
{
616
e311f5e6f904
Got the installer working. Fixed a few bugs including a nasty-to-debug issue where the lang_id was being hardcoded during installation, resulting in strings being inserted with the wrong lang_id causing an infinfinite loop with fetch() throwing a "no strings" error and using template (which calls fetch()) to complain
Dan
diff
changeset
+ − 81
// special case for the Enano installer: it will load its own strings from a JSON file and just use this API for fetching
e311f5e6f904
Got the installer working. Fixed a few bugs including a nasty-to-debug issue where the lang_id was being hardcoded during installation, resulting in strings being inserted with the wrong lang_id causing an infinfinite loop with fetch() throwing a "no strings" error and using template (which calls fetch()) to complain
Dan
diff
changeset
+ − 82
// and templatizing them.
e311f5e6f904
Got the installer working. Fixed a few bugs including a nasty-to-debug issue where the lang_id was being hardcoded during installation, resulting in strings being inserted with the wrong lang_id causing an infinfinite loop with fetch() throwing a "no strings" error and using template (which calls fetch()) to complain
Dan
diff
changeset
+ − 83
// 1.1.4 fix: this was still being called after main API startup from installer payload
243
+ − 84
$this->lang_id = 1;
+ − 85
$this->lang_code = $lang;
205
+ − 86
return true;
+ − 87
}
+ − 88
if ( is_string($lang) )
+ − 89
{
371
dc6026376919
Improved compatibility with PostgreSQL and fixed a number of installer bugs; fixed missing "meta" category declaration in language files
Dan
diff
changeset
+ − 90
$sql_col = 'lang_code=\'' . $db->escape($lang) . '\'';
205
+ − 91
}
+ − 92
else if ( is_int($lang) )
+ − 93
{
+ − 94
$sql_col = 'lang_id=' . $lang . '';
+ − 95
}
+ − 96
else
+ − 97
{
+ − 98
$db->_die('lang.php - attempting to pass invalid value to constructor');
+ − 99
}
+ − 100
616
e311f5e6f904
Got the installer working. Fixed a few bugs including a nasty-to-debug issue where the lang_id was being hardcoded during installation, resulting in strings being inserted with the wrong lang_id causing an infinfinite loop with fetch() throwing a "no strings" error and using template (which calls fetch()) to complain
Dan
diff
changeset
+ − 101
$lang_default = ( $x = getConfig('default_language') ) ? intval($x) : '0';
372
5bd429428101
A number of scattered changes. Profiler added and only enabled in debug mode (currently on), but awfully useful for fixing performance in the future. Started work on Admin:LangManager
Dan
diff
changeset
+ − 102
5bd429428101
A number of scattered changes. Profiler added and only enabled in debug mode (currently on), but awfully useful for fixing performance in the future. Started work on Admin:LangManager
Dan
diff
changeset
+ − 103
$q = $db->sql_query("SELECT lang_id, lang_code, last_changed, ( lang_id = $lang_default ) AS is_default FROM " . table_prefix . "language WHERE $sql_col OR lang_id = $lang_default ORDER BY is_default ASC LIMIT 1;");
205
+ − 104
+ − 105
if ( !$q )
+ − 106
$db->_die('lang.php - main select query');
+ − 107
+ − 108
if ( $db->numrows() < 1 )
+ − 109
$db->_die('lang.php - There are no languages installed');
+ − 110
+ − 111
$row = $db->fetchrow();
+ − 112
+ − 113
$this->lang_id = intval( $row['lang_id'] );
+ − 114
$this->lang_code = $row['lang_code'];
210
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 115
$this->lang_timestamp = $row['last_changed'];
205
+ − 116
}
+ − 117
+ − 118
/**
+ − 119
* Fetches language strings from the database, or a cache file if it's available.
+ − 120
* @param bool If true (default), allows the cache to be used.
+ − 121
*/
+ − 122
+ − 123
function fetch($allow_cache = true)
+ − 124
{
+ − 125
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 126
+ − 127
// Attempt to load the strings from a cache file
607
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 128
$loaded = false;
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 129
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 130
if ( $allow_cache )
205
+ − 131
{
607
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 132
// Load the cache manager
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 133
global $cache;
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 134
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 135
if ( $cached = $cache->fetch("lang_{$this->lang_id}") )
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 136
{
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 137
$this->merge($cached);
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 138
$loaded = true;
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 139
}
205
+ − 140
}
607
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 141
if ( !$loaded )
205
+ − 142
{
+ − 143
// No cache file - select and retrieve from the database
+ − 144
$q = $db->sql_unbuffered_query("SELECT string_category, string_name, string_content FROM " . table_prefix . "language_strings WHERE lang_id = {$this->lang_id};");
+ − 145
if ( !$q )
+ − 146
$db->_die('lang.php - selecting language string data');
+ − 147
if ( $row = $db->fetchrow() )
+ − 148
{
+ − 149
$strings = array();
+ − 150
do
+ − 151
{
+ − 152
$cat =& $row['string_category'];
371
dc6026376919
Improved compatibility with PostgreSQL and fixed a number of installer bugs; fixed missing "meta" category declaration in language files
Dan
diff
changeset
+ − 153
if ( !is_array(@$strings[$cat]) )
205
+ − 154
{
+ − 155
$strings[$cat] = array();
+ − 156
}
+ − 157
$strings[$cat][ $row['string_name'] ] = $row['string_content'];
+ − 158
}
+ − 159
while ( $row = $db->fetchrow() );
+ − 160
// all done fetching
+ − 161
$this->merge($strings);
607
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 162
$this->regen_caches(false);
205
+ − 163
}
+ − 164
else
+ − 165
{
241
c671f3bb8aed
Trying to get lang import to work in the installer; it's not working ATM - cache file is generated with lang_id = 0. Syncing to Nighthawk.
Dan
diff
changeset
+ − 166
if ( !defined('ENANO_ALLOW_LOAD_NOLANG') )
c671f3bb8aed
Trying to get lang import to work in the installer; it's not working ATM - cache file is generated with lang_id = 0. Syncing to Nighthawk.
Dan
diff
changeset
+ − 167
$db->_die('lang.php - No strings for language ' . $this->lang_code);
205
+ − 168
}
+ − 169
}
+ − 170
}
+ − 171
+ − 172
/**
+ − 173
* Loads a file from the disk cache (treated as PHP) and merges it into RAM.
+ − 174
* @param string File to load
+ − 175
*/
+ − 176
+ − 177
function load_cache_file($file)
+ − 178
{
+ − 179
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 180
+ − 181
if ( !file_exists($file) )
+ − 182
$db->_die('lang.php - requested cache file doesn\'t exist');
+ − 183
590
03a60844c7c5
Several optimization changes including getting rid of a few eval()s. Added placeholder functions for the theme manager, which should be working now
Dan
diff
changeset
+ − 184
@include($file);
03a60844c7c5
Several optimization changes including getting rid of a few eval()s. Added placeholder functions for the theme manager, which should be working now
Dan
diff
changeset
+ − 185
205
+ − 186
if ( !isset($lang_cache) || ( isset($lang_cache) && !is_array($lang_cache) ) )
+ − 187
$db->_die('lang.php - the cache file is invalid (didn\'t set $lang_cache as an array)');
+ − 188
+ − 189
$this->merge($lang_cache);
+ − 190
}
+ − 191
+ − 192
/**
243
+ − 193
* Loads a JSON language file and parses the strings into RAM. Will use the cache if possible, but stays far away from the database,
+ − 194
* which we assume doesn't exist yet.
+ − 195
*/
+ − 196
+ − 197
function load_file($file)
+ − 198
{
+ − 199
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 200
+ − 201
if ( !file_exists($file) )
348
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 202
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 203
if ( defined('IN_ENANO_INSTALL') )
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 204
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 205
die('lang.php - requested JSON file (' . htmlspecialchars($file) . ') doesn\'t exist');
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 206
}
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 207
else
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 208
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 209
$db->_die('lang.php - requested JSON file doesn\'t exist');
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 210
}
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 211
}
243
+ − 212
+ − 213
$contents = trim(@file_get_contents($file));
+ − 214
if ( empty($contents) )
+ − 215
$db->_die('lang.php - empty language file...');
+ − 216
+ − 217
// Trim off all text before and after the starting and ending braces
+ − 218
$contents = preg_replace('/^([^{]+)\{/', '{', $contents);
+ − 219
$contents = preg_replace('/\}([^}]+)$/', '}', $contents);
+ − 220
$contents = trim($contents);
+ − 221
+ − 222
if ( empty($contents) )
+ − 223
$db->_die('lang.php - no meat to the language file...');
+ − 224
+ − 225
$checksum = md5($contents);
+ − 226
if ( file_exists("./cache/lang_json_{$checksum}.php") )
+ − 227
{
+ − 228
$this->load_cache_file("./cache/lang_json_{$checksum}.php");
+ − 229
}
+ − 230
else
+ − 231
{
348
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 232
// Correct syntax to be nice to the json parser
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 233
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 234
// eliminate comments
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 235
$contents = preg_replace(array(
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 236
// eliminate single line comments in '// ...' form
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 237
'#^\s*//(.+)$#m',
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 238
// eliminate multi-line comments in '/* ... */' form, at start of string
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 239
'#^\s*/\*(.+)\*/#Us',
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 240
// eliminate multi-line comments in '/* ... */' form, at end of string
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 241
'#/\*(.+)\*/\s*$#Us'
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 242
), '', $contents);
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 243
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 244
$contents = preg_replace('/([,\{\[])([\s]*?)([a-z0-9_]+)([\s]*?):/', '\\1\\2"\\3" :', $contents);
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 245
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 246
try
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 247
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 248
$langdata = enano_json_decode($contents);
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 249
}
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 250
catch(Zend_Json_Exception $e)
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 251
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 252
$db->_die('lang.php - Exception caught by JSON parser</p><pre>' . htmlspecialchars(print_r($e, true)) . '</pre><p>');
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 253
exit;
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 254
}
243
+ − 255
+ − 256
if ( !is_array($langdata) )
+ − 257
$db->_die('lang.php - invalid language file');
+ − 258
+ − 259
if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
+ − 260
$db->_die('lang.php - language file does not contain the proper items');
+ − 261
+ − 262
$this->merge($langdata['strings']);
+ − 263
+ − 264
$lang_file = "./cache/lang_json_{$checksum}.php";
+ − 265
+ − 266
$handle = @fopen($lang_file, 'w');
+ − 267
if ( !$handle )
+ − 268
// Couldn't open the file. Silently fail and let the strings come from RAM.
+ − 269
return false;
+ − 270
+ − 271
// The file's open, that means we should be good.
+ − 272
fwrite($handle, '<?php
+ − 273
// This file was generated automatically by Enano. You should not edit this file because any changes you make
+ − 274
// to it will not be visible in the ACP and all changes will be lost upon any changes to strings in the admin panel.
+ − 275
+ − 276
$lang_cache = ');
+ − 277
+ − 278
$exported = $this->var_export_string($this->strings);
+ − 279
if ( empty($exported) )
+ − 280
// Ehh, that's not good
+ − 281
$db->_die('lang.php - load_file(): var_export_string() failed');
+ − 282
+ − 283
fwrite($handle, $exported . '; ?>');
+ − 284
+ − 285
// Clean up
+ − 286
unset($exported, $langdata);
+ − 287
+ − 288
// Done =)
+ − 289
fclose($handle);
+ − 290
}
+ − 291
}
+ − 292
+ − 293
/**
205
+ − 294
* Merges a standard language assoc array ($arr[cat][stringid]) with the master in RAM.
+ − 295
* @param array
+ − 296
*/
+ − 297
+ − 298
function merge($strings)
+ − 299
{
+ − 300
// This is stupidly simple.
+ − 301
foreach ( $strings as $cat_id => $contents )
+ − 302
{
243
+ − 303
if ( !isset($this->strings[$cat_id]) || ( isset($this->strings[$cat_id]) && !is_array($this->strings[$cat_id]) ) )
205
+ − 304
$this->strings[$cat_id] = array();
+ − 305
foreach ( $contents as $string_id => $string )
+ − 306
{
+ − 307
$this->strings[$cat_id][$string_id] = $string;
+ − 308
}
+ − 309
}
+ − 310
}
+ − 311
+ − 312
/**
+ − 313
* Imports a JSON-format language file into the database and merges with current strings.
+ − 314
* @param string Path to the JSON file to load
514
+ − 315
* @param bool Enable debugging output, makes the process over CLI more interesting
205
+ − 316
*/
+ − 317
514
+ − 318
function import($file, $debug = false)
205
+ − 319
{
+ − 320
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 321
+ − 322
if ( !file_exists($file) )
+ − 323
$db->_die('lang.php - can\'t import language file: string file doesn\'t exist');
+ − 324
241
c671f3bb8aed
Trying to get lang import to work in the installer; it's not working ATM - cache file is generated with lang_id = 0. Syncing to Nighthawk.
Dan
diff
changeset
+ − 325
if ( $this->lang_id == 0 )
c671f3bb8aed
Trying to get lang import to work in the installer; it's not working ATM - cache file is generated with lang_id = 0. Syncing to Nighthawk.
Dan
diff
changeset
+ − 326
$db->_die('lang.php - BUG: trying to perform import when $lang->lang_id == 0');
c671f3bb8aed
Trying to get lang import to work in the installer; it's not working ATM - cache file is generated with lang_id = 0. Syncing to Nighthawk.
Dan
diff
changeset
+ − 327
514
+ − 328
if ( $debug )
516
+ − 329
$br = ( isset($_SERVER['REQUEST_URI']) ) ? '<br />' : '';
+ − 330
+ − 331
if ( $debug )
+ − 332
echo "Importing file: $file$br\n Checking file...$br\n";
514
+ − 333
205
+ − 334
$contents = trim(@file_get_contents($file));
+ − 335
+ − 336
if ( empty($contents) )
+ − 337
$db->_die('lang.php - can\'t load the contents of the language file');
+ − 338
514
+ − 339
if ( $debug )
516
+ − 340
echo " Cleaning up JSON$br\n";
514
+ − 341
205
+ − 342
// Trim off all text before and after the starting and ending braces
+ − 343
$contents = preg_replace('/^([^{]+)\{/', '{', $contents);
+ − 344
$contents = preg_replace('/\}([^}]+)$/', '}', $contents);
+ − 345
334
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 346
// Correct syntax to be nice to the json parser
372
5bd429428101
A number of scattered changes. Profiler added and only enabled in debug mode (currently on), but awfully useful for fixing performance in the future. Started work on Admin:LangManager
Dan
diff
changeset
+ − 347
$contents = enano_clean_json($contents);
334
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 348
514
+ − 349
if ( $debug )
516
+ − 350
echo " Decoding JSON stream$br\n";
514
+ − 351
334
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 352
try
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 353
{
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 354
$langdata = enano_json_decode($contents);
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 355
}
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 356
catch(Zend_Json_Exception $e)
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 357
{
335
67bd3121a12e
Replaced TinyMCE 2.x with 3.0 beta 3. Supports everything but IE. Also rewrote the editor interface completely from the ground up.
Dan
diff
changeset
+ − 358
$db->_die('lang.php - Exception caught by JSON parser</p><pre>' . htmlspecialchars(print_r($e, true)) . '</pre><p>');
334
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 359
exit;
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 360
}
205
+ − 361
+ − 362
if ( !is_array($langdata) )
334
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 363
{
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 364
$db->_die('lang.php - invalid or non-well-formed language file');
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 365
}
205
+ − 366
514
+ − 367
if ( $debug )
516
+ − 368
echo " Starting string import$br\n";
514
+ − 369
+ − 370
return $this->import_array($langdata, $debug);
504
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 371
}
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 372
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 373
/**
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 374
* Imports a JSON-format language file into the database and merges with current strings.
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 375
* @param string Path to plugin file
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 376
*/
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 377
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 378
function import_plugin($file)
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 379
{
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 380
global $db, $session, $paths, $template, $plugins; // Common objects
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 381
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 382
if ( !file_exists($file) )
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 383
$db->_die('lang.php - can\'t import language file: string file doesn\'t exist');
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 384
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 385
if ( $this->lang_id == 0 )
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 386
$db->_die('lang.php - BUG: trying to perform import when $lang->lang_id == 0');
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 387
507
586fd7d3202d
Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
diff
changeset
+ − 388
$block = pluginLoader::parse_plugin_blocks($file, 'language');
586fd7d3202d
Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
diff
changeset
+ − 389
if ( !is_array($block) )
586fd7d3202d
Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
diff
changeset
+ − 390
return false;
586fd7d3202d
Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
diff
changeset
+ − 391
if ( !isset($block[0]) )
586fd7d3202d
Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
diff
changeset
+ − 392
return false;
504
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 393
507
586fd7d3202d
Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
diff
changeset
+ − 394
$contents =& $block[0]['value'];
504
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 395
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 396
// Trim off all text before and after the starting and ending braces
519
94214ec0871c
Started work on the new plugin manager and associated management code. Very incomplete at this point and not usable.
Dan
diff
changeset
+ − 397
$contents = enano_trim_json($contents);
504
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 398
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 399
// Correct syntax to be nice to the json parser
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 400
$contents = enano_clean_json($contents);
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 401
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 402
try
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 403
{
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 404
$langdata = enano_json_decode($contents);
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 405
}
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 406
catch(Zend_Json_Exception $e)
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 407
{
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 408
$db->_die('lang.php - Exception caught by JSON parser</p><pre>' . htmlspecialchars(print_r($e, true)) . '</pre><p>');
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 409
exit;
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 410
}
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 411
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 412
if ( !is_array($langdata) )
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 413
{
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 414
$db->_die('lang.php - invalid or non-well-formed language file');
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 415
}
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 416
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 417
// Does the plugin support the current language?
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 418
if ( isset($langdata[$this->lang_code]) )
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 419
{
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 420
// Yes, import that
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 421
return $this->import_array($langdata[$this->lang_code]);
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 422
}
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 423
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 424
// Just import the first language we run across.
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 425
$supported_langs = array_keys($langdata);
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 426
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 427
if ( !isset($supported_langs[0]) )
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 428
{
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 429
$db->_die('lang.php - plugin has an invalid or corrupt language block');
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 430
}
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 431
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 432
$first_lang = $supported_langs[0];
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 433
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 434
return $this->import_array($langdata[$first_lang]);
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 435
}
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 436
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 437
/**
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 438
* Performs the actual import of string data.
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 439
* @param array Parsed JSON object, should be in the form of an array
514
+ − 440
* @param bool Enable debugging output
504
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 441
* @access private
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 442
*/
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 443
514
+ − 444
protected function import_array($langdata, $debug = false)
504
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 445
{
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 446
global $db, $session, $paths, $template, $plugins; // Common objects
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 447
205
+ − 448
if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
+ − 449
$db->_die('lang.php - language file does not contain the proper items');
+ − 450
516
+ − 451
if ( $debug )
+ − 452
$br = ( isset($_SERVER['REQUEST_URI']) ) ? '<br />' : '';
+ − 453
205
+ − 454
$insert_list = array();
+ − 455
$delete_list = array();
+ − 456
+ − 457
foreach ( $langdata['categories'] as $category )
+ − 458
{
+ − 459
if ( isset($langdata['strings'][$category]) )
+ − 460
{
514
+ − 461
if ( $debug )
+ − 462
{
+ − 463
$desc = ( isset($langdata['strings']['meta'][$category]) ) ? $langdata['strings']['meta'][$category] : $this->get("meta_$category");
516
+ − 464
echo " Indexing category: $category ({$desc})$br\n";
514
+ − 465
}
205
+ − 466
foreach ( $langdata['strings'][$category] as $string_name => $string_value )
+ − 467
{
+ − 468
$string_name = $db->escape($string_name);
+ − 469
$string_value = $db->escape($string_value);
+ − 470
$category_name = $db->escape($category);
+ − 471
$insert_list[] = "({$this->lang_id}, '$category_name', '$string_name', '$string_value')";
+ − 472
$delete_list[] = "( lang_id = {$this->lang_id} AND string_category = '$category_name' AND string_name = '$string_name' )";
+ − 473
}
+ − 474
}
+ − 475
}
+ − 476
514
+ − 477
if ( $debug )
+ − 478
{
+ − 479
echo " Running deletion of old strings...";
+ − 480
$start = microtime_float();
+ − 481
}
205
+ − 482
$delete_list = implode(" OR\n ", $delete_list);
+ − 483
377
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 484
if ( !empty($delete_list) )
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 485
{
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 486
$sql = "DELETE FROM " . table_prefix . "language_strings WHERE $delete_list;";
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 487
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 488
// Free some memory
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 489
unset($delete_list);
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 490
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 491
// Run the query
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 492
$q = $db->sql_query($sql);
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 493
if ( !$q )
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 494
$db->_die('lang.php - couldn\'t kill off them old strings');
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 495
}
205
+ − 496
514
+ − 497
if ( $debug )
+ − 498
{
+ − 499
$time = round(microtime_float() - $start, 5);
516
+ − 500
echo "({$time}s)$br\n";
514
+ − 501
}
+ − 502
377
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 503
if ( !empty($insert_list) )
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 504
{
514
+ − 505
if ( $debug )
+ − 506
{
+ − 507
echo " Inserting strings...";
+ − 508
$start = microtime_float();
+ − 509
}
377
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 510
$insert_list = implode(",\n ", $insert_list);
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 511
$sql = "INSERT INTO " . table_prefix . "language_strings(lang_id, string_category, string_name, string_content) VALUES\n $insert_list;";
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 512
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 513
// Free some memory
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 514
unset($insert_list);
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 515
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 516
// Run the query
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 517
$q = $db->sql_query($sql);
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 518
if ( !$q )
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 519
$db->_die('lang.php - couldn\'t insert strings in import()');
514
+ − 520
+ − 521
if ( $debug )
+ − 522
{
+ − 523
$time = round(microtime_float() - $start, 5);
516
+ − 524
echo "({$time}s)$br\n";
514
+ − 525
}
377
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 526
}
205
+ − 527
+ − 528
// YAY! done!
+ − 529
// This will regenerate the cache file if possible.
514
+ − 530
if ( $debug )
516
+ − 531
echo " Regenerating cache file$br\n";
205
+ − 532
$this->regen_caches();
555
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 533
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 534
return true;
205
+ − 535
}
+ − 536
+ − 537
/**
+ − 538
* Refetches the strings and writes out the cache file.
+ − 539
*/
+ − 540
607
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 541
function regen_caches($refetch = true)
205
+ − 542
{
+ − 543
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 544
+ − 545
// Refresh the strings in RAM to the latest copies in the DB
607
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 546
if ( $refetch )
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 547
$this->fetch(false);
205
+ − 548
607
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 549
// Load the cache manager
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 550
global $cache;
205
+ − 551
607
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 552
// Store it
935f3799b654
First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
diff
changeset
+ − 553
$cache->store("lang_{$this->lang_id}", $this->strings, -1);
205
+ − 554
210
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 555
// Update timestamp in database
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 556
$q = $db->sql_query('UPDATE ' . table_prefix . 'language SET last_changed = ' . time() . ' WHERE lang_id = ' . $this->lang_id . ';');
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 557
if ( !$q )
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 558
$db->_die('lang.php - updating timestamp on language');
613
+ − 559
+ − 560
return true;
205
+ − 561
}
+ − 562
+ − 563
/**
+ − 564
* Calls var_export() on whatever, and returns the function's output.
+ − 565
* @param mixed Whatever you want var_exported. Usually an array.
+ − 566
* @return string
+ − 567
*/
+ − 568
590
03a60844c7c5
Several optimization changes including getting rid of a few eval()s. Added placeholder functions for the theme manager, which should be working now
Dan
diff
changeset
+ − 569
static function var_export_string($val)
205
+ − 570
{
+ − 571
ob_start();
+ − 572
var_export($val);
+ − 573
$contents = ob_get_contents();
+ − 574
ob_end_clean();
+ − 575
return $contents;
+ − 576
}
+ − 577
+ − 578
/**
+ − 579
* Fetches a language string from the cache in RAM. If it isn't there, it will call fetch() again and then try. If it still can't find it, it will ask for the string
+ − 580
* in the default language. If even then the string can't be found, this function will return what was passed to it.
+ − 581
*
+ − 582
* This will also templatize strings. If a string contains variables in the format %foo%, you may specify the second parameter as an associative array in the format
+ − 583
* of 'foo' => 'foo substitute'.
+ − 584
*
+ − 585
* @param string ID of the string to fetch. This will always be in the format of category_stringid.
+ − 586
* @param array Optional. Associative array of substitutions.
+ − 587
* @return string
+ − 588
*/
+ − 589
+ − 590
function get($string_id, $substitutions = false)
+ − 591
{
376
+ − 592
if ( !is_array($substitutions) )
+ − 593
$substitutions = array();
536
+ − 594
// if this isn't a valid language string ID, just return the string unprocessed.
+ − 595
if ( !preg_match('/^([a-z0-9]+)((_[a-z0-9]+)+)$/', $string_id) )
+ − 596
return $string_id;
376
+ − 597
return $this->substitute($this->get_uncensored($string_id), $substitutions);
+ − 598
}
+ − 599
+ − 600
/**
+ − 601
* The same as get(), but does not perform any substitution or filtering. Used in get() (of course) and in the admin panel, where
+ − 602
* strings are updated only if they were changed.
+ − 603
*
+ − 604
* @param string ID of the string to fetch. This will always be in the format of category_stringid.
+ − 605
* @param array Optional. Associative array of substitutions.
+ − 606
* @return string
+ − 607
*/
+ − 608
+ − 609
function get_uncensored($string_id, $substitutions = false)
+ − 610
{
616
e311f5e6f904
Got the installer working. Fixed a few bugs including a nasty-to-debug issue where the lang_id was being hardcoded during installation, resulting in strings being inserted with the wrong lang_id causing an infinfinite loop with fetch() throwing a "no strings" error and using template (which calls fetch()) to complain
Dan
diff
changeset
+ − 611
global $db, $session, $paths, $template, $plugins; // Common objects
e311f5e6f904
Got the installer working. Fixed a few bugs including a nasty-to-debug issue where the lang_id was being hardcoded during installation, resulting in strings being inserted with the wrong lang_id causing an infinfinite loop with fetch() throwing a "no strings" error and using template (which calls fetch()) to complain
Dan
diff
changeset
+ − 612
205
+ − 613
// Extract the category and string ID
+ − 614
$category = substr($string_id, 0, ( strpos($string_id, '_') ));
+ − 615
$string_name = substr($string_id, ( strpos($string_id, '_') + 1 ));
+ − 616
$found = false;
+ − 617
if ( isset($this->strings[$category]) && isset($this->strings[$category][$string_name]) )
+ − 618
{
+ − 619
$found = true;
+ − 620
$string = $this->strings[$category][$string_name];
+ − 621
}
+ − 622
if ( !$found )
+ − 623
{
+ − 624
// Ehh, the string wasn't found. Rerun fetch() and try again.
536
+ − 625
// Or if it's the installer, no use in refetching, so just fail.
616
e311f5e6f904
Got the installer working. Fixed a few bugs including a nasty-to-debug issue where the lang_id was being hardcoded during installation, resulting in strings being inserted with the wrong lang_id causing an infinfinite loop with fetch() throwing a "no strings" error and using template (which calls fetch()) to complain
Dan
diff
changeset
+ − 626
if ( defined('IN_ENANO_INSTALL') || ( defined('ENANO_INSTALLED') && !@$db->_conn ) )
243
+ − 627
{
+ − 628
return $string_id;
+ − 629
}
205
+ − 630
$this->fetch();
593
4f9bec0d65c1
More optimization work. Moved special page init functions to common instead of common_post hook. Allowed paths to cache page metadata on filesystem. Phased out the redundancy in $paths->pages that paired a number with every urlname as foreach loops are allowed now (and have been for some time). Fixed missing includes for several functions. Rewrote str_replace_once to be a lot more efficient.
Dan
diff
changeset
+ − 631
profiler_log('Language(' . $this->lang_code . '): refetched due to missing string: ' . $string_id);
205
+ − 632
if ( isset($this->strings[$category]) && isset($this->strings[$category][$string_name]) )
+ − 633
{
+ − 634
$found = true;
+ − 635
$string = $this->strings[$category][$string_name];
+ − 636
}
+ − 637
if ( !$found )
+ − 638
{
+ − 639
// STILL not found. Check the default language.
+ − 640
$lang_default = ( $x = getConfig('default_language') ) ? intval($x) : $this->lang_id;
+ − 641
if ( $lang_default != $this->lang_id )
+ − 642
{
+ − 643
if ( !is_object($this->default) )
+ − 644
$this->default = new Language($lang_default);
376
+ − 645
return $this->default->get_uncensored($string_id);
205
+ − 646
}
+ − 647
}
+ − 648
}
+ − 649
if ( !$found )
+ − 650
{
+ − 651
// Alright, it's nowhere. Return the input, grumble grumble...
+ − 652
return $string_id;
+ − 653
}
+ − 654
// Found it!
376
+ − 655
return $string;
205
+ − 656
}
+ − 657
+ − 658
/**
+ − 659
* Processes substitutions.
+ − 660
* @param string
+ − 661
* @param array
+ − 662
* @return string
+ − 663
*/
+ − 664
+ − 665
function substitute($string, $subs)
+ − 666
{
+ − 667
preg_match_all('/%this\.([a-z0-9_]+)%/', $string, $matches);
+ − 668
if ( count($matches[0]) > 0 )
+ − 669
{
+ − 670
foreach ( $matches[1] as $i => $string_id )
+ − 671
{
+ − 672
$result = $this->get($string_id);
+ − 673
$string = str_replace($matches[0][$i], $result, $string);
+ − 674
}
+ − 675
}
209
+ − 676
preg_match_all('/%config\.([a-z0-9_]+)%/', $string, $matches);
+ − 677
if ( count($matches[0]) > 0 )
+ − 678
{
+ − 679
foreach ( $matches[1] as $i => $string_id )
+ − 680
{
+ − 681
$result = getConfig($string_id);
+ − 682
$string = str_replace($matches[0][$i], $result, $string);
+ − 683
}
+ − 684
}
205
+ − 685
foreach ( $subs as $key => $value )
+ − 686
{
209
+ − 687
$subs[$key] = strval($value);
+ − 688
$string = str_replace("%{$key}%", "{$subs[$key]}", $string);
205
+ − 689
}
504
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 690
return ( $this->debug ) ? "$string*" : $string;
205
+ − 691
}
+ − 692
+ − 693
} // class Language
+ − 694
+ − 695
?>