Add a unique identifier for blog posts and pages

The unique identifier is permanently attached to the post or page and
doesn't change during its lifetime. It can be used for many purposes that
require identification, such as Disqus comment threads (to avoid thread
duplication), redirection of old URLs, etc.

Signed-off-by: ADTC <send2adtc@gmail.com>
This commit is contained in:
ADTC 2017-04-26 13:28:59 +08:00
parent d1c88a6fdb
commit c5dde14783
5 changed files with 42 additions and 0 deletions

View File

@ -136,6 +136,12 @@ class Content {
return $this->getField('readMore');
}
// Returns the unique identifier
public function uniqueId()
{
return $this->getField('uniqueId');
}
// Returns the field key
public function key()
{
@ -291,6 +297,7 @@ class Content {
public function json($returnsArray=false)
{
$tmp['key'] = $this->key();
$tmp['uniqueId'] = $this->uniqueId();
$tmp['title'] = $this->title();
$tmp['content'] = $this->content(); // Markdown parsed
$tmp['contentRaw'] = $this->contentRaw(); // No Markdown parsed

View File

@ -32,6 +32,12 @@ function updateBludit()
$checksum = md5_file(PATH_POSTS.$key.DS.FILENAME);
$dbPosts->setPostDb($key, 'md5file', $checksum);
}
// Unique identifier
if( empty($post['uniqueId']) ) {
$uniqueId = generateUniqueId();
$dbPosts->setPostDb($key, 'uniqueId', $uniqueId);
}
}
$dbPosts->save();
@ -52,6 +58,12 @@ function updateBludit()
$checksum = md5_file(PATH_PAGES.$key.DS.FILENAME);
$dbPages->setPageDb($key, 'md5file', $checksum);
}
// Unique identifier
if( empty($page['uniqueId']) ) {
$uniqueId = generateUniqueId();
$dbPages->setPageDb($key, 'uniqueId', $uniqueId);
}
}
$dbPages->save();

View File

@ -273,3 +273,9 @@ $Url->checkFilters( $Site->uriFilters() );
// --- Objects shortcuts ---
$L = $Language;
// Function for generating unique identifiers
function generateUniqueId()
{
return md5(uniqid('', true));
}

View File

@ -7,6 +7,7 @@ class dbPages extends dbJSON
private $dbFields = array(
'title'=> array('inFile'=>true, 'value'=>''),
'content'=> array('inFile'=>true, 'value'=>''),
'uniqueId'=> array('inFile'=>false, 'value'=>''),
'description'=> array('inFile'=>false, 'value'=>''),
'username'=> array('inFile'=>false, 'value'=>''),
'tags'=> array('inFile'=>false, 'value'=>array()),
@ -28,6 +29,11 @@ class dbPages extends dbJSON
$dataForFile = array(); // This data will be saved in the file
$key = $this->generateKey($args['slug'], $args['parent']);
// Generate unique identifier
if( empty($args['uniqueId']) ) {
$args['uniqueId'] = generateUniqueId();
}
// The user is always the one loggued.
$args['username'] = Session::get('username');
@ -105,6 +111,11 @@ class dbPages extends dbJSON
$dataForFile = array();
$newKey = $this->generateKey($args['slug'], $args['parent'], false, $args['key']);
// Generate unique identifier
if( empty($args['uniqueId']) ) {
$args['uniqueId'] = generateUniqueId();
}
// The user is always the one loggued.
$args['username'] = Session::get('username');

View File

@ -5,6 +5,7 @@ class dbPosts extends dbJSON
private $dbFields = array(
'title'=> array('inFile'=>true, 'value'=>''),
'content'=> array('inFile'=>true, 'value'=>''),
'uniqueId'=> array('inFile'=>false, 'value'=>''),
'description'=> array('inFile'=>false, 'value'=>''),
'username'=> array('inFile'=>false, 'value'=>''),
'status'=> array('inFile'=>false, 'value'=>'draft'), // published, draft, scheduled
@ -113,6 +114,11 @@ class dbPosts extends dbJSON
// Generate the database key / index
$key = $this->generateKey($args['slug']);
// Generate unique identifier
if( empty($args['uniqueId']) ) {
$args['uniqueId'] = generateUniqueId();
}
// The user is always who is loggued
$args['username'] = Session::get('username');