OSDN Git Service

ブログ記事から画像ギャラリー用に画像を抜き出す処理を追加。
authornaoki hirata <naoki@magic3.org>
Wed, 27 Feb 2019 23:32:40 +0000 (08:32 +0900)
committernaoki hirata <naoki@magic3.org>
Wed, 27 Feb 2019 23:32:40 +0000 (08:32 +0900)
include/manager/imageManager.php
include/sql/update/2018110501_to_2019022701.sql
widgets/blog_main/include/container/admin_blog_mainEntryWidgetContainer.php
widgets/blog_main/include/container/blog_mainEntryWidgetContainer.php
widgets/blog_main/include/db/blog_mainDb.php

index 9d8a812..4918184 100644 (file)
@@ -8,7 +8,7 @@
  *
  * @package    Magic3 Framework
  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
- * @copyright  Copyright 2006-2015 Magic3 Project.
+ * @copyright  Copyright 2006-2019 Magic3 Project.
  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
  * @version    SVN: $Id$
  * @link       http://www.magic3.org
@@ -33,6 +33,8 @@ class ImageManager extends Core
        const PARSE_IMAGE_FORMAT = '/(\d+)([xX]?)(\d*)(.*)\.(gif|png|jpg|jpeg|bmp)$/i';         // 画像フォーマット解析用
 //     const PARSE_IMAGE_FORMAT_TYPE = '/(.*?)\s*=\s*(\d+)([xX]?)(\d*)(.*)\.(gif|png|jpg|jpeg|bmp)$/i';                // 画像フォーマットタイプ解析用
        const PARSE_IMAGE_FORMAT_TYPE = '/(.*?)\s*=\s*(\d+)(.*)\.(gif|png|jpg|jpeg|bmp)$/i';            // 画像フォーマットタイプ解析用
+       const PARSE_IMAGE_TAG = '/<img[^<]*?src\s*=\s*[\'"]+(.+?)[\'"]+[^>]*?>/si';                     // HTMLからIMGタグ取得用
+       
        // DB定義値
 //     const CF_SITE_LOGO_FILENAME     = 'site_logo_filename';         // サイトロゴファイル
        const CF_SITE_LOGO_FORMAT       = 'site_logo_format';           // サイトロゴフォーマット
