1
0
mirror of https://github.com/typemill/typemill.git synced 2025-08-05 21:57:31 +02:00

Fix author in meta for articles and posts

This commit is contained in:
trendschau
2023-11-22 20:37:03 +01:00
parent 3d4bff397c
commit 2f4b77e1bf
4 changed files with 75 additions and 160 deletions

View File

@@ -507,8 +507,11 @@ class ControllerApiAuthorArticle extends Controller
$namePath = $index > 9 ? $index . '-' . $slug : '0' . $index . '-' . $slug;
# create default content
$content = json_encode(['# ' . $params['item_name'], 'Content']);
$content = json_encode(['# ' . $params['item_name'], 'Content']);
# for initial metadata
$meta = new Meta();
if($params['type'] == 'file')
{
if(!$storage->writeFile('contentFolder', $folderPath, $namePath . '.txt', $content))
@@ -519,13 +522,10 @@ class ControllerApiAuthorArticle extends Controller
return $response->withHeader('Content-Type', 'application/json')->withStatus(500);
}
$storage->updateYaml('contentFolder', $folderPath, $namePath . '.yaml',
['meta' => [
'navtitle' => $params['item_name'],
'owner' => $request->getAttribute('c_username'),
'created' => date("Y-m-d"),
'time' => date("H-i-s")
]]);
$metadata = $meta->createInitialMeta($request->getAttribute('c_username'), $params['item_name']);
$storage->updateYaml('contentFolder', $folderPath, $namePath . '.yaml', $metadata);
}
elseif($params['type'] == 'folder')
{
@@ -537,14 +537,12 @@ class ControllerApiAuthorArticle extends Controller
return $response->withHeader('Content-Type', 'application/json')->withStatus(500);
}
$storage->writeFile('contentFolder', $folderPath . DIRECTORY_SEPARATOR . $namePath, 'index.txt', $content);
$storage->updateYaml('contentFolder', $folderPath . DIRECTORY_SEPARATOR . $namePath, 'index.yaml',
['meta' => [
'navtitle' => $params['item_name'],
'owner' => $request->getAttribute('c_username'),
'created' => date("Y-m-d"),
'time' => date("H-i-s")
]]);
$metadata = $meta->createInitialMeta($request->getAttribute('c_username'), $params['item_name']);
$storage->updateYaml('contentFolder', $folderPath . DIRECTORY_SEPARATOR . $namePath, 'index.yaml', $metadata);
# always redirect to a folder
# $url = $urlinfo['baseurl'] . '/tm/content/' . $this->settings['editor'] . $folder->urlRelWoF . '/' . $slug;
@@ -634,6 +632,9 @@ class ControllerApiAuthorArticle extends Controller
# create default content
$content = json_encode(['# ' . $params['item_name'], 'Content']);
# for initial metadata
$meta = new Meta();
if($params['type'] == 'file')
{
@@ -645,13 +646,10 @@ class ControllerApiAuthorArticle extends Controller
return $response->withHeader('Content-Type', 'application/json')->withStatus(500);
}
$storage->updateYaml('contentFolder', $folderPath, $namePath . '.yaml',
['meta' => [
'navtitle' => $params['item_name'],
'owner' => $request->getAttribute('c_username'),
'created' => date("Y-m-d"),
'time' => date("H-i-s")
]]);
$metadata = $meta->createInitialMeta($request->getAttribute('c_username'), $params['item_name']);
$storage->updateYaml('contentFolder', $folderPath, $namePath . '.yaml', $metadata);
}
elseif($params['type'] == 'folder')
{

View File

@@ -4,6 +4,7 @@ namespace Typemill\Models;
use Typemill\Models\StorageWrapper;
use Typemill\Models\Content;
use Typemill\Models\User;
use Typemill\Models\Settings;
class Meta
@@ -70,18 +71,34 @@ class Meta
return $metadefinitions;
}
public function updateMeta($meta, $item)
# used if new articel/post is created
public function createInitialMeta(string $username, string $navtitle)
{
$filename = $item->pathWithoutType . '.yaml';
if($this->storage->updateYaml('contentFolder', '', $filename, $meta))
$author = '';
$user = new User();
if($user->setUser($username))
{
return true;
$author = $user->getFullName();
}
return $this->storage->getError();
$meta = [];
$meta['meta'] = [];
$meta['meta']['owner'] = $username;
$meta['meta']['author'] = $author;
$meta['meta']['created'] = date("Y-m-d");
$meta['meta']['time'] = date("H-i-s");
$meta['meta']['navtitle'] = $navtitle;
return $meta;
}
# used to fill meta data for existing page
public function addMetaDefaults($meta, $item, $authorFromSettings, $currentuser = false)
{
$modified = false;
@@ -97,13 +114,24 @@ class Meta
if(!isset($meta['meta']['owner']))
{
$meta['meta']['owner'] = $currentuser ? $currentuser : false;
$meta['meta']['owner'] = $currentuser;
$modified = true;
}
if(!isset($meta['meta']['author']))
{
$meta['meta']['owner'] = $currentuser ? $currentuser : $authorFromSettings;
$author = $authorFromSettings;
if($currentuser)
{
$user = new User();
if($user->setUser($currentuser))
{
$author = $user->getFullName();
}
}
$meta['meta']['author'] = $author;
$modified = true;
}
@@ -165,6 +193,18 @@ class Meta
return $meta;
}
public function updateMeta($meta, $item)
{
$filename = $item->pathWithoutType . '.yaml';
if($this->storage->updateYaml('contentFolder', '', $filename, $meta))
{
return true;
}
return $this->storage->getError();
}
public function folderContainsFolders($folder)
{
foreach($folder->folderContent as $page)
@@ -219,132 +259,4 @@ class Meta
# return $this->storage->getError();
return false;
}
public function getNavtitle($url)
{
die("meta moddel this method is outdated");
# get the extended structure where the navigation title is stored
$extended = $this->getYaml('cache', 'structure-extended.yaml');
if(isset($extended[$url]['navtitle']))
{
return $extended[$url]['navtitle'];
}
return '';
}
# used by articleApiController and pageController to add title and description if an article is published
public function completePageMeta($content, $settings, $item)
{
die("meta moddel this method is outdated");
$meta = $this->getPageMeta($settings, $item);
if(!$meta)
{
return $this->getPageMetaDefaults($content, $settings, $item);
}
$title = (isset($meta['meta']['title']) AND $meta['meta']['title'] !== '') ? true : false;
$description = (isset($meta['meta']['description']) AND $meta['meta']['description'] !== '') ? true : false;
if($title && $description)
{
return $meta;
}
# initialize parsedown extension
$parsedown = new ParsedownExtension();
# if content is not an array, then transform it
if(!is_array($content))
{
# turn markdown into an array of markdown-blocks
$content = $parsedown->markdownToArrayBlocks($content);
}
# delete markdown from title
if(!$title && isset($content[0]))
{
$meta['meta']['title'] = trim($content[0], "# ");
}
if(!$description && isset($content[1]))
{
$meta['meta']['description'] = $this->generateDescription($content, $parsedown, $item);
}
$this->updateYaml($settings['contentFolder'], $item->pathWithoutType . '.yaml', $meta);
return $meta;
}
private function whitelistMeta($meta, $metascheme)
{
die("meta moddel this method is outdated");
# we have only 2 dimensions, so no recursive needed
foreach($meta as $tab => $values)
{
if(!isset($metascheme[$tab]))
{
unset($meta[$tab]);
}
foreach($values as $key => $value)
{
if(!isset($metascheme[$tab][$key]))
{
unset($meta[$tab][$key]);
}
}
}
return $meta;
}
public function generateDescription($content, $parsedown, $item)
{
die("meta moddel this method is outdated");
$description = isset($content[1]) ? $content[1] : '';
# create description or abstract from content
if($description !== '')
{
$firstLineArray = $parsedown->text($description);
$description = strip_tags($parsedown->markup($firstLineArray, $item->urlAbs));
# if description is very short
if(strlen($description) < 100 && isset($content[2]))
{
$secondLineArray = $parsedown->text($content[2]);
$description .= ' ' . strip_tags($parsedown->markup($secondLineArray, $item->urlAbs));
}
# if description is too long
if(strlen($description) > 300)
{
$description = substr($description, 0, 300);
$lastSpace = strrpos($description, ' ');
$description = substr($description, 0, $lastSpace);
}
}
return $description;
}
}

View File

@@ -69,6 +69,11 @@ class User
return $this->user;
}
public function getFullName()
{
return trim($this->user['firstname'] . ' ' . $this->user['lastname']);
}
public function getError()
{
return $this->error;

View File

@@ -4,12 +4,12 @@ fieldsetsystem:
fields:
title:
type: text
label: Website Title
label: Website title
maxlength: 60
css: lg:w-half
author:
type: text
label: Author
label: Website owner
css: lg:w-half
maxlength: 60
copyright: