DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:remotescript

RemoteScript plugin

Compatible with DokuWiki

No compatibility info given!

plugin Implements an Ajax Remote Scripting controller

Last updated on
2007-04-09

Tagged with ajax, javascript

Needed for indexmenu2, livepreview

This plugin implements Ajax (or - better say - Remote Scripting) controller. It is built on top of the excellent JsHttpRequest library. It's extremely easy to use: on client side you pass simple JavaScript variables, on server-side you receive PHP array of the same variables, and vise-versa. It does encoding conversion, implements browser-independent data transport layer, allowing to work even if browser does not support XmlHttp at all (fe. IE with disabled ActiveX).

Browser compatibility: IE5+, FF0.9+, Mozilla, Opera 7.2+, Safari/Konqueror and others.

Notes

Example

JsHttpRequest/RemoteScript library has the interface nearly the same, as implemented in XmlHttpRequest/XMLHTTP.

How to use it. Client-side example, taken from indexmenu2.

function fetcher (s, callback) {
    if ('undefined' == typeof RemoteScript) {
        callback ({'state' : false,
                   'response' : 'Plugin <a href="http://www.dokuwiki.org/plugin:remotescript" _target="blank">RemoteScript</a> is not available'});
        return;
    }
 
    RemoteScript.query( ['indexmenu','getsubmenu']
                       ,{ 'src'  : s
                         ,'sort' : '{$opts['sort']}'}
                       ,function(js, txt) {
                            callback({'state' : !!js,
                                      'response' : js||txt});
                        }
                       ,true);
}

Server-side code:

require_once (DOKU_PLUGIN.'/indexmenu/syntax/indexmenu.php');
/*
*  Returns submenu tree for specified namespace
*
*  @param string $ns namespace title
*  @param string $sort sorting mode
*  @return {String} html markup for the submenu
*  @access public
*/
function indexmenu_getsubmenu ($ns,$s) {
 
  global $conf;
  $opts = array($ns, array('level' => 1, // get only 1st level
                    'ajax' => true,
                    'js' => false,
                    'navigation' => false,
                    'sort' => $s
                   ));
 
  $im = & new syntax_plugin_indexmenu_indexmenu();
  return preg_replace(array("#^<ul[^>]+>#i","#</ul>$#i"),"",$im->_indexmenu($opts));
}

Yes, that's all i need to write to have complete ajax-driven menu on the page.

Detailed description and use examples you can find at JsHttpRequest home.

Configuration

Actually, no configuration is needed.

How to install

Download the remotescript.zip file (82k) and unpack it into lib/plugins folder or use the plugin manager.

