Automation init...1

This commit is contained in:
최준흠 2024-09-05 20:13:31 +09:00
parent 752ae5f33a
commit 41f83d2234
13 changed files with 339 additions and 96 deletions

View File

@ -4,12 +4,13 @@ namespace App\Controllers\CLI;
use App\Controllers\BaseController;
use App\Libraries\MyWebLibrary;
use App\Libraries\MyStorage\MyStorageFileLibrary;
use App\Libraries\MyCrawlerLibrary;
use App\Entities\Mangboard\FreeboardEntity;
use App\Libraries\Mangboard\FreeboardLibrary;
use App\Libraries\MyCrawlerLibrary;
use App\Libraries\MyStorage\MyStorageFileLibrary;
use App\Libraries\MyWebLibrary;
use App\Libraries\YamapLibrary;
use App\Libraries\Mangboard\UserLibrary;
class Yamap extends BaseController
{
@ -21,12 +22,17 @@ class Yamap extends BaseController
$datas = [];
//Yamap사이트에서 자유게시판에서 최근 게시물 데이터 가져오기
if (!in_array("skip_build", $params)) {
$myWeb = new MyWebLibrary(getenv('yamap.host.url'));
$storage = new MyStorageFileLibrary(WRITEPATH . "uploads");
$storage->setPath("Yamap");
$crawler = new MyCrawlerLibrary();
$yamap = new YamapLibrary();
$yamap->setDebug($isDebug);
$yamap->setMyWeb(new MyWebLibrary(getenv('yamap.host.url')));
$yamap->setMyStorage(new MyStorageFileLibrary(WRITEPATH . "uploads" . DIRECTORY_SEPARATOR . "Yamap"));
$yamap->setMyCrawler(new MyCrawlerLibrary());
list($nickname, $datas) = $yamap->build();
$yamap->setMyWeb($myWeb);
$yamap->setMyStorage($storage);
$yamap->setMyCrawler($crawler);
list($nickname, $mediaInfos, $mediaTags) = $yamap->build();
}
//2. 사이트 로그인 처리
if (!in_array("skip_login", $params)) {
@ -40,9 +46,14 @@ class Yamap extends BaseController
}
//3. 망보드 일반게시판에 게시물 등록 처리
if (!in_array("skip_create", $params)) {
$mangboard = new UserLibrary();
$mangboard = new FreeboardLibrary();
$mangboard->setDebug($isDebug);
// $mangboard->create();
$entity = new FreeboardEntity();
//미디어관련용
$entity->setTitle($nickname);
$entity->setContent(is_array($mediaTags) ? implode("\n", $mediaTags) : $mediaTags);
$mangboard->create($entity);
}
log_message("notice", "완료되었습니다.");
return true;

View File

@ -12,5 +12,5 @@ abstract class CommonEntity extends Entity
abstract public function __toString();
abstract public function getPK();
abstract public function getName();
abstract public function getTitle();
}

View File

@ -8,14 +8,30 @@ class FreeboardEntity extends CommonEntity
{
public function __toString(): string
{
return "{$this->getPK()}:{$this->getName()}";
return "{$this->getPK()}:{$this->getTitle()}";
}
public function getPK(): int
{
return $this->attributes['pid'];
}
public function getName(): string
public function getTitle(): string
{
return $this->attributes['user_name'];
return $this->attributes['title'];
}
//Common Function
public function setTitle(string $title)
{
$this->attributes['title'] = $title;
}
public function getGID(): int
{
return $this->attributes['gid'];
}
public function setContent(string $content)
{
$this->attributes['content'] = $content;
}
}

View File

