]> git.mxchange.org Git - friendica.git/commitdiff
Remove duplicated calls to language detection
authorSandro Santilli <strk@keybit.net>
Wed, 23 Sep 2015 08:47:34 +0000 (10:47 +0200)
committerSandro Santilli <strk@keybit.net>
Wed, 23 Sep 2015 08:47:34 +0000 (10:47 +0200)
Also avoids overriding existing "lang" specification in an item

include/items.php
mod/item.php

index 5915e2ecee24691462c56959c0a2d68cf9fbfcb7..667353d3201e2af7be13b753131dedf4f32baad2 100644 (file)
@@ -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']));
index d203ce64d497bc977666c77e36b4de4b1c53932f..91a94974e9a232cd0b7c1e2a55893fe0ee69c41b 100644 (file)
@@ -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);