/** * Takes ages. * @return string * @todo integrate in design but do flushing and error handling */ public static function createLanguage($__langs = true, $__cunts = true, $__ipmap = false) { $success = true; require_once GWF_CORE_PATH . 'inc/install/data/GWF_LanguageData.php'; set_time_limit(0); # This function takes ages! $cache = array(); $cache2 = array(); # Language $i = 1; $linguas = GWF_LanguageData::getLanguages(); $ret = 'Installing ' . count($linguas) . ' Languages'; // flush(); $lang_t = new GWF_Language(); $supported = explode(';', GWF_SUPPORTED_LANGS); foreach ($linguas as $lang) { // $ret .= '.'; // flush(); array_map('trim', $lang); list($name, $native, $short, $iso) = $lang; if (false !== ($langrow = $lang_t->selectFirst('lang_id', "lang_short='{$short}'"))) { #GWF_Language::getByShort($short))) { $cache[$short] = $langrow['lang_id']; #->getID(); continue; } if ($__langs) { if (false === $lang_t->insertAssoc(array('lang_id' => $i, 'lang_name' => $name, 'lang_nativename' => $native, 'lang_short' => $short, 'lang_iso' => $iso, 'lang_options' => in_array($iso, $supported, true) ? GWF_Language::SUPPORTED : 0))) { $ret .= GWF_HTML::err('ERR_DATABASE', array(__FILE__, __LINE__)); $success = false; continue; } } $i++; $cache[$short] = $i; #langrow['lang_id']; } $ret .= PHP_EOL; # Country and Langmap $countries = GWF_LanguageData::getCountries(); $country_t = new GWF_Country(); $ret .= 'Installing ' . count($countries) . ' Countries'; // flush(); foreach ($countries as $cid => $c) { // $ret .= '.'; // flush(); if (count($c) !== 5) { $ret .= GWF_HTML::error('Country error', sprintf('%s has error.', $c[0]), true, true); } array_map('trim', $c); list($name, $langs, $region, $tld, $pop) = $c; $tld = strtolower($tld); if ($__cunts) { if (false === $country_t->insertAssoc(array('country_id' => $cid, 'country_name' => $name, 'country_tld' => $tld, 'country_pop' => $pop))) { $ret .= GWF_HTML::err('ERR_DATABASE', array(__FILE__, __LINE__), true, true); $success = false; continue; } } $cache2[$tld] = $cid; $langmap_t = new GWF_LangMap(); if ($__cunts) { $langs = explode(':', $langs); foreach ($langs as $langshort) { if (!isset($cache[$langshort])) { $ret .= GWF_HTML::error('', 'Unknown iso-3: ' . $langshort . ' in country ' . $name, true, true); $success = false; $ret .= GWF_HTML::err('ERR_DATABASE', array(__FILE__, __LINE__), true, true); continue; } $langid = $cache[$langshort]; if (false === $langmap_t->insertAssoc(array('langmap_cid' => $cid, 'langmap_lid' => $langid))) { $ret .= GWF_HTML::err('ERR_DATABASE', array(array(__FILE__, __LINE__)), true, true); $success = false; continue; } } } } $ret .= PHP_EOL; if (!$__ipmap) { // return $success; return $ret; } $ret .= 'Installing ip2country' . PHP_EOL; # IP2Country $max = 89323; $now = 0; $filename = GWF_CORE_PATH . "inc/install/data/ip-to-country.csv"; if (false === ($fp = fopen($filename, "r"))) { $ret .= GWF_HTML::err('ERR_FILE_NOT_FOUND', array($filename), true, true); // return false; return $ret; } $ip2c = new GWF_IP2Country(); while (false !== ($line = fgetcsv($fp, 2048))) { if (count($line) !== 5) { $ret .= GWF_HTML::error('', $filename . ' is corrupt!', true, true); $success = false; break; } list($ipstart, $ipend, $tld, $ccode2, $cname) = $line; $tld = strtolower($tld); if (!isset($cache2[$tld])) { $ret .= GWF_HTML::error('', 'Unknown TLD: ' . $tld, true, true); $ret .= GWF_HTML::err('ERR_DATABASE', array(__FILE__, __LINE__), true, true); $success = false; continue; } if (false === $ip2c->insertAssoc(array('ip2c_start' => $ipstart, 'ip2c_end' => $ipend, 'ip2c_cid' => $cache2[$tld]))) { $ret .= GWF_HTML::err('ERR_DATABASE', array(__FILE__, __LINE__), true, true); $success = false; continue; } $now++; if (!($now % 2500)) { $msg = sprintf('%d of %d...', $now, $max); $ret .= GWF_HTML::message('Progress', $msg, true, true); // flush(); } } return $ret; }