@ -8,16 +8,18 @@ class UserEntity extends CommonEntity
{
public function __toString(): string
{
return "{$this->getPK()}:{$this->getID()}:{$this->getName()},{$this->getLevel()}/{$this->getPoint()}";
return "{$this->getPK()}:{$this->getID()}:{$this->getTitle()},{$this->getLevel()}/{$this->getPoint()}";
}
public function getPK(): int
{
return $this->attributes['pid'];
}
public function getName(): string
public function getTitle(): string
{
return $this->attributes['user_name'];
}
//Common Function
public function getID(): string
{
return $this->attributes['user_id'];

View File

@ -15,4 +15,10 @@ abstract class CommonLibrary
{
$this->_debug = $debug;
}
//url에 http 나 https가 포함되어 있으면 true
final public function isContainsHttpOrHttps($url): bool
{
return strpos($url, 'http://') !== false || strpos($url, 'https://') !== false;
}
}

View File

@ -2,8 +2,8 @@
namespace App\Libraries\Mangboard;
use App\Entities\Mangboard\UserEntity;
use App\Models\Mangboard\UserModel;
use App\Entities\Mangboard\FreeboardEntity;
use App\Models\Mangboard\FreeboardModel;
class FreeboardLibrary extends MangboardLibrary
{
@ -13,22 +13,18 @@ class FreeboardLibrary extends MangboardLibrary
{
parent::__construct();
}
private function getModel(): UserModel
private function getModel(): FreeboardModel
{
if ($this->_model === null) {
$this->_model = new UserModel();
$this->_model = new FreeboardModel();
}
return $this->_model;
}
public function create($id, int $point, $sign = '+'): UserEntity
public function create(FreeboardEntity $entity, array $formDatas = []): FreeboardEntity
{
$entity = is_numeric($id) ? $this->getModel()->getEntityByPK(intval($id)) : $this->getModel()->getEntityByID($id);
if (!$entity) {
throw new \Exception("해당 회원[{$id}]이 없습니다.");
}
$entity = $this->getModel()->setPoint($entity, $point);
log_message("debug", __FUNCTION__ . "=>[{$entity}] 회원님의 Level은 {$entity->getLevel()} 입니다.");
$entity = $this->getModel()->create($entity, $formDatas);
log_message("debug", __FUNCTION__ . "=>등록이 완료되었습니다.");
return $entity;
}
}

View File

@ -6,19 +6,18 @@ use App\Libraries\MyStorage\MyStorageLibrary;
class MyStorageFileLibrary extends MyStorageLibrary
{
private $_defaultPath = "";
private $_uploadPath = "";
private $_path = "";
private $_fileName = "";
private $_savePath = "";
public function __construct($defaultPath)
public function __construct($uploadPath)
{
parent::__construct();
$this->_defaultPath = $defaultPath;
$this->_uploadPath = $uploadPath;
}
final public function getDefaultPath(): string
final public function getUploadPath(): string
{
return $this->_defaultPath;
return $this->_uploadPath;
}
final public function getPath(): string
@ -40,7 +39,7 @@ class MyStorageFileLibrary extends MyStorageLibrary
final public function save($content): bool
{
$fullPath = $this->getDefaultPath() . DIRECTORY_SEPARATOR . $this->getPath();
$fullPath = $this->getUploadPath() . DIRECTORY_SEPARATOR . $this->getPath();
if (!is_dir($fullPath)) {
if (!mkdir($fullPath)) {
throw new \Exception("Make Directory Error:" . $fullPath);

View File

@ -37,11 +37,6 @@ class MyWebLibrary extends CommonLibrary
return $this->_cookieJar;
}
//url에 http 나 https가 포함되어 있으면 true
private function isContainsHttpOrHttps($url): bool
{
return strpos($url, 'http://') !== false || strpos($url, 'https://') !== false;
}
public function getContent(string $url, array $options = []): string
{
//url에 http 나 https가 포함되어 있지않으면

View File

@ -92,13 +92,15 @@ class YamapLibrary extends CommonLibrary
log_message("debug", "download:{$options["tag"]},{$options["attr"]}");
$nodes = $this->getMyCrawler()->getNodes($crawler, $options);
foreach ($nodes as $node) {
list($fileName, $content) = $this->getMyWeb()->download($node->attr($options["attr"]));
$original = $node->attr($options["attr"]);
list($fileName, $content) = $this->getMyWeb()->download($original);
$this->getMyStorage()->setFileName($fileName);
if (!$this->getMyStorage()->save($content)) {
continue;
}
$datas[] = [
"path" => $this->getMyStorage()->getDefaultPath() . DIRECTORY_SEPARATOR . $this->getMyStorage()->getPath(),
"orignal" => $node->html(),
"path" => $this->getMyStorage()->getPath(),
"fileName" => $fileName,
"content" => $content
];
@ -143,11 +145,27 @@ class YamapLibrary extends CommonLibrary
public function detailPage($url): array
{
$crawler = $this->getCrawler($url, getenv("yamap.view.content.tag"));
//3. Image
$contents = [];
//3. Image 처리
$images = $this->download_process($crawler, ["tag" => "img", "attr" => "src"]);
//4. Video
foreach ($images as $image) {
if ($this->isContainsHttpOrHttps($image['orignal'])) {
$contents[] = $images['orignal'];
} else {
$contents[] = sprintf("<img src=\"%s\" alt=\"%s\">", $image["path"], $image["fileName"], $image["fileName"]);
};
}
//4. Video(mp4) 처리
$videos = $this->download_process($crawler, ["tag" => "video", "attr" => "src"]);
return array_merge($images, $videos);
foreach ($images as $image) {
if ($this->isContainsHttpOrHttps($image['orignal'])) {
$contents[] = $images['orignal'];
} else {
$contents[] = sprintf("<video src=\"%s\" alt=\"%s\">", $image["path"], $image["fileName"], $image["fileName"]);
};
}
return array(array_merge($images, $videos), $contents);
}
public function build(): array
@ -160,7 +178,7 @@ class YamapLibrary extends CommonLibrary
list($nickname, $detail_url) = $this->mainPage();
}
//2. DetailPage 처리 : bbs_view > div.contents 가진 객체를 찾아서 처리
$detailDatas = $this->detailPage($detail_url);
return array($nickname, $detailDatas);
list($mediaInfos, $mediaTags) = $this->detailPage($detail_url);
return array($nickname, $mediaInfos, $mediaTags);
}
}

View File

@ -45,6 +45,8 @@ abstract class CommonModel extends Model
protected $beforeDelete = [];
protected $afterDelete = [];
protected function __construct() {}
abstract public function getPK(): string;
final public function getEntity()
@ -65,4 +67,75 @@ abstract class CommonModel extends Model
{
return $this->asObject($this->returnType)->findAll();
}
final protected function save_process($entity)
{
// echo var_export($entity, true);
// exit;
if ($entity->hasChanged()) {
if (!$this->save($entity)) {
log_message("error", __FUNCTION__ . "에서 호출:" . $this->getLastQuery());
log_message("error", implode("\n", $this->errors()));
throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . $this->getLastQuery() . "\n" . var_dump($this->errors(), true));
}
} else {
throw new \Exception(__FUNCTION__ . " 오류 발생.\n 기존정보와 동일하여 수정되지 않았습니다.");
}
return $entity;
}
//create , modify 직전 작업용 작업
protected function changeFormData(string $action, string $field, array $formDatas, $entity)
{
switch ($field) {
// case "user_uid": //입력데이터로 있을시 관리툴에서 (사용자,등)추가, 없을시는 입력의 경우에만 자동(장바구니,등)으로 추가
// if (array_key_exists($field, $formDatas) && !is_null($formDatas[$field])) {
// //관리툴 USERSNS에서 사용자 연동 시 추가기능등에 사용
// $entity->$field = $formDatas[$field];
// } elseif ($action == 'create' && $this->_session->get(SESSION_NAMES["ISLOGIN"])) {
// //Front에서 장바구니,게시판등에 추가시 로그온한경우 자동 추가기능등에 사용
// $auth = $this->_session->get(SESSION_NAMES["AUTH"]);
// $entity->$field = $auth[AUTH_FIELDS["ID"]];
// }
// break;
case "passwd":
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
$entity->$field = password_hash($formDatas[$field], PASSWORD_DEFAULT);
}
break;
case "content":
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
$entity->$field = htmlentities($formDatas[$field]);
}
break;
default:
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
$entity->$field = $formDatas[$field];
}
break;
}
return $entity;
}
protected function create_process($entity, array $formDatas)
{
foreach ($this->allowedFields as $field) {
$entity = $this->changeFormData('create', $field, $formDatas, $entity);
}
$entity = $this->save_process($entity);
//primaryKey가 자동입력이면
if ($this->useAutoIncrement) {
$pk = $this->getPK();
$entity->$pk = $this->insertID();
}
return $entity;
}
protected function modify_process($entity, array $formDatas)
{
$entity->updated_at = time(); //수정한 시간정의
foreach ($this->allowedFields as $field) {
$entity = $this->changeFormData('modify', $field, $formDatas, $entity);
}
return $this->save_process($entity);
}
}

