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\Content\Text\BBCode;
12 use Friendica\Core\Hook;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\Session;
17 /* Define the hooks we want to use
18 * that is, we have settings, we need to save the settings and we want
19 * to modify the content of a posting when friendica prepares it.
22 function langfilter_install()
24 Hook::register('prepare_body_content_filter', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body_content_filter', 10);
25 Hook::register('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings');
26 Hook::register('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post');
30 * 1st check if somebody logged in is calling
31 * 2nd get the current settings
32 * 3rd parse a SMARTY3 template, replacing some translateable strings for the form
35 function langfilter_addon_settings(App $a, array &$data)
37 if (!Session::getLocalUser()) {
41 $enabled = DI::pConfig()->get(Session::getLocalUser(), 'langfilter', 'enable',
42 !DI::pConfig()->get(Session::getLocalUser(), 'langfilter', 'disable'));
44 $languages = DI::pConfig()->get(Session::getLocalUser(), 'langfilter', 'languages');
45 $minconfidence = DI::pConfig()->get(Session::getLocalUser(), 'langfilter', 'minconfidence', 0) * 100;
46 $minlength = DI::pConfig()->get(Session::getLocalUser(), 'langfilter', 'minlength', 32);
48 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/langfilter/');
49 $html = Renderer::replaceMacros($t, [
50 '$intro' => DI::l10n()->t('This addon tries to identify the language posts are written in. If it does not match any language specified below, posts will be hidden by collapsing them.'),
51 '$enabled' => ['langfilter_enable', DI::l10n()->t('Use the language filter'), $enabled],
52 '$languages' => ['langfilter_languages', DI::l10n()->t('Able to read'), $languages, DI::l10n()->t('List of abbreviations (ISO 639-1 codes) for languages you speak, comma separated. For example "de,it".')],
53 '$minconfidence' => ['langfilter_minconfidence', DI::l10n()->t('Minimum confidence in language detection'), $minconfidence, DI::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.')],
54 '$minlength' => ['langfilter_minlength', DI::l10n()->t('Minimum length of message body'), $minlength, DI::l10n()->t('Minimum number of characters in message body for filter to be used. Posts shorter than this will not be filtered. Note: Language detection is unreliable for short content (<200 characters).')],
58 'addon' => 'langfilter',
59 'title' => DI::l10n()->t('Language Filter'),
61 'submit' => ['langfilter-settings-submit' => DI::l10n()->t('Save Settings')],
66 * 1st check it's a logged in user calling
67 * 2nd check the langfilter form is to be saved
68 * 3rd save the settings to the DB for later usage
71 function langfilter_addon_settings_post(App $a, array &$b)
73 if (!Session::getLocalUser()) {
77 if (!empty($_POST['langfilter-settings-submit'])) {
78 $enable = intval($_POST['langfilter_enable'] ?? 0);
79 $languages = trim($_POST['langfilter_languages'] ?? '');
80 $minconfidence = max(0, min(100, intval($_POST['langfilter_minconfidence'] ?? 0))) / 100;
81 $minlength = intval($_POST['langfilter_minlength'] ?? 32);
82 if ($minlength <= 0) {
86 DI::pConfig()->set(Session::getLocalUser(), 'langfilter', 'enable' , $enable);
87 DI::pConfig()->set(Session::getLocalUser(), 'langfilter', 'languages' , $languages);
88 DI::pConfig()->set(Session::getLocalUser(), 'langfilter', 'minconfidence', $minconfidence);
89 DI::pConfig()->set(Session::getLocalUser(), 'langfilter', 'minlength' , $minlength);
93 /* Actually filter postings by their language
94 * 1st check if the user wants to filter postings
95 * 2nd get the user settings which languages shall be not filtered out
96 * 3rd extract the language of a posting
97 * 4th if the determined language does not fit to the spoken languages
98 * of the user, then collapse the posting, but provide a link to
102 function langfilter_prepare_body_content_filter(App $a, &$hook_data)
104 $logged_user = Session::getLocalUser();
109 // Never filter own messages
110 // TODO: find a better way to extract this
111 $logged_user_profile = DI::baseUrl()->get() . '/profile/' . $a->getLoggedInUserNickname();
112 if ($logged_user_profile == $hook_data['item']['author-link']) {
116 // Don't filter if language filter is disabled
117 if (!DI::pConfig()->get($logged_user, 'langfilter', 'enable',
118 !DI::pConfig()->get($logged_user, 'langfilter', 'disable'))
123 $naked_body = strip_tags(
124 $hook_data['item']['rendered-html']
125 ??''?: // Equivalent of !empty()
126 BBCode::convert($hook_data['item']['body'], false, BBCode::ACTIVITYPUB, true)
129 $naked_body = preg_replace('#\s+#', ' ', trim($naked_body));
131 // Don't filter if body lenght is below minimum
132 $minlen = DI::pConfig()->get(Session::getLocalUser(), 'langfilter', 'minlength', 32);
137 if (strlen($naked_body) < $minlen) {
141 $read_languages_string = DI::pConfig()->get(Session::getLocalUser(), 'langfilter', 'languages');
142 $minconfidence = DI::pConfig()->get(Session::getLocalUser(), 'langfilter', 'minconfidence');
144 // Don't filter if no spoken languages are configured
145 if (!$read_languages_string) {
148 $read_languages_array = explode(',', $read_languages_string);
150 $iso639 = new Matriphe\ISO639\ISO639;
152 // Extract the language of the post
153 if (!empty($hook_data['item']['language'])) {
154 $languages = json_decode($hook_data['item']['language'], true);
155 if (!is_array($languages)) {
159 foreach ($languages as $iso2 => $confidence) {
167 $lang = $iso639->languageByCode1($iso2);
169 $opts = $hook_data['item']['postopts'];
171 // no options associated to post
175 if (!preg_match('/\blang=([^;]*);([^:]*)/', $opts, $matches)) {
176 // no lang options associated to post
181 $confidence = $matches[2];
183 $iso2 = $iso639->code1ByLanguage($lang);
186 // Do not filter if language detection confidence is too low
187 if ($minconfidence && $confidence < $minconfidence) {
195 if (!in_array($iso2, $read_languages_array)) {
196 $hook_data['filter_reasons'][] = DI::l10n()->t('Filtered language: %s', ucfirst($lang));