From 6ea007c9f8c6830ea4f21ad880e91279e2f4bf10 Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Sat, 16 May 2015 18:40:21 +0200
Subject: [PATCH] a first very basic implementation of the preview mechanism

---
 .gitignore                                |  1 +
 lib/plugins/styler/.travis.yml            | 13 ++++
 lib/plugins/styler/README                 | 27 ++++++++
 lib/plugins/styler/_test/general.test.php | 33 +++++++++
 lib/plugins/styler/action.php             | 84 +++++++++++++++++++++++
 lib/plugins/styler/admin.php              | 74 ++++++++++++++++++++
 lib/plugins/styler/lang/en/lang.php       | 16 +++++
 lib/plugins/styler/plugin.info.txt        |  7 ++
 lib/plugins/styler/script.js              | 40 +++++++++++
 9 files changed, 295 insertions(+)
 create mode 100644 lib/plugins/styler/.travis.yml
 create mode 100644 lib/plugins/styler/README
 create mode 100644 lib/plugins/styler/_test/general.test.php
 create mode 100644 lib/plugins/styler/action.php
 create mode 100644 lib/plugins/styler/admin.php
 create mode 100644 lib/plugins/styler/lang/en/lang.php
 create mode 100644 lib/plugins/styler/plugin.info.txt
 create mode 100644 lib/plugins/styler/script.js

diff --git a/.gitignore b/.gitignore
index acb26dd90..cf1a37300 100644
--- a/.gitignore
+++ b/.gitignore
@@ -47,6 +47,7 @@
 !/lib/plugins/popularity
 !/lib/plugins/revert
 !/lib/plugins/safefnrecode
+!/lib/plugins/styler
 !/lib/plugins/testing
 !/lib/plugins/usermanager
 !/lib/plugins/action.php
