diff options
author | ArthurHoaro <arthur@hoa.ro> | 2016-10-14 13:22:58 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-10-14 13:22:58 +0200 |
commit | 7fde6de1212323418401c15efba06026c704ca87 (patch) | |
tree | 63b7efe598d1c563eee939fe9be3505afff1d873 | |
parent | f63632a6fb84594b8aeacb387a5cd17b4a841d2c (diff) | |
download | Shaarli-7fde6de1212323418401c15efba06026c704ca87.tar.gz Shaarli-7fde6de1212323418401c15efba06026c704ca87.tar.zst Shaarli-7fde6de1212323418401c15efba06026c704ca87.zip |
New init function for plugins, supports errors reporting
All plugins can optionally add an init function named `pluginname_init()` which is called when the plugin is loaded.
This function is aware of the config, and can return initialization errors, which are displayed in the header template.
Note that the previous error system hack no longer work.
-rw-r--r-- | application/PageBuilder.php | 3 | ||||
-rw-r--r-- | application/PluginManager.php | 25 | ||||
-rw-r--r-- | index.php | 1 | ||||
-rw-r--r-- | plugins/demo_plugin/demo_plugin.php | 17 | ||||
-rw-r--r-- | plugins/readityourself/readityourself.php | 19 | ||||
-rw-r--r-- | plugins/wallabag/wallabag.php | 19 | ||||
-rw-r--r-- | tests/plugins/PluginReadityourselfTest.php | 23 | ||||
-rw-r--r-- | tests/plugins/PluginWallabagTest.php | 23 |
8 files changed, 115 insertions, 15 deletions
diff --git a/application/PageBuilder.php b/application/PageBuilder.php index 42932f32..32c7f9f1 100644 --- a/application/PageBuilder.php +++ b/application/PageBuilder.php | |||
@@ -77,9 +77,6 @@ class PageBuilder | |||
77 | $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false)); | 77 | $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false)); |
78 | $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', false)); | 78 | $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', false)); |
79 | $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false)); | 79 | $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false)); |
80 | if (!empty($GLOBALS['plugin_errors'])) { | ||
81 | $this->tpl->assign('plugin_errors', $GLOBALS['plugin_errors']); | ||
82 | } | ||
83 | $this->tpl->assign('token', getToken($this->conf)); | 80 | $this->tpl->assign('token', getToken($this->conf)); |
84 | // To be removed with a proper theme configuration. | 81 | // To be removed with a proper theme configuration. |
85 | $this->tpl->assign('conf', $this->conf); | 82 | $this->tpl->assign('conf', $this->conf); |
diff --git a/application/PluginManager.php b/application/PluginManager.php index 1e132a7f..59ece4fa 100644 --- a/application/PluginManager.php +++ b/application/PluginManager.php | |||
@@ -25,6 +25,11 @@ class PluginManager | |||
25 | protected $conf; | 25 | protected $conf; |
26 | 26 | ||
27 | /** | 27 | /** |
28 | * @var array List of plugin errors. | ||
29 | */ | ||
30 | protected $errors; | ||
31 | |||
32 | /** | ||
28 | * Plugins subdirectory. | 33 | * Plugins subdirectory. |
29 | * @var string $PLUGINS_PATH | 34 | * @var string $PLUGINS_PATH |
30 | */ | 35 | */ |
@@ -44,6 +49,7 @@ class PluginManager | |||
44 | public function __construct(&$conf) | 49 | public function __construct(&$conf) |
45 | { | 50 | { |
46 | $this->conf = $conf; | 51 | $this->conf = $conf; |
52 | $this->errors = array(); | ||
47 | } | 53 | } |
48 | 54 | ||
49 | /** | 55 | /** |
@@ -106,6 +112,7 @@ class PluginManager | |||
106 | 112 | ||
107 | /** | 113 | /** |
108 | * Load a single plugin from its files. | 114 | * Load a single plugin from its files. |
115 | * Call the init function if it exists, and collect errors. | ||
109 | * Add them in $loadedPlugins if successful. | 116 | * Add them in $loadedPlugins if successful. |
110 | * | 117 | * |
111 | * @param string $dir plugin's directory. | 118 | * @param string $dir plugin's directory. |
@@ -128,6 +135,14 @@ class PluginManager | |||
128 | $conf = $this->conf; | 135 | $conf = $this->conf; |
129 | include_once $pluginFilePath; | 136 | include_once $pluginFilePath; |
130 | 137 | ||
138 | $initFunction = $pluginName . '_init'; | ||
139 | if (function_exists($initFunction)) { | ||
140 | $errors = call_user_func($initFunction, $this->conf); | ||
141 | if (!empty($errors)) { | ||
142 | $this->errors = array_merge($this->errors, $errors); | ||
143 | } | ||
144 | } | ||
145 | |||
131 | $this->loadedPlugins[] = $pluginName; | 146 | $this->loadedPlugins[] = $pluginName; |
132 | } | 147 | } |
133 | 148 | ||
@@ -195,6 +210,16 @@ class PluginManager | |||
195 | 210 | ||
196 | return $metaData; | 211 | return $metaData; |
197 | } | 212 | } |
213 | |||
214 | /** | ||
215 | * Return the list of encountered errors. | ||
216 | * | ||
217 | * @return array List of errors (empty array if none exists). | ||
218 | */ | ||
219 | public function getErrors() | ||
220 | { | ||
221 | return $this->errors; | ||
222 | } | ||
198 | } | 223 | } |
199 | 224 | ||
200 | /** | 225 | /** |
@@ -778,6 +778,7 @@ function renderPage($conf, $pluginManager) | |||
778 | $PAGE = new PageBuilder($conf); | 778 | $PAGE = new PageBuilder($conf); |
779 | $PAGE->assign('linkcount', count($LINKSDB)); | 779 | $PAGE->assign('linkcount', count($LINKSDB)); |
780 | $PAGE->assign('privateLinkcount', count_private($LINKSDB)); | 780 | $PAGE->assign('privateLinkcount', count_private($LINKSDB)); |
781 | $PAGE->assign('plugin_errors', $pluginManager->getErrors()); | ||
781 | 782 | ||
782 | // Determine which page will be rendered. | 783 | // Determine which page will be rendered. |
783 | $query = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : ''; | 784 | $query = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : ''; |
diff --git a/plugins/demo_plugin/demo_plugin.php b/plugins/demo_plugin/demo_plugin.php index 18834e53..7335c9d4 100644 --- a/plugins/demo_plugin/demo_plugin.php +++ b/plugins/demo_plugin/demo_plugin.php | |||
@@ -15,6 +15,23 @@ | |||
15 | */ | 15 | */ |
16 | 16 | ||
17 | /** | 17 | /** |
18 | * Initialization function. | ||
19 | * It will be called when the plugin is loaded. | ||
20 | * This function can be used to return a list of initialization errors. | ||
21 | * | ||
22 | * @param $conf ConfigManager instance. | ||
23 | * | ||
24 | * @return array List of errors (optional). | ||
25 | */ | ||
26 | function demo_plugin_init($conf) | ||
27 | { | ||
28 | $conf->get('toto', 'nope'); | ||
29 | |||
30 | $errors[] = 'This a demo init error.'; | ||
31 | return $errors; | ||
32 | } | ||
33 | |||
34 | /** | ||
18 | * Hook render_header. | 35 | * Hook render_header. |
19 | * Executed on every page redering. | 36 | * Executed on every page redering. |
20 | * | 37 | * |
diff --git a/plugins/readityourself/readityourself.php b/plugins/readityourself/readityourself.php index 4bfcf501..961c5bda 100644 --- a/plugins/readityourself/readityourself.php +++ b/plugins/readityourself/readityourself.php | |||
@@ -8,10 +8,21 @@ | |||
8 | // it seems kinda dead. | 8 | // it seems kinda dead. |
9 | // Not tested. | 9 | // Not tested. |
10 | 10 | ||
11 | $riyUrl = $conf->get('plugins.READITYOUSELF_URL'); | 11 | /** |
12 | if (empty($riyUrl)) { | 12 | * Init function, return an error if the server is not set. |
13 | $GLOBALS['plugin_errors'][] = 'Readityourself plugin error: '. | 13 | * |
14 | 'Please define the "READITYOUSELF_URL" setting in the plugin administration page.'; | 14 | * @param $conf ConfigManager instance. |
15 | * | ||
16 | * @return array Eventual error. | ||
17 | */ | ||
18 | function readityourself_init($conf) | ||
19 | { | ||
20 | $riyUrl = $conf->get('plugins.READITYOUSELF_URL'); | ||
21 | if (empty($riyUrl)) { | ||
22 | $error = 'Readityourself plugin error: '. | ||
23 | 'Please define the "READITYOUSELF_URL" setting in the plugin administration page.'; | ||
24 | return array($error); | ||
25 | } | ||
15 | } | 26 | } |
16 | 27 | ||
17 | /** | 28 | /** |
diff --git a/plugins/wallabag/wallabag.php b/plugins/wallabag/wallabag.php index ec09c8a1..641e4cc2 100644 --- a/plugins/wallabag/wallabag.php +++ b/plugins/wallabag/wallabag.php | |||
@@ -6,10 +6,21 @@ | |||
6 | 6 | ||
7 | require_once 'WallabagInstance.php'; | 7 | require_once 'WallabagInstance.php'; |
8 | 8 | ||
9 | $wallabagUrl = $conf->get('plugins.WALLABAG_URL'); | 9 | /** |
10 | if (empty($wallabagUrl)) { | 10 | * Init function, return an error if the server is not set. |
11 | $GLOBALS['plugin_errors'][] = 'Wallabag plugin error: '. | 11 | * |
12 | 'Please define the "WALLABAG_URL" setting in the plugin administration page.'; | 12 | * @param $conf ConfigManager instance. |
13 | * | ||
14 | * @return array Eventual error. | ||
15 | */ | ||
16 | function wallabag_init($conf) | ||
17 | { | ||
18 | $wallabagUrl = $conf->get('plugins.WALLABAG_URL'); | ||
19 | if (empty($wallabagUrl)) { | ||
20 | $error = 'Wallabag plugin error: '. | ||
21 | 'Please define the "WALLABAG_URL" setting in the plugin administration page.'; | ||
22 | return array($error); | ||
23 | } | ||
13 | } | 24 | } |
14 | 25 | ||
15 | /** | 26 | /** |
diff --git a/tests/plugins/PluginReadityourselfTest.php b/tests/plugins/PluginReadityourselfTest.php index d73e666a..532db146 100644 --- a/tests/plugins/PluginReadityourselfTest.php +++ b/tests/plugins/PluginReadityourselfTest.php | |||
@@ -4,8 +4,6 @@ | |||
4 | * PluginReadityourselfTest.php.php | 4 | * PluginReadityourselfTest.php.php |
5 | */ | 5 | */ |
6 | 6 | ||
7 | // FIXME! add an init method. | ||
8 | $conf = new ConfigManager(''); | ||
9 | require_once 'plugins/readityourself/readityourself.php'; | 7 | require_once 'plugins/readityourself/readityourself.php'; |
10 | 8 | ||
11 | /** | 9 | /** |
@@ -23,6 +21,27 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase | |||
23 | } | 21 | } |
24 | 22 | ||
25 | /** | 23 | /** |
24 | * Test Readityourself init without errors. | ||
25 | */ | ||
26 | function testReadityourselfInitNoError() | ||
27 | { | ||
28 | $conf = new ConfigManager(''); | ||
29 | $conf->set('plugins.READITYOUSELF_URL', 'value'); | ||
30 | $errors = readityourself_init($conf); | ||
31 | $this->assertEmpty($errors); | ||
32 | } | ||
33 | |||
34 | /** | ||
35 | * Test Readityourself init with errors. | ||
36 | */ | ||
37 | function testReadityourselfInitError() | ||
38 | { | ||
39 | $conf = new ConfigManager(''); | ||
40 | $errors = readityourself_init($conf); | ||
41 | $this->assertNotEmpty($errors); | ||
42 | } | ||
43 | |||
44 | /** | ||
26 | * Test render_linklist hook. | 45 | * Test render_linklist hook. |
27 | */ | 46 | */ |
28 | function testReadityourselfLinklist() | 47 | function testReadityourselfLinklist() |
diff --git a/tests/plugins/PluginWallabagTest.php b/tests/plugins/PluginWallabagTest.php index 302ee296..2c268cbd 100644 --- a/tests/plugins/PluginWallabagTest.php +++ b/tests/plugins/PluginWallabagTest.php | |||
@@ -4,8 +4,6 @@ | |||
4 | * PluginWallabagTest.php.php | 4 | * PluginWallabagTest.php.php |
5 | */ | 5 | */ |
6 | 6 | ||
7 | // FIXME! add an init method. | ||
8 | $conf = new ConfigManager(''); | ||
9 | require_once 'plugins/wallabag/wallabag.php'; | 7 | require_once 'plugins/wallabag/wallabag.php'; |
10 | 8 | ||
11 | /** | 9 | /** |
@@ -23,6 +21,27 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase | |||
23 | } | 21 | } |
24 | 22 | ||
25 | /** | 23 | /** |
24 | * Test wallabag init without errors. | ||
25 | */ | ||
26 | function testWallabagInitNoError() | ||
27 | { | ||
28 | $conf = new ConfigManager(''); | ||
29 | $conf->set('plugins.WALLABAG_URL', 'value'); | ||
30 | $errors = wallabag_init($conf); | ||
31 | $this->assertEmpty($errors); | ||
32 | } | ||
33 | |||
34 | /** | ||
35 | * Test wallabag init with errors. | ||
36 | */ | ||
37 | function testWallabagInitError() | ||
38 | { | ||
39 | $conf = new ConfigManager(''); | ||
40 | $errors = wallabag_init($conf); | ||
41 | $this->assertNotEmpty($errors); | ||
42 | } | ||
43 | |||
44 | /** | ||
26 | * Test render_linklist hook. | 45 | * Test render_linklist hook. |
27 | */ | 46 | */ |
28 | function testWallabagLinklist() | 47 | function testWallabagLinklist() |