Skip to content

Commit

Permalink
new error_logging functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
finanalyst committed Jun 11, 2018
1 parent baecefd commit 281a4c0
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 67 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# v1.2.4
## 10 June 2018
1. [](#bugfix)
* Allow for WHERE to be optional in `sql-insert`, but set to `WHERE = 1` when not explicitly set.
# v1.3.0
## 11 June 2018
1. [](#enhancement)
* Add `ignore` field in `sql-insert` and `sql-update`
* Add `error_logging` configuration option, so that SQL errors are written to hard drive

# v1.2.3
## 7 June 2018
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ Here is the default configuration and an explanation of available options:
enabled: true
database_route: data
database_name: db.sqlite3
error_logging: false
```
- `enabled` turns on the plugin for the whole site. If `false`, then making it active on a page will have no effect.
- `database_route` is the Grav route (relative to the 'user' subdirectory) to the location of the `SQLite3` database.
- `database_name` is the full name (typically with the extension .sqlite3) of the database file. It is the responsibility of the site developer/maintainer to create the database.
- `error_logging` when false, nothing extra happens. When `true`, the SQL errors are logged to the file `sqlite_errors.txt` in the directory given by `database_route`. If however there is an error in setting `database_route`,
then the directory is `user/data`

>NOTE: The database must exist.
>NOTE: The database must exist. If it does not, then an error is generated.
`error_logging` should not be used in production settings as it writes to the hard drive, slowing performance.

### Per page configuration
Shortcodes can be enabled separately using the `shortcode-core` configuration. To disable shortcodes being used on all pages, but only used on selected pages, configure the shortcode-core plugin inside the Admin panel with `enabled=true` and `active=false`. Then on each page where shortcodes are used, include in the front section of the page:
Expand All @@ -60,6 +64,10 @@ A shortcode and a Form action are provided.

When the plugin is first initialised, it verifies that the database exists. If it does not exist, then every instance of the `[sql-table]` shortcode is replaced with an error message and the Form generates an error message when the submit button is pressed.

>NOTE: Although as many errors as possible are trapped, it should be remembered that GRAV uses redirects
extensively, eg., in login forms or sequential forms, which means the error messages may be overwritten. If
this happens, then set `error_logging` to **true** whilst debugging.

### [sql-table] Shortcode

In the page content the shortcode is used as:
Expand Down Expand Up @@ -134,7 +142,6 @@ process:
twig: true
```


#### Options
The following options are allowed:
- class
Expand Down Expand Up @@ -322,7 +329,7 @@ For this to work, Twig processing in headers needs to be set for the site.
The following fields are defined for these two **Form** processes:

1. `table` - This is mandatory, and is the table to which the sql stanza is applied.
1. `where` - This is mandatory for `sql-update` & optional for `sql-insert` (the string `WHERE 1` is appended by default).
1. `where` - This is mandatory for `sql-update` & ignored for `sql-insert`.
1. `ignore` - This is optional for both. It is followed by an array of field names that are not included in the stanza. Eg.
```yaml
form:
Expand All @@ -345,4 +352,3 @@ Alternatively, using the `Private` plugin, a password can be created for the pag

## To Do
- Internationalise. Add more languages to `langages.yaml`
- Create a `confirm` option for the Form interface so that data is confirmed before being sent to the database.
43 changes: 27 additions & 16 deletions blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
name: Sqlite
version: '1.2.4'
description: Plugin to select and insert into and sqlite3 database
version: '1.3.0'
description: Plugin to select, update and insert into an sqlite3 database
icon: database
author:
name: Richard N Hainsworth
email: rnhainsworth@gmail.com
name: Richard N Hainsworth
email: rnhainsworth@gmail.com
homepage: https://github.com/finanalyst/grav-plugin-sqlite
bugs: https://github.com/finanalyst/grav-plugin-sqlite/issues
keywords: [sqlite, grav, plugin]
readme: https://github.com/finanalyst/grav-plugin-sqlite/README.md
license: MIT

dependencies:
- shortcode-core
- shortcode-core

form:
fields:
database_name:
type: text
default: db.sqlite3
label: PLUGIN_SQLITE.DATABASE_NAME
help: PLUGIN_SQLITE.DATABASE_NAME_HELP
database_route:
type: text
default: data
label: PLUGIN_SQLITE.DATABASE_ROUTE
help: PLUGIN_SQLITE.DATABASE_ROUTE_HELP
fields:
database_name:
type: text
default: db.sqlite3
label: PLUGIN_SQLITE.DATABASE_NAME
help: PLUGIN_SQLITE.DATABASE_NAME_HELP
database_route:
type: text
default: data
label: PLUGIN_SQLITE.DATABASE_ROUTE
help: PLUGIN_SQLITE.DATABASE_ROUTE_HELP
error_logging:
type: toggle
highlight: 0
default: 0
options:
1: Enabled
0: Disabled
validate:
type: bool
label: PLUGIN_SQLITE.ERROR_LOGGING
help: PLUGIN_SQLITE.ERROR_LOGGING_HELP
36 changes: 19 additions & 17 deletions languages.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
en:
PLUGIN_SQLITE:
DATABASE_ERROR: The database file "%s" does not exist.
UNIQUE_FIELD_ERROR: <br>One (or more) of the fields is required to be UNIQUE, but it already exists in the database.<br>Is the same data being added again?
OTHER_SQL_ERROR: <br>The form data is causing a database error. Contact the site developer.
DATABASE_NAME: Name of database
DATABASE_NAME_HELP: The name of the sqlite3 file with extention
DATABASE_ROUTE: Route to database
DATABASE_ROUTE_HELP: The GRAV route to the database (relative to the 'user' directory)
DATABASE_ERROR_TITLE: Database Error
FILE_ERROR_1: The file %s%s%s does not exist. Check that:
FILE_ERROR_2: the database file exists in the correct location,
FILE_ERROR_3: the %slocation%s and %sfilename%s are correct in the sqlite plugin configuration file.
SQL_ERROR_1: SQL Error
SQL_ERROR_2: The SQL statement between the shortcode, viz.
SQL_ERROR_3: generates the error.
UPDATE_WHERE: A where expression is mandatory (either as a parameter or as a form field) when using an "sql-update" Form action.
UPDATE_ERROR: The following UPDATE error was generated:<BR>%s
PLUGIN_SQLITE:
DATABASE_ERROR: The database file "%s" does not exist.
UNIQUE_FIELD_ERROR: <br>One (or more) of the fields is required to be UNIQUE, but it already exists in the database.<br>Is the same data being added again?
OTHER_SQL_ERROR: <br>The form data is causing a database error. Contact the site developer.
DATABASE_NAME: Name of database
DATABASE_NAME_HELP: The name of the sqlite3 file with extention
DATABASE_ROUTE: Route to database
DATABASE_ROUTE_HELP: The GRAV route to the database (relative to the 'user' directory)
DATABASE_ERROR_TITLE: Database Error
FILE_ERROR_1: The file %s%s%s does not exist. Check that:
FILE_ERROR_2: the database file exists in the correct location,
FILE_ERROR_3: the %slocation%s and %sfilename%s are correct in the sqlite plugin configuration file.
SQL_ERROR_1: SQL Error
SQL_ERROR_2: The SQL statement between the shortcode, viz.
SQL_ERROR_3: generates the error.
UPDATE_WHERE: A where expression is mandatory (either as a parameter or as a form field) when using an "sql-update" Form action.
UPDATE_ERROR: The following UPDATE error was generated:<BR>%s
ERROR_LOGGING: Turn on Logging. (Not for production!!)
ERROR_LOGGING_HELP: Log file is saved when an error is detected in DATABASE_ROUTE, or /user/data if former fails.
38 changes: 28 additions & 10 deletions shortcodes/SqlTableShortcode.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Grav\Plugin\Shortcodes;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use RocketTheme\Toolbox\File\File;
use SQLite3;

class SqlTableShortcode extends Shortcode
Expand All @@ -9,6 +10,9 @@ public function init()
{
$this->shortcode->getHandlers()->add('sql-table', function(ShortcodeInterface $sc) {
if ( isset($this->grav['sqlite']['error']) && $this->grav['sqlite']['error'] ) {
if ($this->grav['sqlite']['logging']) {
$this->log_error($this->grav['sqlite']['error']);
}
return
$this->twig->processTemplate(
'partials/sql-db-error.html.twig',
Expand Down Expand Up @@ -58,14 +62,28 @@ public function init()
}
return $output;
} catch( \Exception $e) {
return
$this->twig->processTemplate(
'partials/sql-sql-error.html.twig',
[
'message' => $e->getMessage(),
'content' => $stanza
]);
}
});
}
if ($this->grav['sqlite']['logging']) {
$this->log_error('message: ' . $e->getMessage() . "\ncontent: $stanza");
}
return
$this->twig->processTemplate(
'partials/sql-sql-error.html.twig',
[
'message' => $e->getMessage(),
'content' => $stanza
]);
}
});
}

public function log_error(String $msg) {
$path = $this->grav['sqlite']['path'] . 'sqlite_errors.txt';
$datafh = File::instance($path);
if ( file_exists($path) ) {
$datafh->save($datafh->content() . "\n" . $msg);
} else {
$datafh->save($msg);
chmod($path, 0666);
}
}
}
52 changes: 36 additions & 16 deletions sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\File\File;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use SQLite3;

Expand All @@ -27,12 +28,15 @@ public function onPluginsInitialized()
$route = $this->config->get('plugins.sqlite.database_route');
$dbname = $this->config->get('plugins.sqlite.database_name');
$path = $this->grav['locator']->findResource("user://$route", true);
// path is also used for error logging, so there must be a valid route in case user supplied route fails.
$this->sqlite['path'] = $path?: $this->grav['locator']->findResource("user://data", true);
$this->sqlite['logging'] = $this->config->get('plugins.sqlite.error_logging');
$dbloc = $path . DS . $dbname;
if ( file_exists($dbloc) ) {
$this->sqlite['db'] = new SQLite3($dbloc);
$this->sqlite['db']->enableExceptions(true);
$this->sqlite['db'] = new SQLite3($dbloc);
$this->sqlite['db']->enableExceptions(true);
} else {
$this->sqlite['error'] = "user://$route/$dbname";
$this->sqlite['error'] = "No database found at --user://$route/$dbname--";
}
$this->grav['sqlite'] = $this->sqlite;
$this->enable([
Expand All @@ -58,6 +62,9 @@ public function onShortcodeHandlers(Event $e)
public function onFormProcessed(Event $event)
{
if ( isset($this->grav['sqlite']['error']) && $this->grav['sqlite']['error'] ) {
if ($this->grav['sqlite']['logging']) {
$this->log_error($this->grav['sqlite']['error']);
}
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $event['form'],
'message' => sprintf($this->grav['language']->translate(['PLUGIN_SQLITE.DATABASE_ERROR']), $this->grav['sqlite']['error'])
Expand All @@ -84,24 +91,17 @@ public function onFormProcessed(Event $event)
$values .= ( $nxt ? ',' : '' ) . '"' . $value . '"' ;
$nxt = true;
}
$where = '1'; // default
if (isset($data['where'])) {
// priority to where in form
$where = $data['where'];
unset($data['where']); // dont want it polluting UPDATE as a field
} elseif (isset($params['where'])) { // where is optional for insert, so skip if not set. Then where=1
$where = $params['where'];
unset($data['where']); // dont want it polluting UPDATE as a field. Should be ignored
}
// allows for use of inpage twig
$where = $this->grav['twig']->processString($where);
$set = 'SET ';
$nxt = false;
foreach ( $data as $field => $value ) {
$set .= ( $nxt ? ', ' : '') ;
$set .= $field . '="' . $value . '"' ;
$nxt = true;
}
$sql ="INSERT INTO {$params['table']} ( $fields ) VALUES ( $values ) WHERE $where";
$sql ="INSERT INTO {$params['table']} ( $fields ) VALUES ( $values )";
$db = $this->grav['sqlite']['db'];
try {
$db->exec($sql) ;
Expand All @@ -112,7 +112,10 @@ public function onFormProcessed(Event $event)
} else {
$msg .= $this->grav['language']->translate(['PLUGIN_SQLITE.OTHER_SQL_ERROR']) . "<BR>$sql";
}
$this->grav->fireEvent('onFormValidationError', new Event([
if ($this->grav['sqlite']['logging']) {
$this->log_error($msg);
}
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $event['form'],
'message' => $msg
]));
Expand All @@ -126,6 +129,9 @@ public function onFormProcessed(Event $event)
}
if ( ! isset( $params['where'] ) and ! isset($data['where'])) {
// where expression is mandatory, so fail if not set
if ($this->grav['sqlite']['logging']) {
$this->log_error($this->grav['language']->translate(['PLUGIN_SQLITE.UPDATE_WHERE']));
}
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $event['form'],
'message' => $this->grav['language']->translate(['PLUGIN_SQLITE.UPDATE_WHERE'])
Expand All @@ -145,16 +151,19 @@ public function onFormProcessed(Event $event)
$set = 'SET ';
$nxt = false;
foreach ( $data as $field => $value ) {
$set .= ( $nxt ? ', ' : '') ;
$set .= $field . '="' . $value . '"' ;
$nxt = true;
$set .= ( $nxt ? ', ' : '') ;
$set .= $field . '="' . $value . '"' ;
$nxt = true;
}

$sql ="UPDATE {$params['table']} $set WHERE $where";
$db = $this->grav['sqlite']['db'];
try {
$db->exec($sql) ;
} catch ( \Exception $e ) {
if ($this->grav['sqlite']['logging']) {
$this->log_error(sprintf($this->grav['language']->translate(['PLUGIN_SQLITE.UPDATE_ERROR']),$e->getMessage()));
}
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $event['form'],
'message' => sprintf($this->grav['language']->translate(['PLUGIN_SQLITE.UPDATE_ERROR']),$e->getMessage())
Expand All @@ -164,4 +173,15 @@ public function onFormProcessed(Event $event)
break;
}
}

public function log_error(String $msg) {
$path = $this->grav['sqlite']['path'] . 'sqlite_errors.txt';
$datafh = File::instance($path);
if ( file_exists($path) ) {
$datafh->save($datafh->content() . "\n" . $msg);
} else {
$datafh->save($msg);
chmod($path, 0666);
}
}
}
1 change: 1 addition & 0 deletions sqlite.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
enabled: true
database_route: data
database_name: db.sqlite3
error_logging: false

0 comments on commit 281a4c0

Please sign in to comment.