1
0
mirror of https://github.com/typemill/typemill.git synced 2025-08-04 13:17:29 +02:00

Version 1.4.9-1.5.0: Refactor controller logic

This commit is contained in:
trendschau
2021-10-06 21:39:16 +02:00
parent f279afe888
commit 5c4a7c9f6a
31 changed files with 1037 additions and 1351 deletions

View File

@@ -69,7 +69,7 @@ class Folder
if($fileType == 'md')
{
$folderContent[] = $item;
$folderContent[] = $item;
}
if($draft === true && $fileType == 'txt')
@@ -97,7 +97,7 @@ class Folder
* returns: array of objects. Each object contains information about an item (file or folder).
*/
public static function getFolderContentDetails(array $folderContent, $extended, $baseUrl, $fullSlugWithFolder = NULL, $fullSlugWithoutFolder = NULL, $fullPath = NULL, $keyPath = NULL, $chapter = NULL)
public static function getFolderContentDetails(array $folderContent, $extended, $settings, $baseUrl, $fullSlugWithFolder = NULL, $fullSlugWithoutFolder = NULL, $fullPath = NULL, $keyPath = NULL, $chapter = NULL)
{
$contentDetails = [];
$iteration = 0;
@@ -130,14 +130,14 @@ class Folder
$item->originalName = $key;
$item->elementType = 'folder';
$item->contains = self::getFolderContentType($name, $fullPath . DIRECTORY_SEPARATOR . $key . DIRECTORY_SEPARATOR . 'index.yaml');
$item->contains = self::getFolderContentType($name, $fullPath . DIRECTORY_SEPARATOR . $key . DIRECTORY_SEPARATOR . 'index.yaml');
$item->status = $status;
$item->fileType = $fileType;
$item->order = count($nameParts) > 1 ? array_shift($nameParts) : NULL;
$item->name = implode(" ",$nameParts);
$item->name = iconv(mb_detect_encoding($item->name, mb_detect_order(), true), "UTF-8", $item->name);
$item->slug = implode("-",$nameParts);
$item->slug = URLify::filter(iconv(mb_detect_encoding($item->slug, mb_detect_order(), true), "UTF-8", $item->slug));
$item->slug = self::createSlug($item->slug, $settings);
$item->path = $fullPath . DIRECTORY_SEPARATOR . $key;
$item->pathWithoutType = $fullPath . DIRECTORY_SEPARATOR . $key . DIRECTORY_SEPARATOR . 'index';
$item->urlRelWoF = $fullSlugWithoutFolder . '/' . $item->slug;
@@ -164,7 +164,7 @@ class Folder
rsort($name);
}
$item->folderContent = self::getFolderContentDetails($name, $extended, $baseUrl, $item->urlRel, $item->urlRelWoF, $item->path, $item->keyPath, $item->chapter);
$item->folderContent = self::getFolderContentDetails($name, $extended, $settings, $baseUrl, $item->urlRel, $item->urlRelWoF, $item->path, $item->keyPath, $item->chapter);
}
elseif($name)
{
@@ -200,7 +200,7 @@ class Folder
$item->name = implode(" ",$nameParts);
$item->name = iconv(mb_detect_encoding($item->name, mb_detect_order(), true), "UTF-8", $item->name);
$item->slug = implode("-",$nameParts);
$item->slug = URLify::filter(iconv(mb_detect_encoding($item->slug, mb_detect_order(), true), "UTF-8", $item->slug));
$item->slug = self::createSlug($item->slug, $settings);
$item->path = $fullPath . DIRECTORY_SEPARATOR . $name;
$item->pathWithoutType = $fullPath . DIRECTORY_SEPARATOR . $nameWithoutType;
$item->key = $iteration;
@@ -637,4 +637,31 @@ class Folder
$parts = preg_split('/\./',$fileName);
return $parts[0];
}
public static function createSlug($name, $settings = NULL)
{
$name = iconv(mb_detect_encoding($name, mb_detect_order(), true), "UTF-8", $name);
# prior version 1.5.0 settings was no language and remove stop words from slug
$language = "";
$use_remove_list = true;
# if user has not activated the old slug logig < version 1.5.0 style
if($settings && ( !isset($settings['oldslug']) OR !$settings['oldslug'] ) )
{
# then use the language attr and do not remove stop words as default behavior
$language = isset($settings['langattr']) ? $settings['langattr'] : "";
$use_remove_list = false;
}
return URLify::filter(
$name,
$length = 60,
$language,
$file_name = false,
$use_remove_list,
$lower_case = true,
$treat_underscore_as_space = true
);
}
}

