Untitled

 avatar
unknown
php
2 years ago
11 kB
5
Indexable
<?php
/** @noinspection ALL */

use Ausi\SlugGenerator\SlugGenerator;

class application
{
    public $DB;

    function __construct()
    {

        $this->DB = new MysqliDb ([
                'host' => DB_host,
                'username' => DB_user,
                'password' => DB_pass,
                'db' => DB_name,
                'port' => DB_port,
                'prefix' => DB_prefix,
                'charset' => DB_charset
            ]);

        $this->DB->autoReconnect = true;
    }

    function slugGenerator($strings){
        $generator = new SlugGenerator;
        return $generator->generate($strings);
    }

    function stopCrawlerbyCategory($categorys_array){
        $deny_categories = explode(',',GENRES_STOP_CR);

        foreach ($deny_categories as $deny_category){
            if(in_array(trim($deny_category), $categorys_array)){
                return false;
            }
        }

        return true;
    }

    function newChapters($oldChapters, $newChapters){


        if(empty($newChapters)){
            return [];
        }
//      Lặp tất cả chapter cũ, kiểm ra chap nào có trong array của list chapter mới unset chap mới

        foreach ($oldChapters as $keyOldChapter => $oldChapter){
              foreach ($newChapters as $keyNewChapter => $newChapter){
                  if(ucwords($newChapter['name']) === ucwords($oldChapter['name'])){
                      unset($newChapters[$keyNewChapter]);
                  }
              }
        }

        return $newChapters;
    }

    function isExistCrawlers($name, $type){
        $this->DB->where ("name", "$name");
        $this->DB->where ("type", "$type");

        return $this->DB->getOne("crawlers", ["id"]);
    }

    function isExistManga($name){
        $this->DB->where ("post_title", "$name");
        $this->DB->where ("post_type", "wp-manga");

        return $this->DB->getValue("posts", "ID",1);
    }

    function getTerms($name, $taxonomy){
        $this->DB->where ("name", "$name");

        $term_id = $this->DB->getValue("terms", "term_id", 1);
        if(!$term_id){
            $term_id = $this->DB->insert('terms', [
                "name" => $name,
                "slug" => $this->slugGenerator($name),
                'term_group' => 0
            ]);

            if(!$term_id) {
                echo 'terms: '. $this->DB->getLastError();
            }
        }

        $this->DB->where('term_id', $term_id);
        $this->DB->where('taxonomy', $taxonomy);
        $term_taxonomy_id = $this->DB->getValue('term_taxonomy', 'term_taxonomy_id',1);

        if(!$term_taxonomy_id) {
            $term_taxonomy_id = $this->DB->insert('term_taxonomy', [
                "term_id" => $term_id,
                'taxonomy' => $taxonomy,
                'description' => ""
            ]);
            if(!$term_taxonomy_id) {
                echo 'taxonomy: ' . $this->DB->getLastError();
            }

        }

        return $term_taxonomy_id;
    }

    function listChapters($manga){
        $this->DB->where("post_id", "$manga");

        return $this->DB->get("manga_chapters", null ,[
            "chapter_name as name"
        ]);
    }

    function listChaptersCrawler($manga){
        $this->DB->where("manga_id", "$manga");
        $this->DB->where("chapter_id", NULL, "IS");

        return $this->DB->get("crawler_chapter", null ,[
            "name"
        ]);
    }

    function getCrawlerItem($site, $type){
        $this->DB->where("site", $site);
        $this->DB->where("is_crawling", 0);
        $this->DB->where("type", $type);
        $this->DB->orderBy("id", "desc");
        return $this->DB->getOne("crawlers");
    }

    function removeCrawlers($id){
        $this->DB->where('id', $id);
        $this->DB->delete('crawlers');
    }

    function removeChapterCrawlers($id){
        $this->DB->where('id', $id);
        $this->DB->delete('crawler_chapter');
    }

