Skip to content

Commit

Permalink
исправление ошибок, отправка в Яндекс
Browse files Browse the repository at this point in the history
  • Loading branch information
Ichinya committed Dec 8, 2020
1 parent f7a7631 commit dc55b8c
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 24 deletions.
66 changes: 61 additions & 5 deletions cDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;";
Expand All @@ -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;";
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down
38 changes: 29 additions & 9 deletions cPageList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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;
Expand All @@ -51,21 +54,25 @@ 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);
}
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'];
Expand All @@ -74,20 +81,33 @@ 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()
{
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);
}

}
5 changes: 4 additions & 1 deletion cParse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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['*'];
}
}
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions cRSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion config.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// урл API wiki
'urlAPI' => $server . "/api.php",
// если не найден автор статьи, то будет подставляться этот
'defaultAuthor' => 'Администратор',
'defaultAuthor' => 'Admin',
// меняет авторов статей на defaultAuthor
'replaceAuthor' => false,
// шаблон по-умолчанию
Expand Down
31 changes: 26 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<pre>";
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}<br />";
}
echo "</pre>";
}
1 change: 1 addition & 0 deletions params_init.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"iwurl": 1,
"rawcontinue": 1,
"aplimit": "2",
"redirects": 1,
"utf8": 1,
"formatversion": "2"
}
3 changes: 2 additions & 1 deletion rss_templates/turbo.item.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@
<author><?= $page->user; ?></author>
<metrics>
<yandex schema_identifier="<?= $page->id; ?>">
<breadcrumblist>
<?/* <breadcrumblist>
<?
foreach ($breadcrumblist as $breadcrumb) {
echo "<breadcrumb url=\"{$config['link']}/{$breadcrumb['url']}\" text=\"{$breadcrumb['text']}\" /></breadcrumb>";
}
?>
</breadcrumblist>
*/ ?>
</yandex>
</metrics>
<yandex:related></yandex:related>
Expand Down
2 changes: 0 additions & 2 deletions rss_templates/turbo.rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
<link><?= $config['link']; ?></link>
<description><?= $config['description']; ?></description>
<language>ru</language>
<turbo:analytics></turbo:analytics>
<turbo:adNetwork></turbo:adNetwork>
<?php echo implode('', $data['items']); ?>
</channel>
</rss>

0 comments on commit dc55b8c

Please sign in to comment.