View File

@@ -28,6 +28,7 @@ class Helpers{
return $ip;
}
public static function addLogEntry($action)
{
$line = self::getUserIP();

View File

@@ -1,7 +1,7 @@
<?php
namespace Typemill\Models;
use \URLify;
use Typemill\Models\Folder;
class ProcessAssets
{
@@ -107,7 +107,7 @@ class ProcessAssets
$pathinfo = pathinfo($originalname);
$this->extension = strtolower($pathinfo['extension']);
$this->filename = URLify::filter(iconv(mb_detect_encoding($pathinfo['filename'], mb_detect_order(), true), "UTF-8", $pathinfo['filename']));
$this->filename = Folder::createSlug($pathinfo['filename']);
$filename = $this->filename;

View File

@@ -3,7 +3,6 @@ namespace Typemill\Models;
use Slim\Http\UploadedFile;
use Typemill\Models\Helpers;
use \URLify;
class ProcessFile extends ProcessAssets
{

View File

@@ -1,29 +0,0 @@
<?php
namespace Typemill\Models;
# this check is not in use anymore (was in use to check and store latest version in user settings on page refresh)
class VersionCheck
{
function checkVersion($url)
{
$opts = array(
'http'=>array(
'method' => "GET",
'header' => "Referer: $url\r\n"
)
);
$context = stream_context_create($opts);
if(false === ($version = @file_get_contents('https://typemill.net/api/v1/checkversion', false, $context)))
{
return false;
}
$version = json_decode($version);
die();
return $version->system->typemill;
}
}

View File

@@ -127,8 +127,39 @@ class Write
}
return false;
}
}
public function renamePost($oldPathWithoutType, $newPathWithoutType)
{
$filetypes = array('md', 'txt', 'yaml');
$oldPath = $this->basePath . 'content' . $oldPathWithoutType;
$newPath = $this->basePath . 'content' . $newPathWithoutType;
$result = true;
foreach($filetypes as $filetype)
{
$oldFilePath = $oldPath . '.' . $filetype;
$newFilePath = $newPath . '.' . $filetype;
#check if file with filetype exists and rename
if($oldFilePath != $newFilePath && file_exists($oldFilePath))
{
if(@rename($oldFilePath, $newFilePath))
{
$result = $result;
}
else
{
$result = false;
}
}
}
return $result;
}
public function moveElement($item, $folderPath, $index, $date = null)
{
$filetypes = array('md', 'txt', 'yaml');
@@ -178,35 +209,4 @@ class Write
return $result;
}
public function renamePost($oldPathWithoutType, $newPathWithoutType)
{
$filetypes = array('md', 'txt', 'yaml');
$oldPath = $this->basePath . 'content' . $oldPathWithoutType;
$newPath = $this->basePath . 'content' . $newPathWithoutType;
$result = true;
foreach($filetypes as $filetype)
{
$oldFilePath = $oldPath . '.' . $filetype;
$newFilePath = $newPath . '.' . $filetype;
#check if file with filetype exists and rename
if($oldFilePath != $newFilePath && file_exists($oldFilePath))
{
if(@rename($oldFilePath, $newFilePath))
{
$result = $result;
}
else
{
$result = false;
}
}
}
return $result;
}
}

View File

