Browse Source

Ticket #5281 - Improve default embed mechanism.

AntonLV 11 tháng trước cách đây
mục cha
commit
32ddbd42c0

+ 31 - 21
inc/classes/BxDolEmbed.php

@@ -39,6 +39,7 @@
 class BxDolEmbed extends BxDolFactoryObject
 {
     protected $_bAsync = false;
+    protected $_sSocketName = 'sys_embed';
 
     protected $_sTableData = '';
 
@@ -100,7 +101,7 @@ class BxDolEmbed extends BxDolFactoryObject
 
         $sData = $this->_oDb->getLocal($sUrl, $sTheme);
         if(!$sData)
-            $sData = $this->{'_getData' . ($this->_bAsync ? 'Async' : '')}($sUrl, $sTheme);
+            $sData = $this->{'_getData' . ($sData !== false ? 'Empty' : ($this->_bAsync ? 'Async' : ''))}($sUrl, $sTheme);
 
         return json_decode($sData, true);
     }
@@ -110,18 +111,22 @@ class BxDolEmbed extends BxDolFactoryObject
         // override this function in particular embed provider class
     }
 
-    public function process ($sUrl, $sTheme)
+    public function process ()
     {
-        $sData = $this->getDataFromApi($sUrl, $sTheme);
-        if($sData) {
-            $this->_oDb->updateLocal($sUrl, $sTheme, $sData);
-
-            bx_alert('embed', 'processed', 0, false, [
-                'url' => $sUrl,
-                'theme' => $sTheme,
-                'data' => $sData
-            ]);
-        }
+        $aEmbeds = $this->_oDb->getLocalUnprocessed();
+        if(empty($aEmbeds) || !is_array($aEmbeds))
+            return;
+
+        $oSockets = BxDolSockets::getInstance();
+        $bSockets = $oSockets->isEnabled();
+
+        foreach($aEmbeds as $aEmbed)
+            if(($sData = $this->getDataFromApi($aEmbed['url'], $aEmbed['theme']))) {
+                $this->_oDb->updateLocal(['data' => $sData], ['id' => $aEmbed['id']]);
+
+                if($bSockets)
+                    $oSockets->sendEvent($this->_sSocketName, $aEmbed['id'], 'processed', $sData);
+            }
     }
 
     public function cleanYoutubeUrl($url)
@@ -156,19 +161,24 @@ class BxDolEmbed extends BxDolFactoryObject
 
     protected function _getDataAsync($sUrl, $sTheme)
     {
-        $sName = 'embed_for_' . $sUrl . ($sTheme ? '_' . $sTheme : '');
-        if(($oCronQuery = BxDolCronQuery::getInstance()) && !$oCronQuery->isTransientJobService($sName)) {
-            $this->_oDb->insertLocal($sUrl, $sTheme);
+        $iId = $this->_oDb->insertLocal($sUrl, $sTheme);
 
-            $oCronQuery->addTransientJobService($sName, ['system', 'process_embed', [$sUrl, $sTheme], 'TemplServices']);
-        }
+        if(($sName = 'bx_embed_processing') && ($oCronQuery = BxDolCronQuery::getInstance()) && !$oCronQuery->isTransientJobService($sName))
+            $oCronQuery->addTransientJobService($sName, ['system', 'process_embed', [], 'TemplServices']);
 
-        return json_encode([
-            'url' => $sUrl,
-            'title' => $sUrl,
-            'domain' => parse_url($sUrl, PHP_URL_HOST)
+        return $this->_getDataEmpty($sUrl, $sTheme, [
+            'id' => $iId,
+            'processing' => true
         ]);
     }
+
+    protected function _getDataEmpty($sUrl, $sTheme, $aAddons = [])
+    {
+        return json_encode(array_merge([
+            'url' => $sUrl,
+            'domain' => parse_url($sUrl, PHP_URL_HOST),
+        ], $aAddons));
+    }
 }
 
 /** @} */

+ 11 - 7
inc/classes/BxDolEmbedQuery.php

@@ -38,6 +38,11 @@ class BxDolEmbedQuery extends BxDolFactoryObjectQuery
         ]);
     }
 
+    public function getLocalUnprocessed ()
+    {
+        return $this->getAll("SELECT * FROM `" . $this->_sTableData . "` WHERE `data` = ''");
+    }
+
     public function insertLocal ($sUrl, $sTheme, $sData = '')
     {
         return (int)$this->query("INSERT INTO `" . $this->_sTableData . "` (`url`, `theme`, `data`, `added`) VALUES (:url, :theme, :data, :added)", [
@@ -45,16 +50,15 @@ class BxDolEmbedQuery extends BxDolFactoryObjectQuery
             'theme' => $sTheme,
             'data' => $sData,
             'added' => time(),
-        ]) > 0;
+        ]) > 0 ? $this->lastId() : false;
     }
 
-    public function updateLocal ($sUrl, $sTheme, $sData = '')
+    public function updateLocal($aParamsSet, $aParamsWhere)
     {
-        return (int)$this->query("UPDATE `" . $this->_sTableData . "` SET `data` = :data WHERE `url` = :url AND `theme` = :theme", [
-            'url' => $sUrl,
-            'theme' => $sTheme,
-            'data' => $sData
-        ]) > 0;
+        if(empty($aParamsSet) || empty($aParamsWhere))
+            return false;
+
+        return $this->query("UPDATE `{$this->_sTableData}` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
     }
 }
 

+ 1 - 1
template/scripts/BxBaseEmbedSystem.php

@@ -71,7 +71,7 @@ class BxBaseEmbedSystem extends BxDolEmbed
                     'logo' => $sLogo,
                 ]
             ],
-            'title' => $aData['title'],
+            'title' => $aData['title'] ?? '',
             'description' => $aData['description'] ?? '',
             'domain' => $aData['domain'],
         ]);

+ 2 - 2
template/scripts/BxBaseServices.php

@@ -1703,9 +1703,9 @@ class BxBaseServices extends BxDol implements iBxDolProfileService
         exit;        
     }
 
-    public function serviceProcessEmbed($sUrl, $sTheme)
+    public function serviceProcessEmbed()
     {
-        BxDolEmbed::getObjectInstance()->process($sUrl, $sTheme);
+        BxDolEmbed::getObjectInstance()->process();
     }
 }