diff --git a/cDB.php b/cDB.php index 7ccd29e..2e23adb 100644 --- a/cDB.php +++ b/cDB.php @@ -8,10 +8,11 @@ class cDB public function __construct() { $this->db = new SQLite3("cache.db"); - return $this->createTable(); + $this->createTablePage(); + $this->createTableConfig(); } - public function createTable() + private function createTablePage() { $sql = "CREATE TABLE IF NOT EXISTS page ( id INTEGER UNIQUE, @@ -25,6 +26,40 @@ public function createTable() return $this->db->exec($sql); } + private function createTableConfig() + { + $sql = "CREATE TABLE IF NOT EXISTS config ( + id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + name VARCHAR UNIQUE , + value varchar)"; + return $this->db->exec($sql); + } + + public function getConfig($name) + { + $sql = "SELECT * FROM config WHERE name = :name;"; + $query = $this->db->prepare($sql); + $query->bindValue(':name', $name); + return $query->execute()->fetchArray(SQLITE3_ASSOC); + } + + public function setConfig($name, $value) + { + if ($this->getConfig($name) == false) { + $sql = "INSERT INTO config (name, value) + VALUES (:name, :value)"; + } else { + $sql = "UPDATE config SET value = :value WHERE name = :name"; + } + $query = $this->db->prepare($sql); + $query->bindValue(':name', $name); + $query->bindValue(':value', $value); + if (!$query->execute()) { + return false; + } + return true; + } + public function getPageById(int $id) { $sql = "SELECT * FROM page WHERE id = :id;"; @@ -33,6 +68,17 @@ public function getPageById(int $id) return $query->execute()->fetchArray(SQLITE3_ASSOC); } + public function getEmptyPagesId() + { + $sql = "SELECT id FROM page WHERE revid is null"; + $query = $this->db->query($sql); + $result = []; + while ($row = $query->fetchArray(SQLITE3_ASSOC)) { + $result[$row['id']] = $row['id']; + } + return $result; + } + public function getEmptyUrl() { $sql = "SELECT id FROM page WHERE url is null;"; @@ -54,10 +100,19 @@ public function getCountPage() public function getPageList(int $page, int $count) { $offset = $count * $page; - $sql = "SELECT * FROM page LIMIT {$count} OFFSET {$offset};"; + $sql = "SELECT * FROM page WHERE url not null ORDER BY updateAt ASC LIMIT {$count} OFFSET {$offset};"; $query = $this->db->query($sql); $result = []; while ($row = $query->fetchArray(SQLITE3_ASSOC)) { + if (preg_match('/Файл:.+/m', $row['title'])) { + continue; + } + if (preg_match('/Категория:.+/m', $row['title'])) { + continue; + } + if (preg_match('/Шаблон:.+/m', $row['title'])) { + continue; + } $result[] = $row; } return $result; @@ -89,7 +144,8 @@ private function clearText($text) { $templateClear = [ '/(class|decoding|title|style|width|height)=".+"/mU', - '~~s' + '~~s', + '/href="#[^"]+"/m' ]; foreach ($templateClear as $template) { $text = preg_replace($template, '', $text); @@ -101,7 +157,7 @@ public function updateCache(cPage $page) { if ($this->getPageById($page->id)) { $sql = "UPDATE page - SET revid = :revid, user = :user, title = :title, text=:text,updateAt=:updateAt,categories=:categories + SET revid = :revid, user = :user, title = :title, text=:text, updateAt=:updateAt, categories=:categories WHERE id = :id"; } else { $sql = "INSERT INTO page (id,revid,user,title,text,updateAt,categories) diff --git a/cPageList.php b/cPageList.php index 72a6b6f..5369cb9 100644 --- a/cPageList.php +++ b/cPageList.php @@ -6,13 +6,15 @@ class cPageList extends cContent public array $listPage = []; private string $author; private bool $replaceAuthor; + private cDB $db; public function __construct($config) { parent::__construct($config['urlAPI']); $this->author = $config['defaultAuthor']; $this->replaceAuthor = $config['replaceAuthor']; - $this->getPages(); + //$this->getPages(); + $this->db = new cDB(); } public function init() @@ -24,7 +26,8 @@ public function init() $page = new cPage( $pageIndex['pageid'], $pageIndex['title'], - date(DATE_RFC822, 0)); + date(DATE_RFC822, time() - 86400)); + $page->author = $this->author; $pages[] = $page; } return $pages; @@ -51,8 +54,7 @@ function getPages() public function getPageList(int $page, int $count): array { - $db = new cDB(); - $pagesDB = $db->getPageList($page, $count); + $pagesDB = $this->db->getPageList($page, $count); $pages = []; foreach ($pagesDB as $pageDB) { $pages[] = $this->convertArrayToPage($pageDB); @@ -60,12 +62,17 @@ public function getPageList(int $page, int $count): array return $pages; } + public function getEmptyPages() + { + return $this->db->getEmptyPagesId(); + } + private function convertArrayToPage(array $pageDB): cPage { $page = new cPage($pageDB['id'], $pageDB['title'], $pageDB['updateAt']); $page->url = $pageDB['url']; $page->updateAt = $pageDB ['updateAt']; - $page->categories = explode(',', $pageDB['categories']); + $page->categories = ($pageDB['categories'] == '') ? [] : explode(',', $pageDB['categories']); $page->user = $pageDB['user']; $page->text = $pageDB['text']; $page->revid = $pageDB['revid']; @@ -74,15 +81,13 @@ private function convertArrayToPage(array $pageDB): cPage public function getPageId($id) { - $db = new cDB(); - $pageDB = $db->getPageById($id); + $pageDB = $this->db->getPageById($id); return $this->convertArrayToPage($pageDB); } public function countPageDB() { - $db = new cDB(); - return $db->getCountPage(); + return $this->db->getCountPage(); } public function countPage() @@ -90,4 +95,19 @@ public function countPage() return count($this->listPage); } + public function getConfigDB($name) + { + return $this->db->getConfig($name); + } + + public function setConfigDB($name, $value) + { + return $this->db->setConfig($name, $value); + } + + public function savePageDB(cPage $page) + { + return $this->db->updateCache($page); + } + } \ No newline at end of file diff --git a/cParse.php b/cParse.php index 71f76e4..1cf367c 100644 --- a/cParse.php +++ b/cParse.php @@ -76,6 +76,9 @@ private function updatePageByPage(cPage &$page) $page->revid = $parse['revid']; if (is_array($parse['categories'])) { foreach ($parse['categories'] as $category) { + if (trim($category['*']) == '') { + continue; + } $page->categories[] = $category['*']; } } @@ -92,7 +95,7 @@ public function updateCacheByPageId(cPage $page) // получаем данные из БД $pageCache = $this->db->getPageById($page->id); // нет страницы в кэше - if (!$pageCache || $page->updateAt > $pageCache['updateAt']) { + if (!$pageCache || $page->updateAt > $pageCache['updateAt'] || empty($page->revid)) { // парсим страницу и записываем в БД $this->updatePageByPage($page); $this->db->updateCache($page); diff --git a/cRSS.php b/cRSS.php index e574873..7fd443e 100644 --- a/cRSS.php +++ b/cRSS.php @@ -28,6 +28,7 @@ public function __construct($template) */ public function generateRSS(array $pages) { + header('Content-Type: application/xml; charset=utf-8'); $items = []; foreach ($pages as $page) { $items[] = $this->getPartTemplate('item', ['config' => $this->params, 'page' => $page]); diff --git a/config.inc.php b/config.inc.php index 13f21c7..ce2018b 100644 --- a/config.inc.php +++ b/config.inc.php @@ -6,7 +6,7 @@ // урл API wiki 'urlAPI' => $server . "/api.php", // если не найден автор статьи, то будет подставляться этот - 'defaultAuthor' => 'Администратор', + 'defaultAuthor' => 'Admin', // меняет авторов статей на defaultAuthor 'replaceAuthor' => false, // шаблон по-умолчанию diff --git a/index.php b/index.php index 481ea8b..82c1441 100644 --- a/index.php +++ b/index.php @@ -14,26 +14,47 @@ // модуль парсинга страниц $parse = new cParse($config['urlAPI']); +// инициализация кэша, если первый запуск, заполняем БД текущими статьями +if (!$list->getConfigDB('init')) { + if (!$list->getConfigDB('indexPage')) { + $listInitPages = $list->init(); + foreach ($listInitPages as $page) { + // заносим список страниц в БД + $page->user = $config['defaultAuthor']; + $list->savePageDB($page); + } + $list->setConfigDB('indexPage', 1); + } + $pagesIds = $list->getEmptyPages(); + foreach ($pagesIds as $id) { + $page = $list->getPageId($id); + $parse->updateCacheByPageId($page); + } + $list->setConfigDB('init', 1); +} + +$list->getPages(); // проверяем все страницы foreach ($list->listPage as $page) { // обновляем кэш $parse->updateCacheByPageId($page); } - // модуль формирования RSS $rssTemplate = isset($_GET['template']) ? $_GET['template'] : $config['defaultTemplate']; $rss = new cRSS($rssTemplate); if (isset($_GET['page'])) { // формируем страницу rss - $listPages= $list->getPageList($_GET['page'], $rss->getMaxCount()); + $listPages = $list->getPageList($_GET['page'], $rss->getMaxCount()); $lenta = $rss->generateRSS($listPages); - print_r($lenta); + echo($lenta); } else { // формируем список rss - $countPage = ceil($list->countPage() / $rss->getMaxCount()); + $countPage = ceil($list->countPageDB() / $rss->getMaxCount()); + echo "
"; for ($i = 0; $i < $countPage; $i++) { - $strTemplate = ($config['defaultTemplate'] == $rssTemplate)? '': "template={$rssTemplate}&"; + $strTemplate = ($config['defaultTemplate'] == $rssTemplate) ? '' : "template={$rssTemplate}&"; echo $str = "http://{$config['here']}?{$strTemplate}page={$i}"; } diff --git a/params_init.json b/params_init.json index 171ad5f..b6c659b 100644 --- a/params_init.json +++ b/params_init.json @@ -5,6 +5,7 @@ "iwurl": 1, "rawcontinue": 1, "aplimit": "2", + "redirects": 1, "utf8": 1, "formatversion": "2" } \ No newline at end of file diff --git a/rss_templates/turbo.item.php b/rss_templates/turbo.item.php index c483096..ea86916 100644 --- a/rss_templates/turbo.item.php +++ b/rss_templates/turbo.item.php @@ -36,13 +36,14 @@
"; } + echo "