View File

@ -0,0 +1,172 @@
<?php
namespace App\Models\Mangboard;
use App\Entities\Mangboard\FreeboardEntity;
use App\Models\CommonModel;
class FreeboardModel extends CommonModel
{
protected $table = 'mb_board_free;';
protected $primaryKey = 'pid';
protected $useAutoIncrement = true;
protected $returnType = FreeboardEntity::class;
protected $allowedFields = [
// 'pid',
// 'gid',
'reply',
'depth',
'user_id',
'user_name',
'title',
'passwd',
'homepage',
'email',
'address',
'phone',
'reg_date',
'modify_date',
'calendar_date',
'hit',
'user_pid',
'parent_pid',
'parent_user_pid',
'level',
'file_count',
'comment_count',
'vote_good_count',
'vote_bad_count',
'vote_type',
'ip',
'agent',
'is_notice',
'is_secret',
'status',
'is_show',
'reply_email',
'text',
'content',
'content_type',
'data_type',
'editor_type',
'tag',
'category1',
'category2',
'category3',
'image_path',
'site_link1',
'site_link2',
'gps_latitude',
'gps_longitude',
'ext1',
'ext2',
'ext3',
'ext4',
'ext5',
'ext6',
'ext7',
'ext8',
'ext9',
'ext10',
];
// Validation
// protected $validationRules = [];
protected $validationRules = [
'pid' => 'if_exist|trim|numeric',
'gid' => 'if_exist|trim|numeric',
'reply' => 'if_exist|trim|numeric',
'depth' => 'if_exist|trim|numeric',
'user_id' => 'if_exist|trim|string',
'user_name' => 'if_exist|trim|string',
'title' => 'if_exist|trim|string',
'passwd' => 'if_exist|trim|string',
'homepage' => 'if_exist|trim|string',
'email' => 'if_exist|trim|valid_email',
'address' => 'if_exist|trim|string',
'phone' => 'if_exist|trim|string',
// 'reg_date' => 'if_exist|valid_date[Y-m-d hh:ii:dd]',
// 'modify_date' => 'if_exist|valid_date[Y-m-d hh:ii:dd]',
// 'calendar_date' => 'if_exist|valid_date[Y-m-d hh:ii:dd]',
'reg_date' => 'if_exist|string',
'modify_date' => 'if_exist|string',
'calendar_date' => 'if_exist|string',
'hit' => 'if_exist|trim|numeric',
'user_pid' => 'if_exist|trim|numeric',
'parent_pid' => 'if_exist|trim|numeric',
'parent_user_pid' => 'if_exist|trim|numeric',
'level' => 'if_exist|trim|numeric',
'file_count' => 'if_exist|trim|numeric',
'comment_count' => 'if_exist|trim|numeric',
'vote_good_count' => 'if_exist|trim|numeric',
'vote_bad_count' => 'if_exist|trim|numeric',
'vote_type' => 'if_exist|trim|numeric',
'ip' => 'if_exist|trim|string',
'agent' => 'if_exist|trim|string',
'is_notice' => 'if_exist|trim|numeric',
'is_secret' => 'if_exist|trim|numeric',
'status' => 'if_exist|trim|string',
'is_show' => 'if_exist|trim|numeric',
'reply_email' => 'if_exist|trim|numeric',
'text' => 'if_exist|string',
'content' => 'if_exist|string',
'content_type' => 'if_exist|trim|string',
'data_type' => 'if_exist|trim|string',
'editor_type' => 'if_exist|trim|string',
'tag' => 'if_exist|trim|string',
'category1' => 'if_exist|trim|string',
'category2' => 'if_exist|trim|string',
'category3' => 'if_exist|trim|string',
'image_path' => 'if_exist|trim|string',
'site_link1' => 'if_exist|trim|string',
'site_link2' => 'if_exist|trim|string',
'gps_latitude' => 'if_exist|trim|string',
'gps_longitude' => 'if_exist|trim|string',
'ext1' => 'if_exist|string',
'ext2' => 'if_exist|string',
'ext3' => 'if_exist|string',
'ext4' => 'if_exist|string',
'ext5' => 'if_exist|string',
'ext6' => 'if_exist|string',
'ext7' => 'if_exist|string',
'ext8' => 'if_exist|string',
'ext9' => 'if_exist|string',
'ext10' => 'if_exist|string',
// 'updated_at' => 'if_exist|valid_date',
// 'created_at' => 'if_exist|valid_date',
// 'deleted_at' => 'if_exist|valid_date',
];
public function __construct()
{
parent::__construct();
}
public function getPK(): string
{
return $this->primaryKey;
}
public function getEntityByPK(int $uid): null|FreeboardEntity
{
$this->where($this->getPK(), $uid);
return $this->getEntity();
}
public function getEntityByID(string $id): null|FreeboardEntity
{
$this->where('user_id', $id);
return $this->getEntity();
}
public function create(FreeboardEntity $entity, array $formDatas = []): FreeboardEntity
{
$entity = $this->create_process($entity, $formDatas);
//GID값이 PK랑 같게하기위해
$entity->setGID($entity->getPK());
$entity = $this->modify_process($entity, $formDatas);
return $entity;
}
public function modify(FreeboardEntity $entity, array $formDatas = []): FreeboardEntity
{
return $this->modify_process($entity, $formDatas);
}
}

