]> git.mxchange.org Git - friendica-addons.git/blobdiff - langfilter/langfilter.php
[various] Remove redundant profile_uid field from profile query
[friendica-addons.git] / langfilter / langfilter.php
index 1f23cedaa7b5e2b727ff511e5873832270caf203..798e1a0494ce7e39d0e33a218d47a65770a39472 100644 (file)
@@ -9,9 +9,9 @@
 
 use Friendica\App;
 use Friendica\Content\Text\BBCode;
-use Friendica\Core\Addon;
-use Friendica\Core\L10n;
-use Friendica\Core\PConfig;
+use Friendica\Core\Hook;
+use Friendica\Core\Renderer;
+use Friendica\DI;
 
 /* Define the hooks we want to use
  * that is, we have settings, we need to save the settings and we want
@@ -20,17 +20,17 @@ use Friendica\Core\PConfig;
 
 function langfilter_install()
 {
-       Addon::registerHook('prepare_body_content_filter', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body_content_filter', 10);
-       Addon::registerHook('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings');
-       Addon::registerHook('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post');
+       Hook::register('prepare_body_content_filter', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body_content_filter', 10);
+       Hook::register('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings');
+       Hook::register('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post');
 }
 
 function langfilter_uninstall()
 {
-       Addon::unregisterHook('prepare_body_content_filter', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body_content_filter');
-       Addon::unregisterHook('prepare_body', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body');
-       Addon::unregisterHook('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings');
-       Addon::unregisterHook('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post');
+       Hook::unregister('prepare_body_content_filter', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body_content_filter');
+       Hook::unregister('prepare_body', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body');
+       Hook::unregister('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings');
+       Hook::unregister('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post');
 }
 
 /* The settings
@@ -45,20 +45,20 @@ function langfilter_addon_settings(App $a, &$s)
                return;
        }
 
-       $enable_checked = (intval(PConfig::get(local_user(), 'langfilter', 'disable')) ? '' : ' checked="checked" ');
-       $languages      = PConfig::get(local_user(), 'langfilter', 'languages');
-       $minconfidence  = PConfig::get(local_user(), 'langfilter', 'minconfidence') * 100;
-       $minlength      = PConfig::get(local_user(), 'langfilter', 'minlength');
-
-       $t = get_markup_template("settings.tpl", "addon/langfilter/");
-       $s .= replace_macros($t, [
-               '$title'         => L10n::t("Language Filter"),
-               '$intro'         => L10n::t('This addon tries to identify the language posts are writen in. If it does not match any language specifed below, posts will be hidden by collapsing them.'),
-               '$enabled'       => ['langfilter_enable', L10n::t('Use the language filter'), $enable_checked, ''],
-               '$languages'     => ['langfilter_languages', L10n::t('Able to read'), $languages, L10n::t('List of abbreviations (iso2 codes) for languages you speak, comma separated. For example "de,it".')],
-               '$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.')],
-               '$minlength'     => ['langfilter_minlength', L10n::t('Minimum length of message body'), $minlength, 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).')],
-               '$submit'        => L10n::t('Save Settings'),
+       $enable_checked = (intval(DI::pConfig()->get(local_user(), 'langfilter', 'disable')) ? '' : ' checked="checked" ');
+       $languages      = DI::pConfig()->get(local_user(), 'langfilter', 'languages');
+       $minconfidence  = DI::pConfig()->get(local_user(), 'langfilter', 'minconfidence') * 100;
+       $minlength      = DI::pConfig()->get(local_user(), 'langfilter', 'minlength');
+
+       $t = Renderer::getMarkupTemplate("settings.tpl", "addon/langfilter/");
+       $s .= Renderer::replaceMacros($t, [
+               '$title'         => DI::l10n()->t("Language Filter"),
+               '$intro'         => DI::l10n()->t('This addon tries to identify the language posts are writen in. If it does not match any language specifed below, posts will be hidden by collapsing them.'),
+               '$enabled'       => ['langfilter_enable', DI::l10n()->t('Use the language filter'), $enable_checked, ''],
+               '$languages'     => ['langfilter_languages', DI::l10n()->t('Able to read'), $languages, DI::l10n()->t('List of abbreviations (iso2 codes) for languages you speak, comma separated. For example "de,it".')],
+               '$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.')],
+               '$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).')],
+               '$submit'        => DI::l10n()->t('Save Settings'),
        ]);
 
        return;
@@ -77,10 +77,10 @@ function langfilter_addon_settings_post(App $a, &$b)
        }
 
        if (!empty($_POST['langfilter-settings-submit'])) {
-               PConfig::set(local_user(), 'langfilter', 'languages', trim($_POST['langfilter_languages']));
-               $enable = (x($_POST, 'langfilter_enable') ? intval($_POST['langfilter_enable']) : 0);
+               DI::pConfig()->set(local_user(), 'langfilter', 'languages', trim($_POST['langfilter_languages']));
+               $enable = (!empty($_POST['langfilter_enable']) ? intval($_POST['langfilter_enable']) : 0);
                $disable = 1 - $enable;
-               PConfig::set(local_user(), 'langfilter', 'disable', $disable);
+               DI::pConfig()->set(local_user(), 'langfilter', 'disable', $disable);
                $minconfidence = 0 + $_POST['langfilter_minconfidence'];
                if (!$minconfidence) {
                        $minconfidence = 0;
@@ -89,17 +89,17 @@ function langfilter_addon_settings_post(App $a, &$b)
                } elseif ($minconfidence > 100) {
                        $minconfidence = 100;
                }
-               PConfig::set(local_user(), 'langfilter', 'minconfidence', $minconfidence / 100.0);
+               DI::pConfig()->set(local_user(), 'langfilter', 'minconfidence', $minconfidence / 100.0);
 
                $minlength = 0 + $_POST['langfilter_minlength'];
                if (!$minlength) {
                        $minlength = 32;
-               } elseif ($minlengt8h < 0) {
+               } elseif ($minlength < 0) {
                        $minlength = 32;
                }
-               PConfig::set(local_user(), 'langfilter', 'minlength', $minlength);
+               DI::pConfig()->set(local_user(), 'langfilter', 'minlength', $minlength);
 
-               info(L10n::t('Language Filter Settings saved.') . EOL);
+               info(DI::l10n()->t('Language Filter Settings saved.') . EOL);
        }
 }
 
@@ -121,20 +121,20 @@ function langfilter_prepare_body_content_filter(App $a, &$hook_data)
 
        // Never filter own messages
        // TODO: find a better way to extract this
-       $logged_user_profile = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
+       $logged_user_profile = DI::baseUrl()->get() . '/profile/' . $a->user['nickname'];
        if ($logged_user_profile == $hook_data['item']['author-link']) {
                return;
        }
 
        // Don't filter if language filter is disabled
-       if (PConfig::get($logged_user, 'langfilter', 'disable')) {
+       if (DI::pConfig()->get($logged_user, 'langfilter', 'disable')) {
                return;
        }
 
        $naked_body = BBCode::toPlaintext($hook_data['item']['body'], false);
 
        // Don't filter if body lenght is below minimum
-       $minlen = PConfig::get(local_user(), 'langfilter', 'minlength', 32);
+       $minlen = DI::pConfig()->get(local_user(), 'langfilter', 'minlength', 32);
        if (!$minlen) {
                $minlen = 32;
        }
@@ -143,8 +143,8 @@ function langfilter_prepare_body_content_filter(App $a, &$hook_data)
                return;
        }
 
-       $read_languages_string = PConfig::get(local_user(), 'langfilter', 'languages');
-       $minconfidence = PConfig::get(local_user(), 'langfilter', 'minconfidence');
+       $read_languages_string = DI::pConfig()->get(local_user(), 'langfilter', 'languages');
+       $minconfidence = DI::pConfig()->get(local_user(), 'langfilter', 'minconfidence');
 
        // Don't filter if no spoken languages are configured
        if (!$read_languages_string) {
@@ -163,6 +163,10 @@ function langfilter_prepare_body_content_filter(App $a, &$hook_data)
                        break;
                }
 
+               if (empty($iso2)) {
+                       return;
+               }
+
                $lang = Text_LanguageDetect_ISO639::code2ToName($iso2);
        } else {
                $opts = $hook_data['item']['postopts'];
@@ -192,6 +196,6 @@ function langfilter_prepare_body_content_filter(App $a, &$hook_data)
        }
 
        if (!in_array($iso2, $read_languages_array)) {
-               $hook_data['filter_reasons'][] = L10n::t('Filtered language: %s', ucfirst($lang));
+               $hook_data['filter_reasons'][] = DI::l10n()->t('Filtered language: %s', ucfirst($lang));
        }
 }