cleaned up the code to get a massive boost to publishing and page rendering speeds the issue was related to pages being collected and transformed into objects the system could use multiple times, resulting in sluggish rendering due to available memory getting drained to accomodate a long script running more than it was needed. edited the rendering process so the script is only called one and the data is passed to the respective functions that require that data. also plugged in some minor manual garbage collection that speed up the process a bit more there was also a bug with rendering the tags file that was resulting in the `tags.json` file being unusually large. the issue was tags were getting replicated in the process resulting in adding duplicates to the tag file everytime it was run. go rid of the unnecessary function that was calling the replicate tags array and now the tags file is it's appropriate size whew, this turned out to be a big one that wasn't meant to be, but the peformance boost was worth it
288 lines
10 KiB
PHP
288 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Data;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use function _\find;
|
|
use function _\filter;
|
|
|
|
class SortingService
|
|
{
|
|
private $p_tags = [];
|
|
private $p_archive = [];
|
|
private $settings;
|
|
private $contents;
|
|
private $themes;
|
|
private $info = [];
|
|
|
|
public function __construct(
|
|
SettingsService $settingsService,
|
|
ContentService $contentService,
|
|
ThemeService $themeService,
|
|
) {
|
|
$this->settings = $settingsService;
|
|
$this->contents = $contentService;
|
|
$this->themes = $themeService;
|
|
$global = $this->settings->getGlobal();
|
|
$this->info = [
|
|
'keywords' => isset($global['keywords'])
|
|
? $global['keywords']
|
|
: 'fipamo, blog, jamstack, php, markdown, js',
|
|
'description' => $global['descriptions'],
|
|
'image' => $global['base_url'] . $global['background'],
|
|
'baseURL' => $global['base_url'],
|
|
'dynamicRender' => $global['dynamicRender'],
|
|
'theme' => $global['theme'],
|
|
];
|
|
}
|
|
|
|
public function tags($pages, $debug = true)
|
|
{
|
|
foreach ($pages as $page) {
|
|
$temp = [];
|
|
if (isset($page['tags'])) {
|
|
$temp = explode(',', $page['tags']);
|
|
foreach ($temp as $tag) {
|
|
$label = safeString(trim($tag));
|
|
if (!find($this->p_tags, ['tag_name' => $label])) {
|
|
array_push($this->p_tags, [
|
|
'tag_name' => $label,
|
|
'slug' => safeString($label),
|
|
'pages' => $this->tagPages($label, $pages),
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$tagData = [];
|
|
|
|
$settingsTags = [];
|
|
if (isset($this->settings->getTags()['pages'])) {
|
|
$settingsTags = $this->settings->getTags()['pages'];
|
|
}
|
|
$tagData = [
|
|
'debug' => $debug, // for theme kit
|
|
'layout' => 'page',
|
|
'tags' => $this->p_tags,
|
|
'theme' => $this->info['theme'], // for theme kit
|
|
'title' => 'Pages Tagged as Tag',
|
|
'dynamicRender' => $this->info['dynamicRender'],
|
|
'pages' => $settingsTags,
|
|
'info' => $this->info,
|
|
'menu' => $this->settings->getMenu(),
|
|
'media' => [
|
|
['file' => $this->info['image'],
|
|
'type' => trim(pathinfo($this->info['image'], PATHINFO_EXTENSION))]
|
|
]
|
|
];
|
|
|
|
return $tagData;
|
|
}
|
|
|
|
private function tagPages($tag, $pages)
|
|
{
|
|
$tagged = [];
|
|
foreach ($pages as $page) {
|
|
if (isset($page['tags'])) {
|
|
if (strpos($page['tags'], $tag) !== false) {
|
|
array_push($tagged, [
|
|
'title' => urldecode($page['title']),
|
|
'slug' => $page['slug'],
|
|
'path' => $page['path'],
|
|
'feature' => $page['feature'],
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $tagged;
|
|
}
|
|
|
|
public function archive($pages, $debug = true)
|
|
{
|
|
$years = [];
|
|
$archive = [];
|
|
foreach ($pages as $page) {
|
|
$date = explode('/', $page['path']);
|
|
if (!find($years, ['year' => trim($date[0])])) {
|
|
$findPages = filter($pages, ['createdYear' => trim($date[0])]);
|
|
array_push(
|
|
$years,
|
|
[
|
|
'year' => trim($date[0]),
|
|
'count' => count($findPages),
|
|
]
|
|
);
|
|
}
|
|
}
|
|
foreach ($years as $year) {
|
|
$sorted = [];
|
|
$filtered = filter($pages, ['createdYear' => $year['year']]);
|
|
foreach ($filtered as $obj) {
|
|
$month = date('m', date($obj['rawCreated']));
|
|
if (!find($sorted, ['month' => $month])) {
|
|
$perMonth = filter(
|
|
$pages,
|
|
[
|
|
'path' => $year['year'] . '/' . $month,
|
|
'deleted' => false,
|
|
'published' => true,
|
|
'layout' => 'page',
|
|
]
|
|
);
|
|
array_push($sorted, [
|
|
'month' => $month,
|
|
'full_month' => date('F', date($obj['rawCreated'])),
|
|
'count' => count($perMonth),
|
|
'pages' => $perMonth,
|
|
]);
|
|
}
|
|
}
|
|
array_push($this->p_archive, [
|
|
'year' => $year['year'],
|
|
'year_data' => $sorted,
|
|
]);
|
|
}
|
|
$archive_data = [];
|
|
|
|
$archiveData = [
|
|
'debug' => $debug, // for theme kit
|
|
'theme' => $this->info['theme'], // for theme kit
|
|
'layout' => 'page',
|
|
'title' => 'Archives',
|
|
'dynamicRender' => $this->info['dynamicRender'],
|
|
'archive' => $this->p_archive,
|
|
'info' => $this->info,
|
|
'menu' => $this->settings->getMenu(),
|
|
'media' => [
|
|
['file' => $this->info['image'],
|
|
'type' => trim(pathinfo($this->info['image'], PATHINFO_EXTENSION))]
|
|
],
|
|
];
|
|
|
|
return $archiveData;
|
|
}
|
|
|
|
public function page($page, $pages, $debug = true)
|
|
{
|
|
$pageOptions = [];
|
|
$tags = [];
|
|
if (isset($page['tags'])) {
|
|
$taglist = explode(',', $page['tags']);
|
|
foreach ($taglist as $tag) {
|
|
$label = trim($tag);
|
|
array_push($tags, [
|
|
'label' => $label . ' ',
|
|
'slug' => safeString($label),
|
|
]);
|
|
}
|
|
}
|
|
$meta = [
|
|
'who' => $page['author'],
|
|
'when' => $page['created'],
|
|
'tags' => $tags,
|
|
];
|
|
// if page feature isn't empty, find image from list and set it as background image
|
|
// if it is empty, just use global background
|
|
if ($page['feature'] != '' || $page['feature'] != null) {
|
|
$media = explode(',', $page['feature']);
|
|
$set = false;
|
|
foreach ($media as $file) {
|
|
$item = trim($file);
|
|
$ext = pathinfo($item, PATHINFO_EXTENSION);
|
|
|
|
if ($ext != 'mp4' && !$set) {
|
|
$this->info['image'] = $this->info['baseURL'] . $item;
|
|
$set = true;
|
|
}
|
|
}
|
|
}
|
|
$recent = [];
|
|
$featured = [];
|
|
$limit = 4;
|
|
foreach ($pages as $item) {
|
|
if (
|
|
!$item['deleted'] &&
|
|
$item['published'] &&
|
|
$item['menu'] != 'true'
|
|
) {
|
|
if (count($recent) < $limit) {
|
|
array_push($recent, [
|
|
'path' => $item['path'],
|
|
'slug' => $item['slug'],
|
|
'title' => urldecode($item['title']),
|
|
'feature' => $item['feature'],
|
|
]);
|
|
}
|
|
|
|
if ($item['featured'] == true) {
|
|
if (count($featured) < $limit) {
|
|
array_push($featured, [
|
|
'path' => $item['path'],
|
|
'slug' => $item['slug'],
|
|
'title' => urldecode($item['title']),
|
|
'feature' => $item['feature'],
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$pageData = [
|
|
"debug" => $debug,
|
|
"theme" => $this->info['theme'],
|
|
"status" => session('member') != null ? true : false,
|
|
"title" => urldecode($page['title']),
|
|
"meta" => $meta,
|
|
"menu" => $this->settings->getMenu(),
|
|
"info" => $this->info,
|
|
"media" => $page['media'],
|
|
"files" => $page['docs'],
|
|
"content" => $page['html'],
|
|
"layout" => $page['layout'],
|
|
"recent" => $recent,
|
|
"feature" => $featured,
|
|
"tags" => $meta['tags'],
|
|
"dynamicRender" => $this->info['dynamicRender'],
|
|
];
|
|
return $pageData;
|
|
}
|
|
|
|
public function navigation()
|
|
{
|
|
$pageOptions = [
|
|
'title' => 'Edit Navigation',
|
|
'status' => session('member') != '' ? true : false,
|
|
'menu' => $this->settings->getMenu(),
|
|
];
|
|
|
|
return $pageOptions;
|
|
}
|
|
|
|
public function settings()
|
|
{
|
|
$global = $this->settings->getGlobal();
|
|
$updatedContent = new Carbon($global['last_content_backup']);
|
|
$updatedFiles = new Carbon($global['last_files_backup']);
|
|
$status = session('member') != '' ? true : false;
|
|
$pageOptions = [
|
|
'title' => 'Settings',
|
|
'private' => $global['private'],
|
|
'renderOnSave' => $global['renderOnSave'],
|
|
'background' => $global['background'],
|
|
'member' => session('member'),
|
|
'ftoken' => session('form_token'),
|
|
'siteTitle' => $global['title'],
|
|
'baseUrl' => $global['base_url'],
|
|
'desc' => $global['descriptions'],
|
|
'lastContentBackup' => $updatedContent->format('Y F d H i s'),
|
|
'lastFilesBackup' => $updatedFiles->format('Y F d H i s'),
|
|
'currentTheme' => $global['theme'],
|
|
'themes' => $this->themes->getThemes(),
|
|
'apiStatus' => isset($global['externalAPI']) ? $global['externalAPI'] : 'false',
|
|
'dynamicRenderStatus' => isset($global['dynamicRender']) ? $global['dynamicRender'] : 'false',
|
|
'status' => $status,
|
|
];
|
|
return $pageOptions;
|
|
}
|
|
}
|