diff --git a/app/Entities/CommonEntity.php b/app/Entities/CommonEntity.php index 6dda71e..c200067 100644 --- a/app/Entities/CommonEntity.php +++ b/app/Entities/CommonEntity.php @@ -10,8 +10,12 @@ abstract class CommonEntity extends Entity protected $dates = ['created_at', 'updated_at', 'deleted_at']; protected $casts = []; + public function __construct(array|null $data = null) + { + parent::__construct($data); + } + abstract public function __toString(); - abstract public function getPK(); abstract public function getTitle(); abstract public function setTitle(string $tile): void; } diff --git a/app/Entities/Mangboard/BoardEntity.php b/app/Entities/Mangboard/BoardEntity.php index 9bfc2f9..4034221 100644 --- a/app/Entities/Mangboard/BoardEntity.php +++ b/app/Entities/Mangboard/BoardEntity.php @@ -10,10 +10,6 @@ class BoardEntity extends CommonEntity { return "{$this->getPK()}:{$this->getTitle()}"; } - public function getPK(): int - { - return $this->attributes['pid']; - } public function getTitle(): string { return $this->attributes['title']; @@ -23,4 +19,9 @@ class BoardEntity extends CommonEntity $this->attributes['title'] = $title; } //Common Function + + public function getPK(): int + { + return $this->attributes['pid']; + } } diff --git a/app/Entities/Mangboard/FileEntity.php b/app/Entities/Mangboard/FileEntity.php index ef8ddd2..d3b2575 100644 --- a/app/Entities/Mangboard/FileEntity.php +++ b/app/Entities/Mangboard/FileEntity.php @@ -2,25 +2,18 @@ namespace App\Entities\Mangboard; -use App\Entities\CommonEntity; +use App\Entities\MyStorage\FileEntity as ParentEntity; -class FileEntity extends CommonEntity +class FileEntity extends ParentEntity { public function __toString(): string { return "{$this->getPK()}:{$this->getTitle()}"; } + //Common Function + public function getPK(): int { return $this->attributes['pid']; } - public function getTitle(): string - { - return $this->attributes['file_name']; - } - public function setTitle(string $title): void - { - $this->attributes['file_name'] = $title; - } - //Common Function } diff --git a/app/Entities/Mangboard/UserEntity.php b/app/Entities/Mangboard/UserEntity.php index 40f3103..492f45b 100644 --- a/app/Entities/Mangboard/UserEntity.php +++ b/app/Entities/Mangboard/UserEntity.php @@ -10,10 +10,6 @@ class UserEntity extends CommonEntity { return "{$this->getPK()}:{$this->getID()}:{$this->getTitle()},{$this->getLevel()}/{$this->getPoint()}"; } - public function getPK(): int - { - return $this->attributes['pid']; - } public function getTitle(): string { return $this->attributes['user_name']; @@ -24,6 +20,10 @@ class UserEntity extends CommonEntity } //Common Function + public function getPK(): int + { + return $this->attributes['pid']; + } public function getID(): string { return $this->attributes['user_id']; diff --git a/app/Entities/MyStorage/FileEntity.php b/app/Entities/MyStorage/FileEntity.php new file mode 100644 index 0000000..dd3af3a --- /dev/null +++ b/app/Entities/MyStorage/FileEntity.php @@ -0,0 +1,51 @@ +getTitle()}"; + } + public function getTitle(): string + { + return $this->attributes['file_name']; + } + public function setTitle(string $file_name): void + { + $this->attributes['file_name'] = $file_name; + } + //Common Function + + public function getPath(): string + { + return $this->attributes['file_path']; + } + public function setPath(string $file_path): void + { + $this->attributes['file_path'] = $file_path; + } + final public function getMimeType(): string + { + return $this->attributes['file_type']; + } + public function setMimeType(string $mimetype): void + { + $this->attributes['file_type'] = $mimetype; + } + final public function getMediaHTML(): string + { + return $this->attributes['media_html']; + } + public function setMediaHTML(string $media_html): void + { + $this->attributes['media_html'] = $media_html; + } +} diff --git a/app/Libraries/Mangboard/FileLibrary.php b/app/Libraries/Mangboard/FileLibrary.php index 778a4c2..2d3b0b1 100644 --- a/app/Libraries/Mangboard/FileLibrary.php +++ b/app/Libraries/Mangboard/FileLibrary.php @@ -3,22 +3,39 @@ namespace App\Libraries\Mangboard; -use App\Libraries\MyStorage\FileLibrary as MyStorageLibrary; +use App\Libraries\MyStorage\FileLibrary as ParentLibrary; use App\Models\Mangboard\FileModel; use App\Entities\Mangboard\UserEntity; use App\Entities\Mangboard\FileEntity; -class FileLibrary extends MyStorageLibrary +class FileLibrary extends ParentLibrary { private $_user = null; private $_boardName = ""; private $_boardTable = ""; private $_model = null; - private $_fileEntity = null; public function __construct(string $path) { parent::__construct($path); } + public function getModel(): FileModel + { + if ($this->_model === null) { + return $this->_model = new FileModel(); + } + return $this->_model; + } + + //override + public function getEntity(): FileEntity + { + if ($this->_entity === null) { + $this->_entity = new FileEntity(); + $this->_entity->setPath($this->getPath()); + } + return $this->_entity; + } + public function getUser(): UserEntity { if ($this->_user === null) { @@ -50,43 +67,26 @@ class FileLibrary extends MyStorageLibrary } public function setBoardTable(string $boardTable): void { - $this->_boardName = $boardTable; + $this->_boardTable = $boardTable; } - public function getModel(): FileModel + public function save($content): null|FileEntity { - if ($this->_model === null) { - return $this->_model = new FileModel(); - } - return $this->_model; - } - - public function getFileEntity(): null|FileEntity - { - return $this->_fileEntity; - } - - public function save($content): bool - { - if (!parent::save($content)) { - return false; - } //망보드 파일관리 table에 등록 try { - //mb_files 모델작업 - $entity = new FileEntity(); - $entity->setTitle($this->getFileName()); + $entity = parent::save($content); + if ($entity === null) { + return null; + } $entity->user_pid = $this->getUser()->getPK(); $entity->user_name = $this->getUser()->getTitle(); $entity->board_name = $this->getBoardName(); $entity->table_name = $this->getBoardTable(); - $entity->file_path = $this->getPath(); - $entity->file_type = $this->getMimeType(); $entity->reg_date = date("Y-m-d H:i:s"); - $this->_fileEntity = $this->getModel()->create($entity); - return true; + return $this->getModel()->create($entity); } catch (\Exception $e) { - return false; + log_message("error", $e->getMessage()); + return null; } } } diff --git a/app/Libraries/MyCrawler/MyCrawlerLibrary.php b/app/Libraries/MyCrawler/MyCrawlerLibrary.php index 4d85e44..3719e37 100644 --- a/app/Libraries/MyCrawler/MyCrawlerLibrary.php +++ b/app/Libraries/MyCrawler/MyCrawlerLibrary.php @@ -43,9 +43,9 @@ abstract class MyCrawlerLibrary extends CommonLibrary $mediaTag = sprintf( "\"%s\"", $this->getMyStorage()->getUploadPath(), - $this->getMyStorage()->getPath(), - $this->getMyStorage()->getFieName(), - $this->getMyStorage()->getFieName() + $this->getMyStorage()->getEntity()->getPath(), + $this->getMyStorage()->getEntity()->getTitle(), + $this->getMyStorage()->getEntity()->getTitle() ); break; case "video": @@ -54,11 +54,11 @@ abstract class MyCrawlerLibrary extends CommonLibrary Your browser does not support the video tag. ", - $this->getMyStorage()->getFieName(), + $this->getMyStorage()->getEntity()->getTitle(), $this->getMyStorage()->getUploadPath(), - $this->getMyStorage()->getPath(), - $this->getMyStorage()->getFieName(), - $this->getMyStorage()->getFieMimeType(), + $this->getMyStorage()->getEntity()->getPath(), + $this->getMyStorage()->getEntity()->getTitle(), + $this->getMyStorage()->getEntity()->getMimeType(), ); break; } @@ -75,20 +75,17 @@ abstract class MyCrawlerLibrary extends CommonLibrary } return $downloadInfos; } - protected function save(string $mediaType, array $downloadInfos, $fileInfos = []): array + protected function save(string $mediaType, array $downloadInfos, $fileEntitys = []): array { foreach ($downloadInfos as $downloadInfo) { - $this->getMyStorage()->setFileName($downloadInfo['fileName']); + $entity = $this->getMyStorage()->getEntity(); + $entity->setTitle($downloadInfo['fileName']); if (!$this->getMyStorage()->save($downloadInfo['content'])) { continue; } - $fileInfos[] = [ - "mediatag" => $this->getMediaTag($mediaType), - "path" => $this->getMyStorage()->getPath(), - "mimeType" => $this->getMyStorage()->getMimeType(), - "fileName" => $this->getMyStorage()->getFieName(), - ]; + $entity->setMediaHTML($this->getMediaTag($mediaType)); + $fileEntitys[] = $entity; } - return $fileInfos; + return $fileEntitys; } } diff --git a/app/Libraries/MyCrawler/YamapLibrary.php b/app/Libraries/MyCrawler/YamapLibrary.php index ba30e45..d751778 100644 --- a/app/Libraries/MyCrawler/YamapLibrary.php +++ b/app/Libraries/MyCrawler/YamapLibrary.php @@ -4,12 +4,13 @@ namespace App\Libraries\MyCrawler; use Symfony\Component\DomCrawler\Crawler; -use App\Libraries\MySocket\WebLibrary as MySocket; -use App\Libraries\Mangboard\FileLibrary as MyStorage; -use App\Libraries\Mangboard\BoardLibrary; -use App\Models\Mangboard\BoardModel; -use App\Entities\Mangboard\BoardEntity; use App\Models\Mangboard\UserModel; +use App\Models\Mangboard\BoardModel; +use App\Libraries\MySocket\WebLibrary; +use App\Libraries\Mangboard\FileLibrary; +use App\Libraries\Mangboard\BoardLibrary; +use App\Entities\Mangboard\UserEntity; +use App\Entities\Mangboard\BoardEntity; class YamapLibrary extends MyCrawlerLibrary { @@ -21,36 +22,27 @@ class YamapLibrary extends MyCrawlerLibrary parent::__construct(); } - final public function getMySocket(): mixed + protected function getMySocket(): WebLibrary { if ($this->_mySocket === null) { - $this->_mySocket = new MySocket(getenv('yamap.host.url')); + $this->_mySocket = new WebLibrary(getenv('yamap.host.url')); } return $this->_mySocket; } - final public function setMySocket($mySocket): void - { - $this->_mySocket = $mySocket; - } - final public function getMyStorage(): mixed + protected function getMyStorage(): FileLibrary { if ($this->_myStorage === null) { - $this->_myStorage = new MyStorage(getenv('yamap.storage.upload.path')); - $this->_myStorage->setBoardName(getenv('crawler.yamap.registration.board')); + $this->_myStorage = new FileLibrary(getenv('yamap.storage.upload.path')); + $this->_myStorage->setBoardName(getenv('yamap.storage.board.name')); $this->_myStorage->setBoardTable($this->getBoardModel()->getTable()); } return $this->_myStorage; } - final public function setMyStorage($myStorage): void - { - $this->_myStorage = $myStorage; - } - private function getBoardModel(): BoardModel { if ($this->_boardModel === null) { - $this->_boardModel = new BoardModel(getenv("crawler.yamap.registration.table")); + $this->_boardModel = new BoardModel(getenv("yamap.storage.board.table")); } return $this->_boardModel; } @@ -64,14 +56,14 @@ class YamapLibrary extends MyCrawlerLibrary function (Crawler $node) use (&$items): void { //bbs_item에서 span.g_nickname 객체를 찾아서 작성자가 "관리자" 아닌지 확인 후 Return Bool $nickname = $node->filter(getenv("yamap.list.item.nickname.tag"))->text(); - $time = date("Y-m-d") . " " . $node->filter(getenv("yamap.list.item.time.tag "))->text(); - $hit = intval($node->filter(getenv("yamap.list.item.hit.tag "))->text()); + $hit = $node->filter(getenv("yamap.list.item.hit.tag"))->text(); + $date = $node->filter(getenv("yamap.list.item.date.tag"))->text(); if ($nickname != getenv("yamap.list.item.nickname.except")) { //작성자가 "관리자"가 아니 게시물이면 해당 bbs_item에서 a.list_subject 객체를 찾아서 $link_node = $node->filter(getenv("yamap.list.item.link.tag")); $detail_url = $link_node->attr("href"); $title = $link_node->children()->last()->text(); - $items[] = ['title' => $title, 'nickname' => $nickname, 'detail_url' => $detail_url, 'time' => $time, 'hit' => $hit]; + $items[] = ['title' => $title, 'nickname' => $nickname, 'detail_url' => $detail_url, 'date' => $date, 'hit' => $hit]; } } ); @@ -81,18 +73,18 @@ class YamapLibrary extends MyCrawlerLibrary return $items; } - private function detailPage(string $url, array $fileInfos = []): array + private function detailPage(string $url, array $fileEntitys = []): array { $crawler = $this->getContent($url, getenv("yamap.view.content.tag")); //3. Image 처리 $downloadInfos = $this->download($crawler, ["tag" => "img", "attr" => "src"]); - $fileInfos = $this->save("image", $downloadInfos); + $fileEntitys = $this->save("image", $downloadInfos); //4. Video(mp4) 처리 $downloadInfos = $this->download($crawler, ["tag" => "video", "attr" => "src"]); - return $this->save("video", $downloadInfos, $fileInfos); + return $this->save("video", $downloadInfos, $fileEntitys); } - private function createBoard(array $item, array $fileInfos): BoardEntity + private function createBoard(array $item, array $fileEntitys): BoardEntity { $board = new BoardLibrary($this->getBoardModel()); $board->setDebug($this->getDebug()); @@ -100,14 +92,21 @@ class YamapLibrary extends MyCrawlerLibrary //미디어관련정보 entity에 넣기 $entity = new BoardEntity(); $entity->title = $item["title"]; - $entity->user_name = $item["nickname"]; - $entity->reg_date = $item['time']; + $entity->user_pid = $this->getMyStorage()->getUser()->getPK(); + $entity->user_id = $this->getMyStorage()->getUser()->getID(); + $entity->user_name = $item["nickname"] != "" ? $item["nickname"] : $this->getMyStorage()->getUser()->getTitle(); $entity->hit = $item['hit']; + $entity->reg_date = date("Y-m-d H:i:s", strtotime($item['date'])); $entity->data_type = "html"; $entity->editor_type = "S"; - foreach ($fileInfos as $fileInfo) { - $entity->content .= $fileInfo["mediatag"]; + foreach ($fileEntitys as $fileEntity) { + $entity->content .= $fileEntity->getMediaHTML(); } + // echo $entity->title . "\n"; + // echo $entity->user_name . "\n"; + // echo $entity->hit . "\n"; + // echo $entity->reg_date . "\n"; + // exit; //망보드에 넣기 return $board->create($entity); } @@ -144,8 +143,8 @@ class YamapLibrary extends MyCrawlerLibrary //최초 게시물만 등록하기 위함 $item = $items[0]; //3. DetailPage 처리 : bbs_view > div.contents 가진 객체를 찾아서 처리 - $fileInfos = $this->detailPage($item["detail_url"]); + $fileEntitys = $this->detailPage($item["detail_url"]); //4.망보드 일반게시판에 게시물 등록 처리 - $this->createBoard($item, $fileInfos); + $this->createBoard($item, $fileEntitys); } } diff --git a/app/Libraries/MyStorage/FileLibrary.php b/app/Libraries/MyStorage/FileLibrary.php index c275cb2..9d4f608 100644 --- a/app/Libraries/MyStorage/FileLibrary.php +++ b/app/Libraries/MyStorage/FileLibrary.php @@ -2,36 +2,32 @@ namespace App\Libraries\MyStorage; +use App\Entities\MyStorage\FileEntity; + class FileLibrary extends MyStorageLibrary { private $_path = ""; - private $_fileName = ""; - private $_mimeType = ""; + protected $_entity = null; public function __construct(string $path) { parent::__construct(); $this->_path = $path; } - final public function getPath(): string { return $this->_path; } - final public function getFileName(): string + + public function getEntity(): FileEntity { - return $this->_fileName; - } - final public function setFileName(string $fileName): void - { - $this->_fileName = $fileName; + if ($this->_entity === null) { + $this->_entity = new FileEntity(); + $this->_entity->setPath($this->getPath()); + } + return $this->_entity; } - final public function getMimeType(): string - { - return $this->_mimeType; - } - - public function save($content): bool + public function save($content): null|FileEntity { $fullPath = WRITEPATH . $this->getUploadPath() . DIRECTORY_SEPARATOR . $this->getPath(); if (!is_dir($fullPath)) { @@ -39,13 +35,14 @@ class FileLibrary extends MyStorageLibrary throw new \Exception("Make Directory Error:" . $fullPath); } } - $saveFile = $fullPath . DIRECTORY_SEPARATOR . $this->getFileName(); + $entity = $this->getEntity(); + $saveFile = $fullPath . DIRECTORY_SEPARATOR . $entity->getTitle(); log_message("debug", "Storage Save-> " . $saveFile); if (!file_put_contents($saveFile, $content)) { - return false; + return null; } //File형식에 따른 MimeType 지정 - $this->_mimeType = mime_content_type($saveFile); - return true; + $entity->setMimeType(mime_content_type($saveFile)); + return $entity; } } diff --git a/app/Libraries/MyStorage/MyStorageLibrary.php b/app/Libraries/MyStorage/MyStorageLibrary.php index be584ef..8e64425 100644 --- a/app/Libraries/MyStorage/MyStorageLibrary.php +++ b/app/Libraries/MyStorage/MyStorageLibrary.php @@ -3,6 +3,7 @@ namespace App\Libraries\MyStorage; use App\Libraries\CommonLibrary; +use App\Entities\MyStorage\FileEntity; abstract class MyStorageLibrary extends CommonLibrary { @@ -12,7 +13,7 @@ abstract class MyStorageLibrary extends CommonLibrary parent::__construct(); } - abstract public function save($content): bool; + abstract public function save($content): null|FileEntity; final public function getUploadPath(): string { return $this->_uploadPath; diff --git a/app/Models/CommonModel.php b/app/Models/CommonModel.php index 6e729ee..2e69c66 100644 --- a/app/Models/CommonModel.php +++ b/app/Models/CommonModel.php @@ -150,7 +150,9 @@ abstract class CommonModel extends Model $entity->$field = password_hash($entity->$field, PASSWORD_DEFAULT); break; case "content": - $entity->$field = htmlentities($entity->$field, ENT_QUOTES); + if ($entity->$field !== null) { + $entity->$field = htmlentities($entity->$field, ENT_QUOTES); + } break; } return $entity; diff --git a/app/Models/Mangboard/BoardModel.php b/app/Models/Mangboard/BoardModel.php index a35a786..a880e10 100644 --- a/app/Models/Mangboard/BoardModel.php +++ b/app/Models/Mangboard/BoardModel.php @@ -75,7 +75,7 @@ class BoardModel extends CommonModel public function __construct(string $table) { $this->table = $table; - $fields = ["title", "user_name", "data_type", "editor_type", "reg_date", "hit", "content"]; + $fields = ["title", "user_pid", "user_id", "user_name", "data_type", "editor_type", "reg_date", "hit", "content"]; parent::__construct($fields); } public function getTitleField(): string @@ -101,6 +101,7 @@ class BoardModel extends CommonModel $rules[$field] = "required|trim|string"; break; case 'hit': + case 'user_pid': $rules[$field] = "if_exist|numeric"; break; default: