Cleanup implementation of langfilter_prepare_body, and fix a typo
This commit is contained in:
parent
8c203b00db
commit
2f18407eae
|
@ -43,7 +43,7 @@ function langfilter_addon_settings(&$a,&$s) {
|
||||||
'$title' => t("Language Filter"),
|
'$title' => t("Language Filter"),
|
||||||
'$intro' => 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.'),
|
'$intro' => 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.'),
|
||||||
'$enabled' => array('langfilter_enable', t('Use the language filter'), $enable_checked, ''),
|
'$enabled' => array('langfilter_enable', t('Use the language filter'), $enable_checked, ''),
|
||||||
'$languages' => array('langfilter_languages', t('I speak'), $languages, t('List of abbreviations for languages you speak, comma seperated. For excample "de,it".') ),
|
'$languages' => array('langfilter_languages', t('I speak'), $languages, t('List of abbreviations (iso2 codes) for languages you speak, comma separated. For excample "de,it".') ),
|
||||||
'$submit' => t('Save Settings'),
|
'$submit' => t('Save Settings'),
|
||||||
));
|
));
|
||||||
|
|
||||||
|
@ -63,53 +63,54 @@ function langfilter_addon_settings_post(&$a,&$b) {
|
||||||
$enable = ((x($_POST,'langfilter_enable')) ? intval($_POST['langfilter_enable']) : 0);
|
$enable = ((x($_POST,'langfilter_enable')) ? intval($_POST['langfilter_enable']) : 0);
|
||||||
$disable = 1-$enable;
|
$disable = 1-$enable;
|
||||||
set_pconfig(local_user(),'langfilter','disable', $disable);
|
set_pconfig(local_user(),'langfilter','disable', $disable);
|
||||||
|
set_pconfig(local_user(),'langfilter','minconfidence', $disable);
|
||||||
info( t('Language Filter Settings saved.') . EOL);
|
info( t('Language Filter Settings saved.') . EOL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Actually filter postings by their language
|
/* Actually filter postings by their language
|
||||||
* 1st check if the user wants to filter postings
|
* 1st check if the user wants to filter postings
|
||||||
* 2nd get the user settings which languages shall be not filtered out
|
* 2nd get the user settings which languages shall be not filtered out
|
||||||
* 3rd determine the language of a posting
|
* 3rd extract the language of a posting
|
||||||
* 4th if the determined language does not fit to the spoken languages
|
* 4th if the determined language does not fit to the spoken languages
|
||||||
* of the user, then collapse the posting, but provide a link to
|
* of the user, then collapse the posting, but provide a link to
|
||||||
* expand it again.
|
* expand it again.
|
||||||
*/
|
*/
|
||||||
function langfilter_prepare_body(&$a,&$b) {
|
function langfilter_prepare_body(&$a,&$b) {
|
||||||
if(get_pconfig(local_user(),'langfilter','disable'))
|
|
||||||
return;
|
$logged_user = local_user();
|
||||||
|
if ( ! $logged_user ) return;
|
||||||
|
|
||||||
# Never filter own messages
|
# Never filter own messages
|
||||||
# TODO: find a better way to extract this
|
# TODO: find a better way to extract this
|
||||||
$logged_user_profile = $a->config['system']['url'] . '/profile/' . $a->user['nickname'];
|
$logged_user_profile = $a->config['system']['url'] . '/profile/' . $a->user['nickname'];
|
||||||
if ( $logged_user_profile == $b['item']['author-link'] ) return;
|
if ( $logged_user_profile == $b['item']['author-link'] ) return;
|
||||||
|
|
||||||
if(local_user()) {
|
# Don't filter if language filter is disabled
|
||||||
$langs = get_pconfig(local_user(),'langfilter','languages');
|
if( get_pconfig($logged_user,'langfilter','disable') ) return;
|
||||||
}
|
|
||||||
if($langs) {
|
|
||||||
$arr = explode(',',$langs);
|
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$found = false;
|
$spoken_config = get_pconfig(local_user(),'langfilter','languages');
|
||||||
|
|
||||||
|
# Don't filter if no spoken languages are configured
|
||||||
|
if ( ! $spoken_config ) return;
|
||||||
|
$spoken_languages = explode(',', $spoken_config);
|
||||||
|
|
||||||
|
# Extract the language of the post
|
||||||
$opts = $b['item']['postopts'];
|
$opts = $b['item']['postopts'];
|
||||||
if ( $opts ) {
|
if ( ! $opts ) return; # no options associated to post
|
||||||
if ( preg_match('/^lang=([^;]*)/', $opts, $matches ) )
|
if ( ! preg_match('/\blang=([^;]*);([^:]*)/', $opts, $matches ) )
|
||||||
{
|
return; # no lang options associated to post
|
||||||
$lang = $matches[1];
|
|
||||||
$lng = Text_LanguageDetect_ISO639::nameToCode2($lang);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($lng==null)
|
|
||||||
return;
|
|
||||||
if (! in_array($lng, $arr))
|
|
||||||
$found = true;
|
|
||||||
if ($lng==null)
|
|
||||||
$found = false;
|
|
||||||
|
|
||||||
if($found) {
|
$lang = $matches[1];
|
||||||
|
$confidence = $matches[2];
|
||||||
|
|
||||||
|
# TODO: accept a confidence threshold in settings
|
||||||
|
|
||||||
|
$iso2 = Text_LanguageDetect_ISO639::nameToCode2($lang);
|
||||||
|
|
||||||
|
if ( ! $iso2 ) return;
|
||||||
|
$spoken = in_array($iso2, $spoken_languages);
|
||||||
|
|
||||||
|
if( ! $spoken ) {
|
||||||
$rnd = random_string(8);
|
$rnd = random_string(8);
|
||||||
$b['html'] = '<div id="langfilter-wrap-' . $rnd . '" class="fakelink" onclick=openClose(\'langfilter-' . $rnd . '\'); >' . sprintf( t('unspoken language %s - Click to open/close'),$lang ) . '</div><div id="langfilter-' . $rnd . '" style="display: none; " >' . $b['html'] . '</div>';
|
$b['html'] = '<div id="langfilter-wrap-' . $rnd . '" class="fakelink" onclick=openClose(\'langfilter-' . $rnd . '\'); >' . sprintf( t('unspoken language %s - Click to open/close'),$lang ) . '</div><div id="langfilter-' . $rnd . '" style="display: none; " >' . $b['html'] . '</div>';
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user