@@ -110,124 +110,4 @@ class WriteCache extends Write
return $error;
}
public function getFreshStructure($contentPath, $uri)
{
# scan the content of the folder
$pagetree = Folder::scanFolder('content');
# if there is no content, render an empty page
if(count($pagetree) == 0)
{
return false;
}
# get the extended structure files with changes like navigation title or hidden pages
$yaml = new writeYaml();
$extended = $yaml->getYaml('cache', 'structure-extended.yaml');
# create an array of object with the whole content of the folder
$structure = Folder::getFolderContentDetails($pagetree, $extended, $uri->getBaseUrl(), $uri->getBasePath());
# now update the extended structure
if(!$extended)
{
$extended = $this->createExtended($contentPath, $yaml, $structure);
if(!empty($extended))
{
$yaml->updateYaml('cache', 'structure-extended.yaml', $extended);
# we have to update the structure with extended again
$structure = Folder::getFolderContentDetails($pagetree, $extended, $uri->getBaseUrl(), $uri->getBasePath());
}
else
{
$extended = false;
}
}
# cache structure
$this->updateCache('cache', 'structure.txt', 'lastCache.txt', $structure);
if($extended && $this->containsHiddenPages($extended))
{
# generate the navigation (delete empty pages)
$navigation = $this->createNavigationFromStructure($structure);
# cache navigation
$this->updateCache('cache', 'navigation.txt', false, $navigation);
}
else
{
# make sure no separate navigation file is set
$this->deleteFileWithPath('cache' . DIRECTORY_SEPARATOR . 'navigation.txt');
}
# load and return the cached structure, because might be manipulated with navigation....
$structure = $this->getCachedStructure();
return $structure;
}
# creates a file that holds all hide flags and navigation titles
# reads all meta-files and creates an array with url => ['hide' => bool, 'navtitle' => 'bla']
public function createExtended($contentPath, $yaml, $structure, $extended = NULL)
{
if(!$extended)
{
$extended = [];
}
foreach ($structure as $key => $item)
{
# $filename = ($item->elementType == 'folder') ? DIRECTORY_SEPARATOR . 'index.yaml' : $item->pathWithoutType . '.yaml';
$filename = $item->pathWithoutType . '.yaml';
if(file_exists($contentPath . $filename))
{
# read file
$meta = $yaml->getYaml('content', $filename);
$extended[$item->urlRelWoF]['hide'] = isset($meta['meta']['hide']) ? $meta['meta']['hide'] : false;
$extended[$item->urlRelWoF]['navtitle'] = isset($meta['meta']['navtitle']) ? $meta['meta']['navtitle'] : '';
}
if ($item->elementType == 'folder')
{
$extended = $this->createExtended($contentPath, $yaml, $item->folderContent, $extended);
}
}
return $extended;
}
public function createNavigationFromStructure($navigation)
{
foreach ($navigation as $key => $element)
{
if($element->hide === true)
{
unset($navigation[$key]);
}
elseif(isset($element->folderContent))
{
$navigation[$key]->folderContent = $this->createNavigationFromStructure($element->folderContent);
}
}
return $navigation;
}
# checks if there is a hidden page, returns true on first find
protected function containsHiddenPages($extended)
{
foreach($extended as $element)
{
if(isset($element['hide']) && $element['hide'] === true)
{
return true;
}
}
return false;
}
}

View File

@@ -1,45 +0,0 @@
<?php
namespace Typemill\Models;
class WriteSitemap extends Write
{
public function updateSitemap($folderName, $sitemapFileName, $requestFileName, $data, $baseUrl)
{
$sitemap = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
$sitemap = $this->addUrlSet($sitemap, $baseUrl);
$sitemap .= $this->generateUrlSets($data);
$sitemap .= '</urlset>';
$this->writeFile($folderName, $sitemapFileName, $sitemap);
$this->writeFile($folderName, $requestFileName, time());
}
public function generateUrlSets($data)
{
$urlset = '';
foreach($data as $item)
{
if($item->elementType == 'folder')
{
$urlset = $this->addUrlSet($urlset, $item->urlAbs);
$urlset .= $this->generateUrlSets($item->folderContent, $urlset);
}
else
{
$urlset = $this->addUrlSet($urlset, $item->urlAbs);
}
}
return $urlset;
}
public function addUrlSet($urlset, $url)
{
$urlset .= ' <url>' . "\n";
$urlset .= ' <loc>' . $url . '</loc>' . "\n";
$urlset .= ' </url>' . "\n";
return $urlset;
}
}