mirror of
https://github.com/typemill/typemill.git
synced 2025-08-06 06:07:31 +02:00
Version 1.4.9: Rewrite slug and recreate cache
This commit is contained in:
@@ -8,6 +8,7 @@ use Typemill\Models\Folder;
|
||||
use Typemill\Models\Write;
|
||||
use Typemill\Models\WriteYaml;
|
||||
use Typemill\Models\WriteMeta;
|
||||
use Typemill\Models\WriteCache;
|
||||
use Typemill\Extensions\ParsedownExtension;
|
||||
use Typemill\Events\OnPagePublished;
|
||||
use Typemill\Events\OnPageUnpublished;
|
||||
@@ -407,6 +408,78 @@ class ArticleApiController extends ContentController
|
||||
return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
|
||||
}
|
||||
}
|
||||
|
||||
public function renameArticle(Request $request, Response $response, $args)
|
||||
{
|
||||
# get params from call
|
||||
$this->params = $request->getParams();
|
||||
$this->uri = $request->getUri()->withUserInfo('');
|
||||
$dir = $this->settings['basePath'] . 'cache';
|
||||
$pathToContent = $this->settings['rootPath'] . $this->settings['contentFolder'];
|
||||
|
||||
# minimum permission is that user is allowed to update his own content
|
||||
if(!$this->c->acl->isAllowed($_SESSION['role'], 'mycontent', 'update'))
|
||||
{
|
||||
return $response->withJson(array('data' => false, 'errors' => 'You are not allowed to update content.'), 403);
|
||||
}
|
||||
|
||||
# validate input
|
||||
if(!preg_match("/^[a-z0-9\-]*$/", $this->params['slug']))
|
||||
{
|
||||
return $response->withJson(['errors' => ['message' => 'the slug contains invalid characters.' ]],422);
|
||||
}
|
||||
|
||||
# set structure
|
||||
if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
|
||||
|
||||
# set information for homepage
|
||||
$this->setHomepage($args = false);
|
||||
|
||||
# set item
|
||||
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
|
||||
|
||||
# validate input part 2
|
||||
if($this->params['slug'] == $this->item->slug OR $this->params['slug'] == '')
|
||||
{
|
||||
return $response->withJson(['errors' => ['message' => 'the slug is empty or the same as the old one.' ]],422);
|
||||
}
|
||||
|
||||
# if user has no right to update content from others (eg admin or editor)
|
||||
if(!$this->c->acl->isAllowed($_SESSION['role'], 'content', 'update'))
|
||||
{
|
||||
# check ownership. This code should nearly never run, because there is no button/interface to trigger it.
|
||||
if(!$this->checkContentOwnership())
|
||||
{
|
||||
return $response->withJson(array('data' => $this->structure, 'errors' => 'You are not allowed to move that content.'), 403);
|
||||
}
|
||||
}
|
||||
|
||||
# get the folder where file lives in
|
||||
$pathWithoutFile = str_replace($this->item->originalName, '', $this->item->path);
|
||||
|
||||
# create the new file name with the updated slug
|
||||
$newPathWithoutType = $pathWithoutFile . $this->item->order . '-' . $this->params['slug'];
|
||||
|
||||
# rename the file
|
||||
$write = new WriteCache();
|
||||
$write->renamePost($this->item->pathWithoutType, $newPathWithoutType);
|
||||
|
||||
# delete the cache
|
||||
$error = $write->deleteCacheFiles($dir);
|
||||
if($error)
|
||||
{
|
||||
return $response->withJson(['errors' => $error], 500);
|
||||
}
|
||||
|
||||
# recreates the cache for structure, structure-extended and navigation
|
||||
$write->getFreshStructure($pathToContent, $this->uri);
|
||||
|
||||
$newUrlRel = str_replace($this->item->slug, $this->params['slug'], $this->item->urlRelWoF);
|
||||
|
||||
$url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'] . $newUrlRel;
|
||||
|
||||
return $response->withJson(array('data' => false, 'errors' => false, 'url' => $url));
|
||||
}
|
||||
|
||||
public function sortArticle(Request $request, Response $response, $args)
|
||||
{
|
||||
|
@@ -46,7 +46,7 @@ class PageController extends Controller
|
||||
# if the cached structure is still valid, use it
|
||||
if($cache->validate('cache', 'lastCache.txt', 600))
|
||||
{
|
||||
$structure = $this->getCachedStructure($cache);
|
||||
$structure = $cache->getCachedStructure();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -57,7 +57,7 @@ class PageController extends Controller
|
||||
if(!isset($structure) OR !$structure)
|
||||
{
|
||||
# if not, get a fresh structure of the content folder
|
||||
$structure = $this->getFreshStructure($pathToContent, $cache, $uri);
|
||||
$structure = $cache->getFreshStructure($pathToContent, $uri);
|
||||
|
||||
# if there is no structure at all, the content folder is probably empty
|
||||
if(!$structure)
|
||||
@@ -356,145 +356,7 @@ class PageController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
protected function getCachedStructure($cache)
|
||||
{
|
||||
return $cache->getCache('cache', 'structure.txt');
|
||||
}
|
||||
|
||||
protected function getFreshStructure($pathToContent, $cache, $uri)
|
||||
{
|
||||
/* scan the content of the folder */
|
||||
$pagetree = Folder::scanFolder($pathToContent);
|
||||
|
||||
/* 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($this->pathToContent, $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
|
||||
$cache->updateCache('cache', 'structure.txt', 'lastCache.txt', $structure);
|
||||
|
||||
if($extended && $this->containsHiddenPages($extended))
|
||||
{
|
||||
# generate the navigation (delete empty pages)
|
||||
$navigation = $this->createNavigationFromStructure($structure);
|
||||
|
||||
# cache navigation
|
||||
$cache->updateCache('cache', 'navigation.txt', false, $navigation);
|
||||
}
|
||||
else
|
||||
{
|
||||
# make sure no separate navigation file is set
|
||||
$cache->deleteFileWithPath('cache' . DIRECTORY_SEPARATOR . 'navigation.txt');
|
||||
}
|
||||
|
||||
# load and return the cached structure, because might be manipulated with navigation....
|
||||
$structure = $this->getCachedStructure($cache);
|
||||
|
||||
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']
|
||||
protected 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;
|
||||
}
|
||||
|
||||
# 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;
|
||||
}
|
||||
|
||||
protected 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;
|
||||
}
|
||||
|
||||
# not in use, stored the latest version in user settings, but that does not make sense because checkd on the fly with api in admin
|
||||
protected function updateVersion($baseUrl)
|
||||
{
|
||||
/* check the latest public typemill version */
|
||||
$version = new VersionCheck();
|
||||
$latestVersion = $version->checkVersion($baseUrl);
|
||||
|
||||
if($latestVersion)
|
||||
{
|
||||
/* store latest version */
|
||||
\Typemill\Settings::updateSettings(array('latestVersion' => $latestVersion));
|
||||
}
|
||||
}
|
||||
|
||||
protected function getFirstImage(array $contentBlocks)
|
||||
{
|
||||
foreach($contentBlocks as $block)
|
||||
|
@@ -4,6 +4,7 @@ namespace Typemill\Controllers;
|
||||
|
||||
use \Symfony\Component\Yaml\Yaml;
|
||||
use Typemill\Models\Write;
|
||||
use Typemill\Models\WriteCache;
|
||||
use Typemill\Models\Fields;
|
||||
use Typemill\Models\Validation;
|
||||
use Typemill\Models\User;
|
||||
@@ -971,38 +972,24 @@ class SettingsController extends Controller
|
||||
|
||||
public function clearCache($request, $response, $args)
|
||||
{
|
||||
$settings = $this->c->get('settings');
|
||||
$dir = $settings['basePath'] . 'cache';
|
||||
$iterator = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
|
||||
$files = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
|
||||
$settings = $this->c->get('settings');
|
||||
$dir = $settings['basePath'] . 'cache';
|
||||
$uri = $request->getUri()->withUserInfo('');
|
||||
$pathToContent = $settings['rootPath'] . $settings['contentFolder'];
|
||||
|
||||
$error = false;
|
||||
$writeCache = new writeCache();
|
||||
|
||||
foreach($files as $file)
|
||||
{
|
||||
if ($file->isDir())
|
||||
{
|
||||
if(!rmdir($file->getRealPath()))
|
||||
{
|
||||
$error = 'Could not delete some folders.';
|
||||
}
|
||||
}
|
||||
elseif($file->getExtension() !== 'css')
|
||||
{
|
||||
if(!unlink($file->getRealPath()) )
|
||||
{
|
||||
$error = 'Could not delete some files.';
|
||||
}
|
||||
}
|
||||
}
|
||||
$error = $writeCache->deleteCacheFiles($dir);
|
||||
|
||||
if($error)
|
||||
{
|
||||
return $response->withJson(['errors' => $error], 500);
|
||||
}
|
||||
|
||||
return $response->withJson(array('errors' => false));
|
||||
# this recreates the cache for structure, structure-extended and navigation
|
||||
$writeCache->getFreshStructure($pathToContent, $uri);
|
||||
|
||||
return $response->withJson(array('errors' => false));
|
||||
}
|
||||
|
||||
private function getUserFields($role)
|
||||
|
Reference in New Issue
Block a user