diff --git a/lib/plugins/styler/.travis.yml b/lib/plugins/styler/.travis.yml
new file mode 100644
index 000000000..d80c0691f
--- /dev/null
+++ b/lib/plugins/styler/.travis.yml
@@ -0,0 +1,13 @@
+# Config file for travis-ci.org
+
+language: php
+php:
+  - "5.5"
+  - "5.4"
+  - "5.3"
+env:
+  - DOKUWIKI=master
+  - DOKUWIKI=stable
+before_install: wget https://raw.github.com/splitbrain/dokuwiki-travis/master/travis.sh
+install: sh travis.sh
+script: cd _test && phpunit --stderr --group plugin_styler
\ No newline at end of file
diff --git a/lib/plugins/styler/README b/lib/plugins/styler/README
new file mode 100644
index 000000000..37a966352
--- /dev/null
+++ b/lib/plugins/styler/README
@@ -0,0 +1,27 @@
+styler Plugin for DokuWiki
+
+Allows to edit style.ini replacements
+
+All documentation for this plugin can be found at
+https://www.dokuwiki.org/plugin:styler
+
+If you install this plugin manually, make sure it is installed in
+lib/plugins/styler/ - if the folder is called different it
+will not work!
+
+Please refer to http://www.dokuwiki.org/plugins for additional info
+on how to install plugins in DokuWiki.
+
+----
+Copyright (C) Andreas Gohr <andi@splitbrain.org>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; version 2 of the License
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+See the COPYING file in your DokuWiki folder for details
diff --git a/lib/plugins/styler/_test/general.test.php b/lib/plugins/styler/_test/general.test.php
new file mode 100644
index 000000000..8b0712a20
--- /dev/null
+++ b/lib/plugins/styler/_test/general.test.php
@@ -0,0 +1,33 @@
+<?php
+/**
+ * General tests for the styler plugin
+ *
+ * @group plugin_styler
+ * @group plugins
+ */
+class general_plugin_styler_test extends DokuWikiTest {
+
+    /**
+     * Simple test to make sure the plugin.info.txt is in correct format
+     */
+    public function test_plugininfo() {
+        $file = __DIR__.'/../plugin.info.txt';
+        $this->assertFileExists($file);
+
+        $info = confToHash($file);
+
+        $this->assertArrayHasKey('base', $info);
+        $this->assertArrayHasKey('author', $info);
+        $this->assertArrayHasKey('email', $info);
+        $this->assertArrayHasKey('date', $info);
+        $this->assertArrayHasKey('name', $info);
+        $this->assertArrayHasKey('desc', $info);
+        $this->assertArrayHasKey('url', $info);
+
+        $this->assertEquals('styler', $info['base']);
+        $this->assertRegExp('/^https?:\/\//', $info['url']);
+        $this->assertTrue(mail_isvalid($info['email']));
+        $this->assertRegExp('/^\d\d\d\d-\d\d-\d\d$/', $info['date']);
+        $this->assertTrue(false !== strtotime($info['date']));
+    }
+}
diff --git a/lib/plugins/styler/action.php b/lib/plugins/styler/action.php
new file mode 100644
index 000000000..648190a2a
--- /dev/null
+++ b/lib/plugins/styler/action.php
@@ -0,0 +1,84 @@
+<?php
+/**
+ * DokuWiki Plugin styler (Action Component)
+ *
+ * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
+ * @author  Andreas Gohr <andi@splitbrain.org>
+ */
+
+// must be run within Dokuwiki
+if(!defined('DOKU_INC')) die();
+
+class action_plugin_styler extends DokuWiki_Action_Plugin {
+
+    /**
+     * Registers a callback function for a given event
+     *
+     * @param Doku_Event_Handler $controller DokuWiki's event controller object
+     * @return void
+     */
+    public function register(Doku_Event_Handler $controller) {
+
+       $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
+       $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action');
+
+    }
+
+    /**
+     * [Custom event handler which performs action]
+     *
+     * @param Doku_Event $event  event object by reference
+     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
+     *                           handler was registered]
+     * @return void
+     */
+    public function handle_action(Doku_Event &$event, $param) {
+        $event->data = act_clean($event->data);
+        if($event->data === 'styler_plugin_preview') {
+            msg('handle') ;
+            $event->data = 'show';
+            $this->preview();
+        } elseif ($event->data === 'styler_plugin_save') {
+            $event->data = 'show';
+        }
+    }
+
+    protected function preview(){
+        global $conf;
+        $ini     = $conf['cachedir'].'/preview.ini';
+        io_saveFile($ini, $this->makeini());
+    }
+
+    protected function makeini() {
+        global $INPUT;
+
+        $ini = "[replacements]\n";
+        foreach($INPUT->arr('tpl') as $key => $val) {
+            $ini .= $key .' = "'.addslashes($val).'"'."\n";
+        }
+
+        return $ini;
+    }
+
+    /**
+     * [Custom event handler which performs action]
+     *
+     * @param Doku_Event $event  event object by reference
+     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
+     *                           handler was registered]
+     * @return void
+     */
+
+    public function handle_ajax(Doku_Event &$event, $param) {
+        if($event->data != 'plugin_styler') return;
+        $event->preventDefault();
+        $event->stopPropagation();
+
+        /** @var admin_plugin_styler $hlp */
+        $hlp = plugin_load('admin', 'styler');
+        $hlp->html();
+    }
+
+}
+
+// vim:ts=4:sw=4:et:
diff --git a/lib/plugins/styler/admin.php b/lib/plugins/styler/admin.php
new file mode 100644
index 000000000..8ecbd22a0
--- /dev/null
+++ b/lib/plugins/styler/admin.php
@@ -0,0 +1,74 @@
+<?php
+/**
+ * DokuWiki Plugin styler (Admin Component)
+ *
+ * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
+ * @author  Andreas Gohr <andi@splitbrain.org>
+ */
+
+// must be run within Dokuwiki
+if(!defined('DOKU_INC')) die();
+
+class admin_plugin_styler extends DokuWiki_Admin_Plugin {
+
+    /**
+     * @return int sort number in admin menu
+     */
+    public function getMenuSort() {
+        return 1000;
+    }
+
+    /**
+     * @return bool true if only access for superuser, false is for superusers and moderators
+     */
+    public function forAdminOnly() {
+        return true;
+    }
+
+    /**
+     * Should carry out any processing required by the plugin.
+     */
+    public function handle() {
+        set_doku_pref('styler_plugin', 1);
+    }
+
+    /**
+     * Render HTML output, e.g. helpful text and a form
+     */
+    public function html() {
+        global $conf;
+        $tpl = $conf['template'];
+        define('SIMPLE_TEST',1); // hack, ideally certain functions should be moved out of css.php
+        require_once(DOKU_INC.'lib/exe/css.php');
+        $styleini = css_styleini($conf['template'], true);
+        $replacements = $styleini['replacements'];
+
+        ptln('<h1>'.$this->getLang('menu').'</h1>');
+
+        if (empty($replacements)) {
+            echo '<p class="error">Sorry, this template does not support this functionality.</p>';
+        } else {
+            echo '<p>Intro blah... for the currently active template ("'.$tpl.'")... not all variables preview...</p>';
+
+            echo '<form class="styler" id="plugin__styler" method="post">';
+            echo '<h2>Template variables</h2>';
+            echo '<table>';
+            foreach($replacements as $key => $value){
+                echo '<tr>';
+                echo '<td>'.$key.'</td>';
+                echo '<td><input name="tpl['.hsc($key).']" value="'.hsc($value).'" />';
+                echo '</tr>';
+            }
+            echo '</table>';
+            echo '<input type="submit" name="do[styler_plugin_preview]" value="preview">';
+            echo '</form>';
+        }
+
+
+
+    }
+
+
+}
+
+// vim:ts=4:sw=4:et:
\ No newline at end of file
diff --git a/lib/plugins/styler/lang/en/lang.php b/lib/plugins/styler/lang/en/lang.php
new file mode 100644
index 000000000..dfb472f11
--- /dev/null
+++ b/lib/plugins/styler/lang/en/lang.php
@@ -0,0 +1,16 @@
+<?php
+/**
+ * English language file for styler plugin
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+
+// menu entry for admin plugins
+// $lang['menu'] = 'Your menu entry';
+
+// custom language strings for the plugin
+// $lang['fixme'] = 'FIXME';
+
+
+
+//Setup VIM: ex: et ts=4 :
diff --git a/lib/plugins/styler/plugin.info.txt b/lib/plugins/styler/plugin.info.txt
new file mode 100644
index 000000000..51f2f72f6
--- /dev/null
+++ b/lib/plugins/styler/plugin.info.txt
@@ -0,0 +1,7 @@
+base   styler
+author Andreas Gohr
+email  andi@splitbrain.org
+date   2015-05-16
+name   styler plugin
+desc   Allows to edit style.ini replacements
+url    https://www.dokuwiki.org/plugin:styler
diff --git a/lib/plugins/styler/script.js b/lib/plugins/styler/script.js
new file mode 100644
index 000000000..d09a8b8da
--- /dev/null
+++ b/lib/plugins/styler/script.js
@@ -0,0 +1,40 @@
+jQuery(function () {
+
+
+    if (DokuCookie.getValue('styler_plugin') == 1) {
+
+
+
+        // load dialog
+        var $dialog = jQuery(document.createElement('div'));
+        jQuery('body').append($dialog);
+        $dialog.load(
+                DOKU_BASE + '/lib/exe/ajax.php',
+            {
+                'call': 'plugin_styler'
+            },
+            function () {
+                // load the preview template
+                var now = new Date().getTime();
+                var $style = jQuery('link[rel=stylesheet][href*="lib/exe/css.php"]');
+                $style.attr('href', DOKU_BASE + 'lib/exe/css.php?preview=1&tseed=' + now);
+
+                // open the dialog
+                $dialog.dialog({
+                    'title':    'Template Variables',
+                    'width':    500,
+                    'top':      50,
+                    'position': { 'my': 'left top', 'at': 'left top', 'of': window },
+                    // bring everything back to normal
+                    'close':    function (event, ui) {
+                        // disable the styler plugin again
+                        DokuCookie.setValue('styler_plugin', 0);
+                        // reload
+                        document.location.reload()
+                    }
+                });
+            }
+        );
+
+    }
+});
\ No newline at end of file
-- 
GitLab