Improvements on tags

This commit is contained in:
dignajar 2015-08-30 22:18:06 -03:00
parent 1c1cc786ad
commit 0c70da532b
16 changed files with 250 additions and 300 deletions

View File

@ -16,8 +16,10 @@
// Main after POST
// ============================================================================
$_newPosts = $dbPosts->regenerate();
$_newPages = $dbPages->regenerate();
//$_newPosts = $dbPosts->regenerateCli();
//$_newPages = $dbPages->regenerateCli();
$_newPages = $_newPosts = array();
$_draftPosts = array();
foreach($posts as $Post)

View File

@ -24,9 +24,7 @@ function editPage($args)
// Edit the page.
if( $dbPages->edit($args) )
{
$dbPages->regenerate();
//$dbTags->reindexPages( $dbPages->db );
$dbPages->regenerateCli();
Alert::set($Language->g('The changes have been saved'));
Redirect::page('admin', 'edit-page/'.$args['key']);

View File

@ -1,5 +1,9 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
// ============================================================================
// Check role
// ============================================================================
// ============================================================================
// Functions
// ============================================================================
@ -7,7 +11,6 @@
function editPost($args)
{
global $dbPosts;
global $dbTags;
global $Language;
// Post status, published or draft.
@ -21,10 +24,8 @@ function editPost($args)
// Edit the post.
if( $dbPosts->edit($args) )
{
// Regenerate the database tags
$dbPosts->removeUnpublished();
$dbPosts->sortByDate();
$dbTags->reindexPosts( $dbPosts->db );
// Reindex tags, this function is in 70.posts.php
reIndexTagsPosts();
Alert::set($Language->g('The changes have been saved'));
Redirect::page('admin', 'edit-post/'.$args['key']);
@ -33,18 +34,19 @@ function editPost($args)
{
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to edit the post.');
}
return false;
}
function deletePost($key)
{
global $dbPosts;
global $dbTags;
global $Language;
if( $dbPosts->delete($key) )
{
// Regenerate the database tags
$dbTags->reindexPosts( $dbPosts->db );
// Reindex tags, this function is in 70.posts.php
reIndexTagsPosts();
Alert::set($Language->g('The post has been deleted successfully'));
Redirect::page('admin', 'manage-posts');

View File

@ -24,8 +24,6 @@ function addPage($args)
// Add the page.
if( $dbPages->add($args) )
{
//$dbTags->reindexPages( $dbPages->db );
Alert::set($Language->g('Page added successfully'));
Redirect::page('admin', 'manage-pages');
}

View File

@ -11,7 +11,6 @@
function addPost($args)
{
global $dbPosts;
global $dbTags;
global $Language;
// Page status, published or draft.
@ -25,10 +24,8 @@ function addPost($args)
// Add the page.
if( $dbPosts->add($args) )
{
// Regenerate the database tags
$dbPosts->removeUnpublished();
$dbPosts->sortByDate();
$dbTags->reindexPosts( $dbPosts->db );
// Reindex tags, this function is in 70.posts.php
reIndexTagsPosts();
Alert::set($Language->g('Post added successfully'));
Redirect::page('admin', 'manage-posts');
@ -37,6 +34,8 @@ function addPost($args)
{
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the post.');
}
return false;
}
// ============================================================================

View File

@ -38,10 +38,23 @@ Si cambia el parent
verificar parent
mover directorio adentro del parent
—————————
Nuevo post
- Reindex dbtags
—————————
Editar usuario
1- Usuario logueado
2- Ver si el usuario es administrador o si es el mismo usuario que se esta editando.
—————————
dbTags
Regenerate posts list
- Al momento de regenerarla deberia enviarle la lista de post ordenada por fecha.
- De esta forma la estructura esta ordenada para mostrarla.
- El que hace el trabajo es el administrador
—————————
New post->Publish->Manage posts
New page->Publish->Manage pages

View File

@ -69,7 +69,7 @@ include(PATH_KERNEL.'dblanguage.class.php');
include(PATH_HELPERS.'log.class.php');
include(PATH_HELPERS.'date.class.php');
// Try detect locale/language from HTTP
// Try to detect language from HTTP
$explode = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$localeFromHTTP = empty($explode[0])?'en_US':str_replace('-', '_', $explode[0]);
@ -193,13 +193,19 @@ function install($adminPassword, $email)
if(!mkdir(PATH_PLUGINS_DATABASES.'pages', $dirpermissions, true))
{
$errorText = 'Error when trying to created the directory=>'.PATH_PLUGINS_DATABASES;
$errorText = 'Error when trying to created the directory=>'.PATH_PLUGINS_DATABASES.'pages';
error_log($errorText, 0);
}
if(!mkdir(PATH_PLUGINS_DATABASES.'simplemde', $dirpermissions, true))
{
$errorText = 'Error when trying to created the directory=>'.PATH_PLUGINS_DATABASES;
$errorText = 'Error when trying to created the directory=>'.PATH_PLUGINS_DATABASES.'simplemde';
error_log($errorText, 0);
}
if(!mkdir(PATH_PLUGINS_DATABASES.'tags', $dirpermissions, true))
{
$errorText = 'Error when trying to created the directory=>'.PATH_PLUGINS_DATABASES.'tags';
error_log($errorText, 0);
}
@ -291,14 +297,36 @@ function install($adminPassword, $email)
file_put_contents(PATH_DATABASES.'security.php', $dataHead.json_encode($data, JSON_PRETTY_PRINT), LOCK_EX);
// File plugins/pages/db.php
$data = array(
'homeLink'=>true,
'label'=>$Language->get('Pages'),
'position'=>'0'
// File tags.php
file_put_contents(
PATH_DATABASES.'tags.php',
$dataHead.json_encode(
array(
'postsIndex'=>array(
'bludit'=>array('name'=>'Bludit', 'posts'=>array('first-post')),
'cms'=>array('name'=>'cms', 'posts'=>array('first-post'))
),
'pagesIndex'=>array()
),
JSON_PRETTY_PRINT),
LOCK_EX
);
file_put_contents(PATH_PLUGINS_DATABASES.'pages'.DS.'db.php', $dataHead.json_encode($data, JSON_PRETTY_PRINT), LOCK_EX);
// PLUGINS
// File plugins/pages/db.php
file_put_contents(
PATH_PLUGINS_DATABASES.'pages'.DS.'db.php',
$dataHead.json_encode(
array(
'position'=>0,
'homeLink'=>true,
'label'=>$Language->get('Pages')
),
JSON_PRETTY_PRINT),
LOCK_EX
);
// File plugins/simplemde/db.php
file_put_contents(
@ -311,14 +339,18 @@ function install($adminPassword, $email)
LOCK_EX
);
// File tags.php
$data = array(
'postsIndex'=>array(),
'pagesIndex'=>array()
// File plugins/tags/db.php
file_put_contents(
PATH_PLUGINS_DATABASES.'tags'.DS.'db.php',
$dataHead.json_encode(
array(
'position'=>0,
'label'=>$Language->get('Tags')
),
JSON_PRETTY_PRINT),
LOCK_EX
);
file_put_contents(PATH_DATABASES.'tags.php', $dataHead.json_encode($data, JSON_PRETTY_PRINT), LOCK_EX);
// File index.txt for error page
$data = 'Title: '.$Language->get('Error').'
Content: '.$Language->get('The page has not been found');

View File

@ -3,6 +3,7 @@
class dbJSON
{
public $db;
public $dbBackup;
public $file;
public $firstLine;
@ -12,6 +13,7 @@ class dbJSON
{
$this->file = $file;
$this->db = array();
$this->dbBackup = array();
$this->firstLine = $firstLine;
if(file_exists($file))
@ -35,6 +37,7 @@ class dbJSON
}
else {
$this->db = $array;
$this->dbBackup = $array;
}
}
else
@ -43,6 +46,12 @@ class dbJSON
}
}
public function restoreDb()
{
$this->db = $this->dbBackup;
return true;
}
// Returns the amount of database items.
public function count()
{

View File

@ -39,8 +39,8 @@ if( $layout['slug']==='ajax' )
else
{
// Boot rules
include(PATH_RULES.'70.build_posts.php');
include(PATH_RULES.'70.build_pages.php');
include(PATH_RULES.'70.posts.php');
include(PATH_RULES.'70.pages.php');
include(PATH_RULES.'80.plugins.php');
include(PATH_RULES.'99.header.php');
include(PATH_RULES.'99.paginator.php');

View File

@ -10,6 +10,26 @@ $posts = array();
// Functions
// ============================================================================
function reIndexTagsPosts()
{
global $dbPosts;
global $dbTags;
// Remove unpublished, only drafts.
$dbPosts->removeUnpublished(false);
// Sort posts
$dbPosts->sortByDate();
// Regenerate the tags index for posts
$dbTags->reindexPosts( $dbPosts->db );
// Restore de db on dbPost
$dbPosts->restoreDb();
return true;
}
function buildPost($key)
{
global $dbPosts;
@ -59,14 +79,13 @@ function buildPost($key)
$user = $dbUsers->getDb( $Post->username() );
$Post->setField('authorFirstName', $user['firstName'], false);
$Post->setField('authorLastName', $user['lastName'], false);
}
return $Post;
}
function buildPostForPage($pageNumber=0, $amount=5, $removeUnpublished=false, $tagKey=false)
function buildPostsForPage($pageNumber=0, $amount=POSTS_PER_PAGE_ADMIN, $removeUnpublished=true, $tagKey=false)
{
global $dbPosts;
global $dbTags;
@ -74,34 +93,34 @@ function buildPostForPage($pageNumber=0, $amount=5, $removeUnpublished=false, $t
global $Url;
if($tagKey) {
// Get the keys list from tags database, this database is optimized for this case.
$list = $dbTags->getList($pageNumber, $amount, $tagKey);
}
else {
// Get the keys list from posts database.
$list = $dbPosts->getList($pageNumber, $amount, $removeUnpublished);
}
// There are not post for the pageNumber then set the page notfound
// There are not posts for the page number then set the page notfound
if(empty($list) && $pageNumber>0) {
$Url->setNotFound(true);
}
// Foreach post key, build the post.
foreach($list as $postKey=>$values)
{
$Post = buildPost($postKey);
if($Post!==false) {
array_push($posts, $Post);
}
}
}
// ============================================================================
// Main
// ============================================================================
// Filter by post, then build it
// Build specific post.
if( ($Url->whereAmI()==='post') && ($Url->notFound()===false) )
{
$Post = buildPost( $Url->slug() );
@ -122,20 +141,20 @@ if( ($Url->whereAmI()==='post') && ($Url->notFound()===false) )
}
}
// Build posts by specific tag.
elseif( ($Url->whereAmI()==='tag') && ($Url->notFound()===false) )
{
buildPostForPage($Url->pageNumber(), $Site->postsPerPage(), true, $Url->slug());
buildPostsForPage($Url->pageNumber(), $Site->postsPerPage(), true, $Url->slug());
}
// Build post per page
// Build posts for homepage or admin area.
else
{
// Posts for admin area.
if($Url->whereAmI()==='admin') {
// Build post for admin area with drafts+schedulers
buildPostForPage($Url->pageNumber(), POSTS_PER_PAGE_ADMIN, true);
buildPostsForPage($Url->pageNumber(), POSTS_PER_PAGE_ADMIN, false);
}
else
{
// Build post for the site, without the drafts and scheduleres posts
buildPostForPage($Url->pageNumber(), $Site->postsPerPage(), false);
// Posts for homepage
else {
buildPostsForPage($Url->pageNumber(), $Site->postsPerPage(), true);
}
}
}

View File

@ -1,8 +1,8 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
// Boot rules
include(PATH_RULES.'70.build_posts.php');
include(PATH_RULES.'70.build_pages.php');
include(PATH_RULES.'70.posts.php');
include(PATH_RULES.'70.pages.php');
include(PATH_RULES.'80.plugins.php');
include(PATH_RULES.'99.header.php');
include(PATH_RULES.'99.paginator.php');

View File

@ -26,11 +26,7 @@ class dbPages extends dbJSON
$dataForFile = array(); // This data will be saved in the file
$key = $this->generateKey($args['slug'], $args['parent']);
/*
if($key===false) {
return false;
}
*/
// The user is always the one loggued.
$args['username'] = Session::get('username');
if( Text::isEmpty($args['username']) ) {
@ -289,7 +285,7 @@ class dbPages extends dbJSON
return $this->db;
}
public function regenerate()
public function regenerateCli()
{
$db = $this->db;
$newPaths = array();
@ -361,73 +357,4 @@ class dbPages extends dbJSON
return $this->db!=$db;
}
/*
public function regenerate()
{
$db = $this->db;
$paths = array();
$fields = array();
// Complete $fields with the default values.
foreach($this->dbFields as $field=>$options) {
if(!$options['inFile']) {
$fields[$field] = $options['value'];
}
}
// Foreach new page set the unix time stamp.
$fields['unixTimeCreated'] = Date::unixTime();
// Foreach new page set the owner admin.
$fields['username'] = 'admin';
// Foreach new page set the status.
if(HANDMADE_PUBLISHED) {
$fields['status']='published';
}
// Get the pages from the first level of directories
$tmpPaths = glob(PATH_PAGES.'*', GLOB_ONLYDIR);
foreach($tmpPaths as $directory)
{
$key = basename($directory);
if(file_exists($directory.DS.'index.txt')){
// The key is the directory name
$paths[$key] = true;
}
// Recovery pages from subdirectories
$subPaths = glob($directory.DS.'*', GLOB_ONLYDIR);
foreach($subPaths as $subDirectory)
{
$subKey = basename($subDirectory);
if(file_exists($subDirectory.DS.'index.txt')) {
// The key is composed by the directory/subdirectory
$paths[$key.'/'.$subKey] = true;
}
}
}
// Remove old posts from db
foreach( array_diff_key($db, $paths) as $slug=>$data ) {
unset($this->db[$slug]);
}
// Insert new posts to db
foreach( array_diff_key($paths, $db) as $slug=>$data ) {
$this->db[$slug] = $fields;
}
// Save the database.
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
return $this->db!=$db;
}
*/
}
}

View File

@ -15,7 +15,7 @@ class dbPosts extends dbJSON
private $numberPosts = array(
'total'=>0,
'withoutDrafts'=>0
'published'=>0
);
function __construct()
@ -31,7 +31,7 @@ class dbPosts extends dbJSON
return $this->numberPosts['total'];
}
return $this->numberPosts['withoutDrafts'];
return $this->numberPosts['published'];
}
// Return an array with the post's database, FALSE otherwise.
@ -163,10 +163,6 @@ class dbPosts extends dbJSON
public function edit($args)
{
if( !Valid::date($args['date'], DB_DATE_FORMAT) ) {
$args['date'] = Date::current(DB_DATE_FORMAT);
}
if( $this->delete($args['key']) ) {
return $this->add($args);
}
@ -203,7 +199,109 @@ class dbPosts extends dbJSON
return true;
}
public function regenerate()
// Returns an array with a list of posts keys, filtered by a page number.
public function getList($pageNumber, $postPerPage, $removeUnpublished=true)
{
$totalPosts = $this->numberPosts['total'];
// Remove the unpublished posts.
if($removeUnpublished) {
$this->removeUnpublished();
$totalPosts = $this->numberPosts['published'];
}
$init = (int) $postPerPage * $pageNumber;
$end = (int) min( ($init + $postPerPage - 1), $totalPosts - 1 );
$outrange = $init<0 ? true : $init>$end;
if(!$outrange)
{
// Sort posts
$this->sortByDate();
return array_slice($this->db, $init, $postPerPage, true);
}
return array();
}
// Delete all posts from an user.
public function deletePostsByUser($username)
{
foreach($this->db as $key=>$value)
{
if($value['username']==$username) {
unset($this->db[$key]);
}
}
// Save the database.
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
return true;
}
// Link-up all posts from an user to another user.
public function linkPostsToUser($oldUsername, $newUsername)
{
foreach($this->db as $key=>$value)
{
if($value['username']==$oldUsername) {
$this->db[$key]['username'] = $newUsername;
}
}
// Save the database.
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
return true;
}
// Remove the posts not published, status != published or date grater than current date.
public function removeUnpublished($scheduled=true)
{
$currentDate = Date::current(DB_DATE_FORMAT);
foreach($this->db as $key=>$values)
{
if( ($values['status']!='published') || ( ($values['date']>$currentDate) && $scheduled ) ) {
unset($this->db[$key]);
}
}
$this->numberPosts['published'] = count($this->db);
return true;
}
// Sort posts by date.
public function sortByDate($HighToLow=true)
{
if($HighToLow) {
uasort($this->db, array($this, 'sortHighToLow'));
}
else {
uasort($this->db, array($this, 'sortLowToHigh'));
}
return true;
}
private function sortLowToHigh($a, $b) {
return $a['date']>$b['date'];
}
private function sortHighToLow($a, $b) {
return $a['date']<$b['date'];
}
public function regenerateCli()
{
$db = $this->db;
$newPaths = array();
@ -265,160 +363,4 @@ class dbPosts extends dbJSON
return $this->db!=$db;
}
/*
public function regenerate()
{
$db = $this->db;
$paths = array();
$fields = array();
// Default fields and value
foreach($this->dbFields as $field=>$options) {
if(!$options['inFile']) {
$fields[$field] = $options['value'];
}
}
// Unix time stamp
$fields['date'] = Date::current(DB_DATE_FORMAT);
// Username
$fields['username'] = 'admin';
if(HANDMADE_PUBLISHED) {
$fields['status']='published';
}
// Recovery pages from the first level of directories
$tmpPaths = glob(PATH_POSTS.'*', GLOB_ONLYDIR);
foreach($tmpPaths as $directory)
{
$key = basename($directory);
if(file_exists($directory.DS.'index.txt')) {
// The key is the directory name
$paths[$key] = true;
}
}
// Remove old posts from db
foreach( array_diff_key($db, $paths) as $slug=>$data ) {
unset($this->db[$slug]);
}
// Insert new posts to db
foreach( array_diff_key($paths, $db) as $slug=>$data ) {
$this->db[$slug] = $fields;
}
// Save the database.
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
return $this->db!=$db;
}
*/
public function getList($pageNumber, $postPerPage, $draftPosts=false)
{
// DEBUG: Ver una mejor manera de eliminar draft post antes de ordenarlos
// DEBUG: Se eliminan antes de ordenarlos porque sino los draft cuentan como publicados en el PostPerPage.
if(!$draftPosts) {
$this->removeUnpublished();
$this->numberPosts['withoutDrafts'] = count($this->db);
}
$init = (int) $postPerPage * $pageNumber;
$end = (int) min( ($init + $postPerPage - 1), count($this->db) - 1 );
$outrange = $init<0 ? true : $init>$end;
// Sort posts
$this->sortByDate();
if(!$outrange) {
$tmp = $this->db;
return array_slice($tmp, $init, $postPerPage, true);
}
return array();
}
// Delete all posts from an user.
public function deletePostsByUser($username)
{
foreach($this->db as $key=>$value)
{
if($value['username']==$username) {
unset($this->db[$key]);
}
}
// Save the database.
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
return true;
}
// Link-up all posts from an user to another user.
public function linkPostsToUser($oldUsername, $newUsername)
{
foreach($this->db as $key=>$value)
{
if($value['username']==$oldUsername) {
$this->db[$key]['username'] = $newUsername;
}
}
// Save the database.
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
return true;
}
// Remove the posts not published, status != published and date grater than current date.
// DEBUG: Ver una mejor manera de eliminar draft post antes de ordenarlos
private function removeUnpublished()
{
$tmp = array();
$currentDate = Date::current(DB_DATE_FORMAT);
foreach($this->db as $key=>$values)
{
if( ($values['status']==='published') && ($values['date']<=$currentDate) ) {
$tmp[$key]=$values;
}
}
$this->db = $tmp;
}
private function sortByDate($low_to_high=false)
{
// high to low
function high_to_low($a, $b) {
return $a['date']<$b['date'];
}
// low to high
function low_to_high($a, $b) {
return $a['date']>$b['date'];
}
$tmp = $this->db;
if($low_to_high)
uasort($tmp, 'low_to_high');
else
uasort($tmp, 'high_to_low');
return true;
}
}
}

