Skip to content

Commit

Permalink
delete linkto cache replace recursive render
Browse files Browse the repository at this point in the history
- small admin interface improvements
  • Loading branch information
vincent-peugnet committed Jan 7, 2024
1 parent 8e252bd commit a3f420e
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 125 deletions.
10 changes: 5 additions & 5 deletions MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ Where `*` is the page ID of the rendered page.
%<element>?tag=(0|1)%

This will determine if HTML tags will be printed around included element. This may be usefull for advanced users to achieve more precise HTML editing.
The default behavior can be set globaly in the [admin panel](#admin).
The default behavior can be set globaly in the [admin panel](#administration).

Example :

Expand Down Expand Up @@ -698,7 +698,7 @@ This settings is deactivated by default.

%<element>?urllinker=(0|1)%

URL Linker is a tool that will transform a plain text URL into a link. This can be enabled or disabled specificly for each elements. The default behavior can be set globaly in the [admin panel](#admin).
URL Linker is a tool that will transform a plain text URL into a link. This can be enabled or disabled specificly for each elements. The default behavior can be set globaly in the [admin panel](#administration).

##### Everylink

Expand Down Expand Up @@ -860,7 +860,7 @@ CSS properties for weight only use `normal` and `bold` as absolute values. other

This contain two files:

- the `global.css` file, that admins can edit in the [admin panel](#admin).
- the `global.css` file, that admins can edit in the [admin panel](#administration).
- the `fonts.css` file, that is automatcly generated according to content of [fonts folder](#fonts-folder).


Expand Down Expand Up @@ -904,7 +904,7 @@ Acces to admin menu is reserved to [admin](#administrator) users.

### Theming

You change the interface theme in the [admin panel](#admin), under *interface*. There is a kit of presets, but you can even add your own themes ! And it is actualy *super easy* 😎. To do so, go to the folder `/assets/css/themes/` and duplicate one of the preset you prefer. Then you can edit the CSS variables to quickly stylise your theme colors.
You change the interface theme in the [admin panel](#administration), under *interface*. There is a kit of presets, but you can even add your own themes ! And it is actualy *super easy* 😎. To do so, go to the folder `/assets/css/themes/` and duplicate one of the preset you prefer. Then you can edit the CSS variables to quickly stylise your theme colors.



Expand Down Expand Up @@ -1111,7 +1111,7 @@ Invite Editors are the lowest editor status possible. They can only access the [

> level : 10
- Can access the [admin interface](#admin).
- Can access the [admin interface](#administration).
- Can create new users.

### URL based command interface
Expand Down
10 changes: 5 additions & 5 deletions app/class/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class Config
protected static $externallinkblank = true;
protected static $internallinkblank = false;
protected static $urllinker = true;
protected static $recursiverender = true;
protected static bool $deletelinktocache = true;
protected static $defaultprivacy = 0;
protected static $homepage = 'default';
protected static $homeredirect = null;
Expand Down Expand Up @@ -280,9 +280,9 @@ public static function urllinker(): bool
return self::$urllinker;
}

public static function recursiverender()
public static function deletelinktocache(): bool
{
return self::$recursiverender;
return self::$deletelinktocache;
}

public static function defaultprivacy()
Expand Down Expand Up @@ -501,9 +501,9 @@ public static function seturllinker($urllinker)
self::$urllinker = boolval($urllinker);
}

public static function setrecursiverender($recursiverender)
public static function setdeletelinktocache($deletelinktocache)
{
self::$recursiverender = boolval($recursiverender);
self::$deletelinktocache = boolval($deletelinktocache);
}

public static function setdefaultprivacy($defaultprivacy)
Expand Down
3 changes: 2 additions & 1 deletion app/class/Controllerhome.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public function flushrendercache(): void
Model::sendflashmessage('Render cache successfully deleted', Model::FLASH_SUCCESS);
} catch (RuntimeException $e) {
Model::sendflashmessage($e->getMessage(), Model::FLASH_ERROR);
Logger::errorex($e);
}
$this->routedirect('home');
}
Expand Down Expand Up @@ -318,7 +319,7 @@ public function multidelete()
$total = count($pagelist);
$count = 0;
foreach ($pagelist as $id) {
if ($this->pagemanager->delete($id)) {
if ($this->pagemanager->delete(new Pagev2(['id' => $id]))) {
$count++;
}
}
Expand Down
157 changes: 77 additions & 80 deletions app/class/Controllerpage.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ public function render($page)
$this->setpage($page, 'pageupdate');

if ($this->importpage() && $this->user->iseditor()) {
if (Config::recursiverender()) {
$this->recursiverender($this->page);
}
$this->page = $this->pagemanager->renderpage($this->page, $this->router);
$this->pagemanager->update($this->page);
$this->templaterender($this->page);
Expand All @@ -85,26 +82,27 @@ public function render($page)
}

/**
* Render all other pages that are linked from this page
* Delete render cache of all related pages
*
* @param string[] $relatedpages List of page ids
*/
public function recursiverender(Page $page): void
public function deletelinktocache(array $relatedpages): void
{
$relatedpages = array_diff($page->linkto(), [$page->id()]);
foreach ($relatedpages as $pageid) {
try {
$page = $this->pagemanager->get($pageid);
$page = $this->pagemanager->renderpage($page, $this->router);
$this->pagemanager->update($page);
$this->pagemanager->unlink($pageid);
} catch (RuntimeException $e) {
Logger::errorex($e, true);
}
}
}

/**
* Render all page templates if they need to
* Render page's JS and CSS templates if they need to
*
* @param Page $page page to check templates
*
* @todo Move this function in Modelpage
*/
private function templaterender(Page $page)
{
Expand Down Expand Up @@ -138,89 +136,88 @@ private function templaterender(Page $page)
public function read($page)
{
$this->setpage($page, 'pageread');

$pageexist = $this->importpage();
$canread = false;
$needtoberendered = false;
$filedir = Model::HTML_RENDER_DIR . $page . '.html';
$reccursiverender = false;

if ($pageexist) {
$canread = $this->user->level() >= $this->page->secure();
if (!$this->importpage()) {
http_response_code(404);
$this->showtemplate(
'alertexistnot',
['page' => $this->page, 'subtitle' => Config::existnot()]
);
exit;
}

// Check page password
if (!empty($this->page->password())) {
if (empty($_POST['pagepassword']) || $_POST['pagepassword'] !== $this->page->password()) {
$this->showtemplate('pagepassword', ['pageid' => $this->page->id()]);
exit;
}
// Check page password
if (!empty($this->page->password())) {
if (empty($_POST['pagepassword']) || $_POST['pagepassword'] !== $this->page->password()) {
$this->showtemplate('pagepassword', ['pageid' => $this->page->id()]);
exit;
}
}

if ($canread) {
if ($this->pagemanager->needtoberendered($this->page)) {
$this->page = $this->pagemanager->renderpage($this->page, $this->router);
$needtoberendered = true;
}
$this->templaterender($this->page);


$this->page->adddisplaycount();
if ($this->user->isvisitor()) {
$this->page->addvisitcount();
}

// redirection using Location and 302
if (!empty($this->page->redirection()) && $this->page->refresh() === 0 && $this->page->sleep() === 0) {
try {
if (Model::idcheck($this->page->redirection())) {
$this->routedirect('pageread', ['page' => $this->page->redirection()]);
} else {
$url = getfirsturl($this->page->redirection());
$this->redirect($url);
}
} catch (RuntimeException $e) {
// TODO : send synthax error to editor
}
}
$html = file_get_contents($filedir);
if ($this->user->level() < $this->page->secure()) {
http_response_code(403);
switch ($this->page->secure()) {
case Page::NOT_PUBLISHED:
$this->showtemplate(
'alertnotpublished',
['page' => $this->page, 'subtitle' => Config::notpublished()]
);
break;

case Page::PRIVATE:
$this->showtemplate(
'alertprivate',
['page' => $this->page, 'subtitle' => Config::private()]
);
break;
}
exit;
}

$postprocessor = new Servicepostprocess($this->page, $this->user);
$html = $postprocessor->process($html);
if ($this->pagemanager->needtoberendered($this->page)) {
if (Config::deletelinktocache() && $this->page->daterender() <= $this->page->datemodif()) {
$oldlinkto = $this->page->linkto();
}
$this->page = $this->pagemanager->renderpage($this->page, $this->router);
if (isset($oldlinkto)) {
$relatedpages = array_unique(array_merge($oldlinkto, $this->page->linkto()));
$this->deletelinktocache($relatedpages);
}
}

sleep($this->page->sleep());
$this->templaterender($this->page);

echo $html;

$this->pagemanager->update($this->page);
$this->page->adddisplaycount();
if ($this->user->isvisitor()) {
$this->page->addvisitcount();
}

if ($needtoberendered && Config::recursiverender()) {
$this->recursiverender($this->page);
}
} else {
http_response_code(403);
switch ($this->page->secure()) {
case Page::NOT_PUBLISHED:
$this->showtemplate(
'alertnotpublished',
['page' => $this->page, 'subtitle' => Config::notpublished()]
);
break;

case Page::PRIVATE:
$this->showtemplate(
'alertprivate',
['page' => $this->page, 'subtitle' => Config::private()]
);
break;
// redirection using Location and 302
if (!empty($this->page->redirection()) && $this->page->refresh() === 0 && $this->page->sleep() === 0) {
try {
if (Model::idcheck($this->page->redirection())) {
$this->routedirect('pageread', ['page' => $this->page->redirection()]);
} else {
$url = getfirsturl($this->page->redirection());
$this->redirect($url);
}
exit;
} catch (RuntimeException $e) {
// TODO : send synthax error to editor
}
} else {
http_response_code(404);
$this->showtemplate(
'alertexistnot',
['page' => $this->page, 'subtitle' => Config::existnot()]
);
}
$html = file_get_contents($filedir);

$postprocessor = new Servicepostprocess($this->page, $this->user);
$html = $postprocessor->process($html);

sleep($this->page->sleep());

echo $html;

$this->pagemanager->update($this->page);
}

public function edit($page)
Expand Down
2 changes: 2 additions & 0 deletions app/class/Fs.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ public static function recursecopy($src, $dst, $perm = Model::FOLDER_PERMISSION)
* @throws Notfoundexception If file does not exist
* @throws Fileexception If file cannot be deleted
* @throws Unlinkexception If PHP unlink function fails for another reason
*
* @todo Maybe do not send Notfoundexception
*/
public static function deletefile(string $file): void
{
Expand Down
39 changes: 24 additions & 15 deletions app/class/Modelpage.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function pagelistbyid(array $idlist = []): array
$pagelist = [];
foreach ($pagedatalist as $id => $pagedata) {
try {
$this->pagelist[$id] = $this->parsepage($pagedata);
$pagelist[$id] = $this->parsepage($pagedata);
} catch (RuntimeException $e) {
Logger::error("Could not load Page with ID \"$id\" : $e");
}
Expand Down Expand Up @@ -247,38 +247,43 @@ public function getpagethumbnail(Page $page): string
/**
* Delete a page and it's linked rendered html and css files
*
* @param Page|string $page could be an Page object or a id string
* @param Page $page Page to delete
*
* @return bool true if success otherwise false
*
* @todo use Exception istead of returning boolean
*/
public function delete($page): bool
public function delete(Page $page): bool
{
if ($page instanceof Page) {
$page = $page->id();
}
if (is_string($page)) {
$this->unlink($page);
return $this->repo->delete($page);
} else {
try {
$this->unlink($page->id());
} catch (Filesystemexception $e) {
return false;
}
return $this->repo->delete($page->id());
}

/**
* Delete rendered CSS and HTML files
* Delete rendered CSS, JS and HTML files associated with given Page
*
* @param string $pageid
*
* @throws Filesystemexception If a file deletion failure occurs
*/
public function unlink(string $pageid)
{
$files = ['.css', '.quick.css', '.js'];
foreach ($files as $file) {
if (file_exists(Model::RENDER_DIR . $pageid . $file)) {
unlink(Model::RENDER_DIR . $pageid . $file);
try {
Fs::deletefile(Model::RENDER_DIR . $pageid . $file);
} catch (Notfoundexception $e) {
// do nothing, this means file is already deleted
}
}
if (file_exists(Model::HTML_RENDER_DIR . $pageid . '.html')) {
unlink(Model::HTML_RENDER_DIR . $pageid . '.html');
try {
Fs::deletefile(Model::HTML_RENDER_DIR . $pageid . '.html');
} catch (Notfoundexception $e) {
// do nothing, this means file is already deleted
}
}

Expand Down Expand Up @@ -400,6 +405,10 @@ public function reset(Page $page, array $reset): Page
/**
* Check if a page need to be rendered
*
* This will compare edit and render dates,
* then if render file exists,
* then if the templatebody is set and has been updated.
*
* @param Page $page Page to be checked
*
* @return bool true if the page need to be rendered otherwise false
Expand Down
4 changes: 2 additions & 2 deletions app/class/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ public function interface($type = 'string')
public function linkto($option = 'array')
{
if ($option == 'json') {
$linkto = json_encode($this->linkto);
return json_encode($this->linkto);
} elseif ($option == 'array') {
$linkto = $this->linkto;
return $this->linkto;
} elseif ($option == 'sort') {
return count($this->linkto);
} elseif ($option == 'string') {
Expand Down
Loading

0 comments on commit a3f420e

Please sign in to comment.