Skip to content
Permalink
Browse files

guess user languages; fixes #809

  • Loading branch information...
following5 committed Sep 9, 2015
1 parent 5125258 commit cfb6cb6abc91c4ec19101cbe994b39ca4bfb6acc
@@ -613,6 +613,12 @@ function dbv_138() // add some reasonable indices, e.g. to optimize special-pur
sql("ALTER TABLE `watches_logqueue` ADD INDEX `user_id` (`user_id`)");
}
function dbv_139()
{
if (!sql_field_exists('user', 'language_guessed'))
sql("ALTER TABLE `user` ADD COLUMN `language_guessed` tinyint(1) NOT NULL default '0' AFTER `language`");
}
// When adding new mutations, take care that they behave well if run multiple
// times. This improves robustness of database versioning.
@@ -113,11 +113,13 @@
$opt['locale']['EN']['format']['dot1000'] = ',';
$opt['locale']['EN']['format']['colonspace'] = '';
$opt['locale']['EN']['country'] = 'GB';
$opt['locale']['EN']['primary_lang_of'] = array('GB', 'US', 'AU', 'NZ', 'ZA', 'IE', 'SG');
$opt['locale']['EN']['page']['subtitle1'] = 'Geocaching with Opencaching';
$opt['locale']['EN']['page']['subtitle2'] = '';
$opt['locale']['EN']['page']['license_url'] = 'http://creativecommons.org/licenses/by-nc-nd/3.0/de/deed.en';
$opt['locale']['EN']['page']['license'] = '<a rel="license" href="%1"><img alt="Creative Commons License Terms" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-nd/3.0/de/88x31.png" /></a><div style="text-align:center; margin:8px 0 0 6px;">The Opencaching.de <a href="articles.php?page=impressum#datalicense">content</a> is licensed under Creative Commons <a rel="license" href="%1" target="_blank">BY-BC-ND 3.0 DE</a>.</div>';
$opt['locale']['EN']['helpwiki'] = "http://wiki.opencaching.de/index.php/";
$opt['locale']['EN']['mostly_translated'] = true;
$opt['locale']['DE']['format']['dm'] = '%d.%m.';
$opt['locale']['DE']['format']['dateshort'] = '%d.%m.%y';
@@ -131,11 +133,13 @@
$opt['locale']['DE']['format']['phpdatetime'] = 'd.m.Y H:i';
$opt['locale']['DE']['format']['colonspace'] = '';
$opt['locale']['DE']['country'] = 'DE';
$opt['locale']['DE']['primary_lang_of'] = array('DE', 'AT', 'CH', 'LI');
$opt['locale']['DE']['page']['subtitle1'] = 'Geocaching mit Opencaching';
$opt['locale']['DE']['page']['subtitle2'] = '';
$opt['locale']['DE']['page']['license_url'] = 'http://creativecommons.org/licenses/by-nc-nd/3.0/de/';
$opt['locale']['DE']['page']['license'] = '<a rel="license" href="%1"><img alt="Creative Commons Lizenzvertrag" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-nd/3.0/de/88x31.png" /></a><div style="text-align:center; margin:8px 0 0 6px;">Die <a href="articles.php?page=impressum#datalicense">Inhalte</a> von Opencaching.de stehen unter der Creative-Commons-Lizenz <a rel="license" href="%1">BY-NC-ND 3.0 DE</a>.</div>';
$opt['locale']['DE']['helpwiki'] = "http://wiki.opencaching.de/index.php/";
$opt['locale']['DE']['mostly_translated'] = true;
$opt['locale']['IT']['format']['dateshort'] = '%d/%m/%y';
$opt['locale']['IT']['format']['dm'] = '%d/%m';
@@ -149,10 +153,12 @@
$opt['locale']['IT']['format']['phpdatetime'] = 'd/m/Y H:i';
$opt['locale']['IT']['format']['colonspace'] = '';
$opt['locale']['IT']['country'] = 'IT';
$opt['locale']['IT']['primary_lang_of'] = array('IT', 'SM', 'VA');
$opt['locale']['IT']['page']['subtitle1'] = 'Geocaching con Opencaching';
$opt['locale']['IT']['page']['subtitle2'] = '';
$opt['locale']['IT']['page']['license_url'] = 'http://creativecommons.org/licenses/by-nc-nd/3.0/de/deed.it';
$opt['locale']['IT']['page']['license'] = '<a rel="license" href="%1" target="_blank"><img alt="Creative Commons License Terms" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-nd/3.0/de/88x31.png" /></a><div style="text-align:center; margin:8px 0 0 6px;">Il <a href="articles.php?page=impressum#datalicense">contenuto</a> di Opencaching.de è rilasciato sotto licenza Creative Commons <a rel="license" href="%1" target="_blank">BY-NC-ND 3.0 DE</a>.</div>';
$opt['locale']['IT']['mostly_translated'] = true;
$opt['locale']['ES']['format']['dateshort'] = '%d/%m/%y';
$opt['locale']['ES']['format']['dm'] = '%d/%m';
@@ -166,10 +172,12 @@
$opt['locale']['ES']['format']['phpdatetime'] = 'd/m/Y H:i';
$opt['locale']['ES']['format']['colonspace'] = '';
$opt['locale']['ES']['country'] = 'ES';
$opt['locale']['ES']['primary_lang_of'] = array('ES', 'AR', 'CL', 'CR', 'DO', 'EC', 'SV', 'GT', 'HN', 'CO', 'CU', 'MX', 'NI', 'PA', 'PY', 'PE', 'UY', 'VE');
$opt['locale']['ES']['page']['subtitle1'] = 'Geocaching con Opencaching';
$opt['locale']['ES']['page']['subtitle2'] = '';
$opt['locale']['ES']['page']['license_url'] = 'http://creativecommons.org/licenses/by-nc-nd/3.0/de/deed.es_ES';
$opt['locale']['ES']['page']['license'] = '<a rel="license" href="%1" target="_blank"><img alt="Creative Commons License Terms" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-nd/3.0/de/88x31.png" /></a><div style="text-align:center; margin:8px 0 0 6px;">El <a href="articles.php?page=impressum#datalicense">contenido</a> está disponible bajo Creative Commons <a rel="license" href="%1" target="_blank">BY-NC-ND 3.0 DE</a> licencia.</div>';
$opt['locale']['ES']['mostly_translated'] = true;
$opt['locale']['FR']['format']['dm'] = '%d.%m.';
$opt['locale']['FR']['format']['dateshort'] = '%d.%m.%y';
@@ -183,11 +191,13 @@
$opt['locale']['FR']['format']['phpdatetime'] = 'd-m-Y H:i';
$opt['locale']['FR']['format']['colonspace'] = ' ';
$opt['locale']['FR']['country'] = 'FR';
$opt['locale']['FR']['primary_lang_of'] = array('FR');
$opt['locale']['FR']['page']['subtitle1'] = 'Geocaching avec Opencaching';
$opt['locale']['FR']['page']['subtitle2'] = '';
$opt['locale']['FR']['page']['license_url'] = 'http://creativecommons.org/licenses/by-nc-nd/3.0/de/deed.fr';
$opt['locale']['FR']['page']['license'] = '<a rel="license" href="%1"><img alt="Creative Commons License Terms" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-nd/3.0/de/88x31.png" /></a><div style="text-align:center; margin:8px 0 0 6px;">Le<a href="articles.php?page=impressum#datalicense">contenu</a> de Opencaching.de sont sous licence Creative Commons <a rel="license" href="%1" target="_blank">BY-BC-ND 3.0 DE</a>.</div>';
$opt['locale']['FR']['helpwiki'] = "http://wiki.opencaching.de/index.php/";
$opt['locale']['FR']['mostly_translated'] = true;
/*
$opt['locale']['SV']['format']['dateshort'] = '%y-%m-%d';
@@ -40,6 +40,7 @@ CREATE TABLE `user` (
`notify_radius` int(10) unsigned NOT NULL default '0',
`notify_oconly` tinyint(1) NOT NULL default '1',
`language` char(2) default NULL,
`language_guessed` tinyint(1) NOT NULL default '0',
`admin` smallint(5) unsigned NOT NULL default '0',
`data_license` tinyint(1) NOT NULL default '0',
`description` mediumtext NOT NULL,
@@ -120,7 +120,7 @@ function verify()
}
if (isset($locale))
sql("UPDATE `user` SET `last_login`=NOW(), `language`='&2' WHERE `user_id`='&1'", $this->userid, $locale);
sql("UPDATE `user` SET `last_login`=NOW(), `language`='&2', `language_guessed`=0 WHERE `user_id`='&1'", $this->userid, $locale);
else
sql("UPDATE `user` SET `last_login`=NOW() WHERE `user_id`='&1'", $this->userid);
@@ -12,8 +12,13 @@
function register_errorhandlers()
{
set_error_handler('errorhandler', E_ERROR);
register_shutdown_function('shutdownhandler');
global $opt;
if ($opt['gui'] == GUI_HTML)
{
set_error_handler('errorhandler', E_ERROR);
register_shutdown_function('shutdownhandler');
}
}
function errorhandler($errno, $errstr, $errfile, $errline)
@@ -1336,5 +1336,55 @@ function showStatFounds()
// TODO: make customisable in user profile, see #241
return false;
}
function guessLanguage()
{
global $opt;
$language = false;
// If the user has selected a country and a translation is available for
// that country's primary language, use this language.
$country = $this->getCountryCode();
if ($country)
{
foreach ($opt['locale'] as $code => $props)
if ($props['mostly_translated'] && in_array($country, $props['primary_lang_of']))
$language = $code;
}
if (!$language)
{
// If the user has logged caches with at least three descriptions,
// at least 65% of those descriptions have the same language,
// and a translation is available for that language, use it.
$rs = sql("
SELECT COUNT(*) AS `count`, `cache_desc`.`language`
FROM `cache_logs`
JOIN `cache_desc` ON `cache_desc`.`cache_id`=`cache_logs`.`cache_id`
WHERE `cache_logs`.`user_id`='&1'
GROUP BY `cache_desc`.`language`
ORDER BY `count` DESC",
$this->nUserId
);
$total = 0;
while ($r = sql_fetch_assoc($rs))
{
if ($total == 0)
$first = $r;
$total += $r['count'];
}
sql_free_result($rs);
if ($total >= 3 && $first['count'] / $total >= 0.65)
if (isset($opt['locale'][$first['language']])
&& $opt['locale'][$first['language']]['mostly_translated'])
$language = $first['language'];
}
return $language;
}
}
?>
@@ -122,7 +122,7 @@ function verify()
}
if (isset($opt['template']['locale']))
sqlf("UPDATE `user` SET `last_login`=NOW(), `language`='&2' WHERE `user_id`='&1'", $this->userid, $opt['template']['locale']);
sqlf("UPDATE `user` SET `last_login`=NOW(), `language`='&2', `language_guessed`=0 WHERE `user_id`='&1'", $this->userid, $opt['template']['locale']);
else
sqlf("UPDATE `user` SET `last_login`=NOW() WHERE `user_id`='&1'", $this->userid);
@@ -0,0 +1,40 @@
<?php
/***************************************************************************
* For license information see doc/license.txt
*
* This script guesses the primary language of all users who did not login
* after release of OC 3.0.14.
*
* Unicode Reminder メモ
***************************************************************************/
$opt['rootpath'] = __DIR__ . '/../../htdocs/';
require($opt['rootpath'] . 'lib2/cli.inc.php');
require($opt['rootpath'] . 'lib2/logic/user.class.php');
$write = ($argc == 2 && $argv[1] = 'write');
if (!$write)
echo "use parameter 'write' to write changes to database\n";
$processed = 0;
$set = 0;
$rs = sql("
SELECT `user_id` FROM `user` WHERE `language` IS NULL");
while ($r = sql_fetch_assoc($rs))
{
$user = new user($r['user_id']);
$lang = $user->guessLanguage();
if ($lang)
{
if ($write)
sql("UPDATE `user` SET `language`='&2', `language_guessed`=1 WHERE `user_id`='&1'",
$r['user_id'], $lang);
++$set;
}
if (++$processed % 1000 == 0)
echo "$set of $processed " . ($write ? '' : 'would be ') . "set\n";
}
echo "$set of $processed " . ($write ? '' : 'would be ') . "set\n";
sql_free_result($rs);
?>

0 comments on commit cfb6cb6

Please sign in to comment.
You can’t perform that action at this time.