From 2a2dd6a1b918b646e51cdca4c93d817ac11c2cf5 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Wed, 23 Sep 2015 10:47:34 +0200 Subject: [PATCH] Remove duplicated calls to language detection Also avoids overriding existing "lang" specification in an item --- include/items.php | 66 ++++++++++++++++++++++++++++++----------------- mod/item.php | 29 ++------------------- 2 files changed, 45 insertions(+), 50 deletions(-) diff --git a/include/items.php b/include/items.php index 5915e2ece..667353d32 100644 --- a/include/items.php +++ b/include/items.php @@ -1096,6 +1096,48 @@ function add_guid($item) { dbesc($item["uri"]), dbesc($item["network"])); } +// Adds a "lang" specification in a "postopts" element of given $arr, +// if possible and not already present. +// Expects "body" element to exist in $arr. +// TODO: add a parameter to request forcing override +function item_add_language_opt(&$arr) { + + if (version_compare(PHP_VERSION, '5.3.0', '<')) return; // LanguageDetect.php not available ? + + if ( $arr['postopts'] ) + { + if ( strstr($arr['postopts'], 'lang=') ) + { + // do not override + // TODO: add parameter to request overriding + return; + } + $postopts = $arr['postopts']; + } + else + { + $postopts = ""; + } + + require_once('library/langdet/Text/LanguageDetect.php'); + $naked_body = preg_replace('/\[(.+?)\]/','',$arr['body']); + $l = new Text_LanguageDetect; + //$lng = $l->detectConfidence($naked_body); + //$arr['postopts'] = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : ''); + $lng = $l->detect($naked_body, 3); + + if (sizeof($lng) > 0) { + if ($postopts) $postopts .= '^'; // arbitrary separator, to be reviewed + $postopts .= 'lang='; + $sep = ""; + foreach ($lng as $language => $score) { + $postopts .= $sep . $language.";".$score; + $sep = ':'; + } + $arr['postopts'] = $postopts; + } +} + function item_store($arr,$force_parent = false, $notify = false, $dontcache = false) { // If it is a posting where users should get notifications, then define it as wall posting @@ -1185,29 +1227,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa //if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false)) // $arr['body'] = strip_tags($arr['body']); - - if (version_compare(PHP_VERSION, '5.3.0', '>=')) { - require_once('library/langdet/Text/LanguageDetect.php'); - $naked_body = preg_replace('/\[(.+?)\]/','',$arr['body']); - $l = new Text_LanguageDetect; - //$lng = $l->detectConfidence($naked_body); - //$arr['postopts'] = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : ''); - $lng = $l->detect($naked_body, 3); - - if (sizeof($lng) > 0) { - $postopts = ""; - - foreach ($lng as $language => $score) { - if ($postopts == "") - $postopts = "lang="; - else - $postopts .= ":"; - - $postopts .= $language.";".$score; - } - $arr['postopts'] = $postopts; - } - } + item_add_language_opt($arr); $arr['wall'] = ((x($arr,'wall')) ? intval($arr['wall']) : 0); $arr['guid'] = ((x($arr,'guid')) ? notags(trim($arr['guid'])) : get_guid(32, $arr['network'])); diff --git a/mod/item.php b/mod/item.php index d203ce64d..91a94974e 100644 --- a/mod/item.php +++ b/mod/item.php @@ -18,7 +18,6 @@ require_once('include/crypto.php'); require_once('include/enotify.php'); require_once('include/email.php'); -require_once('library/langdet/Text/LanguageDetect.php'); require_once('include/tags.php'); require_once('include/files.php'); require_once('include/threads.php'); @@ -268,32 +267,8 @@ function item_post(&$a) { $guid = get_guid(32); - $naked_body = preg_replace('/\[(.+?)\]/','',$body); - - if (version_compare(PHP_VERSION, '5.3.0', '>=')) { - $l = new Text_LanguageDetect; - //$lng = $l->detectConfidence($naked_body); - //$postopts = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : ''); - - $lng = $l->detect($naked_body, 3); - - if (sizeof($lng) > 0) { - $postopts = ""; - - foreach ($lng as $language => $score) { - if ($postopts == "") - $postopts = "lang="; - else - $postopts .= ":"; - - $postopts .= $language.";".$score; - } - } - - logger('mod_item: detect language' . print_r($lng,true) . $naked_body, LOGGER_DATA); - } - else - $postopts = ''; + item_add_language_opt($_REQUEST); + $postopts = $_REQUEST['postopts'] ? $_REQUEST['postopts'] : ""; $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);