Skip to content

Instantly share code, notes, and snippets.

@PhrozenByte
Created July 14, 2019 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PhrozenByte/e8d73c2aa8013efa679d78e25ceec97e to your computer and use it in GitHub Desktop.
Save PhrozenByte/e8d73c2aa8013efa679d78e25ceec97e to your computer and use it in GitHub Desktop.
A simple Pico plugin adding a page's last modification time to its page data. Pico is a stupidly simple, blazing fast, flat file CMS. http://picocms.org/
<?php
/**
* Pico page modification time plugin
*
* Adds a page's last modification time to its page data.
*
* Example:
*
* ```twig
* <p>Last modified: {{ current_page.modificationTime|date("Y-m-d H:i:s") }}</p>
* ```
*
* @author Daniel Rudolf
* @link http://picocms.org
* @license http://opensource.org/licenses/MIT The MIT License
* @version 0.0.1
*/
class PicoPageModPlugin extends AbstractPicoPlugin
{
const API_VERSION = 2;
public function onSinglePageLoaded(array &$pageData)
{
if ($pageData['id']) {
$fileName = $this->getConfig('content_dir') . $pageData['id'] . $this->getConfig('content_ext');
if (is_file($fileName) && !isset($pageData['modificationTime'])) {
$pageData['modificationTime'] = filemtime($fileName);
}
}
}
}
@gphg
Copy link

gphg commented Jul 12, 2020

Hi. It is possible to configuration modificationTime?

I tried to add modificationTime on front-end. But it only produces content's meta. Eg.

---
Title: Welcome
Description: Pico is a stupidly simple, blazing fast, flat file CMS.
modificationTime: 1594500000
---
## Hello and Welcome to Pico

I did tweaked it a little bit to get how thing I expected works. See forked gist by me later.

Update:
Gist: https://gist.github.com/gphg/8e265e4ebb3bf81378776251f6ad2766/3381874efb0d04d29ed31ad152c245ece51062c6

I need this for HTTP caching. It's would be nice if PicoCMS able to cache based on HTTP. I wish this kind of plugin exists.

I'm working on it. But it's slow.

@PhrozenByte
Copy link
Author

I'd always recommend to write a plugin that meets your needs. However, if you really want to use this instead you could use the meta header first and then fall back to filemtime():

$lastModified = $pageData['meta']['modificationTime'] ?? $pageData['modificationTime'] ?? null;
<p>Last modified: {{ (meta.modificationTime ?? current_page.modificationTime)|date("Y-m-d H:i:s") }}</p>

@gphg
Copy link

gphg commented Jul 13, 2020

@PhrozenByte That's a great idea!

By the way, I read this: https://github.com/picocms/Pico/blob/master/plugins/DummyPlugin.php

I just don't have the best idea where to put the HTTP Caching script. I normally put it on index.php. Something like this:

$ret = $pico->run(); // exec pico and returns output

// Begin HTTP caching evaluation
$pageData = $pico->getCurrentPage();
$lastModified = $pageData['meta']['modificationTime'] ?? $pageData['modificationTime'] ?? null;

if (!is_null(lastModified) && isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']))
{
	header('HTTP/1.1 304 Not Modified');
	exit;
}

header('Pragma: public');
if (isset($pageData['meta']['CacheControl'])) {
   $maxAge = $pageData['meta']['CacheControl'] - $_SERVER['REQUEST_TIME'];
   header('Cache-Control: max-age='.$maxAge.', public');
   header('Expires: '.gmdate('D, d M Y H:i:s', $expiration).' GMT');
}
if ($last_modified) {
   header('Last-modified: '.gmdate('D, d M Y H:i:s', $last_modified).' GMT');
}

echo $ret; // print output

I wish I could done it better.

EDIT:
Was it onMetaParsed or onContentParsing? I assumed it's safe to execute exit; under a Pico plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment