3 * Name: Language Filter
5 * Description: Filters out postings in languages not spoken by the users
6 * Author: Tobias Diekershoff <https://f.diekershoff.de/u/tobias>
11 use Friendica\Core\Addon;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
15 /* Define the hooks we want to use
16 * that is, we have settings, we need to save the settings and we want
17 * to modify the content of a posting when friendica prepares it.
20 function langfilter_install()
22 Addon::registerHook('prepare_body', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body', 10);
23 Addon::registerHook('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings');
24 Addon::registerHook('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post');
27 function langfilter_uninstall()
29 Addon::unregisterHook('prepare_body', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body');
30 Addon::unregisterHook('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings');
31 Addon::unregisterHook('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post');
35 * 1st check if somebody logged in is calling
36 * 2nd get the current settings
37 * 3rd parse a SMARTY3 template, replacing some translateable strings for the form
39 function langfilter_addon_settings(&$a,&$s) {
43 $enable_checked = (intval(get_pconfig(local_user(),'langfilter','disable')) ? '' : ' checked="checked" ');
44 $languages = get_pconfig(local_user(),'langfilter','languages');
45 $minconfidence = get_pconfig(local_user(),'langfilter','minconfidence')*100;
46 $minlength = get_pconfig(local_user(),'langfilter','minlength');
48 $languages = 'en,de,fr,it,es';
50 $t = get_markup_template("settings.tpl", "addon/langfilter/");
51 $s .= replace_macros($t, [
52 '$title' => L10n::t("Language Filter"),
53 '$intro' => L10n::t('This addon tries to identify the language of a postings. If it does not match any language spoken by you (see below) the posting will be collapsed. Remember detecting the language is not perfect, especially with short postings.'),
54 '$enabled' => ['langfilter_enable', L10n::t('Use the language filter'), $enable_checked, ''],
55 '$languages' => ['langfilter_languages', L10n::t('I speak'), $languages, L10n::t('List of abbreviations (iso2 codes) for languages you speak, comma separated. For example "de,it".')],
56 '$minconfidence' => ['langfilter_minconfidence', L10n::t('Minimum confidence in language detection'), $minconfidence, L10n::t('Minimum confidence in language detection being correct, from 0 to 100. Posts will not be filtered when the confidence of language detection is below this percent value.')],
57 '$minlength' => ['langfilter_minlength', L10n::t('Minimum length of message body'), $minlength, L10n::t('Minimum length of message body for language filter to be used. Posts shorter than this number of characters will not be filtered.')],
58 '$submit' => L10n::t('Save Settings'),
64 * 1st check it's a logged in user calling
65 * 2nd check the langfilter form is to be saved
66 * 3rd save the settings to the DB for later usage
68 function langfilter_addon_settings_post(&$a,&$b) {
72 if ($_POST['langfilter-settings-submit']) {
73 PConfig::set(local_user(), 'langfilter', 'languages', trim($_POST['langfilter_languages']));
74 $enable = ((x($_POST, 'langfilter_enable')) ? intval($_POST['langfilter_enable']) : 0);
75 $disable = 1 - $enable;
76 PConfig::set(local_user(), 'langfilter', 'disable', $disable);
77 $minconfidence = 0 + $_POST['langfilter_minconfidence'];
78 if (!$minconfidence) {
80 } elseif ($minconfidence < 0) {
82 } elseif ($minconfidence > 100) {
85 PConfig::set(local_user(), 'langfilter', 'minconfidence', $minconfidence / 100.0);
87 $minlength = 0 + $_POST['langfilter_minlength'];
90 } elseif ($minlengt8h < 0) {
93 PConfig::set(local_user(), 'langfilter', 'minlength', $minlength);
95 info(L10n::t('Language Filter Settings saved.') . EOL);
98 /* Actually filter postings by their language
99 * 1st check if the user wants to filter postings
100 * 2nd get the user settings which languages shall be not filtered out
101 * 3rd extract the language of a posting
102 * 4th if the determined language does not fit to the spoken languages
103 * of the user, then collapse the posting, but provide a link to
106 function langfilter_prepare_body(&$a,&$b) {
108 $logged_user = local_user();
109 if ( ! $logged_user ) return;
111 # Never filter own messages
112 # TODO: find a better way to extract this
113 $logged_user_profile = $a->config['system']['url'] . '/profile/' . $a->user['nickname'];
114 if ( $logged_user_profile == $b['item']['author-link'] ) return;
116 # Don't filter if language filter is disabled
117 if( get_pconfig($logged_user,'langfilter','disable') ) return;
119 # Don't filter if body lenght is below minimum
120 $minlen = get_pconfig(local_user(),'langfilter','minlength');
121 if ( ! $minlen ) $minlen = 32;
122 if ( strlen($b['item']['body']) < $minlen ) return;
124 $spoken_config = get_pconfig(local_user(),'langfilter','languages');
125 $minconfidence = get_pconfig(local_user(),'langfilter','minconfidence');
127 # Don't filter if no spoken languages are configured
128 if ( ! $spoken_config ) return;
129 $spoken_languages = explode(',', $spoken_config);
131 # Extract the language of the post
132 $opts = $b['item']['postopts'];
133 if ( ! $opts ) return; # no options associated to post
134 if ( ! preg_match('/\blang=([^;]*);([^:]*)/', $opts, $matches ) )
135 return; # no lang options associated to post
138 $confidence = $matches[2];
140 # Do not filter if language detection confidence is too low
141 if ( $minconfidence && $confidence < $minconfidence ) return;
143 $iso2 = Text_LanguageDetect_ISO639::nameToCode2($lang);
145 if ( ! $iso2 ) return;
146 $spoken = in_array($iso2, $spoken_languages);
149 $rnd = random_string(8);
150 $b['html'] = '<div id="langfilter-wrap-' . $rnd . '" class="fakelink" onclick=openClose(\'langfilter-' . $rnd . '\'); >' . L10n::t('unspoken language %s - Click to open/close', $lang) . '</div><div id="langfilter-' . $rnd . '" style="display: none; " >' . $b['html'] . '</div>';