summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2013-08-09 20:04:17 +0200
committerAndreas Gohr <andi@splitbrain.org>2013-08-09 20:04:17 +0200
commit519895b5625277197a88748c515919515f1113b8 (patch)
tree8fbc02ed6efb617d2ba7045232ed795fd7dfee05
parent61746d5540723d7463a0c1d666decb9c164c6678 (diff)
downloadrpg-519895b5625277197a88748c515919515f1113b8.tar.gz
rpg-519895b5625277197a88748c515919515f1113b8.tar.bz2
added tests for the find_folders method
-rw-r--r--lib/plugins/extension/_test/extension.test.php234
-rw-r--r--lib/plugins/extension/_test/testdata/either1/script.js0
-rw-r--r--lib/plugins/extension/_test/testdata/eithersub2/either2/script.js0
-rw-r--r--lib/plugins/extension/_test/testdata/plgfoo5/plugin.info.txt7
-rw-r--r--lib/plugins/extension/_test/testdata/plgsub3/plugin3/syntax.php0
-rw-r--r--lib/plugins/extension/_test/testdata/plgsub4/plugin4/plugin.info.txt7
-rw-r--r--lib/plugins/extension/_test/testdata/plgsub6/plgfoo6/plugin.info.txt7
-rw-r--r--lib/plugins/extension/_test/testdata/plugin1/syntax.php0
-rw-r--r--lib/plugins/extension/_test/testdata/plugin2/plugin.info.txt7
-rw-r--r--lib/plugins/extension/_test/testdata/template1/main.php0
-rw-r--r--lib/plugins/extension/_test/testdata/template1/style.ini0
-rw-r--r--lib/plugins/extension/_test/testdata/template2/template.info.txt7
-rw-r--r--lib/plugins/extension/_test/testdata/tplfoo5/template.info.txt7
-rw-r--r--lib/plugins/extension/_test/testdata/tplsub3/template3/main.php0
-rw-r--r--lib/plugins/extension/_test/testdata/tplsub3/template3/style.ini0
-rw-r--r--lib/plugins/extension/_test/testdata/tplsub4/template4/template.info.txt7
-rw-r--r--lib/plugins/extension/_test/testdata/tplsub6/tplfoo6/template.info.txt7
-rw-r--r--lib/plugins/extension/admin.php10
-rw-r--r--lib/plugins/extension/helper/extension.php190
19 files changed, 382 insertions, 108 deletions
diff --git a/lib/plugins/extension/_test/extension.test.php b/lib/plugins/extension/_test/extension.test.php
index 6dfa49a46..97726d212 100644
--- a/lib/plugins/extension/_test/extension.test.php
+++ b/lib/plugins/extension/_test/extension.test.php
@@ -1,10 +1,23 @@
<?php
+
+/**
+ * Class mock_helper_plugin_extension_extension
+ *
+ * makes protected methods accessible
+ */
+class mock_helper_plugin_extension_extension extends helper_plugin_extension_extension {
+ public function find_folders(&$result, $base, $default_type = 'plugin', $dir = '') {
+ return parent::find_folders($result, $base, $default_type, $dir);
+ }
+
+}
+
/**
* @group plugin_extension
*/
class helper_plugin_extension_extension_test extends DokuWikiTest {
- protected $pluginsEnabled = array('extension');
+ protected $pluginsEnabled = array('extension');
/**
* FIXME should we test this without internet first?
@@ -12,7 +25,6 @@ class helper_plugin_extension_extension_test extends DokuWikiTest {
* @group internet
*/
public function testExtensionParameters() {
-
$extension = new helper_plugin_extension_extension();
$extension->setExtension('extension');
@@ -56,7 +68,225 @@ class helper_plugin_extension_extension_test extends DokuWikiTest {
$this->assertTrue($extension->isEnabled());
$this->assertTrue($extension->isInstalled());
$this->assertTrue($extension->isBundled());
+ }
+
+ public function testFindFoldersPlugins() {
+ $extension = new mock_helper_plugin_extension_extension();
+ $tdir = dirname(__FILE__).'/testdata';
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/plugin1", 'plugin');
+ $this->assertTrue($ok);
+ $this->assertEquals(0, count($result['new']));
+ $this->assertEquals(1, count($result['old']));
+ $this->assertEquals('plugin', $result['old'][0]['type']);
+ $this->assertEquals('plugin1', $this->extdir($result['old'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/plugin2", 'plugin');
+ $this->assertTrue($ok);
+ $this->assertEquals(1, count($result['new']));
+ $this->assertEquals('plugin', $result['new'][0]['type']);
+ $this->assertEquals('plugin2', $result['new'][0]['base']);
+ $this->assertEquals('plugin2', $this->extdir($result['new'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/plgsub3", 'plugin');
+ $this->assertTrue($ok);
+ $this->assertEquals(0, count($result['new']));
+ $this->assertEquals(1, count($result['old']));
+ $this->assertEquals('plugin', $result['old'][0]['type']);
+ $this->assertEquals('plgsub3/plugin3', $this->extdir($result['old'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/plgsub4", 'plugin');
+ $this->assertTrue($ok);
+ $this->assertEquals(1, count($result['new']));
+ $this->assertEquals('plugin', $result['new'][0]['type']);
+ $this->assertEquals('plugin4', $result['new'][0]['base']);
+ $this->assertEquals('plgsub4/plugin4', $this->extdir($result['new'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/plgfoo5", 'plugin');
+ $this->assertTrue($ok);
+ $this->assertEquals(1, count($result['new']));
+ $this->assertEquals('plugin', $result['new'][0]['type']);
+ $this->assertEquals('plugin5', $result['new'][0]['base']);
+ $this->assertEquals('plgfoo5', $this->extdir($result['new'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/plgsub6/plgfoo6", 'plugin');
+ $this->assertTrue($ok);
+ $this->assertEquals(1, count($result['new']));
+ $this->assertEquals('plugin', $result['new'][0]['type']);
+ $this->assertEquals('plugin6', $result['new'][0]['base']);
+ $this->assertEquals('plgsub6/plgfoo6', $this->extdir($result['new'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/either1", 'plugin');
+ $this->assertTrue($ok);
+ $this->assertEquals(0, count($result['new']));
+ $this->assertEquals(1, count($result['old']));
+ $this->assertEquals('plugin', $result['old'][0]['type']);
+ $this->assertEquals('either1', $this->extdir($result['old'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/eithersub2/either2", 'plugin');
+ $this->assertTrue($ok);
+ $this->assertEquals(0, count($result['new']));
+ $this->assertEquals(1, count($result['old']));
+ $this->assertEquals('plugin', $result['old'][0]['type']);
+ $this->assertEquals('eithersub2/either2', $this->extdir($result['old'][0]['tmp']));
+ }
+
+ public function testFindFoldersTemplates() {
+ $extension = new mock_helper_plugin_extension_extension();
+ $tdir = dirname(__FILE__).'/testdata';
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/template1", 'template');
+ $this->assertTrue($ok);
+ $this->assertEquals(0, count($result['new']));
+ $this->assertEquals(1, count($result['old']));
+ $this->assertEquals('template', $result['old'][0]['type']);
+ $this->assertEquals('template1', $this->extdir($result['old'][0]['tmp']));
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/template2", 'template');
+ $this->assertTrue($ok);
+ $this->assertEquals(1, count($result['new']));
+ $this->assertEquals('template', $result['new'][0]['type']);
+ $this->assertEquals('template2', $result['new'][0]['base']);
+ $this->assertEquals('template2', $this->extdir($result['new'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/tplsub3", 'template');
+ $this->assertTrue($ok);
+ $this->assertEquals(0, count($result['new']));
+ $this->assertEquals(1, count($result['old']));
+ $this->assertEquals('template', $result['old'][0]['type']);
+ $this->assertEquals('tplsub3/template3', $this->extdir($result['old'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/tplsub4", 'template');
+ $this->assertTrue($ok);
+ $this->assertEquals(1, count($result['new']));
+ $this->assertEquals('template', $result['new'][0]['type']);
+ $this->assertEquals('template4', $result['new'][0]['base']);
+ $this->assertEquals('tplsub4/template4', $this->extdir($result['new'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/tplfoo5", 'template');
+ $this->assertTrue($ok);
+ $this->assertEquals(1, count($result['new']));
+ $this->assertEquals('template', $result['new'][0]['type']);
+ $this->assertEquals('template5', $result['new'][0]['base']);
+ $this->assertEquals('tplfoo5', $this->extdir($result['new'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/tplsub6/tplfoo6", 'template');
+ $this->assertTrue($ok);
+ $this->assertEquals(1, count($result['new']));
+ $this->assertEquals('template', $result['new'][0]['type']);
+ $this->assertEquals('template6', $result['new'][0]['base']);
+ $this->assertEquals('tplsub6/tplfoo6', $this->extdir($result['new'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/either1", 'template');
+ $this->assertTrue($ok);
+ $this->assertEquals(0, count($result['new']));
+ $this->assertEquals(1, count($result['old']));
+ $this->assertEquals('template', $result['old'][0]['type']);
+ $this->assertEquals('either1', $this->extdir($result['old'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/eithersub2/either2", 'template');
+ $this->assertTrue($ok);
+ $this->assertEquals(0, count($result['new']));
+ $this->assertEquals(1, count($result['old']));
+ $this->assertEquals('template', $result['old'][0]['type']);
+ $this->assertEquals('eithersub2/either2', $this->extdir($result['old'][0]['tmp']));
+ }
+
+ public function testFindFoldersTemplatesAutodetect() {
+ $extension = new mock_helper_plugin_extension_extension();
+ $tdir = dirname(__FILE__).'/testdata';
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/template1");
+ $this->assertTrue($ok);
+ $this->assertEquals(0, count($result['new']));
+ $this->assertEquals(1, count($result['old']));
+ $this->assertEquals('template', $result['old'][0]['type']);
+ $this->assertEquals('template1', $this->extdir($result['old'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/template2");
+ $this->assertTrue($ok);
+ $this->assertEquals(1, count($result['new']));
+ $this->assertEquals('template', $result['new'][0]['type']);
+ $this->assertEquals('template2', $result['new'][0]['base']);
+ $this->assertEquals('template2', $this->extdir($result['new'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/tplsub3");
+ $this->assertTrue($ok);
+ $this->assertEquals(0, count($result['new']));
+ $this->assertEquals(1, count($result['old']));
+ $this->assertEquals('template', $result['old'][0]['type']);
+ $this->assertEquals('tplsub3/template3', $this->extdir($result['old'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/tplsub4");
+ $this->assertTrue($ok);
+ $this->assertEquals(1, count($result['new']));
+ $this->assertEquals('template', $result['new'][0]['type']);
+ $this->assertEquals('template4', $result['new'][0]['base']);
+ $this->assertEquals('tplsub4/template4', $this->extdir($result['new'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/tplfoo5");
+ $this->assertTrue($ok);
+ $this->assertEquals(1, count($result['new']));
+ $this->assertEquals('template', $result['new'][0]['type']);
+ $this->assertEquals('template5', $result['new'][0]['base']);
+ $this->assertEquals('tplfoo5', $this->extdir($result['new'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/tplsub6/tplfoo6");
+ $this->assertTrue($ok);
+ $this->assertEquals(1, count($result['new']));
+ $this->assertEquals('template', $result['new'][0]['type']);
+ $this->assertEquals('template6', $result['new'][0]['base']);
+ $this->assertEquals('tplsub6/tplfoo6', $this->extdir($result['new'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/either1");
+ $this->assertTrue($ok);
+ $this->assertEquals(0, count($result['new']));
+ $this->assertEquals(1, count($result['old']));
+ $this->assertEquals('plugin', $result['old'][0]['type']);
+ $this->assertEquals('either1', $this->extdir($result['old'][0]['tmp']));
+
+ $result = array('old' => array(), 'new' => array());
+ $ok = $extension->find_folders($result, "$tdir/eithersub2/either2");
+ $this->assertTrue($ok);
+ $this->assertEquals(0, count($result['new']));
+ $this->assertEquals(1, count($result['old']));
+ $this->assertEquals('plugin', $result['old'][0]['type']);
+ $this->assertEquals('eithersub2/either2', $this->extdir($result['old'][0]['tmp']));
}
+ /**
+ * remove the test data directory from a dir name for cross install comparison
+ *
+ * @param string $dir
+ * @return string
+ */
+ protected function extdir($dir) {
+ $tdir = dirname(__FILE__).'/testdata';
+ $len = strlen($tdir);
+ $dir = trim(substr($dir, $len), '/');
+ return $dir;
+ }
} \ No newline at end of file
diff --git a/lib/plugins/extension/_test/testdata/either1/script.js b/lib/plugins/extension/_test/testdata/either1/script.js
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/either1/script.js
diff --git a/lib/plugins/extension/_test/testdata/eithersub2/either2/script.js b/lib/plugins/extension/_test/testdata/eithersub2/either2/script.js
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/eithersub2/either2/script.js
diff --git a/lib/plugins/extension/_test/testdata/plgfoo5/plugin.info.txt b/lib/plugins/extension/_test/testdata/plgfoo5/plugin.info.txt
new file mode 100644
index 000000000..cc4532d29
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/plgfoo5/plugin.info.txt
@@ -0,0 +1,7 @@
+base plugin5
+author Andreas Gohr
+email andi@splitbrain.org
+date 2013-05-02
+name Dummy Plugin
+desc Dummy plugin data
+url http://example.com/plugin:plugin5
diff --git a/lib/plugins/extension/_test/testdata/plgsub3/plugin3/syntax.php b/lib/plugins/extension/_test/testdata/plgsub3/plugin3/syntax.php
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/plgsub3/plugin3/syntax.php
diff --git a/lib/plugins/extension/_test/testdata/plgsub4/plugin4/plugin.info.txt b/lib/plugins/extension/_test/testdata/plgsub4/plugin4/plugin.info.txt
new file mode 100644
index 000000000..374b6bf24
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/plgsub4/plugin4/plugin.info.txt
@@ -0,0 +1,7 @@
+base plugin4
+author Andreas Gohr
+email andi@splitbrain.org
+date 2013-05-02
+name Dummy Plugin
+desc Dummy plugin data
+url http://example.com/plugin:plugin4
diff --git a/lib/plugins/extension/_test/testdata/plgsub6/plgfoo6/plugin.info.txt b/lib/plugins/extension/_test/testdata/plgsub6/plgfoo6/plugin.info.txt
new file mode 100644
index 000000000..461ff8735
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/plgsub6/plgfoo6/plugin.info.txt
@@ -0,0 +1,7 @@
+base plugin6
+author Andreas Gohr
+email andi@splitbrain.org
+date 2013-05-02
+name Dummy Plugin
+desc Dummy plugin data
+url http://example.com/plugin:plugin6
diff --git a/lib/plugins/extension/_test/testdata/plugin1/syntax.php b/lib/plugins/extension/_test/testdata/plugin1/syntax.php
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/plugin1/syntax.php
diff --git a/lib/plugins/extension/_test/testdata/plugin2/plugin.info.txt b/lib/plugins/extension/_test/testdata/plugin2/plugin.info.txt
new file mode 100644
index 000000000..d56758fe9
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/plugin2/plugin.info.txt
@@ -0,0 +1,7 @@
+base plugin2
+author Andreas Gohr
+email andi@splitbrain.org
+date 2013-05-02
+name Dummy Plugin
+desc Dummy Plugin data
+url http://example.com/plugin:plugin2
diff --git a/lib/plugins/extension/_test/testdata/template1/main.php b/lib/plugins/extension/_test/testdata/template1/main.php
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/template1/main.php
diff --git a/lib/plugins/extension/_test/testdata/template1/style.ini b/lib/plugins/extension/_test/testdata/template1/style.ini
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/template1/style.ini
diff --git a/lib/plugins/extension/_test/testdata/template2/template.info.txt b/lib/plugins/extension/_test/testdata/template2/template.info.txt
new file mode 100644
index 000000000..882a7b914
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/template2/template.info.txt
@@ -0,0 +1,7 @@
+base template2
+author Andreas Gohr
+email andi@splitbrain.org
+date 2013-05-02
+name Dummy Template
+desc Dummy template data
+url http://example.com/template:template2
diff --git a/lib/plugins/extension/_test/testdata/tplfoo5/template.info.txt b/lib/plugins/extension/_test/testdata/tplfoo5/template.info.txt
new file mode 100644
index 000000000..4d7ecb8ef
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/tplfoo5/template.info.txt
@@ -0,0 +1,7 @@
+base template5
+author Andreas Gohr
+email andi@splitbrain.org
+date 2013-05-02
+name Dummy Template
+desc Dummy template data
+url http://example.com/template:template5
diff --git a/lib/plugins/extension/_test/testdata/tplsub3/template3/main.php b/lib/plugins/extension/_test/testdata/tplsub3/template3/main.php
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/tplsub3/template3/main.php
diff --git a/lib/plugins/extension/_test/testdata/tplsub3/template3/style.ini b/lib/plugins/extension/_test/testdata/tplsub3/template3/style.ini
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/tplsub3/template3/style.ini
diff --git a/lib/plugins/extension/_test/testdata/tplsub4/template4/template.info.txt b/lib/plugins/extension/_test/testdata/tplsub4/template4/template.info.txt
new file mode 100644
index 000000000..f050555e5
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/tplsub4/template4/template.info.txt
@@ -0,0 +1,7 @@
+base template4
+author Andreas Gohr
+email andi@splitbrain.org
+date 2013-05-02
+name Dummy Template
+desc Dummy template data
+url http://example.com/template:template4
diff --git a/lib/plugins/extension/_test/testdata/tplsub6/tplfoo6/template.info.txt b/lib/plugins/extension/_test/testdata/tplsub6/tplfoo6/template.info.txt
new file mode 100644
index 000000000..ea4dc230d
--- /dev/null
+++ b/lib/plugins/extension/_test/testdata/tplsub6/tplfoo6/template.info.txt
@@ -0,0 +1,7 @@
+base template6
+author Andreas Gohr
+email andi@splitbrain.org
+date 2013-05-02
+name Dummy Template
+desc Dummy template data
+url http://example.com/template:template6
diff --git a/lib/plugins/extension/admin.php b/lib/plugins/extension/admin.php
index cf4eb79d7..e28fd612c 100644
--- a/lib/plugins/extension/admin.php
+++ b/lib/plugins/extension/admin.php
@@ -67,11 +67,9 @@ class admin_plugin_extension extends DokuWiki_Admin_Plugin {
$this->infoFor = $extname;
break;
case 'install':
- msg('Not implemented');
- break;
case 'reinstall':
case 'update':
- $extension->setExtension($extname, false);
+ $extension->setExtension($extname);
$status = $extension->installOrUpdate();
if ($status !== true) {
msg($status, -1);
@@ -80,7 +78,7 @@ class admin_plugin_extension extends DokuWiki_Admin_Plugin {
}
break;
case 'uninstall':
- $extension->setExtension($extname, false);
+ $extension->setExtension($extname);
$status = $extension->uninstall();
if ($status !== true) {
msg($status, -1);
@@ -89,7 +87,7 @@ class admin_plugin_extension extends DokuWiki_Admin_Plugin {
}
break;
case 'enable';
- $extension->setExtension($extname, false);
+ $extension->setExtension($extname);
$status = $extension->enable();
if ($status !== true) {
msg($status, -1);
@@ -98,7 +96,7 @@ class admin_plugin_extension extends DokuWiki_Admin_Plugin {
}
break;
case 'disable';
- $extension->setExtension($extname, false);
+ $extension->setExtension($extname);
$status = $extension->disable();
if ($status !== true) {
msg($status, -1);
diff --git a/lib/plugins/extension/helper/extension.php b/lib/plugins/extension/helper/extension.php
index 244ec9b9a..62f950cac 100644
--- a/lib/plugins/extension/helper/extension.php
+++ b/lib/plugins/extension/helper/extension.php
@@ -127,6 +127,7 @@ class helper_plugin_extension_extension extends DokuWiki_Plugin {
* @return bool If an update is available
*/
public function updateAvailable() {
+ if(!$this->isInstalled()) return false;
$lastupdate = $this->getLastUpdate();
if ($lastupdate === false) return false;
$installed = $this->getInstalledVersion();
@@ -518,21 +519,24 @@ class helper_plugin_extension_extension extends DokuWiki_Plugin {
/**
* Install or update the extension
*
- * @return bool|string True or an error message
+ * @throws \Exception when something goes wrong
+ * @return array The list of installed extensions
*/
public function installOrUpdate() {
- if (($status = $this->download($this->getDownloadURL(), $path)) === true) {
- if (($status = $this->installArchive($path, $installed_extensions, $this->isInstalled(), $this->getBase())) == true) {
- // refresh extension information
- if (!isset($installed_extensions[$this->getBase()])) {
- $status = 'Error, the requested extension hasn\'t been installed or updated';
- }
- $this->setExtension($this->getID());
- $this->purgeCache();
+ try {
+ $path = $this->download($this->getDownloadURL());
+ $installed = $this->installArchive($path, $this->isInstalled(), $this->getBase());
+
+ // refresh extension information
+ if (!isset($installed[$this->getBase()])) {
+ throw new Exception('Error, the requested extension hasn\'t been installed or updated');
}
- $this->dir_delete(dirname($path));
+ $this->setExtension($this->getID());
+ $this->purgeCache();
+ }catch (Exception $e){
+ throw $e;
}
- return $status;
+ return $installed;
}
/**
@@ -695,112 +699,108 @@ class helper_plugin_extension_extension extends DokuWiki_Plugin {
/**
* Download an archive to a protected path
+ *
* @param string $url The url to get the archive from
- * @param string $path The path where the archive was saved (output parameter)
- * @return bool|string True on success, an error message on failure
+ * @throws Exception when something goes wrong
+ * @return string The path where the archive was saved
*/
- public function download($url, &$path) {
+ public function download($url) {
// check the url
$matches = array();
if(!preg_match('/[^\/]*$/', $url, $matches) || !$matches[0]) {
- return $this->getLang('baddownloadurl');
+ throw new Exception($this->getLang('baddownloadurl'));
}
$file = $matches[0];
// create tmp directory for download
if(!($tmp = io_mktmpdir())) {
- return $this->getLang('error_dircreate');
+ throw new Exception($this->getLang('error_dircreate'));
}
// download
- if(!$file = io_download($url, $tmp.'/', true, $file)) {
+ if(!$file = io_download($url, $tmp.'/', true, $file, 0)) {
$this->dir_delete($tmp);
- return sprintf($this->getLang('error_download'), $url);
+ throw new Exception(sprintf($this->getLang('error_download'), hsc($url)));
}
- $path = $tmp.'/'.$file;
-
- return true;
+ return $tmp.'/'.$file;
}
/**
* @param string $file The path to the archive that shall be installed
* @param bool $overwrite If an already installed plugin should be overwritten
- * @param array $installed_extensions Array of all installed extensions in the form $base => ('type' => $type, 'action' => 'update'|'install')
* @param string $base The basename of the plugin if it's known
+ * @throws Exception when something went wrong
* @return bool|string True on success, an error message on failure
*/
- public function installArchive($file, &$installed_extensions, $overwrite=false, $base = '') {
+ public function installArchive($file, $overwrite=false, $base = '') {
$error = false;
// create tmp directory for decompression
if(!($tmp = io_mktmpdir())) {
- return $this->getLang('error_dircreate');
+ throw new Exception($this->getLang('error_dircreate'));
}
// add default base folder if specified to handle case where zip doesn't contain this
if($base && !@mkdir($tmp.'/'.$base)) {
- $error = $this->getLang('error_dircreate');
+ throw new Exception($this->getLang('error_dircreate'));
}
- if(!$error && !$this->decompress("$tmp/$file", "$tmp/".$base)) {
- $error = sprintf($this->getLang('error_decompress'), $file);
+ // decompress
+ if(!$this->decompress("$tmp/$file", "$tmp/".$base)) {
+ throw new Exception(sprintf($this->getLang('error_decompress'), $file));
}
// search $tmp/$base for the folder(s) that has been created
// move the folder(s) to lib/..
- if(!$error) {
- $result = array('old'=>array(), 'new'=>array());
-
- if(!$this->find_folders($result, $tmp.'/'.$base, ($this->isTemplate() ? 'template' : 'plugin'))) {
- $error = $this->getLang('error_findfolder');
+ $result = array('old'=>array(), 'new'=>array());
+ if(!$this->find_folders($result, $tmp.'/'.$base, ($this->isTemplate() ? 'template' : 'plugin'))) {
+ throw new Exception($this->getLang('error_findfolder'));
+ }
- } else {
- // choose correct result array
- if(count($result['new'])) {
- $install = $result['new'];
- }else{
- $install = $result['old'];
- }
+ // choose correct result array
+ if(count($result['new'])) {
+ $install = $result['new'];
+ }else{
+ $install = $result['old'];
+ }
- // now install all found items
- foreach($install as $item) {
- // where to install?
- if($item['type'] == 'template') {
- $target_base_dir = DOKU_TPLLIB;
- }else{
- $target_base_dir = DOKU_PLUGIN;
- }
+ // now install all found items
+ foreach($install as $item) {
+ // where to install?
+ if($item['type'] == 'template') {
+ $target_base_dir = DOKU_TPLLIB;
+ }else{
+ $target_base_dir = DOKU_PLUGIN;
+ }
- if(!empty($item['base'])) {
- // use base set in info.txt
- } elseif($base && count($install) == 1) {
- $item['base'] = $base;
- } else {
- // default - use directory as found in zip
- // plugins from github/master without *.info.txt will install in wrong folder
- // but using $info->id will make 'code3' fail (which should install in lib/code/..)
- $item['base'] = basename($item['tmp']);
- }
+ if(!empty($item['base'])) {
+ // use base set in info.txt
+ } elseif($base && count($install) == 1) {
+ $item['base'] = $base;
+ } else {
+ // default - use directory as found in zip
+ // plugins from github/master without *.info.txt will install in wrong folder
+ // but using $info->id will make 'code3' fail (which should install in lib/code/..)
+ $item['base'] = basename($item['tmp']);
+ }
- // check to make sure we aren't overwriting anything
- $target = $target_base_dir.$item['base'];
- if(!$overwrite && @file_exists($target)) {
- // TODO remember our settings, ask the user to confirm overwrite
- continue;
- }
+ // check to make sure we aren't overwriting anything
+ $target = $target_base_dir.$item['base'];
+ if(!$overwrite && @file_exists($target)) {
+ // TODO remember our settings, ask the user to confirm overwrite
+ continue;
+ }
- $action = @file_exists($target) ? 'update' : 'install';
+ $action = @file_exists($target) ? 'update' : 'install';
- // copy action
- if($this->dircopy($item['tmp'], $target)) {
- // TODO: write manager.dat!
- $installed_extensions[$item['base']] = array('type' => $item['type'], 'action' => $action);
- } else {
- $error = sprintf($this->getLang('error_copy').DOKU_LF, $item['base']);
- break;
- }
- }
+ // copy action
+ if($this->dircopy($item['tmp'], $target)) {
+ // TODO: write manager.dat!
+ $installed_extensions[$item['base']] = array('type' => $item['type'], 'action' => $action);
+ } else {
+ $error = sprintf($this->getLang('error_copy').DOKU_LF, $item['base']);
+ break;
}
}
@@ -830,25 +830,24 @@ class helper_plugin_extension_extension extends DokuWiki_Plugin {
*
* @author Andreas Gohr <andi@splitbrain.org>
* @param array $result - results are stored here
- * @param string $base - the temp directory where the package was unpacked to
+ * @param string $directory - the temp directory where the package was unpacked to
* @param string $default_type - type used if no info.txt available
- * @param string $dir - a subdirectory. do not set. used by recursion
+ * @param string $subdir - a subdirectory. do not set. used by recursion
* @return bool - false on error
*/
- private function find_folders(&$result, $base, $default_type, $dir='') {
- $this_dir = "$base$dir";
+ protected function find_folders(&$result, $directory, $default_type='plugin', $subdir='') {
+ $this_dir = "$directory$subdir";
$dh = @opendir($this_dir);
if(!$dh) return false;
$found_dirs = array();
$found_files = 0;
$found_template_parts = 0;
- $found_info_txt = false;
while (false !== ($f = readdir($dh))) {
if($f == '.' || $f == '..') continue;
if(is_dir("$this_dir/$f")) {
- $found_dirs[] = "$dir/$f";
+ $found_dirs[] = "$subdir/$f";
} else {
// it's a file -> check for config
@@ -856,7 +855,7 @@ class helper_plugin_extension_extension extends DokuWiki_Plugin {
switch ($f) {
case 'plugin.info.txt':
case 'template.info.txt':
- $found_info_txt = true;
+ // we have found a clear marker, save and return
$info = array();
$type = explode('.', $f, 2);
$info['type'] = $type[0];
@@ -864,7 +863,7 @@ class helper_plugin_extension_extension extends DokuWiki_Plugin {
$conf = confToHash("$this_dir/$f");
$info['base'] = basename($conf['base']);
$result['new'][] = $info;
- break;
+ return true;
case 'main.php':
case 'details.php':
@@ -877,33 +876,24 @@ class helper_plugin_extension_extension extends DokuWiki_Plugin {
}
closedir($dh);
- // URL downloads default to 'plugin', try extra hard to indentify templates
- if(!$default_type && $found_template_parts > 2 && !$found_info_txt) {
+ // files where found but no info.txt - use old method
+ if($found_files){
$info = array();
- $info['type'] = 'template';
$info['tmp'] = $this_dir;
- $result['new'][] = $info;
- }
+ // does this look like a template or should we use the default type?
+ if($found_template_parts >= 2) {
+ $info['type'] = 'template';
+ } else {
+ $info['type'] = $default_type;
+ }
- // files in top level but no info.txt, assume this is zip missing a base directory
- // works for all downloads unless direct URL where $base will be the tmp directory ($info->id was empty)
- if(!$dir && $found_files > 0 && !$found_info_txt && $default_type) {
- $info = array();
- $info['type'] = $default_type;
- $info['tmp'] = $base;
$result['old'][] = $info;
return true;
}
+ // we have no files yet -> recurse
foreach ($found_dirs as $found_dir) {
- // if top level add to dir list for old method, then recurse
- if(!$dir) {
- $info = array();
- $info['type'] = ($default_type ? $default_type : 'plugin');
- $info['tmp'] = "$base$found_dir";
- $result['old'][] = $info;
- }
- $this->find_folders($result, $base, $default_type, "$found_dir");
+ $this->find_folders($result, $directory, $default_type, "$found_dir");
}
return true;
}