From f57e765bb7ef4644a0de7987f0a1ff842762d4ab Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 02:53:53 +0100 Subject: [PATCH 01/19] move methods to JLanguageHelper --- libraries/joomla/language/helper.php | 254 +++++++++++++++++++++---- libraries/joomla/language/language.php | 133 +++---------- 2 files changed, 248 insertions(+), 139 deletions(-) diff --git a/libraries/joomla/language/helper.php b/libraries/joomla/language/helper.php index 25b01dc2eee8d..5be3b84be8eda 100644 --- a/libraries/joomla/language/helper.php +++ b/libraries/joomla/language/helper.php @@ -30,41 +30,25 @@ class JLanguageHelper */ public static function createLanguageList($actualLanguage, $basePath = JPATH_BASE, $caching = false, $installed = false) { - $list = array(); - - // Cache activation - $langs = JLanguage::getKnownLanguages($basePath); + $languages = static::getKnownLanguages($basePath); if ($installed) { - $db = JFactory::getDbo(); - $query = $db->getQuery(true) - ->select('element') - ->from('#__extensions') - ->where('type=' . $db->quote('language')) - ->where('state=0') - ->where('enabled=1') - ->where('client_id=' . ($basePath == JPATH_ADMINISTRATOR ? 1 : 0)); - $db->setQuery($query); - $installed_languages = $db->loadObjectList('element'); - } - - foreach ($langs as $lang => $metadata) - { - if (!$installed || array_key_exists($lang, $installed_languages)) - { - $option = array(); + $installedLanguages = static::getInstalledLanguages($basePath == JPATH_ADMINISTRATOR ? 1 : 0); - $option['text'] = $metadata['name']; - $option['value'] = $lang; + // Remove not installed languages. + $languages = array_intersect_key($languages, $installedLanguages); + } - if ($lang == $actualLanguage) - { - $option['selected'] = 'selected="selected"'; - } + $list = array(); - $list[] = $option; - } + foreach ($languages as $languageCode => $metadata) + { + $list[] = array( + 'text' => $metadata['name'], + 'value' => $languageCode, + 'selected' => $languageCode == $actualLanguage ? 'selected="selected"' : null, + ); } return $list; @@ -82,12 +66,12 @@ public static function detectLanguage() if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $browserLangs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); - $systemLangs = self::getLanguages(); + $systemLangs = static::getLanguages(); foreach ($browserLangs as $browserLang) { // Slice out the part before ; on first step, the part before - on second, place into array - $browserLang = substr($browserLang, 0, strcspn($browserLang, ';')); + $browserLang = substr($browserLang, 0, strcspn($browserLang, ';')); $primary_browserLang = substr($browserLang, 0, 2); foreach ($systemLangs as $systemLang) @@ -137,7 +121,7 @@ public static function getLanguages($key = 'default') if (JFactory::getApplication()->getClientId() == 2) { $languages[$key] = array(); - $knownLangs = JLanguage::getKnownLanguages(JPATH_BASE); + $knownLangs = static::getKnownLanguages(JPATH_BASE); foreach ($knownLangs as $metadata) { @@ -181,4 +165,210 @@ public static function getLanguages($key = 'default') return $languages[$key]; } + + /** + * Get a list of installed languages. + * + * @param integer $clientId The client app id. + * + * @return array Array with the language code and name. + * + * @since __DEPLOY_VERSION__ + */ + public static function getInstalledLanguages($clientId = null) + { + static $installedLanguages = null; + + if (is_null($installedLanguages)) + { + $db = JFactory::getDbo(); + + $query = $db->getQuery(true) + ->select($db->quoteName(array('element', 'name', 'client_id'))) + ->from($db->quoteName('#__extensions')) + ->where($db->quoteName('type') . ' = ' . $db->quote('language')) + ->where($db->quoteName('state') . ' = 0') + ->where($db->quoteName('enabled') . ' = 1'); + + $installedLanguages = $db->setQuery($query)->loadObjectList('element'); + } + + $languages = $installedLanguages; + + if (!is_null($clientId)) + { + foreach($languages as $languageCode => $language) + { + if ((int) $language->client_id !== $clientId) + { + unset($languages[$languageCode]); + } + } + } + + return $languages; + } + + /** + * Checks if a language exists. + * + * This is a simple, quick check for the directory that should contain language files for the given user. + * + * @param string $lang Language to check. + * @param string $basePath Optional path to check. + * + * @return boolean True if the language exists. + * + * @since __DEPLOY_VERSION__ + */ + public static function exists($lang, $basePath = JPATH_BASE) + { + static $paths = array(); + + // Return false if no language was specified + if (!$lang) + { + return false; + } + + $path = $basePath . '/language/' . $lang; + + // Return previous check results if it exists + if (isset($paths[$path])) + { + return $paths[$path]; + } + + // Check if the language exists + $paths[$path] = is_dir($path); + + return $paths[$path]; + } + + /** + * Returns an associative array holding the metadata. + * + * @param string $languageCode The name of the language. + * + * @return mixed If language code exists return key/value pair with the language metadata, otherwise return NULL. + * + * @since __DEPLOY_VERSION__ + */ + public static function getMetadata($languageCode) + { + try + { + return self::parseXMLLanguageFile(static::getLanguagePath(JPATH_BASE, $languageCode) . '/' . $languageCode . '.xml'); + } + catch (RuntimeException $e) + { + return null; + } + } + + /** + * Returns a list of known languages for an area + * + * @param string $basePath The basepath to use + * + * @return array key/value pair with the language file and real name. + * + * @since __DEPLOY_VERSION__ + */ + public static function getKnownLanguages($basePath = JPATH_BASE) + { + return static::parseLanguageFiles(static::getLanguagePath($basePath)); + } + + /** + * Get the path to a language + * + * @param string $basePath The basepath to use. + * @param string $language The language tag. + * + * @return string language related path or null. + * + * @since __DEPLOY_VERSION__ + */ + public static function getLanguagePath($basePath = JPATH_BASE, $language = null) + { + return $basePath . '/language' . (!empty($language) ? '/' . $language : ''); + } + + /** + * Searches for language directories within a certain base dir. + * + * @param string $dir directory of files. + * + * @return array Array holding the found languages as filename => real name pairs. + * + * @since __DEPLOY_VERSION__ + */ + public static function parseLanguageFiles($dir = null) + { + $languages = array(); + + // Search main language directory for subdirectories + foreach (glob($dir . '/*', GLOB_NOSORT | GLOB_ONLYDIR) as $directory) + { + // But only directories with lang code format + if (preg_match('#/[a-z]{2,3}-[A-Z]{2}$#', $directory)) + { + $languageCode = pathinfo($directory, PATHINFO_FILENAME); + + // Get installed language metadata from xml file and merge it with lang array + try + { + $languages[$languageCode] = self::parseXMLLanguageFile($directory . '/' . $languageCode . '.xml'); + } + catch (RuntimeException $e) + { + // Do nothing. + } + } + } + + return $languages; + } + + /** + * Parse XML file for language information. + * + * @param string $path Path to the XML files. + * + * @return array Array holding the found metadata as a key => value pair. + * + * @since __DEPLOY_VERSION__ + * @throws RuntimeException + */ + public static function parseXMLLanguageFile($path) + { + if (!is_readable($path)) + { + throw new RuntimeException('File not found or not readable'); + } + + // Try to load the file + $xml = simplexml_load_string(file_get_contents($path)); + + if (!$xml) + { + return; + } + + // Check that it's a metadata file + if ((string) $xml->getName() != 'metafile') + { + return; + } + + $metadata = array(); + + foreach ($xml->metadata->children() as $child) + { + $metadata[$child->getName()] = (string) $child; + } + + return $metadata; + } } diff --git a/libraries/joomla/language/language.php b/libraries/joomla/language/language.php index e83bde35ab2c5..c21aa7a0e79e0 100644 --- a/libraries/joomla/language/language.php +++ b/libraries/joomla/language/language.php @@ -190,8 +190,8 @@ public function __construct($lang = null, $debug = false) $lang = $this->default; } - $this->lang = $lang; - $this->metadata = $this->getMetadata($this->lang); + $this->lang = $lang; + $this->metadata = JLanguageHelper::getMetadata($this->lang); $this->setDebug($debug); $filename = JPATH_BASE . "/language/overrides/$lang.override.ini"; @@ -683,29 +683,14 @@ public function setSearchDisplayedCharactersNumberCallback($function) * @return boolean True if the language exists. * * @since 11.1 + * + * @deprecated 4.0 Use JLanguageHelper::exists instead. */ public static function exists($lang, $basePath = JPATH_BASE) { - static $paths = array(); + JLog::add(__METHOD__ . '() is deprecated. Use JLanguageHelper::exists() instead.', JLog::WARNING, 'deprecated'); - // Return false if no language was specified - if (!$lang) - { - return false; - } - - $path = $basePath . '/language/' . $lang; - - // Return previous check results if it exists - if (isset($paths[$path])) - { - return $paths[$path]; - } - - // Check if the language exists - $paths[$path] = is_dir($path); - - return $paths[$path]; + return JLanguageHelper::exists($lang, $basePath); } /** @@ -736,7 +721,7 @@ public function load($extension = 'joomla', $basePath = JPATH_BASE, $lang = null $this->load($extension, $basePath, $this->default, false, true); } - $path = self::getLanguagePath($basePath, $lang); + $path = JLanguageHelper::getLanguagePath($basePath, $lang); $internal = $extension == 'joomla' || $extension == ''; $filename = $internal ? $lang : $lang . '.' . $extension; @@ -759,7 +744,7 @@ public function load($extension = 'joomla', $basePath = JPATH_BASE, $lang = null $oldFilename = $filename; // Check the standard file name - $path = self::getLanguagePath($basePath, $this->default); + $path = JLanguageHelper::getLanguagePath($basePath, $this->default); $filename = $internal ? $this->default : $this->default . '.' . $extension; $filename = "$path/$filename.ini"; @@ -1206,25 +1191,13 @@ public function hasKey($string) * @return mixed If $lang exists return key/value pair with the language metadata, otherwise return NULL. * * @since 11.1 + * @deprecated 4.0 Use JLanguageHelper::getMetadata() instead. */ public static function getMetadata($lang) { - $path = self::getLanguagePath(JPATH_BASE, $lang); - $file = $lang . '.xml'; + JLog::add(__METHOD__ . ' is deprecated. Use JLanguageHelper::getMetadata() instead.', JLog::WARNING, 'deprecated'); - $result = null; - - if (is_file("$path/$file")) - { - $result = self::parseXMLLanguageFile("$path/$file"); - } - - if (empty($result)) - { - return; - } - - return $result; + return JLanguageHelper::getMetadata($lang); } /** @@ -1235,13 +1208,13 @@ public static function getMetadata($lang) * @return array key/value pair with the language file and real name. * * @since 11.1 + * @deprecated 4.0 Use JLanguageHelper::getKnownLanguages() instead. */ public static function getKnownLanguages($basePath = JPATH_BASE) { - $dir = self::getLanguagePath($basePath); - $knownLanguages = self::parseLanguageFiles($dir); + JLog::add(__METHOD__ . ' is deprecated. Use JLanguageHelper::getKnownLanguages() instead.', JLog::WARNING, 'deprecated'); - return $knownLanguages; + return JLanguageHelper::getKnownLanguages($basePath); } /** @@ -1253,17 +1226,13 @@ public static function getKnownLanguages($basePath = JPATH_BASE) * @return string language related path or null. * * @since 11.1 + * @deprecated 4.0 Use JLanguageHelper::getLanguagePath() instead. */ public static function getLanguagePath($basePath = JPATH_BASE, $language = null) { - $dir = $basePath . '/language'; + JLog::add(__METHOD__ . ' is deprecated. Use JLanguageHelper::getLanguagePath() instead.', JLog::WARNING, 'deprecated'); - if (!empty($language)) - { - $dir .= '/' . $language; - } - - return $dir; + return JLanguageHelper::getLanguagePath($basePath, $language); } /** @@ -1282,9 +1251,9 @@ public function setLanguage($lang) { JLog::add(__METHOD__ . ' is deprecated. Instantiate a new JLanguage object instead.', JLog::WARNING, 'deprecated'); - $previous = $this->lang; - $this->lang = $lang; - $this->metadata = $this->getMetadata($this->lang); + $previous = $this->lang; + $this->lang = $lang; + $this->metadata = JLanguageHelper::getMetadata($this->lang); return $previous; } @@ -1347,40 +1316,13 @@ public function getWeekEnd() * @return array Array holding the found languages as filename => real name pairs. * * @since 11.1 + * @deprecated 4.0 Use JLanguageHelper::parseLanguageFiles() instead. */ public static function parseLanguageFiles($dir = null) { - $languages = array(); - - // Search main language directory for subdirectories - foreach (glob($dir . '/*', GLOB_NOSORT | GLOB_ONLYDIR) as $directory) - { - // But only directories with lang code format - if (preg_match('#/[a-z]{2,3}-[A-Z]{2}$#', $directory)) - { - $dirPathParts = pathinfo($directory); - $file = $directory . '/' . $dirPathParts['filename'] . '.xml'; + JLog::add(__METHOD__ . ' is deprecated. Use JLanguageHelper::parseLanguageFiles() instead.', JLog::WARNING, 'deprecated'); - if (!is_file($file)) - { - continue; - } - - try - { - // Get installed language metadata from xml file and merge it with lang array - if ($metadata = self::parseXMLLanguageFile($file)) - { - $languages = array_replace($languages, array($dirPathParts['filename'] => $metadata)); - } - } - catch (RuntimeException $e) - { - } - } - } - - return $languages; + return JLanguageHelper::parseLanguageFiles($dir); } /** @@ -1392,35 +1334,12 @@ public static function parseLanguageFiles($dir = null) * * @since 11.1 * @throws RuntimeException + * @deprecated 4.0 Use JLanguageHelper::parseLanguageFiles() instead. */ public static function parseXMLLanguageFile($path) { - if (!is_readable($path)) - { - throw new RuntimeException('File not found or not readable'); - } - - // Try to load the file - $xml = simplexml_load_file($path); - - if (!$xml) - { - return; - } - - // Check that it's a metadata file - if ((string) $xml->getName() != 'metafile') - { - return; - } - - $metadata = array(); - - foreach ($xml->metadata->children() as $child) - { - $metadata[$child->getName()] = (string) $child; - } + JLog::add(__METHOD__ . ' is deprecated. Use JLanguageHelper::parseXMLLanguageFile() instead.', JLog::WARNING, 'deprecated'); - return $metadata; + return JLanguageHelper::parseXMLLanguageFile($path); } } From 638b2c312faea183510e7a226b617c16f48ca0a5 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 03:01:28 +0100 Subject: [PATCH 02/19] change in core --- .../components/com_languages/models/installed.php | 3 +-- .../components/com_languages/models/overrides.php | 4 ++-- components/com_users/helpers/html/users.php | 8 ++++---- installation/application/web.php | 4 ++-- installation/model/database.php | 2 +- installation/model/languages.php | 3 +-- libraries/cms/application/administrator.php | 6 +++--- libraries/cms/application/site.php | 12 ++++++------ modules/mod_languages/helper.php | 2 +- plugins/system/languagecode/languagecode.php | 2 +- 10 files changed, 22 insertions(+), 24 deletions(-) diff --git a/administrator/components/com_languages/models/installed.php b/administrator/components/com_languages/models/installed.php index 52c8dbd337676..117a46c7992e9 100644 --- a/administrator/components/com_languages/models/installed.php +++ b/administrator/components/com_languages/models/installed.php @@ -456,8 +456,7 @@ protected function getPath() { if (is_null($this->path)) { - $client = $this->getClient(); - $this->path = JLanguage::getLanguagePath($client->path); + $this->path = JLanguageHelper::getLanguagePath($this->getClient()->path); } return $this->path; diff --git a/administrator/components/com_languages/models/overrides.php b/administrator/components/com_languages/models/overrides.php index e8db0ba11ff7b..d55f780504fe7 100644 --- a/administrator/components/com_languages/models/overrides.php +++ b/administrator/components/com_languages/models/overrides.php @@ -204,8 +204,8 @@ public function getLanguages() // Get all languages of frontend and backend. $languages = array(); - $site_languages = JLanguage::getKnownLanguages(JPATH_SITE); - $admin_languages = JLanguage::getKnownLanguages(JPATH_ADMINISTRATOR); + $site_languages = JLanguageHelper::getKnownLanguages(JPATH_SITE); + $admin_languages = JLanguageHelper::getKnownLanguages(JPATH_ADMINISTRATOR); // Create a single array of them. foreach ($site_languages as $tag => $language) diff --git a/components/com_users/helpers/html/users.php b/components/com_users/helpers/html/users.php index b9c6b1245d57b..ba40b0fb566e3 100644 --- a/components/com_users/helpers/html/users.php +++ b/components/com_users/helpers/html/users.php @@ -156,14 +156,14 @@ public static function admin_language($value) } else { - $path = JLanguage::getLanguagePath(JPATH_ADMINISTRATOR, $value); + $path = JLanguageHelper::getLanguagePath(JPATH_ADMINISTRATOR, $value); $file = "$value.xml"; $result = null; if (is_file("$path/$file")) { - $result = JLanguage::parseXMLLanguageFile("$path/$file"); + $result = JLanguageHelper::parseXMLLanguageFile("$path/$file"); } if ($result) @@ -194,14 +194,14 @@ public static function language($value) } else { - $path = JLanguage::getLanguagePath(JPATH_SITE, $value); + $path = JLanguageHelper::getLanguagePath(JPATH_SITE, $value); $file = "$value.xml"; $result = null; if (is_file("$path/$file")) { - $result = JLanguage::parseXMLLanguageFile("$path/$file"); + $result = JLanguageHelper::parseXMLLanguageFile("$path/$file"); } if ($result) diff --git a/installation/application/web.php b/installation/application/web.php index 3bff6954e7534..2e93d76113127 100644 --- a/installation/application/web.php +++ b/installation/application/web.php @@ -312,11 +312,11 @@ public function getLocalise() public function getLocaliseAdmin($db = false) { // Read the files in the admin area. - $path = JLanguage::getLanguagePath(JPATH_ADMINISTRATOR); + $path = JLanguageHelper::getLanguagePath(JPATH_ADMINISTRATOR); $langfiles['admin'] = JFolder::folders($path); // Read the files in the site area. - $path = JLanguage::getLanguagePath(JPATH_SITE); + $path = JLanguageHelper::getLanguagePath(JPATH_SITE); $langfiles['site'] = JFolder::folders($path); if ($db) diff --git a/installation/model/database.php b/installation/model/database.php index b9a5755468de9..ee574b136cbc3 100644 --- a/installation/model/database.php +++ b/installation/model/database.php @@ -98,7 +98,7 @@ public function initialise($options) $currentLang = $lang->getTag(); // Load the selected language - if (JLanguage::exists($currentLang, JPATH_ADMINISTRATOR)) + if (JLanguageHelper::exists($currentLang, JPATH_ADMINISTRATOR)) { $lang->load('joomla', JPATH_ADMINISTRATOR, $currentLang, true); } diff --git a/installation/model/languages.php b/installation/model/languages.php index aa751a82c9700..5a8abb48c3310 100644 --- a/installation/model/languages.php +++ b/installation/model/languages.php @@ -407,8 +407,7 @@ protected function getPath() { if (is_null($this->path)) { - $client = $this->getClient(); - $this->path = JLanguage::getLanguagePath($client->path); + $this->path = JLanguageHelper::getLanguagePath($this->getClient()->path); } return $this->path; diff --git a/libraries/cms/application/administrator.php b/libraries/cms/application/administrator.php index de59ef7b34734..83ecc735eb52c 100644 --- a/libraries/cms/application/administrator.php +++ b/libraries/cms/application/administrator.php @@ -263,7 +263,7 @@ protected function initialiseApp($options = array()) $lang = $user->getParam('admin_language'); // Make sure that the user's language exists - if ($lang && JLanguage::exists($lang)) + if ($lang && JLanguageHelper::exists($lang)) { $options['language'] = $lang; } @@ -275,11 +275,11 @@ protected function initialiseApp($options = array()) } // One last check to make sure we have something - if (!JLanguage::exists($options['language'])) + if (!JLanguageHelper::exists($options['language'])) { $lang = $this->get('language', 'en-GB'); - if (JLanguage::exists($lang)) + if (JLanguageHelper::exists($lang)) { $options['language'] = $lang; } diff --git a/libraries/cms/application/site.php b/libraries/cms/application/site.php index 3cc6e61858b99..561e7160c32fa 100644 --- a/libraries/cms/application/site.php +++ b/libraries/cms/application/site.php @@ -612,7 +612,7 @@ protected function initialiseApp($options = array()) $lang = $this->input->getString('language', null); // Make sure that the user's language exists - if ($lang && JLanguage::exists($lang)) + if ($lang && JLanguageHelper::exists($lang)) { $options['language'] = $lang; } @@ -624,7 +624,7 @@ protected function initialiseApp($options = array()) $lang = $this->input->cookie->get(md5($this->get('secret') . 'language'), null, 'string'); // Make sure that the user's language exists - if ($lang && JLanguage::exists($lang)) + if ($lang && JLanguageHelper::exists($lang)) { $options['language'] = $lang; } @@ -636,7 +636,7 @@ protected function initialiseApp($options = array()) $lang = $user->getParam('language'); // Make sure that the user's language exists - if ($lang && JLanguage::exists($lang)) + if ($lang && JLanguageHelper::exists($lang)) { $options['language'] = $lang; } @@ -648,7 +648,7 @@ protected function initialiseApp($options = array()) $lang = JLanguageHelper::detectLanguage(); // Make sure that the user's language exists - if ($lang && JLanguage::exists($lang)) + if ($lang && JLanguageHelper::exists($lang)) { $options['language'] = $lang; } @@ -662,11 +662,11 @@ protected function initialiseApp($options = array()) } // One last check to make sure we have something - if (!JLanguage::exists($options['language'])) + if (!JLanguageHelper::exists($options['language'])) { $lang = $this->config->get('language', 'en-GB'); - if (JLanguage::exists($lang)) + if (JLanguageHelper::exists($lang)) { $options['language'] = $lang; } diff --git a/modules/mod_languages/helper.php b/modules/mod_languages/helper.php index 140574d52ecff..6b3593971a73c 100644 --- a/modules/mod_languages/helper.php +++ b/modules/mod_languages/helper.php @@ -107,7 +107,7 @@ public static function getList(&$params) // If not loaded language fetch metadata directly for performance else { - $languageMetadata = JLanguage::getMetadata($language->lang_code); + $languageMetadata = JLanguageHelper::getMetadata($language->lang_code); $language->rtl = $languageMetadata['rtl']; } diff --git a/plugins/system/languagecode/languagecode.php b/plugins/system/languagecode/languagecode.php index 16f640259e961..f3ca348028c88 100644 --- a/plugins/system/languagecode/languagecode.php +++ b/plugins/system/languagecode/languagecode.php @@ -130,7 +130,7 @@ public function onContentPrepareForm($form, $data) } // Get site languages. - if ($languages = JLanguage::getKnownLanguages(JPATH_SITE)) + if ($languages = JLanguageHelper::getKnownLanguages(JPATH_SITE)) { // Inject fields into the form. foreach ($languages as $tag => $language) From 417d3d160749c7260dd3ab343e9c60f61066bd79 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 03:09:15 +0100 Subject: [PATCH 03/19] move to helper in unit tests too --- .../joomla/language/JLanguageHelperTest.php | 191 ++++++++++++++++- .../joomla/language/JLanguageTest.php | 200 ------------------ 2 files changed, 189 insertions(+), 202 deletions(-) diff --git a/tests/unit/suites/libraries/joomla/language/JLanguageHelperTest.php b/tests/unit/suites/libraries/joomla/language/JLanguageHelperTest.php index 2fe0ca61f9181..710d4665fc89c 100644 --- a/tests/unit/suites/libraries/joomla/language/JLanguageHelperTest.php +++ b/tests/unit/suites/libraries/joomla/language/JLanguageHelperTest.php @@ -23,10 +23,11 @@ class JLanguageHelperTest extends PHPUnit_Framework_TestCase public function testCreateLanguageList() { $option = array( - 'text' => 'English (United Kingdom)', - 'value' => 'en-GB', + 'text' => 'English (United Kingdom)', + 'value' => 'en-GB', 'selected' => 'selected="selected"' ); + $listCompareEqual = array( 0 => $option, ); @@ -52,4 +53,190 @@ public function testDetectLanguage() $lang ); } + + /** + * Test... + * + * @return void + */ + public function testExists() + { + $this->assertFalse( + JLanguageHelper::exists(null) + ); + + $basePath = __DIR__ . '/data'; + + $this->assertTrue( + JLanguageHelper::exists('en-GB', $basePath) + ); + + $this->assertFalse( + JLanguageHelper::exists('es-ES', $basePath) + ); + } + + /** + * Test... + * + * @return void + */ + public function testGetMetadata() + { + // Language doesn't exist, retun NULL + $this->assertNull( + JLanguageHelper::getMetadata('es-ES') + ); + + $localeString = 'en_GB.utf8, en_GB.UTF-8, en_GB, eng_GB, en, english, english-uk, uk, gbr, britain, england, great britain, ' . + 'uk, united kingdom, united-kingdom'; + + // In this case, returns array with default language + // - same operation of get method with metadata property + $options = array( + 'name' => 'English (en-GB)', + 'tag' => 'en-GB', + 'rtl' => '0', + 'locale' => $localeString, + 'firstDay' => '0', + 'weekEnd' => '0,6' + ); + + // Language exists, returns array with values + $this->assertEquals( + $options, + JLanguageHelper::getMetadata('en-GB') + ); + } + + /** + * Test... + * + * @return void + */ + public function testGetKnownLanguages() + { + // This method returns a list of known languages + $basePath = __DIR__ . '/data'; + $option1 = array( + 'name' => 'English (United Kingdom)', + 'tag' => 'en-GB', + 'rtl' => '0', + 'locale' => 'en_GB.utf8, en_GB.UTF-8, en_GB, eng_GB, en, english, english-uk, uk, gbr, britain, england, great britain,' . + ' uk, united kingdom, united-kingdom', + 'firstDay' => '0', + 'weekEnd' => '0,6', + ); + + $listCompareEqual1 = array( + 'en-GB' => $option1, + ); + + $this->assertEquals( + $listCompareEqual1, + JLanguageHelper::getKnownLanguages($basePath), + 'Line: ' . __LINE__ + ); + } + + /** + * Test... + * + * @return void + */ + public function testGetLanguagePath() + { + $basePath = 'test'; + + // $language = null, returns language directory + $this->assertEquals( + 'test/language', + JLanguageHelper::getLanguagePath($basePath, null), + 'Line: ' . __LINE__ + ); + + // $language = value (en-GB, for example), returns en-GB language directory + $this->assertEquals( + 'test/language/en-GB', + JLanguageHelper::getLanguagePath($basePath, 'en-GB'), + 'Line: ' . __LINE__ + ); + + // With no argument JPATH_BASE should be returned + $this->assertEquals( + JPATH_BASE . '/language', + JLanguageHelper::getLanguagePath(), + 'Line: ' . __LINE__ + ); + } + + /** + * Test... + * + * @return void + */ + public function testParseLanguageFiles() + { + $dir = __DIR__ . '/data/language'; + $option = array( + 'name' => 'English (United Kingdom)', + 'tag' => 'en-GB', + 'rtl' => '0', + 'locale' => 'en_GB.utf8, en_GB.UTF-8, en_GB, eng_GB, en, english, english-uk, uk, gbr, britain, england,' . + ' great britain, uk, united kingdom, united-kingdom', + 'firstDay' => '0', + 'weekEnd' => '0,6', + ); + + $expected = array( + 'en-GB' => $option + ); + + $result = JLanguageHelper::parseLanguageFiles($dir); + + $this->assertEquals( + $expected, + $result, + 'Line: ' . __LINE__ + ); + } + + /** + * Test... + * + * @return void + */ + public function testParseXMLLanguageFile() + { + $path = __DIR__ . '/data/language/en-GB/en-GB.xml'; + $option = array( + 'name' => 'English (United Kingdom)', + 'tag' => 'en-GB', + 'rtl' => '0', + 'locale' => 'en_GB.utf8, en_GB.UTF-8, en_GB, eng_GB, en, english, english-uk, uk, gbr, britain, england, great britain,' . + ' uk, united kingdom, united-kingdom', + 'firstDay' => '0', + 'weekEnd' => '0,6', + ); + + $this->assertEquals( + $option, + JLanguageHelper::parseXMLLanguageFile($path), + 'Line: ' . __LINE__ + ); + } + + /** + * Test... + * + * @expectedException RuntimeException + * + * @return void + */ + public function testParseXMLLanguageFileException() + { + $path = __DIR__ . '/data/language/es-ES/es-ES.xml'; + + JLanguageHelper::parseXMLLanguageFile($path); + } } diff --git a/tests/unit/suites/libraries/joomla/language/JLanguageTest.php b/tests/unit/suites/libraries/joomla/language/JLanguageTest.php index 804a1971be656..697fa242f8ae7 100644 --- a/tests/unit/suites/libraries/joomla/language/JLanguageTest.php +++ b/tests/unit/suites/libraries/joomla/language/JLanguageTest.php @@ -731,28 +731,6 @@ public function testSetSearchDisplayedCharactersNumberCallback() ); } - /** - * Test... - * - * @return void - */ - public function testExists() - { - $this->assertFalse( - $this->object->exists(null) - ); - - $basePath = __DIR__ . '/data'; - - $this->assertTrue( - $this->object->exists('en-GB', $basePath) - ); - - $this->assertFalse( - $this->object->exists('es-ES', $basePath) - ); - } - /** * Test... * @@ -1023,106 +1001,6 @@ public function testHasKey() ); } - /** - * Test... - * - * @return void - */ - public function testGetMetadata() - { - // Language doesn't exist, retun NULL - $this->assertNull( - $this->inspector->getMetadata('es-ES') - ); - - $localeString = 'en_GB.utf8, en_GB.UTF-8, en_GB, eng_GB, en, english, english-uk, uk, gbr, britain, england, great britain, ' . - 'uk, united kingdom, united-kingdom'; - - // In this case, returns array with default language - // - same operation of get method with metadata property - $options = array( - 'name' => 'English (en-GB)', - 'tag' => 'en-GB', - 'rtl' => '0', - 'locale' => $localeString, - 'firstDay' => '0', - 'weekEnd' => '0,6' - ); - - // Language exists, returns array with values - $this->assertEquals( - $options, - $this->inspector->getMetadata('en-GB') - ); - } - - /** - * Test... - * - * @return void - */ - public function testGetKnownLanguages() - { - // This method returns a list of known languages - $basePath = __DIR__ . '/data'; - - $localeString = 'en_GB.utf8, en_GB.UTF-8, en_GB, eng_GB, en, english, english-uk, uk, gbr, britain, england, great britain,' . - ' uk, united kingdom, united-kingdom'; - - $weekEnd = '0,6'; - - $option1 = array( - 'name' => 'English (United Kingdom)', - 'tag' => 'en-GB', - 'rtl' => '0', - 'locale' => $localeString, - 'firstDay' => '0', - 'weekEnd' => $weekEnd - ); - - $listCompareEqual1 = array( - 'en-GB' => $option1, - ); - - $list = JLanguage::getKnownLanguages($basePath); - $this->assertEquals( - $listCompareEqual1, - $list, - 'Line: ' . __LINE__ - ); - } - - /** - * Test... - * - * @return void - */ - public function testGetLanguagePath() - { - $basePath = 'test'; - - // $language = null, returns language directory - $this->assertEquals( - 'test/language', - JLanguage::getLanguagePath($basePath, null), - 'Line: ' . __LINE__ - ); - - // $language = value (en-GB, for example), returns en-GB language directory - $this->assertEquals( - 'test/language/en-GB', - JLanguage::getLanguagePath($basePath, 'en-GB'), - 'Line: ' . __LINE__ - ); - - // With no argument JPATH_BASE should be returned - $this->assertEquals( - JPATH_BASE . '/language', - JLanguage::getLanguagePath(), - 'Line: ' . __LINE__ - ); - } - /** * Test... * @@ -1142,82 +1020,4 @@ public function testSetLanguage() 'Line: ' . __LINE__ ); } - - /** - * Test... - * - * @return void - */ - public function testParseLanguageFiles() - { - $dir = __DIR__ . '/data/language'; - $option = array( - 'name' => 'English (United Kingdom)', - 'tag' => 'en-GB', - 'rtl' => '0', - 'locale' => 'en_GB.utf8, en_GB.UTF-8, en_GB, eng_GB, en, english, english-uk, uk, gbr, britain, england,' . - ' great britain, uk, united kingdom, united-kingdom', - 'firstDay' => '0', - 'weekEnd' => '0,6' - ); - - $expected = array( - 'en-GB' => $option - ); - - $result = JLanguage::parseLanguageFiles($dir); - - $this->assertEquals( - $expected, - $result, - 'Line: ' . __LINE__ - ); - } - - /** - * Test... - * - * @return void - */ - public function testParseXMLLanguageFile() - { - $option = array( - 'name' => 'English (United Kingdom)', - 'tag' => 'en-GB', - 'rtl' => '0', - 'locale' => 'en_GB.utf8, en_GB.UTF-8, en_GB, eng_GB, en, english, english-uk, uk, gbr, britain, england, great britain,' . - ' uk, united kingdom, united-kingdom', - 'firstDay' => '0', - 'weekEnd' => '0,6' - ); - - $path = __DIR__ . '/data/language/en-GB/en-GB.xml'; - - $this->assertEquals( - $option, - JLanguage::parseXMLLanguageFile($path), - 'Line: ' . __LINE__ - ); - - $path2 = __DIR__ . '/data/language/es-ES/es-ES.xml'; - $this->assertEquals( - $option, - JLanguage::parseXMLLanguageFile($path), - 'Line: ' . __LINE__ - ); - } - - /** - * Test... - * - * @expectedException RuntimeException - * - * @return void - */ - public function testParseXMLLanguageFileException() - { - $path = __DIR__ . '/data/language/es-ES/es-ES.xml'; - - JLanguage::parseXMLLanguageFile($path); - } } From 734e2a7763dacabde69b3ff6567ec99734525bf3 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 03:23:17 +0100 Subject: [PATCH 04/19] deprecate JLanguageMultilang::getSiteLangs --- libraries/cms/language/multilang.php | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/libraries/cms/language/multilang.php b/libraries/cms/language/multilang.php index 0d4905c6a544d..03f316735daf5 100644 --- a/libraries/cms/language/multilang.php +++ b/libraries/cms/language/multilang.php @@ -69,28 +69,13 @@ public static function isEnabled() * @return array of language extension objects. * * @since 3.5 + * @deprecated 4.0 Use JLanguageHelper::getInstalledLanguages(0) instead. */ public static function getSiteLangs() { - // To avoid doing duplicate database queries. - static $multilangSiteLangs = null; - - if (!isset($multilangSiteLangs)) - { - // Check for published Site Languages. - $db = JFactory::getDbo(); - $query = $db->getQuery(true) - ->select('element') - ->from('#__extensions') - ->where('type = ' . $db->quote('language')) - ->where('client_id = 0') - ->where('enabled = 1'); - $db->setQuery($query); - - $multilangSiteLangs = $db->loadObjectList('element'); - } + JLog::add(__METHOD__ . ' is deprecated. Use JLanguageHelper::getInstalledLanguages(0) instead.', JLog::WARNING, 'deprecated'); - return $multilangSiteLangs; + return JLanguageHelper::getInstalledLanguages(0); } /** From 84246f797a1e780f942faff632bdb3865e8470f1 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 03:27:01 +0100 Subject: [PATCH 05/19] replace in core --- .../components/com_languages/helpers/multilangstatus.php | 6 +++--- .../com_languages/views/multilangstatus/view.html.php | 2 +- modules/mod_languages/helper.php | 2 +- plugins/system/languagefilter/languagefilter.php | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/administrator/components/com_languages/helpers/multilangstatus.php b/administrator/components/com_languages/helpers/multilangstatus.php index 71d7b54754257..9e8d18bfdcc3e 100644 --- a/administrator/components/com_languages/helpers/multilangstatus.php +++ b/administrator/components/com_languages/helpers/multilangstatus.php @@ -79,13 +79,13 @@ public static function getContentlangs() * * @return array of language extension objects. * - * @deprecated 4.0 Use JLanguageMultilang::getSiteLangs() instead. + * @deprecated 4.0 Use JLanguageHelper::getInstalledLanguages(0) instead. */ public static function getSitelangs() { - JLog::add(__METHOD__ . ' is deprecated, use JLanguageMultilang::getSiteLangs() instead.', JLog::WARNING, 'deprecated'); + JLog::add(__METHOD__ . ' is deprecated, use JLanguageHelper::getInstalledLanguages(0) instead.', JLog::WARNING, 'deprecated'); - return JLanguageMultilang::getSiteLangs(); + return JLanguageHelper::getInstalledLanguages(0); } /** diff --git a/administrator/components/com_languages/views/multilangstatus/view.html.php b/administrator/components/com_languages/views/multilangstatus/view.html.php index 1e33e60e97425..5cee40bbc2abb 100644 --- a/administrator/components/com_languages/views/multilangstatus/view.html.php +++ b/administrator/components/com_languages/views/multilangstatus/view.html.php @@ -32,7 +32,7 @@ public function display($tpl = null) $this->switchers = MultilangstatusHelper::getLangswitchers(); $this->listUsersError = MultilangstatusHelper::getContacts(); $this->contentlangs = MultilangstatusHelper::getContentlangs(); - $this->site_langs = JLanguageMultilang::getSiteLangs(); + $this->site_langs = JLanguageHelper::getInstalledLanguages(0); $this->statuses = MultilangstatusHelper::getStatus(); $this->homepages = JLanguageMultilang::getSiteHomePages(); diff --git a/modules/mod_languages/helper.php b/modules/mod_languages/helper.php index 6b3593971a73c..33fd4f0b0d294 100644 --- a/modules/mod_languages/helper.php +++ b/modules/mod_languages/helper.php @@ -73,7 +73,7 @@ public static function getList(&$params) } $levels = $user->getAuthorisedViewLevels(); - $sitelangs = JLanguageMultilang::getSiteLangs(); + $sitelangs = JLanguageHelper::getInstalledLanguages(0); $multilang = JLanguageMultilang::isEnabled(); // Filter allowed languages diff --git a/plugins/system/languagefilter/languagefilter.php b/plugins/system/languagefilter/languagefilter.php index d345ebd25c0dc..d740368dc3f75 100644 --- a/plugins/system/languagefilter/languagefilter.php +++ b/plugins/system/languagefilter/languagefilter.php @@ -105,7 +105,7 @@ public function __construct(&$subject, $config) // @todo: In Joomla 2.5.4 and earlier access wasn't set. Non modified Content Languages got 0 as access value // we also check if frontend language exists and is enabled if (($language->access && !in_array($language->access, $levels)) - || (!array_key_exists($language->lang_code, JLanguageMultilang::getSiteLangs()))) + || (!array_key_exists($language->lang_code, JLanguageHelper::getInstalledLanguages(0)))) { unset($this->lang_codes[$language->lang_code]); unset($this->sefs[$language->sef]); @@ -735,7 +735,7 @@ public function onAfterDispatch() switch (true) { // Language without frontend UI || Language without specific home menu || Language without authorized access level - case (!array_key_exists($i, JLanguageMultilang::getSiteLangs())): + case (!array_key_exists($i, JLanguageHelper::getInstalledLanguages(0))): case (!isset($homes[$i])): case (isset($language->access) && $language->access && !in_array($language->access, $levels)): unset($languages[$i]); From 7199eb6cbf751e5dbd9c418d97a04f1904a89b8b Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 03:33:55 +0100 Subject: [PATCH 06/19] use the new method --- .../com_languages/models/installed.php | 16 ++-------------- installation/application/web.php | 11 +++-------- libraries/joomla/language/helper.php | 2 +- 3 files changed, 6 insertions(+), 23 deletions(-) diff --git a/administrator/components/com_languages/models/installed.php b/administrator/components/com_languages/models/installed.php index 117a46c7992e9..74635b59961df 100644 --- a/administrator/components/com_languages/models/installed.php +++ b/administrator/components/com_languages/models/installed.php @@ -198,23 +198,11 @@ public function getData() { $this->data = array(); - // Get information. - $db = $this->getDbo(); - $query = $db->getQuery(true); - // Select languages installed from the extensions table. - $query->select($db->quoteName(array('a.element', 'a.client_id', 'a.extension_id'))) - ->from($db->quoteName('#__extensions', 'a')) - ->where($db->quoteName('a.type') . ' = ' . $db->quote('language')) - ->where($db->quoteName('state') . ' = 0') - ->where($db->quoteName('enabled') . ' = 1'); - - // For client_id = 1 do we need to check language table also? - $db->setQuery($query); - $langlist = $db->loadObjectList(); + $langlist = JLanguageHelper::getInstalledLanguages(); // Compute all the languages. - foreach ($langlist as $lang) + foreach ($langlist as $languageCode => $lang) { $client = JApplicationHelper::getClientInfo($lang->client_id); $clientPath = (int) $lang->client_id === 0 ? JPATH_SITE : JPATH_ADMINISTRATOR; diff --git a/installation/application/web.php b/installation/application/web.php index 2e93d76113127..e68c2fa6c6871 100644 --- a/installation/application/web.php +++ b/installation/application/web.php @@ -326,14 +326,9 @@ public function getLocaliseAdmin($db = false) $langfiles['admin'] = array(); $langfiles['site'] = array(); - $query = $db->getQuery(true) - ->select($db->quoteName(array('element','client_id'))) - ->from($db->quoteName('#__extensions')) - ->where($db->quoteName('type') . ' = ' . $db->quote('language')); - $db->setQuery($query); - $langs = $db->loadObjectList(); - - foreach ($langs as $lang) + $langs = JLanguageHelper::getInstalledLanguages(); + + foreach ($langs as $languageCode => $lang) { switch ($lang->client_id) { diff --git a/libraries/joomla/language/helper.php b/libraries/joomla/language/helper.php index 5be3b84be8eda..2beb5a8018dd5 100644 --- a/libraries/joomla/language/helper.php +++ b/libraries/joomla/language/helper.php @@ -184,7 +184,7 @@ public static function getInstalledLanguages($clientId = null) $db = JFactory::getDbo(); $query = $db->getQuery(true) - ->select($db->quoteName(array('element', 'name', 'client_id'))) + ->select($db->quoteName(array('element', 'name', 'client_id', 'extension_id'))) ->from($db->quoteName('#__extensions')) ->where($db->quoteName('type') . ' = ' . $db->quote('language')) ->where($db->quoteName('state') . ' = 0') From 63c7d80e8baed160f64a55f147d35ea4b0832c4d Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 12:22:44 +0100 Subject: [PATCH 07/19] micro optimizations and code style --- libraries/joomla/language/helper.php | 73 +++++++++++++++------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/libraries/joomla/language/helper.php b/libraries/joomla/language/helper.php index 2beb5a8018dd5..0dc79d81e686a 100644 --- a/libraries/joomla/language/helper.php +++ b/libraries/joomla/language/helper.php @@ -121,13 +121,13 @@ public static function getLanguages($key = 'default') if (JFactory::getApplication()->getClientId() == 2) { $languages[$key] = array(); - $knownLangs = static::getKnownLanguages(JPATH_BASE); + $knownLanguages = static::getKnownLanguages(JPATH_BASE); - foreach ($knownLangs as $metadata) + foreach ($knownLanguages as $languageCode => $metadata) { // Take off 3 letters iso code languages as they can't match browsers' languages and default them to en - $obj = new stdClass; - $obj->lang_code = $metadata['tag']; + $obj = new stdClass; + $obj->lang_code = $languageCode; $languages[$key][] = $obj; } } @@ -138,22 +138,22 @@ public static function getLanguages($key = 'default') if (!$languages = $cache->get('languages')) { $db = JFactory::getDbo(); + $query = $db->getQuery(true) ->select('*') - ->from('#__languages') - ->where('published=1') - ->order('ordering ASC'); - $db->setQuery($query); + ->from($db->quoteName('#__languages')) + ->where($db->quoteName('published') . ' = 1') + ->order($db->quoteName('ordering') . ' ASC'); - $languages['default'] = $db->loadObjectList(); - $languages['sef'] = array(); + $languages['default'] = $db->setQuery($query)->loadObjectList(); + $languages['sef'] = array(); $languages['lang_code'] = array(); if (isset($languages['default'][0])) { foreach ($languages['default'] as $lang) { - $languages['sef'][$lang->sef] = $lang; + $languages['sef'][$lang->sef] = $lang; $languages['lang_code'][$lang->lang_code] = $lang; } } @@ -171,7 +171,7 @@ public static function getLanguages($key = 'default') * * @param integer $clientId The client app id. * - * @return array Array with the language code and name. + * @return array Array with the language code, name, client_id and extension_id. * * @since __DEPLOY_VERSION__ */ @@ -214,24 +214,24 @@ public static function getInstalledLanguages($clientId = null) * * This is a simple, quick check for the directory that should contain language files for the given user. * - * @param string $lang Language to check. - * @param string $basePath Optional path to check. + * @param string $languageCode Language to check. + * @param string $basePath Optional path to check. * * @return boolean True if the language exists. * * @since __DEPLOY_VERSION__ */ - public static function exists($lang, $basePath = JPATH_BASE) + public static function exists($languageCode, $basePath = JPATH_BASE) { static $paths = array(); // Return false if no language was specified - if (!$lang) + if (!$languageCode) { return false; } - $path = $basePath . '/language/' . $lang; + $path = static::getLanguagePath($basePath, $languageCode); // Return previous check results if it exists if (isset($paths[$path])) @@ -258,7 +258,7 @@ public static function getMetadata($languageCode) { try { - return self::parseXMLLanguageFile(static::getLanguagePath(JPATH_BASE, $languageCode) . '/' . $languageCode . '.xml'); + return static::parseXMLLanguageFile(static::getLanguagePath(JPATH_BASE, $languageCode) . '/' . $languageCode . '.xml'); } catch (RuntimeException $e) { @@ -283,43 +283,50 @@ public static function getKnownLanguages($basePath = JPATH_BASE) /** * Get the path to a language * - * @param string $basePath The basepath to use. - * @param string $language The language tag. + * @param string $basePath The basepath to use. + * @param string $languageCode The language tag. * * @return string language related path or null. * * @since __DEPLOY_VERSION__ */ - public static function getLanguagePath($basePath = JPATH_BASE, $language = null) + public static function getLanguagePath($basePath = JPATH_BASE, $languageCode = null) { - return $basePath . '/language' . (!empty($language) ? '/' . $language : ''); + return $basePath . '/language' . (!empty($languageCode) ? '/' . $languageCode : ''); } /** * Searches for language directories within a certain base dir. * - * @param string $dir directory of files. + * @param string $path Path of the language folder. * * @return array Array holding the found languages as filename => real name pairs. * * @since __DEPLOY_VERSION__ */ - public static function parseLanguageFiles($dir = null) + public static function parseLanguageFiles($path = null) { - $languages = array(); + static $languages = array(); + + if (isset($languages[$path])) + { + return $languages[$path]; + } + + $languages[$path] = array(); // Search main language directory for subdirectories - foreach (glob($dir . '/*', GLOB_NOSORT | GLOB_ONLYDIR) as $directory) + foreach (glob($path . '/*', GLOB_NOSORT | GLOB_ONLYDIR) as $languagePath) { // But only directories with lang code format - if (preg_match('#/[a-z]{2,3}-[A-Z]{2}$#', $directory)) + if (preg_match('#/[a-z]{2,3}-[A-Z]{2}$#', $languagePath)) { - $languageCode = pathinfo($directory, PATHINFO_FILENAME); + $languageCode = pathinfo($languagePath, PATHINFO_FILENAME); // Get installed language metadata from xml file and merge it with lang array try { - $languages[$languageCode] = self::parseXMLLanguageFile($directory . '/' . $languageCode . '.xml'); + $languages[$path][$languageCode] = static::parseXMLLanguageFile($languagePath . '/' . $languageCode . '.xml'); } catch (RuntimeException $e) { @@ -328,7 +335,7 @@ public static function parseLanguageFiles($dir = null) } } - return $languages; + return $languages[$path]; } /** @@ -349,17 +356,17 @@ public static function parseXMLLanguageFile($path) } // Try to load the file - $xml = simplexml_load_string(file_get_contents($path)); + $xml = simplexml_load_file($path); if (!$xml) { - return; + return null; } // Check that it's a metadata file if ((string) $xml->getName() != 'metafile') { - return; + return null; } $metadata = array(); From 8d1d206ee5058bd76a4bfe75f792383557867714 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 12:26:43 +0100 Subject: [PATCH 08/19] less code --- libraries/joomla/language/helper.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libraries/joomla/language/helper.php b/libraries/joomla/language/helper.php index 0dc79d81e686a..8e894bfc9be01 100644 --- a/libraries/joomla/language/helper.php +++ b/libraries/joomla/language/helper.php @@ -32,12 +32,10 @@ public static function createLanguageList($actualLanguage, $basePath = JPATH_BAS { $languages = static::getKnownLanguages($basePath); + // If checking if installed remove not installed languages. if ($installed) { - $installedLanguages = static::getInstalledLanguages($basePath == JPATH_ADMINISTRATOR ? 1 : 0); - - // Remove not installed languages. - $languages = array_intersect_key($languages, $installedLanguages); + $languages = array_intersect_key($languages, static::getInstalledLanguages($basePath == JPATH_ADMINISTRATOR ? 1 : 0)); } $list = array(); From e0039d11facf75f69bb3852d62feddc11ebaff3f Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 13:25:50 +0100 Subject: [PATCH 09/19] correct bugs --- .../com_languages/models/installed.php | 68 +++++++------------ libraries/joomla/language/helper.php | 39 ++++++++--- 2 files changed, 54 insertions(+), 53 deletions(-) diff --git a/administrator/components/com_languages/models/installed.php b/administrator/components/com_languages/models/installed.php index 74635b59961df..e6d8a95513baf 100644 --- a/administrator/components/com_languages/models/installed.php +++ b/administrator/components/com_languages/models/installed.php @@ -199,55 +199,35 @@ public function getData() $this->data = array(); // Select languages installed from the extensions table. - $langlist = JLanguageHelper::getInstalledLanguages(); + $langlist = JLanguageHelper::getInstalledLanguages(null, false, true); // Compute all the languages. - foreach ($langlist as $languageCode => $lang) + foreach ($langlist as $clientId => $language) { - $client = JApplicationHelper::getClientInfo($lang->client_id); - $clientPath = (int) $lang->client_id === 0 ? JPATH_SITE : JPATH_ADMINISTRATOR; - $metafilePath = $clientPath . '/language/' . $lang->element . '/' . $lang->element . '.xml'; - - $info = JApplicationHelper::parseXMLLangMetaFile($metafilePath); - if (!is_array($info)) - { - $app = JFactory::getApplication(); - $app->enqueueMessage(JText::sprintf('COM_LANGUAGES_ERROR_LANGUAGE_METAFILE_MISSING', $lang->element, $metafilePath), 'warning'); - - continue; - } - - $row = new StdClass; - - $row->language = $lang->element; - $row->client_id = (int) $lang->client_id; - $row->extension_id = (int) $lang->extension_id; - - foreach ($info as $key => $value) - { - $row->$key = $value; - } - - // Fix wrongly set parentheses in RTL languages - if (JFactory::getLanguage()->isRtl()) + foreach ($language as $languageCode => $lang) { - $row->name = html_entity_decode($row->name . '‎', ENT_QUOTES, 'UTF-8'); + $row = new StdClass; + $row->language = $lang->element; + $row->client_id = (int) $lang->client_id; + $row->extension_id = (int) $lang->extension_id; + + foreach ($lang->metafile as $key => $value) + { + $row->$key = $value; + } + + // Fix wrongly set parentheses in RTL languages + if (JFactory::getLanguage()->isRtl()) + { + $row->name = html_entity_decode($row->name . '‎', ENT_QUOTES, 'UTF-8'); + } + + // If current than set published. + $clientInfo = JApplicationHelper::getClientInfo($lang->client_id); + $row->published = (int) JComponentHelper::getParams('com_languages')->get($clientInfo->name, 'en-GB') == $row->language; + $row->checked_out = 0; + $this->data[] = $row; } - - // If current than set published. - $params = JComponentHelper::getParams('com_languages'); - - if ($params->get($client->name, 'en-GB') == $row->language) - { - $row->published = 1; - } - else - { - $row->published = 0; - } - - $row->checked_out = 0; - $this->data[] = $row; } } diff --git a/libraries/joomla/language/helper.php b/libraries/joomla/language/helper.php index 8e894bfc9be01..56e1772877483 100644 --- a/libraries/joomla/language/helper.php +++ b/libraries/joomla/language/helper.php @@ -167,13 +167,15 @@ public static function getLanguages($key = 'default') /** * Get a list of installed languages. * - * @param integer $clientId The client app id. + * @param integer $clientId The client app id. + * @param boolean $processMetaData Fetch Language metadata. + * @param boolean $processMetaFile Fetch Language metafile. * * @return array Array with the language code, name, client_id and extension_id. * * @since __DEPLOY_VERSION__ */ - public static function getInstalledLanguages($clientId = null) + public static function getInstalledLanguages($clientId = null, $processMetaData = false, $processMetaFile = false) { static $installedLanguages = null; @@ -188,23 +190,42 @@ public static function getInstalledLanguages($clientId = null) ->where($db->quoteName('state') . ' = 0') ->where($db->quoteName('enabled') . ' = 1'); - $installedLanguages = $db->setQuery($query)->loadObjectList('element'); + $installedLanguages = $db->setQuery($query)->loadObjectList(); } - $languages = $installedLanguages; + $languages = array(); - if (!is_null($clientId)) + foreach($installedLanguages as $language) { - foreach($languages as $languageCode => $language) + $languages[$language->client_id][$language->element] = $language; + + if ($processMetaData) { - if ((int) $language->client_id !== $clientId) + $languages[$language->client_id][$language->element]->metadata = static::getMetadata($language->element); + + // No metadata found, not a valid language. + if (!is_array($languages[$language->client_id][$language->element]->metadata)) + { + unset($languages[$language->client_id][$language->element]); + } + } + + if (isset($languages[$language->client_id][$language->element]) && $processMetaFile) + { + $clientPath = (int) $language->client_id === 0 ? JPATH_SITE : JPATH_ADMINISTRATOR; + $metafile = static::getLanguagePath($clientPath, $language->element) . '/' . $language->element . '.xml'; + + $languages[$language->client_id][$language->element]->metafile = JInstaller::parseXMLInstallFile($metafile); + + // No metadata found, not a valid language. + if (!is_array($languages[$language->client_id][$language->element]->metafile)) { - unset($languages[$languageCode]); + unset($languages[$language->client_id][$language->element]); } } } - return $languages; + return !is_null($clientId) ? $languages[$clientId] : $languages; } /** From ff333a893a45fa93c3b82fbfcfd1e023e31e1303 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 13:35:50 +0100 Subject: [PATCH 10/19] improvements --- libraries/joomla/language/helper.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/libraries/joomla/language/helper.php b/libraries/joomla/language/helper.php index 56e1772877483..517db361682b8 100644 --- a/libraries/joomla/language/helper.php +++ b/libraries/joomla/language/helper.php @@ -199,28 +199,39 @@ public static function getInstalledLanguages($clientId = null, $processMetaData { $languages[$language->client_id][$language->element] = $language; + $clientPath = (int) $language->client_id === 0 ? JPATH_SITE : JPATH_ADMINISTRATOR; + $metafile = static::getLanguagePath($clientPath, $language->element) . '/' . $language->element . '.xml'; + + // Check if language folder exists. + if (!static::exists($language->element, $clientPath)) + { + unset($languages[$language->client_id][$language->element]); + continue; + } + + // Process the metadata. if ($processMetaData) { - $languages[$language->client_id][$language->element]->metadata = static::getMetadata($language->element); + $languages[$language->client_id][$language->element]->metadata = static::getMetadata($metafile); // No metadata found, not a valid language. if (!is_array($languages[$language->client_id][$language->element]->metadata)) { unset($languages[$language->client_id][$language->element]); + continue; } } - if (isset($languages[$language->client_id][$language->element]) && $processMetaFile) + // Process the metafile. + if ($processMetaFile) { - $clientPath = (int) $language->client_id === 0 ? JPATH_SITE : JPATH_ADMINISTRATOR; - $metafile = static::getLanguagePath($clientPath, $language->element) . '/' . $language->element . '.xml'; - $languages[$language->client_id][$language->element]->metafile = JInstaller::parseXMLInstallFile($metafile); // No metadata found, not a valid language. if (!is_array($languages[$language->client_id][$language->element]->metafile)) { unset($languages[$language->client_id][$language->element]); + continue; } } } From 50c8ffa0a04db8c8ed36e95bb643a3da83ac5138 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 13:37:00 +0100 Subject: [PATCH 11/19] correct bug --- administrator/components/com_languages/models/installed.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/components/com_languages/models/installed.php b/administrator/components/com_languages/models/installed.php index e6d8a95513baf..8f97cd3a1b45f 100644 --- a/administrator/components/com_languages/models/installed.php +++ b/administrator/components/com_languages/models/installed.php @@ -224,7 +224,7 @@ public function getData() // If current than set published. $clientInfo = JApplicationHelper::getClientInfo($lang->client_id); - $row->published = (int) JComponentHelper::getParams('com_languages')->get($clientInfo->name, 'en-GB') == $row->language; + $row->published = (int) (JComponentHelper::getParams('com_languages')->get($clientInfo->name, 'en-GB') == $row->language); $row->checked_out = 0; $this->data[] = $row; } From 49625a64d76e9993ff10e4d7ff3056e7b12f9024 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 13:49:30 +0100 Subject: [PATCH 12/19] further improvements --- libraries/joomla/language/helper.php | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/libraries/joomla/language/helper.php b/libraries/joomla/language/helper.php index 517db361682b8..86e9ce6699716 100644 --- a/libraries/joomla/language/helper.php +++ b/libraries/joomla/language/helper.php @@ -30,20 +30,15 @@ class JLanguageHelper */ public static function createLanguageList($actualLanguage, $basePath = JPATH_BASE, $caching = false, $installed = false) { - $languages = static::getKnownLanguages($basePath); - - // If checking if installed remove not installed languages. - if ($installed) - { - $languages = array_intersect_key($languages, static::getInstalledLanguages($basePath == JPATH_ADMINISTRATOR ? 1 : 0)); - } + $clientId = $basePath === JPATH_ADMINISTRATOR ? 1 : 0; + $languages = !$installed ? static::getKnownLanguages($basePath) : static::getInstalledLanguages($clientId, true); $list = array(); - foreach ($languages as $languageCode => $metadata) + foreach ($languages as $languageCode => $language) { $list[] = array( - 'text' => $metadata['name'], + 'text' => isset($language->metadata) ? $language->metadata['name'] : $language['name'], 'value' => $languageCode, 'selected' => $languageCode == $actualLanguage ? 'selected="selected"' : null, ); @@ -197,22 +192,21 @@ public static function getInstalledLanguages($clientId = null, $processMetaData foreach($installedLanguages as $language) { - $languages[$language->client_id][$language->element] = $language; - $clientPath = (int) $language->client_id === 0 ? JPATH_SITE : JPATH_ADMINISTRATOR; $metafile = static::getLanguagePath($clientPath, $language->element) . '/' . $language->element . '.xml'; - // Check if language folder exists. - if (!static::exists($language->element, $clientPath)) + // Check if metafile is readable. + if (!is_readable($metafile)) { - unset($languages[$language->client_id][$language->element]); continue; } + $languages[$language->client_id][$language->element] = $language; + // Process the metadata. if ($processMetaData) { - $languages[$language->client_id][$language->element]->metadata = static::getMetadata($metafile); + $languages[$language->client_id][$language->element]->metadata = static::getMetadata($language->element); // No metadata found, not a valid language. if (!is_array($languages[$language->client_id][$language->element]->metadata)) From 72330cd13bc0794de3d84b34909e27f1a87c6537 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 13:52:52 +0100 Subject: [PATCH 13/19] correct in install --- installation/application/web.php | 41 +++++++++++++++++--------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/installation/application/web.php b/installation/application/web.php index e68c2fa6c6871..8fb9d4c5a174b 100644 --- a/installation/application/web.php +++ b/installation/application/web.php @@ -328,27 +328,30 @@ public function getLocaliseAdmin($db = false) $langs = JLanguageHelper::getInstalledLanguages(); - foreach ($langs as $languageCode => $lang) + foreach ($langs as $clientId => $language) { - switch ($lang->client_id) + foreach ($language as $languageCode => $lang) { - // Site. - case 0: - if (in_array($lang->element, $langfiles_disk['site'])) - { - $langfiles['site'][] = $lang->element; - } - - break; - - // Administrator. - case 1: - if (in_array($lang->element, $langfiles_disk['admin'])) - { - $langfiles['admin'][] = $lang->element; - } - - break; + switch ($lang->client_id) + { + // Site. + case 0: + if (in_array($lang->element, $langfiles_disk['site'])) + { + $langfiles['site'][] = $lang->element; + } + + break; + + // Administrator. + case 1: + if (in_array($lang->element, $langfiles_disk['admin'])) + { + $langfiles['admin'][] = $lang->element; + } + + break; + } } } } From 5af31255004033a2f58b3a32ab85c31f4ba38c75 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 19:34:01 +0100 Subject: [PATCH 14/19] installation improvements --- installation/application/web.php | 47 ++++++++------------------------ 1 file changed, 12 insertions(+), 35 deletions(-) diff --git a/installation/application/web.php b/installation/application/web.php index 8fb9d4c5a174b..de8771a704cc8 100644 --- a/installation/application/web.php +++ b/installation/application/web.php @@ -311,50 +311,27 @@ public function getLocalise() */ public function getLocaliseAdmin($db = false) { - // Read the files in the admin area. - $path = JLanguageHelper::getLanguagePath(JPATH_ADMINISTRATOR); - $langfiles['admin'] = JFolder::folders($path); - - // Read the files in the site area. - $path = JLanguageHelper::getLanguagePath(JPATH_SITE); - $langfiles['site'] = JFolder::folders($path); + $langfiles = array(); + // if db connection, fetch them from the database. if ($db) { - $langfiles_disk = $langfiles; - $langfiles = array(); - $langfiles['admin'] = array(); - $langfiles['site'] = array(); - - $langs = JLanguageHelper::getInstalledLanguages(); - - foreach ($langs as $clientId => $language) + foreach (JLanguageHelper::getInstalledLanguages() as $clientId => $language) { + $clientName = $clientId === 0 ? 'site' : 'admin'; + foreach ($language as $languageCode => $lang) { - switch ($lang->client_id) - { - // Site. - case 0: - if (in_array($lang->element, $langfiles_disk['site'])) - { - $langfiles['site'][] = $lang->element; - } - - break; - - // Administrator. - case 1: - if (in_array($lang->element, $langfiles_disk['admin'])) - { - $langfiles['admin'][] = $lang->element; - } - - break; - } + $langfiles[$clientName][] = $lang->element; } } } + // Read the folder names in the site and admin area. + else + { + $langfiles['site'] = JFolder::folders(JLanguageHelper::getLanguagePath(JPATH_SITE)); + $langfiles['admin'] = JFolder::folders(JLanguageHelper::getLanguagePath(JPATH_ADMINISTRATOR)); + } return $langfiles; } From 23f385d2ba0a46130e15bd65d65b6b07653d44b0 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 19:53:39 +0100 Subject: [PATCH 15/19] minor improvements --- .../com_languages/models/installed.php | 2 +- libraries/joomla/language/helper.php | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/administrator/components/com_languages/models/installed.php b/administrator/components/com_languages/models/installed.php index 8f97cd3a1b45f..1e2eee73ea239 100644 --- a/administrator/components/com_languages/models/installed.php +++ b/administrator/components/com_languages/models/installed.php @@ -211,7 +211,7 @@ public function getData() $row->client_id = (int) $lang->client_id; $row->extension_id = (int) $lang->extension_id; - foreach ($lang->metafile as $key => $value) + foreach ($lang->manifest as $key => $value) { $row->$key = $value; } diff --git a/libraries/joomla/language/helper.php b/libraries/joomla/language/helper.php index 86e9ce6699716..d336e442cfaa4 100644 --- a/libraries/joomla/language/helper.php +++ b/libraries/joomla/language/helper.php @@ -30,11 +30,10 @@ class JLanguageHelper */ public static function createLanguageList($actualLanguage, $basePath = JPATH_BASE, $caching = false, $installed = false) { + $list = array(); $clientId = $basePath === JPATH_ADMINISTRATOR ? 1 : 0; $languages = !$installed ? static::getKnownLanguages($basePath) : static::getInstalledLanguages($clientId, true); - $list = array(); - foreach ($languages as $languageCode => $language) { $list[] = array( @@ -164,13 +163,13 @@ public static function getLanguages($key = 'default') * * @param integer $clientId The client app id. * @param boolean $processMetaData Fetch Language metadata. - * @param boolean $processMetaFile Fetch Language metafile. + * @param boolean $processManifest Fetch Language manifest. * * @return array Array with the language code, name, client_id and extension_id. * * @since __DEPLOY_VERSION__ */ - public static function getInstalledLanguages($clientId = null, $processMetaData = false, $processMetaFile = false) + public static function getInstalledLanguages($clientId = null, $processMetaData = false, $processManifest = false) { static $installedLanguages = null; @@ -203,7 +202,7 @@ public static function getInstalledLanguages($clientId = null, $processMetaData $languages[$language->client_id][$language->element] = $language; - // Process the metadata. + // Process the language metadata. if ($processMetaData) { $languages[$language->client_id][$language->element]->metadata = static::getMetadata($language->element); @@ -216,13 +215,13 @@ public static function getInstalledLanguages($clientId = null, $processMetaData } } - // Process the metafile. - if ($processMetaFile) + // Process the language manifest. + if ($processManifest) { - $languages[$language->client_id][$language->element]->metafile = JInstaller::parseXMLInstallFile($metafile); + $languages[$language->client_id][$language->element]->manifest = JInstaller::parseXMLInstallFile($metafile); // No metadata found, not a valid language. - if (!is_array($languages[$language->client_id][$language->element]->metafile)) + if (!is_array($languages[$language->client_id][$language->element]->manifest)) { unset($languages[$language->client_id][$language->element]); continue; From e5e0383b1eddb2f681b5e6b34fcfbeb8ea839294 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Mon, 19 Sep 2016 23:44:27 +0100 Subject: [PATCH 16/19] allow ordering and improve performance --- libraries/joomla/language/helper.php | 67 +++++++++++++++++++--------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/libraries/joomla/language/helper.php b/libraries/joomla/language/helper.php index d336e442cfaa4..7ef4a97d0da52 100644 --- a/libraries/joomla/language/helper.php +++ b/libraries/joomla/language/helper.php @@ -9,6 +9,8 @@ defined('JPATH_PLATFORM') or die; +use Joomla\Utilities\ArrayHelper; + /** * Language helper class * @@ -164,12 +166,15 @@ public static function getLanguages($key = 'default') * @param integer $clientId The client app id. * @param boolean $processMetaData Fetch Language metadata. * @param boolean $processManifest Fetch Language manifest. + * @param string $orderField Field to order the results. + * @param string $orderDirection Direction to order the results. * * @return array Array with the language code, name, client_id and extension_id. * * @since __DEPLOY_VERSION__ */ - public static function getInstalledLanguages($clientId = null, $processMetaData = false, $processManifest = false) + public static function getInstalledLanguages($clientId = null, $processMetaData = false, $processManifest = false, + $orderField = null, $orderDirection = null) { static $installedLanguages = null; @@ -188,44 +193,66 @@ public static function getInstalledLanguages($clientId = null, $processMetaData } $languages = array(); + $clientId = (int) $clientId; + $clients = is_null($clientId) ? array(0, 1) : array($clientId); foreach($installedLanguages as $language) { - $clientPath = (int) $language->client_id === 0 ? JPATH_SITE : JPATH_ADMINISTRATOR; - $metafile = static::getLanguagePath($clientPath, $language->element) . '/' . $language->element . '.xml'; - - // Check if metafile is readable. - if (!is_readable($metafile)) + // If the language client is not needed continue cycle. Drop for performance. + if (!in_array((int) $language->client_id, $clients)) { continue; } $languages[$language->client_id][$language->element] = $language; - // Process the language metadata. - if ($processMetaData) + if ($processMetaData || $processManifest) { - $languages[$language->client_id][$language->element]->metadata = static::getMetadata($language->element); + $clientPath = (int) $language->client_id === 0 ? JPATH_SITE : JPATH_ADMINISTRATOR; + $metafile = static::getLanguagePath($clientPath, $language->element) . '/' . $language->element . '.xml'; - // No metadata found, not a valid language. - if (!is_array($languages[$language->client_id][$language->element]->metadata)) + // Process the language metadata. + if ($processMetaData) { - unset($languages[$language->client_id][$language->element]); - continue; + $languages[$language->client_id][$language->element]->metadata = static::getMetadata($language->element); + + // No metadata found, not a valid language. + if (!is_array($languages[$language->client_id][$language->element]->metadata)) + { + unset($languages[$language->client_id][$language->element]); + continue; + } + } + + // Process the language manifest. + if ($processManifest) + { + $languages[$language->client_id][$language->element]->manifest = JInstaller::parseXMLInstallFile($metafile); + + // No metadata found, not a valid language. + if (!is_array($languages[$language->client_id][$language->element]->manifest)) + { + unset($languages[$language->client_id][$language->element]); + continue; + } } } + } - // Process the language manifest. - if ($processManifest) - { - $languages[$language->client_id][$language->element]->manifest = JInstaller::parseXMLInstallFile($metafile); + // Order the list if needed. + if (!is_null($orderField) && !is_null($orderDirection)) + { + $orderDirection = strtolower($orderDirection) === 'desc' ? -1 : 1; - // No metadata found, not a valid language. - if (!is_array($languages[$language->client_id][$language->element]->manifest)) + foreach ($languages as $cId => $language) + { + // If the language client is not needed continue cycle. Drop for performance. + if (!in_array($cId, $clients)) { - unset($languages[$language->client_id][$language->element]); continue; } + + $languages[$cId] = ArrayHelper::sortObjects($languages[$cId], $orderField, $orderDirection, true, true); } } From 10fbe8031a0755906153d4efcd70471e87d5049b Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Tue, 20 Sep 2016 01:49:34 +0100 Subject: [PATCH 17/19] improvements --- libraries/joomla/language/helper.php | 145 +++++++++++++++++++-------- 1 file changed, 101 insertions(+), 44 deletions(-) diff --git a/libraries/joomla/language/helper.php b/libraries/joomla/language/helper.php index 7ef4a97d0da52..e303f883aa97f 100644 --- a/libraries/joomla/language/helper.php +++ b/libraries/joomla/language/helper.php @@ -107,9 +107,9 @@ public static function detectLanguage() */ public static function getLanguages($key = 'default') { - static $languages; + static $languages = array(); - if (empty($languages)) + if (!isset($languages[$key])) { // Installation uses available languages if (JFactory::getApplication()->getClientId() == 2) @@ -127,69 +127,111 @@ public static function getLanguages($key = 'default') } else { - $cache = JFactory::getCache('com_languages', ''); + $languages['default'] = static::getContentLanguages(true, null, 'ordering', 'ASC'); + $languages['lang_code'] = ArrayHelper::pivot($languages['default'], 'lang_code'); + $languages['sef'] = ArrayHelper::pivot($languages['default'], 'sef'); + } + } - if (!$languages = $cache->get('languages')) - { - $db = JFactory::getDbo(); + return $languages[$key]; + } - $query = $db->getQuery(true) - ->select('*') - ->from($db->quoteName('#__languages')) - ->where($db->quoteName('published') . ' = 1') - ->order($db->quoteName('ordering') . ' ASC'); + /** + * Get a list of content languages. + * + * @param integer $pivot The pivot of the array. + * @param integer $checkInstalled Check if the content language is installed. + * @param string $orderField Field to order the results. + * @param string $orderDirection Direction to order the results. + * + * @return array Array of the content languages. + * + * @since __DEPLOY_VERSION__ + */ + public static function getContentLanguages($checkInstalled = true, $pivot = 'lang_code', $orderField = null, $orderDirection = null) + { + static $contentLanguages = null; - $languages['default'] = $db->setQuery($query)->loadObjectList(); - $languages['sef'] = array(); - $languages['lang_code'] = array(); + if (is_null($contentLanguages)) + { + $cache = JFactory::getCache('com_languages', ''); - if (isset($languages['default'][0])) - { - foreach ($languages['default'] as $lang) - { - $languages['sef'][$lang->sef] = $lang; - $languages['lang_code'][$lang->lang_code] = $lang; - } - } + if (!$contentLanguages = $cache->get('contentlanguages')) + { + $db = JFactory::getDbo(); - $cache->store($languages, 'languages'); - } + $query = $db->getQuery(true) + ->select('*') + ->from($db->quoteName('#__languages')) + ->where($db->quoteName('published') . ' = 1'); + + $contentLanguages = $db->setQuery($query)->loadObjectList(); + + $cache->store($contentLanguages, 'contentlanguages'); } } - return $languages[$key]; + $languages = $contentLanguages; + + // Check if the language is installed, if needed. + if ($checkInstalled) + { + $languages = array_values(array_intersect_key(ArrayHelper::pivot($languages, 'lang_code'), static::getInstalledLanguages(0))); + } + + // Order the list, if needed. + if (!is_null($orderField) && !is_null($orderDirection)) + { + $languages = ArrayHelper::sortObjects($languages, $orderField, strtolower($orderDirection) === 'desc' ? -1 : 1, true, true); + } + + // Add the pivot, if needed. + if (!is_null($pivot)) + { + $languages = ArrayHelper::pivot($languages, $pivot); + } + + return $languages; } /** * Get a list of installed languages. * + * @param integer $pivot The pivot of the array. * @param integer $clientId The client app id. * @param boolean $processMetaData Fetch Language metadata. * @param boolean $processManifest Fetch Language manifest. * @param string $orderField Field to order the results. * @param string $orderDirection Direction to order the results. * - * @return array Array with the language code, name, client_id and extension_id. + * @return array Array with the installed languages. * * @since __DEPLOY_VERSION__ */ - public static function getInstalledLanguages($clientId = null, $processMetaData = false, $processManifest = false, - $orderField = null, $orderDirection = null) + public static function getInstalledLanguages($clientId = null, $processMetaData = false, + $processManifest = false, $pivot = 'element', $orderField = null, $orderDirection = null) { static $installedLanguages = null; if (is_null($installedLanguages)) { - $db = JFactory::getDbo(); + $cache = JFactory::getCache('com_languages', ''); + + if (!$installedLanguages = $cache->get('installedlanguages')) + { + $db = JFactory::getDbo(); + + $query = $db->getQuery(true) + ->select($db->quoteName(array('element', 'name', 'client_id', 'extension_id'))) + ->from($db->quoteName('#__extensions')) + ->where($db->quoteName('type') . ' = ' . $db->quote('language')) + ->where($db->quoteName('state') . ' = 0') + ->where($db->quoteName('enabled') . ' = 1'); - $query = $db->getQuery(true) - ->select($db->quoteName(array('element', 'name', 'client_id', 'extension_id'))) - ->from($db->quoteName('#__extensions')) - ->where($db->quoteName('type') . ' = ' . $db->quote('language')) - ->where($db->quoteName('state') . ' = 0') - ->where($db->quoteName('enabled') . ' = 1'); + $installedLanguages = $db->setQuery($query)->loadObjectList(); - $installedLanguages = $db->setQuery($query)->loadObjectList(); + $cache->store($installedLanguages, 'installedlanguages'); + } } $languages = array(); @@ -204,7 +246,7 @@ public static function getInstalledLanguages($clientId = null, $processMetaData continue; } - $languages[$language->client_id][$language->element] = $language; + $lang = $language; if ($processMetaData || $processManifest) { @@ -214,12 +256,11 @@ public static function getInstalledLanguages($clientId = null, $processMetaData // Process the language metadata. if ($processMetaData) { - $languages[$language->client_id][$language->element]->metadata = static::getMetadata($language->element); + $lang->metadata = static::getMetadata($language->element); // No metadata found, not a valid language. - if (!is_array($languages[$language->client_id][$language->element]->metadata)) + if (!is_array($lang->metadata)) { - unset($languages[$language->client_id][$language->element]); continue; } } @@ -227,19 +268,20 @@ public static function getInstalledLanguages($clientId = null, $processMetaData // Process the language manifest. if ($processManifest) { - $languages[$language->client_id][$language->element]->manifest = JInstaller::parseXMLInstallFile($metafile); + $lang->manifest = JInstaller::parseXMLInstallFile($metafile); // No metadata found, not a valid language. - if (!is_array($languages[$language->client_id][$language->element]->manifest)) + if (!is_array($lang->manifest)) { - unset($languages[$language->client_id][$language->element]); continue; } } } + + $languages[$language->client_id][] = $lang; } - // Order the list if needed. + // Order the list, if needed. if (!is_null($orderField) && !is_null($orderDirection)) { $orderDirection = strtolower($orderDirection) === 'desc' ? -1 : 1; @@ -256,6 +298,21 @@ public static function getInstalledLanguages($clientId = null, $processMetaData } } + // Add the pivot, if needed. + if (!is_null($pivot)) + { + foreach ($languages as $cId => $language) + { + // If the language client is not needed continue cycle. Drop for performance. + if (!in_array($cId, $clients)) + { + continue; + } + + $languages[$cId] = ArrayHelper::pivot($languages[$cId], $pivot); + } + } + return !is_null($clientId) ? $languages[$clientId] : $languages; } From b22185b622bbe4633da96a820851b6286c7d4d10 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Tue, 20 Sep 2016 02:02:57 +0100 Subject: [PATCH 18/19] use the new API --- libraries/cms/html/contentlanguage.php | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/libraries/cms/html/contentlanguage.php b/libraries/cms/html/contentlanguage.php index 185bcb50ba50a..bd79aa27b6d2a 100644 --- a/libraries/cms/html/contentlanguage.php +++ b/libraries/cms/html/contentlanguage.php @@ -37,32 +37,29 @@ abstract class JHtmlContentLanguage */ public static function existing($all = false, $translate = false) { - if (empty(static::$items)) + if (is_null($items)) { - // Get the database object and a new query object. - $db = JFactory::getDbo(); - $query = $db->getQuery(true); + $contentLanguages = JLanguageHelper::getContentLanguages(true, null, 'title', 'ASC'); - // Build the query. - $query->select('a.lang_code AS value, a.title AS text, a.title_native') - ->from('#__languages AS a') - ->where('a.published >= 0') - ->order('a.title'); + $items = array(); - // Set the query and load the options. - $db->setQuery($query); - static::$items = $db->loadObjectList(); + foreach($contentLanguages as $key => $language) + { + $items[$key]->value = $language->lang_code; + $items[$key]->text = $language->title; + $items[$key]->title_native = $language->title_native; + } } if ($all) { $all_option = array(new JObject(array('value' => '*', 'text' => $translate ? JText::alt('JALL', 'language') : 'JALL_LANGUAGE'))); - return array_merge($all_option, static::$items); + return array_merge($all_option, $items); } else { - return static::$items; + return $items; } } } From f51bc35b5cafd86a4529269eadf39bbbc85b65b7 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Tue, 20 Sep 2016 02:21:52 +0100 Subject: [PATCH 19/19] fix bugs --- libraries/joomla/language/helper.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libraries/joomla/language/helper.php b/libraries/joomla/language/helper.php index e303f883aa97f..4835567b2ed08 100644 --- a/libraries/joomla/language/helper.php +++ b/libraries/joomla/language/helper.php @@ -235,8 +235,7 @@ public static function getInstalledLanguages($clientId = null, $processMetaData } $languages = array(); - $clientId = (int) $clientId; - $clients = is_null($clientId) ? array(0, 1) : array($clientId); + $clients = is_null($clientId) ? array(0, 1) : array((int) $clientId); foreach($installedLanguages as $language) { @@ -256,7 +255,7 @@ public static function getInstalledLanguages($clientId = null, $processMetaData // Process the language metadata. if ($processMetaData) { - $lang->metadata = static::getMetadata($language->element); + $lang->metadata = static::parseXMLLanguageFile($metafile); // No metadata found, not a valid language. if (!is_array($lang->metadata))