Passed
Pull Request — development (#3540)
by Emanuele
07:11
created

list_getNumLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file contains the database work for languages.
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * @version 2.0 dev
11
 *
12
 */
13
14
use ElkArte\Util;
15
use ElkArte\XmlArray;
16
17
/**
18
 * How many languages?
19
 *
20
 * - Callback for the list in action_edit().
21
 *
22
 * @package Languages
23
 */
24
function list_getNumLanguages()
25
{
26
	return count(getLanguages());
27
}
28
29
/**
30
 * Fetch the actual language information.
31
 *
32
 * What it does:
33
 *
34
 * - Callback for $listOptions['get_items']['function'] in action_edit.
35
 * - Determines which languages are available by looking for the "index.{language}.php" file.
36
 * - Also figures out how many users are using a particular language.
37
 *
38
 * @package Languages
39
 */
40
function list_getLanguages()
41
{
42
	global $settings, $language, $txt;
43
44
	$db = database();
45
46
	$languages = array();
47
	// Keep our old entries.
48
	$old_txt = $txt;
49
	$backup_actual_theme_dir = $settings['actual_theme_dir'];
50
	$backup_base_theme_dir = !empty($settings['base_theme_dir']) ? $settings['base_theme_dir'] : '';
51
52
	// Override these for now.
53
	$settings['actual_theme_dir'] = $settings['base_theme_dir'] = $settings['default_theme_dir'];
54
	$all_languages = getLanguages();
55
56
	// Put them back.
57
	$settings['actual_theme_dir'] = $backup_actual_theme_dir;
58
	if (!empty($backup_base_theme_dir))
59
	{
60
		$settings['base_theme_dir'] = $backup_base_theme_dir;
61
	}
62
	else
63
	{
64
		unset($settings['base_theme_dir']);
65
	}
66
67
	// Get the language files and data...
68
	foreach ($all_languages as $lang)
69
	{
70
		// Load the file to get the character set.
71
		require($lang['location']);
72
73
		$languages[$lang['filename']] = array(
74
			'id' => basename($lang['filename'], '.php'),
75
			'count' => 0,
76
			'char_set' => 'UTF-8',
77
			'default' => $language == $lang['name'] || ($language == '' && strtolower($lang['name']) == 'english'),
78
			'locale' => $txt['lang_locale'],
79
			'name' => Util::ucwords(strtr($lang['name'], array('_' => ' ', '-utf8' => ''))),
80
		);
81
	}
82
83
	// Work out how many people are using each language.
84
	$db->fetchQuery('
85
		SELECT 
86
			lngfile, COUNT(*) AS num_users
87
		FROM {db_prefix}members
88
		GROUP BY lngfile',
89
		array()
90
	)->fetch_callback(
91
		function ($row) use (&$languages, $language) {
92
			// Default?
93
			if (empty($row['lngfile']) || !isset($languages[$row['lngfile']]))
94
			{
95
				$row['lngfile'] = $language;
96
			}
97
98
			if (!isset($languages[$row['lngfile']]) && isset($languages['english']))
99
			{
100
				$languages['english']['count'] += $row['num_users'];
101
			}
102
			elseif (isset($languages[$row['lngfile']]))
103
			{
104
				$languages[$row['lngfile']]['count'] += $row['num_users'];
105
			}
106
		}
107
	);
108
109
	// Restore the current users language.
110
	$txt = $old_txt;
111
112
	// Return how many we have.
113
	return $languages;
114
}
115
116
/**
117
 * Gets a list of available languages from the mother ship
118
 *
119
 * - Will return a subset if searching, otherwise all available
120
 *
121
 * @return array
122
 * @package Languages
123
 */
124
function list_getLanguagesList()
125
{
126
	global $context, $txt, $scripturl;
127
128
	// We're going to use this URL.
129
	// @todo no we are not, this needs to be changed - again
130
	$url = 'http://download.elkarte.net/fetch_language.php?version=' . urlencode(strtr(FORUM_VERSION, array('ElkArte ' => '')));
131
132
	// Load the class file and stick it into an array.
133
	$language_list = new XmlArray(fetch_web_data($url), true);
134
135
	// Check that the site responded and that the language exists.
136
	if (!$language_list->exists('languages'))
137
	{
138
		$context['langfile_error'] = 'no_response';
139
	}
140
	elseif (!$language_list->exists('languages/language'))
141
	{
142
		$context['langfile_error'] = 'no_files';
143
	}
144
	else
145
	{
146
		$language_list = $language_list->path('languages[0]');
147
		$lang_files = $language_list->set('language');
148
		$languages = array();
149
		foreach ($lang_files as $file)
150
		{
151
			// Were we searching?
152
			if (!empty($context['elk_search_term']) && strpos($file->fetch('name'), Util::strtolower($context['elk_search_term'])) === false)
153
			{
154
				continue;
155
			}
156
157
			$languages[] = array(
158
				'id' => $file->fetch('id'),
159
				'name' => Util::ucwords($file->fetch('name')),
160
				'version' => $file->fetch('version'),
161
				'utf8' => $txt['yes'],
162
				'description' => $file->fetch('description'),
163
				'install_link' => '<a href="' . $scripturl . '?action=admin;area=languages;sa=downloadlang;did=' . $file->fetch('id') . ';' . $context['session_var'] . '=' . $context['session_id'] . '">' . $txt['add_language_elk_install'] . '</a>',
164
			);
165
		}
166
		if (empty($languages))
167
		{
168
			$context['langfile_error'] = 'no_files';
169
		}
170
		else
171
		{
172
			return $languages;
173
		}
174
	}
175
}
176
177
/**
178
 * Finds installed language files of type lang
179
 *
180
 * @param string $lang
181
 *
182
 * @return array|bool
183
 */
184
function findPossiblePackages($lang)
185
{
186
	$db = database();
187
188
	$request = $db->query('', '
189
		SELECT 
190
			id_install, filename
191
		FROM {db_prefix}log_packages
192
		WHERE package_id LIKE {string:contains_lang}
193
			AND install_state = {int:installed}',
194
		array(
195
			'contains_lang' => 'elk_' . $lang . '_contribs:elk_' . $lang . '',
196
			'installed' => 1,
197
		)
198
	);
199
	$file_name = '';
200
	if ($request->num_rows() > 0)
201
	{
202
		list ($pid, $file_name) = $request->fetch_row();
203
	}
204
	$request->free_result();
205
206
	if (!empty($pid))
207
	{
208
		return array($pid, $file_name);
209
	}
210
211
	return false;
212
}
213