@@ -98,7 +100,6 @@ class ImageManager extends Core
                
                for ($i = 0; $i < count($formatArray); $i++){
                        $format = trim($formatArray[$i]);
-//                     $ret = preg_match('/(\d+)(.*)\.(gif|png|jpg|jpeg|bmp)$/i', $format, $matches);
                        $ret = $this->parseImageFormat($format, $imageType, $imageAttr, $imageSize, $imageWidthHeight);
                        if ($ret){
                                $thumbSize = $imageWidthHeight;
@@ -791,8 +792,7 @@ class ImageManager extends Core
        {
                global $gEnvManager;
                
-               $exp = '/<img[^<]*?src\s*=\s*[\'"]+(.+?)[\'"]+[^>]*?>/si';
-               $ret = preg_match($exp, $html, $matches);
+               $ret = preg_match(self::PARSE_IMAGE_TAG, $html, $matches);
                if ($ret){
                        $path = $gEnvManager->getMacroPath($matches[1]);                // 画像URL
                        $path = str_replace(M3_TAG_START . M3_TAG_MACRO_ROOT_URL . M3_TAG_END, $gEnvManager->getSystemRootPath(), $path);// マクロ変換
@@ -801,6 +801,25 @@ class ImageManager extends Core
                return '';
        }
        /**
+        * コンテンツからサイト内のすべての画像のパスを取得
+        *
+        * @param string $html          検索コンテンツ(画像パスはマクロ変換済み)
+        * @return array                        画像パス(リソースディレクトリからの相対パス)
+        */
+       function getSiteImagePath($html)
+       {
+               $imagePaths = array();
+               preg_match_all(self::PARSE_IMAGE_TAG, $html, $matches, PREG_SET_ORDER);
+               for ($i = 0; $i < count($matches); $i++){
+                       $path = $matches[$i][1];
+                       if (strStartsWith($path, M3_TAG_START . M3_TAG_MACRO_ROOT_URL . M3_TAG_END . '/' . M3_DIR_NAME_RESOURCE)){
+                               $path = str_replace(M3_TAG_START . M3_TAG_MACRO_ROOT_URL . M3_TAG_END . '/' . M3_DIR_NAME_RESOURCE, '', $path);
+                               if (!in_array($path, $imagePaths)) $imagePaths[] = $path;
+                       }
+               }
+               return $imagePaths;
+       }
+       /**
         * サイトロゴ画像のURLを取得
         *
         * @param string $type                  画像タイプ「sm」「md」「lg」。空の場合はロゴ格納ディレクトリURLを返す。
@@ -1214,7 +1233,6 @@ class ImageManager extends Core
        function parseImageFormat($format, &$imageType, &$imageAttr, &$imageSize, &$imageWidthHeight = null)
        {
                $format = trim($format);
-               //$ret = preg_match('/(\d+)([xX]?)(\d*)(.*)\.(gif|png|jpg|jpeg|bmp)$/i', $format, $matches);    // 「MMxNNc.jpg」タイプのフォーマットを解析
                $ret = preg_match(self::PARSE_IMAGE_FORMAT, $format, $matches);         // 「MMxNNc.jpg」タイプのフォーマットを解析
                if ($ret){
                        $imageSize = $matches[1];
@@ -1262,8 +1280,6 @@ class ImageManager extends Core
                for ($i = 0; $i < count($lines); $i++){
                        $line = trim($lines[$i]);
                        if (!empty($line)){
-                               //$ret = preg_match(, $line, $matches);
-                               //'/(.*?)\s*=\s*(\d+)([xX]?)(\d*)(.*)\.(gif|png|jpg|jpeg|bmp)$/i';              // 画像フォーマットタイプ解析用
                                $ret = preg_match(self::PARSE_IMAGE_FORMAT_TYPE, $line, $matches);
                                if ($ret){
                                        $imageType = $matches[1];
index a0aeeac..6332985 100644 (file)
@@ -20,8 +20,8 @@
 
 -- *** システム標準テーブル ***
 -- ブログ記事画像マスター
-DROP TABLE IF EXISTS blog_entry_image;
-CREATE TABLE blog_entry_image (
+DROP TABLE IF EXISTS blog_image;
+CREATE TABLE blog_image (
     bm_serial            INT            AUTO_INCREMENT,                              -- レコードシリアル番号
     bm_entry_serial      INT            DEFAULT 0                     NOT NULL,      -- ブログ記事シリアル番号
     bm_index             INT            DEFAULT 0                     NOT NULL,      -- インデックス番号
index 52f07a5..d1408a3 100644 (file)
@@ -488,6 +488,11 @@ class admin_blog_mainEntryWidgetContainer extends admin_blog_mainBaseWidgetConta
                                $html = $this->gInstance->getTextConvManager()->convToContentMacro($html);
                                $html2 = $this->gInstance->getTextConvManager()->convToContentMacro($html2);
                                
+                               // 記事に含まれるすべての画像のパスを取得
+                               $imagePathArray = $this->gInstance->getImageManager()->getSiteImagePath($html);
+                               $imagePathArray2 = $this->gInstance->getImageManager()->getSiteImagePath($html2);
+                               $imagePathArray = array_unique(array_merge($imagePathArray, $imagePathArray2));
+                               
                                // 追加パラメータ
                                $otherParams = array(   'be_description'                => $desc,               // 簡易説明
                                                                                'be_meta_description'   => $metaDesc,           // ページ要約(METAタグ)
@@ -499,10 +504,10 @@ class admin_blog_mainEntryWidgetContainer extends admin_blog_mainBaseWidgetConta
 
                                // 記事データを追加
                                if (($this->isMultiLang && $this->langId == $this->gEnv->getDefaultLanguage()) || !$this->isMultiLang){         // 多言語でデフォルト言語、または単一言語のとき
-                                       $ret = self::$_mainDb->addEntryItem($nextEntryId * (-1)/*次のコンテンツIDのチェック*/, $this->langId, $name, $html, $html2, $status, $this->categoryArray, $this->blogId, 
+                                       $ret = self::$_mainDb->addEntryItem($nextEntryId * (-1)/*次のコンテンツIDのチェック*/, $this->langId, $name, $html, $html2, $status, $this->categoryArray, $imagePathArray, $this->blogId, 
                                                                                                        $this->_userId, $regDt, $startDt, $endDt, $showComment, $receiveComment, $newSerial, $otherParams);
                                } else {
-                                       $ret = self::$_mainDb->addEntryItem($this->entryId, $this->langId, $name, $html, $html2, $status, $this->categoryArray, $this->blogId, 
+                                       $ret = self::$_mainDb->addEntryItem($this->entryId, $this->langId, $name, $html, $html2, $status, $this->categoryArray, $imagePathArray, $this->blogId, 
                                                                                                        $this->_userId, $regDt, $startDt, $endDt, $showComment, $receiveComment, $newSerial, $otherParams);
                                }
                                if ($ret){
@@ -621,6 +626,11 @@ class admin_blog_mainEntryWidgetContainer extends admin_blog_mainBaseWidgetConta
                                $html = $this->gInstance->getTextConvManager()->convToContentMacro($html);
                                $html2 = $this->gInstance->getTextConvManager()->convToContentMacro($html2);
                                
+                               // 記事に含まれるすべての画像のパスを取得
+                               $imagePathArray = $this->gInstance->getImageManager()->getSiteImagePath($html);
+                               $imagePathArray2 = $this->gInstance->getImageManager()->getSiteImagePath($html2);
+                               $imagePathArray = array_unique(array_merge($imagePathArray, $imagePathArray2));
+                               
                                // 追加パラメータ
                                $otherParams = array(   'be_description'                => $desc,               // 簡易説明
                                                                                'be_meta_description'   => $metaDesc,           // ページ要約(METAタグ)
@@ -639,7 +649,7 @@ class admin_blog_mainEntryWidgetContainer extends admin_blog_mainBaseWidgetConta
                                }
                        
                                // 記事データを更新
-                               $ret = self::$_mainDb->updateEntryItem($this->serialNo, $name, $html, $html2, $status, $this->categoryArray, $this->blogId, 
+                               $ret = self::$_mainDb->updateEntryItem($this->serialNo, $name, $html, $html2, $status, $this->categoryArray, $imagePathArray, $this->blogId, 
                                                                                                        ''/*投稿者そのまま*/, $regDt, $startDt, $endDt, $showComment, $receiveComment, $newSerial, $oldRecord, $otherParams);
 /*                             if ($ret){
                                        // コンテンツに画像がなくなった場合は、サムネールを削除
index c838ff6..c4f6793 100644 (file)
@@ -490,6 +490,11 @@ class blog_mainEntryWidgetContainer extends blog_mainBaseWidgetContainer
                                $html = $this->gInstance->getTextConvManager()->convToContentMacro($html);
                                $html2 = $this->gInstance->getTextConvManager()->convToContentMacro($html2);
                                
+                               // 記事に含まれるすべての画像のパスを取得
+                               $imagePathArray = $this->gInstance->getImageManager()->getSiteImagePath($html);
+                               $imagePathArray2 = $this->gInstance->getImageManager()->getSiteImagePath($html2);
+                               $imagePathArray = array_unique(array_merge($imagePathArray, $imagePathArray2));
+                               
                                // 追加パラメータ
                                $otherParams = array(   'be_description'                => $desc,               // 簡易説明
                                                                                'be_meta_description'   => $metaDesc,           // ページ要約(METAタグ)
@@ -501,10 +506,10 @@ class blog_mainEntryWidgetContainer extends blog_mainBaseWidgetContainer
 
                                // 記事データを追加
                                if (($this->isMultiLang && $this->langId == $this->gEnv->getDefaultLanguage()) || !$this->isMultiLang){         // 多言語でデフォルト言語、または単一言語のとき
-                                       $ret = self::$_mainDb->addEntryItem($nextEntryId * (-1)/*次のコンテンツIDのチェック*/, $this->langId, $name, $html, $html2, $status, $this->categoryArray, $this->blogId, 
+                                       $ret = self::$_mainDb->addEntryItem($nextEntryId * (-1)/*次のコンテンツIDのチェック*/, $this->langId, $name, $html, $html2, $status, $this->categoryArray, $imagePathArray, $this->blogId, 
                                                                                                        $this->_userId, $regDt, $startDt, $endDt, $showComment, $receiveComment, $newSerial, $otherParams);
                                } else {
-                                       $ret = self::$_mainDb->addEntryItem($this->entryId, $this->langId, $name, $html, $html2, $status, $this->categoryArray, $this->blogId, 
+                                       $ret = self::$_mainDb->addEntryItem($this->entryId, $this->langId, $name, $html, $html2, $status, $this->categoryArray, $imagePathArray, $this->blogId, 
                                                                                                        $this->_userId, $regDt, $startDt, $endDt, $showComment, $receiveComment, $newSerial, $otherParams);
                                }
                                if ($ret){
@@ -623,6 +628,11 @@ class blog_mainEntryWidgetContainer extends blog_mainBaseWidgetContainer
                                $html = $this->gInstance->getTextConvManager()->convToContentMacro($html);
                                $html2 = $this->gInstance->getTextConvManager()->convToContentMacro($html2);
                                
+                               // 記事に含まれるすべての画像のパスを取得
+                               $imagePathArray = $this->gInstance->getImageManager()->getSiteImagePath($html);
+                               $imagePathArray2 = $this->gInstance->getImageManager()->getSiteImagePath($html2);
+                               $imagePathArray = array_unique(array_merge($imagePathArray, $imagePathArray2));
+                               
                                // 追加パラメータ
                                $otherParams = array(   'be_description'                => $desc,               // 簡易説明
                                                                                'be_meta_description'   => $metaDesc,           // ページ要約(METAタグ)
@@ -641,7 +651,7 @@ class blog_mainEntryWidgetContainer extends blog_mainBaseWidgetContainer
                                }
                        
                                // 記事データを更新
-                               $ret = self::$_mainDb->updateEntryItem($this->serialNo, $name, $html, $html2, $status, $this->categoryArray, $this->blogId, 
+                               $ret = self::$_mainDb->updateEntryItem($this->serialNo, $name, $html, $html2, $status, $this->categoryArray, $imagePathArray, $this->blogId, 
                                                                                                        ''/*投稿者そのまま*/, $regDt, $startDt, $endDt, $showComment, $receiveComment, $newSerial, $oldRecord, $otherParams);
 /*                             if ($ret){
                                        // コンテンツに画像がなくなった場合は、サムネールを削除
index ebc7b7a..c8e090c 100644 (file)
@@ -8,7 +8,7 @@
  *
  * @package    Magic3 Framework
  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
- * @copyright  Copyright 2006-2017 Magic3 Project.
+ * @copyright  Copyright 2006-2019 Magic3 Project.
  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
  * @version    SVN: $Id$
  * @link       http://www.magic3.org
@@ -251,6 +251,7 @@ class blog_mainDb extends BaseDb
         * @param string  $html2                HTML(続き)
         * @param int     $status               エントリー状態(0=未設定、1=編集中、2=公開、3=非公開)
         * @param array   $category             カテゴリーID
+        * @param array   $images               記事画像
         * @param string  $blogId               ブログID
         * @param int     $regUserId    投稿者ユーザID
         * @param timestamp $regDt              投稿日時
@@ -262,7 +263,7 @@ class blog_mainDb extends BaseDb
         * @param array   $otherParams  その他のフィールド値
         * @return bool                                 true = 成功、false = 失敗
         */
-       function addEntryItem($id, $langId, $name, $html, $html2, $status, $category, $blogId, $regUserId, $regDt, $startDt, $endDt, $showComment, $receiveComment, &$newSerial, $otherParams = null)
+       function addEntryItem($id, $langId, $name, $html, $html2, $status, $category, $images, $blogId, $regUserId, $regDt, $startDt, $endDt, $showComment, $receiveComment, &$newSerial, $otherParams = null)
        {
                $now = date("Y/m/d H:i:s");     // 現在日時
                $userId = $this->gEnv->getCurrentUserId();      // 現在のユーザ
@@ -359,7 +360,16 @@ class blog_mainDb extends BaseDb
                                return false;
                        }
                }
-                       
+
+               // 記事画像の更新
+               for ($i = 0; $i < count($images); $i++){
+                       $ret = $this->updateEntryImage($newSerial, $i, $images[$i]);
+                       if (!$ret){
+                               $this->endTransaction();
+                               return false;
+                       }
+               }
+               
                // トランザクション確定
                $ret = $this->endTransaction();
                return $ret;
@@ -373,6 +383,7 @@ class blog_mainDb extends BaseDb
         * @param string  $html2                HTML(続き)
         * @param int     $status               エントリー状態(0=未設定、1=編集中、2=公開、3=非公開)
         * @param array   $category             カテゴリーID
+        * @param array   $images               記事画像
         * @param string  $blogId               ブログID
         * @param int     $regUserId    投稿者ユーザID(0のときは更新しない)
         * @param timestamp $regDt              投稿日時(空のときは更新しない)
@@ -385,7 +396,7 @@ class blog_mainDb extends BaseDb
         * @param array   $otherParams  その他のフィールド値
         * @return bool                                 true = 成功、false = 失敗
         */
-       function updateEntryItem($serial, $name, $html, $html2, $status, $category, $blogId, $regUserId, $regDt, $startDt, $endDt, $showComment, $receiveComment, &$newSerial, 
+       function updateEntryItem($serial, $name, $html, $html2, $status, $category, $images, $blogId, $regUserId, $regDt, $startDt, $endDt, $showComment, $receiveComment, &$newSerial, 
                                                                &$oldRecord, $otherParams = null)
        {
                $now = date("Y/m/d H:i:s");     // 現在日時
@@ -486,6 +497,15 @@ class blog_mainDb extends BaseDb
                                return false;
                        }
                }
+
+               // 記事画像の更新
+               for ($i = 0; $i < count($images); $i++){
+                       $ret = $this->updateEntryImage($newSerial, $i, $images[$i]);
+                       if (!$ret){
+                               $this->endTransaction();
+                               return false;
+                       }
+               }
                
                // トランザクション確定
                $ret = $this->endTransaction();
@@ -602,6 +622,27 @@ class blog_mainDb extends BaseDb
                return $ret;
        }
        /**
+        * 記事画像の更新
+        *
+        * @param int        $serial            記事シリアル番号
+        * @param int        $index                     インデックス番号
+        * @param string     $path                      画像パス
+        * @return bool          true = 成功、false = 失敗
+        */
+       function updateEntryImage($serial, $index, $path)
+       {
+               // 新規レコード追加
+               $queryStr = 'INSERT INTO blog_image ';
+               $queryStr .=  '(';
+               $queryStr .=  'bm_entry_serial, ';
+               $queryStr .=  'bm_index, ';
+               $queryStr .=  'bm_image_src) ';
+               $queryStr .=  'VALUES ';
+               $queryStr .=  '(?, ?, ?)';
+               $ret =$this->execStatement($queryStr, array(intval($serial), $index, $path));
+               return $ret;
+       }
+       /**
         * エントリー項目をシリアル番号で取得
         *
         * @param string        $serial                         シリアル番号