]> git.mxchange.org Git - friendica-addons.git/blob - langfilter/langfilter.php
Merge branch '3.6-rc'
[friendica-addons.git] / langfilter / langfilter.php
1 <?php
2 /*
3  * Name: Language Filter
4  * Version: 0.1
5  * Description: Filters out postings in languages not spoken by the users
6  * Author: Tobias Diekershoff <https://f.diekershoff.de/u/tobias>
7  * License: MIT
8  */
9
10 use Friendica\App;
11 use Friendica\Core\Addon;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
14
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.
18  */
19
20 function langfilter_install()
21 {
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');
25 }
26
27 function langfilter_uninstall()
28 {
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');
32 }
33
34 /* The settings
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
38  */
39 function langfilter_addon_settings(&$a,&$s) {
40         if(! local_user())
41                 return;
42
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');
47         if(! $languages)
48                 $languages = 'en,de,fr,it,es';
49
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'),
59         ]);
60
61         return;
62 }
63 /* Save the 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
67  */
68 function langfilter_addon_settings_post(&$a,&$b) {
69         if(! local_user())
70                 return;
71
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) {
79                         $minconfidence = 0;
80                 } elseif ($minconfidence < 0) {
81                         $minconfidence = 0;
82                 } elseif ($minconfidence > 100) {
83                         $minconfidence = 100;
84                 }
85                 PConfig::set(local_user(), 'langfilter', 'minconfidence', $minconfidence / 100.0);
86
87                 $minlength = 0 + $_POST['langfilter_minlength'];
88                 if (!$minlength) {
89                         $minlength = 32;
90                 } elseif ($minlengt8h < 0) {
91                         $minlength = 32;
92                 }
93                 PConfig::set(local_user(), 'langfilter', 'minlength', $minlength);
94
95                 info(L10n::t('Language Filter Settings saved.') . EOL);
96         }
97 }
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
104  *     expand it again.
105  */
106 function langfilter_prepare_body(&$a,&$b) {
107
108     $logged_user = local_user();
109     if ( ! $logged_user ) return;
110
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;
115
116     # Don't filter if language filter is disabled
117     if( get_pconfig($logged_user,'langfilter','disable') ) return;
118
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;
123
124     $spoken_config = get_pconfig(local_user(),'langfilter','languages');
125     $minconfidence = get_pconfig(local_user(),'langfilter','minconfidence');
126
127     # Don't filter if no spoken languages are configured 
128     if ( ! $spoken_config ) return;
129     $spoken_languages = explode(',', $spoken_config);
130
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
136
137     $lang = $matches[1];
138     $confidence = $matches[2];
139
140     # Do not filter if language detection confidence is too low
141     if ( $minconfidence && $confidence < $minconfidence ) return;
142
143     $iso2 = Text_LanguageDetect_ISO639::nameToCode2($lang);
144
145     if ( ! $iso2 ) return;
146     $spoken = in_array($iso2, $spoken_languages);
147
148         if (!$spoken) {
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>';
151         }
152 }
153 ?>