View File

@ -18,18 +18,25 @@ class dbTags extends dbJSON
parent::__construct(PATH_DATABASES.'tags.php');
}
// Returns an array with a list of posts keys, filtered by a page number and a tag key.
public function getList($pageNumber, $postPerPage, $tagKey)
{
if( !isset($this->db['postsIndex'][$tagKey]) ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying get the posts list by the tag key: '.$tagKey);
return array();
}
$init = (int) $postPerPage * $pageNumber;
$end = (int) min( ($init + $postPerPage - 1), $this->countPostsByTag($tagKey) - 1 );
$outrange = $init<0 ? true : $init > $end;
if(!$outrange) {
$list = $this->db['postsIndex'][$tagKey]['posts'];
$tmp = array_flip($list);
$tmp = array_flip($list); // Change the posts keys list in the array key.
return array_slice($tmp, $init, $postPerPage, true);
}
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying get the list of posts, out of range?. Pagenumber: '.$pageNumber);
return array();
}
@ -38,11 +45,12 @@ class dbTags extends dbJSON
if( isset($this->db['postsIndex'][$tagKey]) ) {
return count($this->db['postsIndex'][$tagKey]['posts']);
}
else {
return 0;
}
return 0;
}
// Regenerate the posts index for each tag.
// (array) $db, the $db must be sorted by date and the posts published only.
public function reindexPosts($db)
{
$tagsIndex = array();
@ -74,6 +82,8 @@ class dbTags extends dbJSON
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
return true;
}
}

View File

@ -179,7 +179,6 @@ class Text {
return preg_replace_callback('/<pre.*?><code(.*?)>(.*?)<\/code><\/pre>/imsu',
create_function('$input', 'return "<pre><code $input[1]>".htmlentities($input[2])."</code></pre>";'),
$string);
}
}