Bug fixes for API plugin

This commit is contained in:
Diego Najar 2019-02-08 08:53:26 +01:00
parent 56eb4a3a93
commit 78cacae128
3 changed files with 22 additions and 37 deletions

View File

@ -77,14 +77,11 @@ class Pages extends dbJSON {
// Content // Content
// This variable is not belong to the database so is not defined in $row // This variable is not belong to the database so is not defined in $row
$contentRaw = $args['content']; $contentRaw = (empty($args['content'])?'':$args['content']);
// Parent // Parent
// This variable is not belong to the database so is not defined in $row // This variable is not belong to the database so is not defined in $row
$parent = ''; $parent = (empty($args['parent'])?'':$args['parent']);
if (!empty($args['parent'])) {
$parent = $args['parent'];
}
// Slug from the title or the content // Slug from the title or the content
// This variable is not belong to the database so is not defined in $row // This variable is not belong to the database so is not defined in $row
@ -118,13 +115,13 @@ class Pages extends dbJSON {
} }
// Create the directory // Create the directory
if( Filesystem::mkdir(PATH_PAGES.$key, true) === false ) { if (Filesystem::mkdir(PATH_PAGES.$key, true) === false) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the directory ['.PATH_PAGES.$key.']',LOG_TYPE_ERROR); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the directory ['.PATH_PAGES.$key.']',LOG_TYPE_ERROR);
return false; return false;
} }
// Create the index.txt and save the file // Create the index.txt and save the file
if( file_put_contents(PATH_PAGES.$key.DS.FILENAME, $contentRaw) === false ) { if (file_put_contents(PATH_PAGES.$key.DS.FILENAME, $contentRaw) === false) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the content in the file ['.FILENAME.']',LOG_TYPE_ERROR); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the content in the file ['.FILENAME.']',LOG_TYPE_ERROR);
return false; return false;
} }
@ -149,7 +146,6 @@ class Pages extends dbJSON {
// Old key // Old key
// This variable is not belong to the database so is not defined in $row // This variable is not belong to the database so is not defined in $row
$key = $args['key']; $key = $args['key'];
$row = array(); $row = array();
// Check values on args or set default values // Check values on args or set default values
foreach ($this->dbFields as $field=>$value) { foreach ($this->dbFields as $field=>$value) {
@ -172,23 +168,19 @@ class Pages extends dbJSON {
// Content // Content
// This variable is not belong to the database so is not defined in $row // This variable is not belong to the database so is not defined in $row
$contentRaw = $args['content']; $contentRaw = (empty($args['content'])?'':$args['content']);
// Parent // Parent
// This variable is not belong to the database so is not defined in $row // This variable is not belong to the database so is not defined in $row
$parent = ''; $parent = (empty($args['parent'])?'':$args['parent']);
if (!empty($args['parent'])) {
$parent = $args['parent'];
}
// Slug from the title or the content // Slug
// If the user change the slug the page key changes
// If the user send an empty slug the page key doesn't need to change
// This variable is not belong to the database so is not defined in $row // This variable is not belong to the database so is not defined in $row
if (empty($args['slug'])) { if (empty($args['slug'])) {
if (!empty($row['title'])) { $explode = explode('/', $key);
$slug = $this->generateSlug($row['title']); $slug = end($explode);
} else {
$slug = $this->generateSlug($contentRaw);
}
} else { } else {
$slug = $args['slug']; $slug = $args['slug'];
} }
@ -214,7 +206,7 @@ class Pages extends dbJSON {
// Move the directory from old key to new key. // Move the directory from old key to new key.
if ($newKey!==$key) { if ($newKey!==$key) {
if( Filesystem::mv(PATH_PAGES.$key, PATH_PAGES.$newKey) === false ) { if (Filesystem::mv(PATH_PAGES.$key, PATH_PAGES.$newKey) === false) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to move the directory to '.PATH_PAGES.$newKey); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to move the directory to '.PATH_PAGES.$newKey);
return false; return false;
} }
@ -227,7 +219,7 @@ class Pages extends dbJSON {
} }
// Remove the old key // Remove the old key
unset ($this->db[$key]); unset($this->db[$key]);
// Reindex Orphan Children // Reindex Orphan Children
$this->reindexChildren($key, $newKey); $this->reindexChildren($key, $newKey);
@ -669,14 +661,12 @@ class Pages extends dbJSON {
if ($newKey!==$oldKey) { if ($newKey!==$oldKey) {
// Verify if the key is already been used // Verify if the key is already been used
if( isset($this->db[$newKey]) ) { if (isset($this->db[$newKey])) {
if( !Text::endsWithNumeric($newKey) ) { $i = 0;
$newKey = $newKey.'-0'; while (isset($this->db[$newKey.'-'.$i])) {
} $i++;
while( isset($this->db[$newKey]) ) {
$newKey++;
} }
$newKey = $newKey.'-'.$i;
} }
} }

View File

@ -416,12 +416,7 @@ class Page {
public function slug() public function slug()
{ {
$explode = explode('/', $this->key()); $explode = explode('/', $this->key());
return end($explode);
// Remove the parent
if (!empty($explode[1])) {
return $explode[1];
}
return $explode[0];
} }
// Returns the parent key, if the page doesn't have a parent returns FALSE // Returns the parent key, if the page doesn't have a parent returns FALSE

View File

@ -69,7 +69,7 @@ class pluginAPI extends Plugin {
$inputs = $this->getMethodInputs(); $inputs = $this->getMethodInputs();
if ( empty($inputs) ) { if ( empty($inputs) ) {
$this->response(404, 'Not Found', array('message'=>'Missing method inputs.')); $this->response(400, 'Bad Request', array('message'=>'Missing method inputs.'));
} }
// ENDPOINT PARAMETERS // ENDPOINT PARAMETERS
@ -77,7 +77,7 @@ class pluginAPI extends Plugin {
$parameters = $this->getEndpointParameters($URI); $parameters = $this->getEndpointParameters($URI);
if ( empty($parameters) ) { if ( empty($parameters) ) {
$this->response(404, 'Not Found', array('message'=>'Missing endpoint parameters.')); $this->response(400, 'Bad Request', array('message'=>'Missing endpoint parameters.'));
} }
// API TOKEN // API TOKEN
@ -87,7 +87,7 @@ class pluginAPI extends Plugin {
// Check empty token // Check empty token
if (empty($inputs['token'])) { if (empty($inputs['token'])) {
$this->response(404, 'Not Found', array('message'=>'Missing API token.')); $this->response(400, 'Bad Request', array('message'=>'Missing API token.'));
} }
// Check if the token is valid // Check if the token is valid