    function CreateManga($manga = []){

        $MangaID = $this->DB->insert('posts', [
            'post_title' => $manga['name'],
            'post_name' => $this->SlugGenerator($manga['name']),
            'post_author' => POSTED_BY,
            'post_content' => $manga['description'] == null ? "" : $manga['description'],
            'post_status' => 'publish',
            'post_type' => 'wp-manga',
            'post_excerpt' => "",
            'pinged' => "",
            'to_ping' => "",
            'post_content_filtered' => "",
            'post_date' => $this->DB->now(),
            'post_date_gmt' => $this->DB->now(),
            'post_modified' => $this->DB->now(),
            'post_modified_gmt' => $this->DB->now()
        ]);

        if($MangaID) {
            $raw_cover_url = add_http($manga['cover']);

            $cover_url = save_cover($raw_cover_url);
            $cover_basename = basename($cover_url);
            $ThumbnailID = $this->DB->insert('posts', ['post_title' => $cover_basename, 'post_name' => $cover_basename, 'post_content' => "", 'guid' => $cover_url, 'post_author' => POSTED_BY, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_parent' => $MangaID, 'post_mime_type' => 'image/jpeg', 'post_excerpt' => "", 'pinged' => "", 'to_ping' => "", 'post_content_filtered' => "", 'post_date' => $this->DB->now(), 'post_date_gmt' => $this->DB->now(), 'post_modified' => $this->DB->now(), 'post_modified_gmt' => $this->DB->now(),]);

            if (!$ThumbnailID) {
                echo "Lỗi Lưu Cover: " . $this->DB->getLastError();
            } else {
                $postmeta_thumbnail = ['_wp_attached_file' => $cover_basename, '_wp_attachment_metadata' => serialize(['file' => $cover_basename, 'width' => 300, 'height' => 423])];
                foreach ($postmeta_thumbnail as $key => $value){
                    if(!$this->DB->insert('postmeta', [
                        'post_id' => $ThumbnailID,
                        'meta_key' => $key,
                        'meta_value' => $value
                    ])){
                        echo "Lỗi PostMeta Thumbnail: ". $this->DB->getLastError();
                    }
                }
            }

            $postmeta_manga = [
                '_wp_manga_alternative' => $manga['other_name'],
                '_wp_manga_status' => $manga['status'] == 0 ? 'on-going' : 'end',
                '_wp_manga_chapter_type' => 'manga',
                '_thumbnail_id' => $ThumbnailID,
                'manga_reading_content_gaps' => 'default',
                'manga_reading_style' => 'default',
                'manga_title_badges' => 'no',
                '_latest_update' => time(),
                '_edit_last' => POSTED_BY,
                'manga_unique_id' => 'manga_' . md5(time())
            ];

            foreach ($postmeta_manga as $key => $value){
                if(!$this->DB->insert('postmeta', [
                    'post_id' => $MangaID,
                    'meta_key' => $key,
                    'meta_value' => $value
                ])){
                    echo "Lỗi PostMeta Manga: ". $this->DB->getLastError();
                }
            }



            if (!empty($manga['authors'])) {
                $author_id = $this->getTerms($manga['authors'], 'wp-manga-author');
                if ($author_id) {
                    $this->DB->insert('term_relationships', [
                        'object_id' => $MangaID,
                        'term_taxonomy_id' => $author_id,
                        'term_order' => 0,
                    ]);
                }
            }

            if (!empty($manga['list_categories'])) {
                foreach ($manga['list_categories'] as $genre) {
                    $genre_id = $this->getTerms($genre, 'wp-manga-genre');
                    if ($genre_id) {
                        $this->DB->insert('term_relationships', [
                            'object_id' => $MangaID,
                            'term_taxonomy_id' => $genre_id,
                            'term_order' => 0,
                        ]);
                    }


                }

            }
        } else {
            die($this->DB->getLastError());
        }
        return $MangaID;
    }

    function UpdatePostStatus($manga, $status){
        $this->DB->where('ID', $manga);
        $this->DB->update('posts', [
            "post_status" => $status,
            'post_date' => $this->DB->now(),
            'post_date_gmt' => $this->DB->now(),
            'post_modified' => $this->DB->now(),
            'post_modified_gmt' => $this->DB->now(),
        ]);
    }

    function UpdateCrawlerStatus($crawler, $chapter_id){
        $this->DB->where('id', $crawler);
        $this->DB->update('crawler_chapter', [
            "is_crawling" => 0,
            'chapter_id' =>$chapter_id,
        ]);
    }

    function CreateChapter($chapter = []){
       $ChapterId = $this->DB->insert('manga_chapters', [
            'post_id' => $chapter['manga_id'],
            'volume_id' => 0,
            'chapter_name' => $chapter['chapter_name'],
            'chapter_name_extend' => "",
            'chapter_slug' => $this->slugGenerator($chapter['chapter_name']),
            'storage_in_use' => $chapter['storage_in_use'],
            'date' => $this->DB->now(),
            'date_gmt' => $this->DB->now(),
            'chapter_status' => 0,
            'chapter_metas' => null,
            'chapter_warning' => null,
            'chapter_index' => $this->GetIndexChapter($chapter['manga_id'])
        ]);

       return $ChapterId;
    }

    function CreateChapterData($chapter_id, $data, $storage){
        $this->DB->insert('manga_chapters_data', [
           "storage" => $storage,
           "chapter_id" => $chapter_id,
           "data" => json_encode($data, JSON_FORCE_OBJECT)
        ]);
    }

    function GetIndexChapter($manga_id){
        $this->DB->where('post_id', $manga_id);
        return $this->DB->getValue ("manga_chapters", "count(*)") + 1;
    }

    function UpdateMangaStatus($manga, $status){
        $this->DB->where('post_id', $manga);
        $this->DB->where('meta_key', '_wp_manga_status');
        $this->DB->update('postmeta', [
            'meta_value' => $status == 0 ? 'on-going' : 'end',
        ]);
    }

    function UpdateMangaTime($manga){
        $this->DB->where('post_id', $manga);
        $this->DB->where('meta_key', '_latest_update');
        $this->DB->update('postmeta', [
            'meta_value' => time(),
        ]);
    }

    function RawArrayConvert($list_url){
        $content_proxy = [];
        foreach ($list_url as $key => $url) {
            $content_proxy[$key]['src'] = $url;
            $content_proxy[$key]['mine'] = 'image/jpeg';
        }

        return $content_proxy;
    }

    function __destruct()
    {
        $this->DB->disconnectAll();
    }
}
Editor is loading...