Changelog

  • 2007-04-09 Version 0.8.3:
    • JsHttpRequest switched to 5.01 release
  • 2007-02-13 Version 0.8.3:
    • special Safari update
    • added UTF decoding routines, due to Safari's bugs in UTF-8 delivery
  • 2007-02-13 Version 0.8.2:
    • removed calculation of DOKU_BASE constant, fixed issue FS#10
  • 2007-02-10 Version 0.8.1:
    • fixed possible issue with str_replace, replaced with preg_replace
    • fixed missing semicolons (thanks to the weird DokuWiki's script compressor)
  • 2007-02-10 Version 0.8:
    • added simple 'query' method, similar to JsHttpRequest
    • fixed bug FS#10 - plugin set wrong DOKU_BASE constant, when wiki is linked to the site with Alias directive
  • 2006-12-22 Version 0.7:
    • forced to use UTF-8 output, due to incorrect charset autodetection in the different cases
    • backend updated to v4.17
  • 2006-12-05 Version 0.5:
    • Added compatibility to develsnap 2006-12-01.
  • 2006-10-25 Version 0.4:
    • Fixed issue with incorrect BASE_URL calculation under PHP/Win
  • 2006-10-15 Version 0.3:
    • Fixed issue with incorrect DokuWiki installation detection in PHP part (derived from DokuWiki's core files).
    • JsHttpRequest controller is updated to v4.10
  • 2006-09-13 Version 0.2:
    • Fixed issue with incorrect DokuWiki installation detection.
  • 2006-08-24 Version 0.1:
    • Released.

Todo

Bugs

2007-11-15

Two byte language encoded by UTF-8 is not correctly displayed.
I use RemoteScript with indexmenu2.
I test in Korean(UTF-8) – Alex K ( http://wiki.alexk.name )

What should be displayed and what is actually displayed?
Could you, please, make a test page somewhere in the wiki?
Ilya Lebedev 2007-11-15 15:11

The error sample link is http://ukira.cafe24.com/wiki/doku.php?id=wiki:navigation
There is some broken strings like '~~/a>'.
I found a clue.
At the line 311 of source $DOKU_PLUGINS/remotescript/lib/JsHttpRequest/JsHttpRequest.php file, there is '$v = iconv($fromEnc, 'UTF-8', $v);'.
I changed it to '$v = iconv('IOS-8859-1', 'UTF-8', $v);'. Then it works well. Apache 2.x and php 5.x.
But I tested in Apache 1.x and php 4.x. It works fail again.
Becuase of this I study AJAX now. :)

I solved the problem. But I don't know what I did. :(
Finally I modified JsHttpRequest.php file.
I added 3 line ( 272,275,279 line).

    244     /**
    245      * Called in case of error too!
    246      */
    247     function _obHandler($text)
    248     {
    249         // Check for error.
    250         if (preg_match('{'.$this->_uniqHash.'(.*?)'.$this->_uniqHash.'}sx', $text)) {
    251             $text = str_replace($this->_uniqHash, '', $text);
    252         }
    253
    254         // Make a resulting hash.
    255         if (!isset($this->RESULT)) {
    256             $this->RESULT = isset($GLOBALS['_RESULT'])? $GLOBALS['_RESULT'] : null;
    257         }
    258         $encoding = $this->SCRIPT_ENCODING;
    259         $result = array(
    260             'id'   => $this->ID,
    261             'js'   => $this->RESULT,
    262             'text' => $text,
    263         );
    264         if (function_exists('array_walk_recursive') && function_exists('iconv') && function_exists('json_encode')) {
    265             $encoding = "UTF-8";
    266             $this->_nonAsciiChars = join("", array_map('chr', range(128, 255)));
    267             $this->_toUtfFailed = false;
    268             array_walk_recursive($result, array(&$this, '_toUtf8_callback'), $this->SCRIPT_ENCODING);
    269             if (!$this->_toUtfFailed) {
    270                 // If some key contains non-ASCII character, convert everything manually.
    271                 $text = json_encode($result);
    272                 $text = iconv('ISO-8859-1','UTF-8',$text);// I added this line
    273             } else {
    274                 $text = $this->php2js($result);
    275                 $text = iconv('ISO-8859-1','UTF-8',$text); // I added this line
    276             }
    277         } else {
    278             $text = $this->php2js($result);
    279             $text = iconv('ISO-8859-1','UTF-8',$text);  // I added this line
    280         }
    281
Try to rollback the changes and use the following line in 'rs.php'
$JsHttpRequest =& new JsHttpRequest('UTF-8');


Maybe, your server does not recognize lowercase encoding name. — Ilya Lebedev 2007-11-17 21:54

I did it today that you said.
But, it's not a problem my server's recognition.
I add the 3 lines code again. Thank you.

Please, try to replace line in remotescript/script.js

onready(_from_utf8(req.responseJS), _from_utf8(req.responseText));

with

onready(req.responseJS, req.responseText);

This conversion was made to fix Safari UTF-8 issues, maybe it breaks multibyte strings. — Ilya Lebedev 2007-11-18 16:03

Discussion

Feature request

Possible features

plugin/remotescript.txt · Last modified: 2010/10/06 20:08 by HåkanS