From 25633d0dbc17cf43cdf54b6d0011c051f92d7ecc Mon Sep 17 00:00:00 2001 From: dignajar Date: Sat, 29 Aug 2015 20:26:46 -0300 Subject: [PATCH 1/9] Improvements on tags --- admin/controllers/edit-post.php | 2 ++ admin/controllers/new-post.php | 2 ++ kernel/boot/rules/70.build_posts.php | 26 +++++++++------- kernel/dbposts.class.php | 9 +++--- kernel/dbtags.class.php | 44 ++++++++++++++++++---------- plugins/tags/plugin.php | 8 ++--- themes/pure/index.php | 2 +- 7 files changed, 58 insertions(+), 35 deletions(-) diff --git a/admin/controllers/edit-post.php b/admin/controllers/edit-post.php index a52ee912..df7dbb04 100644 --- a/admin/controllers/edit-post.php +++ b/admin/controllers/edit-post.php @@ -22,6 +22,8 @@ function editPost($args) if( $dbPosts->edit($args) ) { // Regenerate the database tags + $dbPosts->removeUnpublished(); + $dbPosts->sortByDate(); $dbTags->reindexPosts( $dbPosts->db ); Alert::set($Language->g('The changes have been saved')); diff --git a/admin/controllers/new-post.php b/admin/controllers/new-post.php index 92131b52..bffb24be 100644 --- a/admin/controllers/new-post.php +++ b/admin/controllers/new-post.php @@ -26,6 +26,8 @@ function addPost($args) if( $dbPosts->add($args) ) { // Regenerate the database tags + $dbPosts->removeUnpublished(); + $dbPosts->sortByDate(); $dbTags->reindexPosts( $dbPosts->db ); Alert::set($Language->g('Post added successfully')); diff --git a/kernel/boot/rules/70.build_posts.php b/kernel/boot/rules/70.build_posts.php index bfdd20dd..525a251a 100644 --- a/kernel/boot/rules/70.build_posts.php +++ b/kernel/boot/rules/70.build_posts.php @@ -66,22 +66,28 @@ function buildPost($key) return $Post; } -function build_posts_per_page($pageNumber=0, $amount=5, $draftPosts=false) +function buildPostForPage($pageNumber=0, $amount=5, $removeUnpublished=false, $tagKey=false) { global $dbPosts; + global $dbTags; global $posts; global $Url; - $list = $dbPosts->getPage($pageNumber, $amount, $draftPosts); + if($tagKey) { + $list = $dbTags->getList($pageNumber, $amount, $tagKey); + } + else { + $list = $dbPosts->getList($pageNumber, $amount, $removeUnpublished); + } - // There are not post for the pageNumber then true Not found page + // There are not post for the pageNumber then set the page notfound if(empty($list) && $pageNumber>0) { $Url->setNotFound(true); } - foreach($list as $slug=>$db) + foreach($list as $postKey=>$values) { - $Post = buildPost($slug); + $Post = buildPost($postKey); if($Post!==false) { array_push($posts, $Post); @@ -118,18 +124,18 @@ if( ($Url->whereAmI()==='post') && ($Url->notFound()===false) ) } elseif( ($Url->whereAmI()==='tag') && ($Url->notFound()===false) ) { - + buildPostForPage($Url->pageNumber(), $Site->postsPerPage(), true, $Url->slug()); } // Build post per page else { if($Url->whereAmI()==='admin') { - // Build post for admin area with drafts - build_posts_per_page($Url->pageNumber(), POSTS_PER_PAGE_ADMIN, true); + // Build post for admin area with drafts+schedulers + buildPostForPage($Url->pageNumber(), POSTS_PER_PAGE_ADMIN, true); } else { - // Build post for the site, without the drafts posts - build_posts_per_page($Url->pageNumber(), $Site->postsPerPage(), false); + // Build post for the site, without the drafts and scheduleres posts + buildPostForPage($Url->pageNumber(), $Site->postsPerPage(), false); } } diff --git a/kernel/dbposts.class.php b/kernel/dbposts.class.php index 97aad227..a0c20729 100644 --- a/kernel/dbposts.class.php +++ b/kernel/dbposts.class.php @@ -320,7 +320,7 @@ class dbPosts extends dbJSON return $this->db!=$db; } */ - public function getPage($pageNumber, $postPerPage, $draftPosts=false) + 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. @@ -331,12 +331,13 @@ class dbPosts extends dbJSON $init = (int) $postPerPage * $pageNumber; $end = (int) min( ($init + $postPerPage - 1), count($this->db) - 1 ); - $outrange = $init<0 ? true : $init > $end; + $outrange = $init<0 ? true : $init>$end; // Sort posts - $tmp = $this->sortByDate(); + $this->sortByDate(); if(!$outrange) { + $tmp = $this->db; return array_slice($tmp, $init, $postPerPage, true); } @@ -417,7 +418,7 @@ class dbPosts extends dbJSON else uasort($tmp, 'high_to_low'); - return $tmp; + return true; } } diff --git a/kernel/dbtags.class.php b/kernel/dbtags.class.php index 4c3776d6..22f51ced 100644 --- a/kernel/dbtags.class.php +++ b/kernel/dbtags.class.php @@ -18,13 +18,28 @@ class dbTags extends dbJSON parent::__construct(PATH_DATABASES.'tags.php'); } + public function getList($pageNumber, $postPerPage, $tagKey) + { + $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); + return array_slice($tmp, $init, $postPerPage, true); + } + + return array(); + } + public function countPostsByTag($tagKey) { if( isset($this->db['postsIndex'][$tagKey]) ) { return count($this->db['postsIndex'][$tagKey]['posts']); } else { - return false; + return 0; } } @@ -36,23 +51,20 @@ class dbTags extends dbJSON // Foreach post foreach($db as $postKey=>$values) { - if( ($values['status']==='published') && ($values['date']<=$currentDate) ) + $explode = explode(',', $values['tags']); + + // Foreach tag from post + foreach($explode as $tagName) { - $explode = explode(',', $values['tags']); + $tagName = trim($tagName); + $tagKey = Text::cleanUrl($tagName); - // Foreach tag from post - foreach($explode as $tagName) - { - $tagName = trim($tagName); - $tagKey = Text::cleanUrl($tagName); - - if( isset($tagsIndex[$tagKey]) ) { - array_push($tagsIndex[$tagKey]['posts'], $postKey); - } - else { - $tagsIndex[$tagKey]['name'] = $tagName; - $tagsIndex[$tagKey]['posts'] = array($postKey); - } + if( isset($tagsIndex[$tagKey]) ) { + array_push($tagsIndex[$tagKey]['posts'], $postKey); + } + else { + $tagsIndex[$tagKey]['name'] = $tagName; + $tagsIndex[$tagKey]['posts'] = array($postKey); } } } diff --git a/plugins/tags/plugin.php b/plugins/tags/plugin.php index 52902cc4..ea12d601 100755 --- a/plugins/tags/plugin.php +++ b/plugins/tags/plugin.php @@ -27,12 +27,12 @@ class pluginTags extends Plugin { global $dbTags; global $Url; + $db = $dbTags->db['postsIndex']; + $filter = trim($Url->filters('tag'), '/'); + $html = '
'; $html .= '

'.$this->getDbField('label').'

'; $html .= '
'; - - $db = $dbTags->db['postsIndex']; - $html .= ''; diff --git a/themes/pure/index.php b/themes/pure/index.php index 0e38280b..027a6b57 100644 --- a/themes/pure/index.php +++ b/themes/pure/index.php @@ -24,7 +24,7 @@ whereAmI()=='home') + if( ($Url->whereAmI()=='home') || ($Url->whereAmI()=='tag') ) { include('php/home.php'); } From 0c70da532b76fd173b5e47b9c053e4104853bb91 Mon Sep 17 00:00:00 2001 From: dignajar Date: Sun, 30 Aug 2015 22:18:06 -0300 Subject: [PATCH 2/9] Improvements on tags --- admin/controllers/dashboard.php | 6 +- admin/controllers/edit-page.php | 4 +- admin/controllers/edit-post.php | 18 +- admin/controllers/new-page.php | 2 - admin/controllers/new-post.php | 9 +- features.txt | 13 + install.php | 62 +++- kernel/abstract/dbjson.class.php | 9 + kernel/boot/admin.php | 4 +- .../{70.build_pages.php => 70.pages.php} | 0 .../{70.build_posts.php => 70.posts.php} | 51 ++-- kernel/boot/site.php | 4 +- kernel/dbpages.class.php | 79 +---- kernel/dbposts.class.php | 270 +++++++----------- kernel/dbtags.class.php | 18 +- kernel/helpers/text.class.php | 1 - 16 files changed, 250 insertions(+), 300 deletions(-) rename kernel/boot/rules/{70.build_pages.php => 70.pages.php} (100%) rename kernel/boot/rules/{70.build_posts.php => 70.posts.php} (74%) diff --git a/admin/controllers/dashboard.php b/admin/controllers/dashboard.php index 602e33e0..09f40ab2 100644 --- a/admin/controllers/dashboard.php +++ b/admin/controllers/dashboard.php @@ -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) diff --git a/admin/controllers/edit-page.php b/admin/controllers/edit-page.php index e591d578..b6466732 100644 --- a/admin/controllers/edit-page.php +++ b/admin/controllers/edit-page.php @@ -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']); diff --git a/admin/controllers/edit-post.php b/admin/controllers/edit-post.php index df7dbb04..435f6ef2 100644 --- a/admin/controllers/edit-post.php +++ b/admin/controllers/edit-post.php @@ -1,5 +1,9 @@ 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'); diff --git a/admin/controllers/new-page.php b/admin/controllers/new-page.php index 0b54d994..0dd12df7 100644 --- a/admin/controllers/new-page.php +++ b/admin/controllers/new-page.php @@ -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'); } diff --git a/admin/controllers/new-post.php b/admin/controllers/new-post.php index bffb24be..04ef4c8e 100644 --- a/admin/controllers/new-post.php +++ b/admin/controllers/new-post.php @@ -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; } // ============================================================================ diff --git a/features.txt b/features.txt index e764b0c4..f3895ccc 100644 --- a/features.txt +++ b/features.txt @@ -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 diff --git a/install.php b/install.php index d5cf4a85..16e3ce0e 100755 --- a/install.php +++ b/install.php @@ -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'); diff --git a/kernel/abstract/dbjson.class.php b/kernel/abstract/dbjson.class.php index e7098d1d..3bc0252b 100644 --- a/kernel/abstract/dbjson.class.php +++ b/kernel/abstract/dbjson.class.php @@ -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() { diff --git a/kernel/boot/admin.php b/kernel/boot/admin.php index 3f7e2719..90b121b5 100644 --- a/kernel/boot/admin.php +++ b/kernel/boot/admin.php @@ -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'); diff --git a/kernel/boot/rules/70.build_pages.php b/kernel/boot/rules/70.pages.php similarity index 100% rename from kernel/boot/rules/70.build_pages.php rename to kernel/boot/rules/70.pages.php diff --git a/kernel/boot/rules/70.build_posts.php b/kernel/boot/rules/70.posts.php similarity index 74% rename from kernel/boot/rules/70.build_posts.php rename to kernel/boot/rules/70.posts.php index 525a251a..eb5af84b 100644 --- a/kernel/boot/rules/70.build_posts.php +++ b/kernel/boot/rules/70.posts.php @@ -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); } -} +} \ No newline at end of file diff --git a/kernel/boot/site.php b/kernel/boot/site.php index 6d4d763f..a031f722 100644 --- a/kernel/boot/site.php +++ b/kernel/boot/site.php @@ -1,8 +1,8 @@ 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; - } - */ -} +} \ No newline at end of file diff --git a/kernel/dbposts.class.php b/kernel/dbposts.class.php index a0c20729..f4314697 100644 --- a/kernel/dbposts.class.php +++ b/kernel/dbposts.class.php @@ -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; - } - -} +} \ No newline at end of file diff --git a/kernel/dbtags.class.php b/kernel/dbtags.class.php index 22f51ced..4b02f27e 100644 --- a/kernel/dbtags.class.php +++ b/kernel/dbtags.class.php @@ -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; } } \ No newline at end of file diff --git a/kernel/helpers/text.class.php b/kernel/helpers/text.class.php index a71f0315..ae23bd39 100644 --- a/kernel/helpers/text.class.php +++ b/kernel/helpers/text.class.php @@ -179,7 +179,6 @@ class Text { return preg_replace_callback('/(.*?)<\/code><\/pre>/imsu', create_function('$input', 'return "
".htmlentities($input[2])."
";'), $string); - } } From 83ec1402973e90fc748d77e6b099a56cbdf4aeec Mon Sep 17 00:00:00 2001 From: dignajar Date: Tue, 1 Sep 2015 21:42:21 -0300 Subject: [PATCH 3/9] Improvements on tags --- kernel/abstract/dbjson.class.php | 11 +++-- kernel/boot/rules/70.posts.php | 16 ++++--- kernel/dbposts.class.php | 73 ++++++++++++++++++++++++++------ kernel/dbtags.class.php | 1 + kernel/post.class.php | 6 +-- 5 files changed, 81 insertions(+), 26 deletions(-) diff --git a/kernel/abstract/dbjson.class.php b/kernel/abstract/dbjson.class.php index 3bc0252b..2b7aec1d 100644 --- a/kernel/abstract/dbjson.class.php +++ b/kernel/abstract/dbjson.class.php @@ -61,17 +61,20 @@ class dbJSON // Save the JSON file. public function save() { + $data = ''; + if($this->firstLine) { $data = "".PHP_EOL; } - else { - $data = ''; - } + // Serialize database $data .= $this->serialize($this->db); + // Backup the new database. + $this->dbBackup = $this->db; + // LOCK_EX flag to prevent anyone else writing to the file at the same time. - file_put_contents($this->file, $data, LOCK_EX); + return file_put_contents($this->file, $data, LOCK_EX); } private function serialize($data) diff --git a/kernel/boot/rules/70.posts.php b/kernel/boot/rules/70.posts.php index eb5af84b..0b08e20f 100644 --- a/kernel/boot/rules/70.posts.php +++ b/kernel/boot/rules/70.posts.php @@ -15,11 +15,8 @@ function reIndexTagsPosts() global $dbPosts; global $dbTags; - // Remove unpublished, only drafts. - $dbPosts->removeUnpublished(false); - - // Sort posts - $dbPosts->sortByDate(); + // Remove unpublished. + $dbPosts->removeUnpublished(); // Regenerate the tags index for posts $dbTags->reindexPosts( $dbPosts->db ); @@ -102,7 +99,8 @@ function buildPostsForPage($pageNumber=0, $amount=POSTS_PER_PAGE_ADMIN, $removeU } // There are not posts for the page number then set the page notfound - if(empty($list) && $pageNumber>0) { + //if(empty($list) && $pageNumber>0) { + if(empty($list)) { $Url->setNotFound(true); } @@ -120,6 +118,12 @@ function buildPostsForPage($pageNumber=0, $amount=POSTS_PER_PAGE_ADMIN, $removeU // Main // ============================================================================ +// Execute the scheduler. +if( $dbPosts->scheduler() ) { + // Reindex dbTags. + reIndexTagsPosts(); +} + // Build specific post. if( ($Url->whereAmI()==='post') && ($Url->notFound()===false) ) { diff --git a/kernel/dbposts.class.php b/kernel/dbposts.class.php index f4314697..5cb0ba7d 100644 --- a/kernel/dbposts.class.php +++ b/kernel/dbposts.class.php @@ -7,7 +7,7 @@ class dbPosts extends dbJSON 'content'=> array('inFile'=>true, 'value'=>''), 'description'=> array('inFile'=>false, 'value'=>''), 'username'=> array('inFile'=>false, 'value'=>''), - 'status'=> array('inFile'=>false, 'value'=>'draft'), + 'status'=> array('inFile'=>false, 'value'=>'draft'), // published, draft, scheduled 'tags'=> array('inFile'=>false, 'value'=>''), 'allowComments'=> array('inFile'=>false, 'value'=>false), 'date'=> array('inFile'=>false, 'value'=>'') @@ -91,6 +91,7 @@ class dbPosts extends dbJSON { $dataForDb = array(); // This data will be saved in the database $dataForFile = array(); // This data will be saved in the file + $currentDate = Date::current(DB_DATE_FORMAT); // Generate the database key. $key = $this->generateKey($args['slug']); @@ -101,8 +102,14 @@ class dbPosts extends dbJSON return false; } + // Date if(!Valid::date($args['date'], DB_DATE_FORMAT)) { - $args['date'] = Date::current(DB_DATE_FORMAT); + $args['date'] = $currentDate; + } + + // Schedule post? + if( ($args['date']>$currentDate) && ($args['status']=='published') ) { + $args['status'] = 'scheduled'; } // Verify arguments with the database fields. @@ -153,6 +160,10 @@ class dbPosts extends dbJSON // Save the database $this->db[$key] = $dataForDb; + + // Sort posts before save. + $this->sortByDate(); + if( $this->save() === false ) { Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.'); return false; @@ -214,11 +225,7 @@ class dbPosts extends dbJSON $end = (int) min( ($init + $postPerPage - 1), $totalPosts - 1 ); $outrange = $init<0 ? true : $init>$end; - if(!$outrange) - { - // Sort posts - $this->sortByDate(); - + if(!$outrange) { return array_slice($this->db, $init, $postPerPage, true); } @@ -254,6 +261,9 @@ class dbPosts extends dbJSON } } + // Sort posts before save. + $this->sortByDate(); + // Save the database. if( $this->save() === false ) { Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.'); @@ -263,14 +273,12 @@ class dbPosts extends dbJSON return true; } - // Remove the posts not published, status != published or date grater than current date. - public function removeUnpublished($scheduled=true) + // Remove unpublished posts, status != published. + public function removeUnpublished() { - $currentDate = Date::current(DB_DATE_FORMAT); - foreach($this->db as $key=>$values) { - if( ($values['status']!='published') || ( ($values['date']>$currentDate) && $scheduled ) ) { + if($values['status']!='published') { unset($this->db[$key]); } } @@ -280,6 +288,44 @@ class dbPosts extends dbJSON return true; } + // Return TRUE if there are new posts published, FALSE otherwise. + public function scheduler() + { + // Get current date. + $currentDate = Date::current(DB_DATE_FORMAT); + + $saveDatabase = false; + + // Check scheduled posts and publish. + foreach($this->db as $postKey=>$values) + { + if($values['status']=='scheduled') + { + // Publish post. + if($values['date']<=$currentDate) { + $this->db[$postKey]['status'] = 'published'; + $saveDatabase = true; + } + } + elseif($values['status']=='published') { + break; + } + } + + // Save the database. + if($saveDatabase) + { + if( $this->save() === false ) { + Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.'); + return false; + } + + return true; + } + + return false; + } + // Sort posts by date. public function sortByDate($HighToLow=true) { @@ -290,6 +336,9 @@ class dbPosts extends dbJSON uasort($this->db, array($this, 'sortLowToHigh')); } + + Log::set(__METHOD__.LOG_SEP.'Sorted.'.$HighToLow); + return true; } diff --git a/kernel/dbtags.class.php b/kernel/dbtags.class.php index 4b02f27e..0faacddc 100644 --- a/kernel/dbtags.class.php +++ b/kernel/dbtags.class.php @@ -78,6 +78,7 @@ class dbTags extends dbJSON } $this->db['postsIndex'] = $tagsIndex; + if( $this->save() === false ) { Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.'); return false; diff --git a/kernel/post.class.php b/kernel/post.class.php index 6a8d2aea..ef5fb616 100644 --- a/kernel/post.class.php +++ b/kernel/post.class.php @@ -63,15 +63,13 @@ class Post extends fileContent // Returns TRUE if the post is published, FALSE otherwise. public function published() { - $currentDate = Date::current(DB_DATE_FORMAT); - return ( ($this->getField('status')==='published') && ($this->getField('date')<=$currentDate) ); + return ($this->getField('status')==='published'); } // Returns TRUE if the post is scheduled, FALSE otherwise. public function scheduled() { - $currentDate = Date::current(DB_DATE_FORMAT); - return ( ($this->getField('status')==='published') && ($this->getField('date')>$currentDate) ); + return ($this->getField('status')==='scheduled'); } // Returns TRUE if the post is draft, FALSE otherwise. From c10e09c5f98aac7ae28a899bc2b5176ba4417691 Mon Sep 17 00:00:00 2001 From: dignajar Date: Thu, 3 Sep 2015 21:46:17 -0300 Subject: [PATCH 4/9] Bug fixes --- admin/views/add-user.php | 2 +- install.php | 4 +- kernel/dbtags.class.php | 16 +++++--- kernel/helpers/text.class.php | 25 +----------- plugins/simplemde/css/simplemde.min.css | 4 +- plugins/simplemde/js/simplemde.min.js | 17 ++++----- plugins/simplemde/language/en_US.json | 5 ++- plugins/simplemde/plugin.php | 51 ++++++++++++++++++------- 8 files changed, 67 insertions(+), 57 deletions(-) diff --git a/admin/views/add-user.php b/admin/views/add-user.php index 7090db0d..b2dcedd2 100644 --- a/admin/views/add-user.php +++ b/admin/views/add-user.php @@ -2,7 +2,7 @@ -
+