removing posts

This commit is contained in:
Diego 2017-05-10 20:40:28 +02:00
parent a6fab1c23b
commit 5145694752
5 changed files with 152 additions and 135 deletions

View File

@ -99,8 +99,8 @@ define('PAGE_BREAK', '<!-- pagebreak -->');
// No parent character, md5('No parent') // No parent character, md5('No parent')
define('NO_PARENT_CHAR', '3849abb4cb7abd24c2d8dac17b216f17'); define('NO_PARENT_CHAR', '3849abb4cb7abd24c2d8dac17b216f17');
// Post per page on Manage->Posts // Items per page for admin area
define('POSTS_PER_PAGE_ADMIN', 10); define('ITEMS_PER_PAGE_ADMIN', 10);
// Enable or disable Cli mode // Enable or disable Cli mode
define('CLI_MODE', FALSE); define('CLI_MODE', FALSE);

View File

@ -42,16 +42,18 @@ if( $Url->whereAmI()==='page' ) {
} }
} }
elseif( $Url->whereAmI()==='tag' ) { elseif( $Url->whereAmI()==='tag' ) {
$pages = buildPagesByTag( $Url->slug() ); $tagKey = $Url->slug();
$pages = buildPagesByTag($tagKey);
} }
elseif( $Url->whereAmI()==='category' ) { elseif( $Url->whereAmI()==='category' ) {
$pages = buildPagesByCategory( $Url->slug() ); $categoryKey = $Url->slug();
$pages = buildPagesByCategory($categoryKey);
} }
elseif( $Url->whereAmI()==='home' ) { elseif( $Url->whereAmI()==='home' ) {
$pages = buildPagesForHome( $Url->slug() ); $pages = buildPagesForHome();
} }
elseif( $Url->whereAmI()==='admin' ) { elseif( $Url->whereAmI()==='admin' ) {
$pages = buildPagesForAdmin( $Url->slug() ); $pages = buildPagesForAdmin();
} }
if( $Url->notFound() ) { if( $Url->notFound() ) {

View File

@ -219,6 +219,18 @@ class dbPages extends dbJSON
return true; return true;
} }
// Returns a database with published pages
public function getPublishedDB()
{
$tmp = $this->db;
foreach($tmp as $key=>$fields) {
if($fields['status']!='published') {
unset($tmp[$key]);
}
}
return $tmp;
}
// Return an array with the database for a page, FALSE otherwise. // Return an array with the database for a page, FALSE otherwise.
public function getPageDB($key) public function getPageDB($key)
{ {
@ -229,6 +241,45 @@ class dbPages extends dbJSON
return false; return false;
} }
// Returns an array with a list of pages
// (int) $pageNumber, the page number
// (int) $amountOfItems, amount of items to return
// (boolean) $onlyPublished, TRUE to return only published pages
public function getList($pageNumber, $amountOfItems, $onlyPublished=true)
{
$db = $this->db;
if( $onlyPublished ) {
$db = $this->getPublishedDB();
}
$total = count($db);
$init = (int) $amountOfItems * $pageNumber;
$end = (int) min( ($init + $amountOfItems - 1), $total );
$outrange = $init<0 ? true : $init>$end;
if(!$outrange) {
return array_slice($db, $init, $amountOfItems, true);
}
return array();
}
// Returns the amount of pages
// (boolean) $total, TRUE returns the total of pages
// (boolean) $total, FALSE returns the total of published pages (without draft and scheduled)
public function numberPages($onlyPublished=true)
{
if( $onlyPublished ) {
$db = $this->getPublishedDB();
return count($db);
}
return count($this->db);
}
// ----- OLD
// Set a field of the database // Set a field of the database
public function setField($key, $field, $value) public function setField($key, $field, $value)
{ {

View File

@ -7,147 +7,119 @@ function buildPage($key)
global $Parsedown; global $Parsedown;
global $Site; global $Site;
// Page object, content from FILE // Page object, content from index.txt file
$Page = new Page($key); $page = new Page($key);
if( !$Page->isValid() ) { if( !$page->isValid() ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from file with key: '.$key); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from file with key: '.$key);
return false; return false;
} }
// Page database, content from DATABASE JSON. // Get the database from dbPages
$db = $dbPages->getPageDB($key); $db = $dbPages->getPageDB($key);
if( !$db ) { if( !$db ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from database with key: '.$key); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from database with key: '.$key);
return false; return false;
} }
// Foreach field from DATABASE. // Foreach field from database set on the object
foreach($db as $field=>$value) { foreach($db as $field=>$value) {
$Page->setField($field, $value); $page->setField($field, $value);
} }
// Content in raw format // Parse Markdown
$contentRaw = $Page->content(); $contentRaw = $page->contentRaw();
$Page->setField('contentRaw', $Page->content(), true);
// Parse markdown content.
$content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities $content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities
$content = $Parsedown->text($content); // Parse Markdown. $content = $Parsedown->text($content); // Parse Markdown
$content = Text::imgRel2Abs($content, HTML_PATH_UPLOADS); // Parse img src relative to absolute. $content = Text::imgRel2Abs($content, HTML_PATH_UPLOADS); // Parse img src relative to absolute.
$Page->setField('content', $content, true); $page->setField('content', $content, true);
// Pagebrake // Pagebrake
$explode = explode(PAGE_BREAK, $content); $explode = explode(PAGE_BREAK, $content);
$Page->setField('breakContent', $explode[0], true); $page->setField('contentBreak', $explode[0], true);
$Page->setField('readMore', !empty($explode[1]), true); $page->setField('readMore', !empty($explode[1]), true);
// Date format // Date format
$pageDate = $Page->date(); $pageDate = $page->date();
$Page->setField('dateRaw', $pageDate, true); $page->setField('dateRaw', $pageDate, true);
$pageDateFormated = $Page->dateRaw( $Site->dateFormat() ); $pageDateFormated = $page->dateRaw( $Site->dateFormat() );
$Page->setField('date', $pageDateFormated, true); $page->setField('date', $pageDateFormated, true);
// User object // Generate and set the User object
$username = $Page->username(); $username = $page->username();
$Page->setField('user', $dbUsers->getUser($username)); $page->setField('user', $dbUsers->getUser($username));
return $Page; return $page;
}
function reindexCategories()
{
global $dbPages;
global $dbCategories;
// Get a database with published pages
$db = $dbPages->getPublishedDB();
// Regenerate the tags
$dbCategories->reindex($db);
return true;
}
function reindexTags()
{
global $dbPages;
global $dbCategories;
// Get a database with published pages
$db = $dbPages->getPublishedDB();
// Regenerate the tags
$dbTags->reindex($db);
return true;
}
function buildPagesForAdmin($pageNumber)
{
return buildPagesFor('admin', $pageNumber);
}
function buildPagesForHome($pageNumber)
{
return buildPagesFor('home', $pageNumber);
}
function buildPagesFor($for, $pageNumber)
{
global $dbPages;
global $Site;
if($for=='admin') {
$list = $dbPages->getList($pageNumber, ITEMS_PER_PAGE_ADMIN, false);
}
elseif($for=='home') {
$list = $dbPages->getList($pageNumber, $Site->postsPerPage(), true);
}
// There are not items for the page number then set the page notfound
if( empty($list) && $pageNumber>0 ) {
$Url->setNotFound(true);
}
$pages = array();
foreach($list as $pageKey=>$fields) {
$page = buildPage($pageKey);
if($page!==false) {
array_push($pages, $page);
}
}
return $pages;
} }
// ---- OLD // ---- OLD
// POST FUNCTIONS
// ----------------------------------------------------------------------------
function reIndexTagsPosts()
{
global $dbPosts;
global $dbTags;
// Remove unpublished.
$dbPosts->removeUnpublished();
// Regenerate the tags index for posts.
$dbTags->reindexPosts( $dbPosts->db );
// Restore the database, before remove the unpublished.
$dbPosts->restoreDB();
return true;
}
function reIndexCategoriesPosts()
{
global $dbPosts;
global $dbCategories;
// Remove unpublished.
$dbPosts->removeUnpublished();
// Regenerate the tags index for posts.
$dbCategories->reindexPosts( $dbPosts->db );
// Restore the database, before remove the unpublished.
$dbPosts->restoreDB();
return true;
}
function buildPost($key)
{
global $dbPosts;
global $dbUsers;
global $Parsedown;
global $Site;
// Post object, content from FILE.
$Post = new Post($key);
if( !$Post->isValid() ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the post from file with key: '.$key);
return false;
}
// Post database, content from DATABASE JSON.
$db = $dbPosts->getPostDB($key);
if( !$db ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the post from database with key: '.$key);
return false;
}
// Foreach field from DATABASE.
foreach($db as $field=>$value) {
$Post->setField($field, $value);
}
// Content in raw format
$contentRaw = $Post->content();
$Post->setField('contentRaw', $contentRaw, true);
// Parse the content
$content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities
$content = $Parsedown->text($content); // Parse Markdown.
$content = Text::imgRel2Abs($content, HTML_PATH_UPLOADS); // Parse img src relative to absolute.
$Post->setField('content', $content, true);
// Pagebrake
$explode = explode(PAGE_BREAK, $content);
$Post->setField('breakContent', $explode[0], true);
$Post->setField('readMore', !empty($explode[1]), true);
// Date format
$postDate = $Post->date();
$Post->setField('dateRaw', $postDate, true);
$postDateFormated = $Post->dateRaw( $Site->dateFormat() );
$Post->setField('date', $postDateFormated, true);
// User object
$username = $Post->username();
$Post->setField('user', $dbUsers->getUser($username));
return $Post;
}
function buildPostsForPage($pageNumber=0, $amount=POSTS_PER_PAGE_ADMIN, $removeUnpublished=true, $key=false, $type='tag') function buildPostsForPage($pageNumber=0, $amount=POSTS_PER_PAGE_ADMIN, $removeUnpublished=true, $key=false, $type='tag')
{ {
@ -199,19 +171,6 @@ function sortPages($a, $b)
return ($a['position'] < $b['position']) ? -1 : 1; return ($a['position'] < $b['position']) ? -1 : 1;
} }
function reIndexCategoriesPages()
{
global $dbPages;
global $dbCategories;
// Regenerate the tags index for posts.
$dbCategories->reindexPages( $dbPages->db );;
return true;
}
function buildAllPages() function buildAllPages()
{ {
global $pagesParents; global $pagesParents;

View File

@ -56,7 +56,7 @@ class Page {
} }
$implode = implode($output); $implode = implode($output);
$this->vars['content'] = $implode; $this->vars['contentRaw'] = $implode;
} }
} }
@ -96,7 +96,7 @@ class Page {
$content = $this->getField('content'); $content = $this->getField('content');
if(!$fullContent) { if(!$fullContent) {
$content = $this->getField('breakContent'); return $this->contentBreak();
} }
if($noSanitize) { if($noSanitize) {
@ -106,6 +106,11 @@ class Page {
return Sanitize::html($content); return Sanitize::html($content);
} }
public function contentBreak()
{
return $this->getField('contentBreak');
}
// Returns the raw content // Returns the raw content
// This content is not markdown parser // This content is not markdown parser
// (boolean) $noSanitize, TRUE returns the content without sanitized // (boolean) $noSanitize, TRUE returns the content without sanitized