View File

@ -31,6 +31,11 @@ class UserModel extends CommonModel
// 'updated_at' => 'if_exist|valid_date',
// 'created_at' => 'if_exist|valid_date',
];
public function __construct()
{
parent::__construct();
}
public function getPK(): string
{
return $this->primaryKey;

View File

@ -1,50 +0,0 @@
<?php
namespace App\Models\Mangboard;
use App\Entities\Mangboard\FreeboardEntity;
use App\Models\CommonModel;
class FreeboardModel extends CommonModel
{
protected $table = 'mb_board_free;';
protected $primaryKey = 'pid';
protected $useAutoIncrement = true;
protected $returnType = FreeboardEntity::class;
protected $allowedFields = ['pid', 'user_id', 'passwd', 'user_name', 'user_email', 'user_state', 'user_level', 'user_point'];
// Validation
// protected $validationRules = [];
protected $validationRules = [
'pid' => 'if_exist|numeric',
'user_id' => 'if_exist|trim|string',
'passwd' => 'if_exist|trim|string',
// 'confirmpassword' => 'if_exist|trim|matches[passwd]',
'user_name' => 'if_exist|trim|string',
'user_state' => 'if_exist|trim|string',
'user_email' => 'if_exist|trim|valid_email',
'user_level' => 'if_exist|numeric',
'user_point' => 'if_exist|numeric',
// 'proxied' => 'if_exist|in_list[on,off]',
// 'fixed' => 'if_exist|in_list[on,off]',
// 'locked' => 'if_exist|in_list[on,off]',
// 'updated_at' => 'if_exist|valid_date',
// 'created_at' => 'if_exist|valid_date',
];
public function getPK(): string
{
return $this->primaryKey;
}
public function getEntityByPK(int $uid): null|FreeboardEntity
{
$this->where($this->getPK(), $uid);
return $this->getEntity();
}
public function getEntityByID(string $id): null|FreeboardEntity
{
$this->where('user_id', $id);
return $this->getEntity();
}
}