🗃️ add an archive database for pages

Show pages by dates (monthly or yearly)
This commit is contained in:
Kazhnuz 2025-01-19 11:38:07 +01:00
parent e1f77f2eac
commit a5805a1f3f
4 changed files with 64 additions and 0 deletions

View file

@ -0,0 +1,49 @@
<?php defined('KOBLOG') or die('Koblog CMS.');
class Archives extends dbList {
function __construct()
{
parent::__construct(DB_ARCHIVES);
}
function numberOfPages($key)
{
return $this->countItems($key);
}
public function reindex()
{
global $pages;
$db = $pages->getDB($onlyKeys=false);
$archiveIndex = array();
foreach ($db as $pageKey=>$pageFields) {
if (in_array($pageFields['type'], $GLOBALS['DB_TAGS_TYPES'])) {
$date = $pageFields['date'];
$year = mb_substr($date, 0, 4);
$month = mb_substr($date, 0, 7);
// Index by years
if (isset($archiveIndex[$year])) {
array_push($archiveIndex[$year]['list'], $pageKey);
} else {
$archiveIndex[$year]['name'] = $year;
$archiveIndex[$year]['list'] = array($pageKey);
}
// Index by month
if (isset($archiveIndex[$month])) {
array_push($archiveIndex[$month]['list'], $pageKey);
} else {
$archiveIndex[$month]['name'] = $month;
$archiveIndex[$month]['list'] = array($pageKey);
}
}
}
$this->db = $archiveIndex;
$this->sortAlphanumeric();
return $this->save();
}
}

View file

@ -67,6 +67,7 @@ define('DB_SITE', PATH_DATABASES . 'site.php');
define('DB_CATEGORIES', PATH_DATABASES . 'categories.php');
define('DB_TAGS', PATH_DATABASES . 'tags.php');
define('DB_AUTHORS', PATH_DATABASES . 'authors.php');
define('DB_ARCHIVES', PATH_DATABASES . 'archives.php');
define('DB_SYSLOG', PATH_DATABASES . 'syslog.php');
define('DB_USERS', PATH_DATABASES . 'users.php');
define('DB_SECURITY', PATH_DATABASES . 'security.php');
@ -90,6 +91,7 @@ include(PATH_KERNEL . 'pages.class.php');
include(PATH_KERNEL . 'users.class.php');
include(PATH_KERNEL . 'tags.class.php');
include(PATH_KERNEL . 'authors.class.php');
include(PATH_KERNEL . 'archives.class.php');
include(PATH_KERNEL . 'language.class.php');
include(PATH_KERNEL . 'site.class.php');
include(PATH_KERNEL . 'categories.class.php');
@ -129,6 +131,7 @@ $pages = new Pages();
$users = new Users();
$tags = new Tags();
$authors = new Authors();
$archives = new Archives();
$categories = new Categories();
$site = new Site();
$url = new Url();

View file

@ -24,6 +24,14 @@ function reindexAuthors()
return $authors->reindex();
}
// Re-index database of archives
// If you create/edit/remove a page is necessary regenerate the database of archives
function reindexArchives()
{
global $archives;
return $archives->reindex();
}
// Generate the page 404 Not found
function buildErrorPage()
{
@ -363,6 +371,7 @@ function createPage($args)
reindexCategories();
reindexTags();
reindexAuthors();
reindexArchives();
// Add to syslog
$syslog->add(array(
@ -415,6 +424,7 @@ function editPage($args)
reindexCategories();
reindexTags();
reindexAuthors();
reindexArchives();
// Add to syslog
$syslog->add(array(
@ -441,6 +451,7 @@ function deletePage($key)
reindexCategories();
reindexTags();
reindexAuthors();
reindexArchives();
// Add to syslog
$syslog->add(array(

View file

@ -155,6 +155,7 @@ EOF;
reindexCategories();
reindexTags();
reindexAuthors();
reindexArchives();
}
return true;