DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:xfortune

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
plugin:xfortune [2010-06-04 14:10] 38.113.92.61plugin:xfortune [2018-06-06 10:06] – [Download] Klap-in
Line 6: Line 6:
 email      : andi@splitbrain.org email      : andi@splitbrain.org
 type       : syntax type       : syntax
-lastupdate : 2008-01-25 +lastupdate : 2017-04-20 
-compatible : 2006-03-05 and higher+compatible : 2009-12-25, 2010-11-07
 depends    :  depends    : 
 conflicts  conflicts 
-similar    : +similar    : fortune
 tags       : quotes, ajax, random tags       : quotes, ajax, random
 +
 +downloadurl: https://github.com/splitbrain/dokuwiki-plugin-xfortune/zipball/master
 +sourcerepo : https://github.com/splitbrain/dokuwiki-plugin-xfortune
 +bugtracker : https://github.com/splitbrain/dokuwiki-plugin-xfortune/issues
 +donationurl: http://donate.dokuwiki.org/xfortune
 ---- ----
  
 This plugin is able to read datafiles used by the popular Unix tool [[man>fortune]] and to display a random one. It then replaces the displayed one every few seconds with a new one using an AJAX request. This plugin is able to read datafiles used by the popular Unix tool [[man>fortune]] and to display a random one. It then replaces the displayed one every few seconds with a new one using an AJAX request.
 +
 +===== Download =====
 +
 +Download and install the plugin using the [[plugin:extension|Extension Manager]]. Refer to [[:Plugins]] on how to install plugins manually.
 +
 +==== Changes ====
 +
 +{{rss>https://github.com/splitbrain/dokuwiki-plugin-xfortune/commits/master.atom date}}
  
 ===== Usage ===== ===== Usage =====
Line 30: Line 43:
 ===== Input files ===== ===== Input files =====
  
-The cookie file needs to be a text file ((the uncompiled source files for fortune, not the binary ones created by [[man>strfile]])) where each cookie is separated by a % char on it's own line. You need to upload those files through the [[mediamanager]].+The cookie file needs to be a text file ((the uncompiled source files for fortune, not the binary ones created by [[man>strfile]])) where each cookie is separated by a % char on it's own line. You need to upload those files through the [[:fullscreen_mediamanager|mediamanager]].
  
 Here is an example: Here is an example:
Line 45: Line 58:
 </file> </file>
  
-===== Code =====+===== Known Shortcomings =====
  
-Create a directory called ''xfortune'' in the ''lib/plugins'' directory and place the following files in there. +  * If your input files are not in UTF-8 this plugin will assume they are in latin1 and tries to convert them to UTF-8. If you get any charset problems, make sure the file is valid UTF-8. 
-==== syntax.php ====+  * Using compiled fortune cookies would probably be more effective 
 +  * It is implied above, but I think it is better to state it explicitly: at least for DW //Lemming// and the latest version of the plugin, the fortune file **must** have UNIX line endings (''LF'')Windows line endings won't do --- [[user>ryan.chappelle]] //2011/02/07 02:48//
  
-Create this file in ''lib/plugins/xfortune/syntax.php'' 
  
-<code php syntax.php> 
-<?php 
-/** 
- * Display Fortune cookies 
- * 
- * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html) 
- * @author     Andreas Gohr <andi@splitbrain.org> 
- */ 
- 
-if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 
-if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 
-require_once(DOKU_PLUGIN.'syntax.php'); 
- 
-class syntax_plugin_xfortune extends DokuWiki_Syntax_Plugin { 
-    /** 
-     * return some info 
-     */ 
-    function getInfo(){ 
-        return array( 
-            'author' => 'Andreas Gohr', 
-            'email'  => 'andi@splitbrain.org', 
-            'date'   => '2008-01-25', 
-            'name'   => 'Fortune Plugin', 
-            'desc'   => 'Displays random fortune cookies using AJAX requests', 
-            'url'    => 'http://www.dokuwiki.org/plugin:gallery', 
-        ); 
-    } 
- 
-    /** 
-     * What kind of syntax are we? 
-     */ 
-    function getType(){ 
-        return 'substition'; 
-    } 
- 
-    /** 
-     * What about paragraphs? 
-     */ 
-    function getPType(){ 
-        return 'block'; 
-    } 
- 
-    /** 
-     * Where to sort in? 
-     */ 
-    function getSort(){ 
-        return 302; 
-    } 
- 
-    /** 
-     * Connect pattern to lexer 
-     */ 
-    function connectTo($mode) { 
-        $this->Lexer->addSpecialPattern('\{\{xfortune>[^}]*\}\}',$mode,'plugin_xfortune'); 
-    } 
- 
- 
-    /** 
-     * Handle the match 
-     */ 
-    function handle($match, $state, $pos, &$handler){ 
-        $match = substr($match,11,-2); //strip markup from start and end 
- 
-        $data = array(); 
- 
-        //handle params 
-        list($cookie,$params) = explode('?',$match,2); 
- 
-        //xfortune cookie file 
-        $data['cookie'] = cleanID($cookie); 
- 
-        //time interval for changing cookies 
-        if(preg_match('/\b(\d+)\b/i',$params,$match)){ 
-            $data['time'] = $match[1]; 
-        }else{ 
-            $data['time'] = 30; 
-        } 
-        //no hammering please! 
-        if($data['time'] < 5) $data['time'] = 5; 
- 
-        return $data; 
-    } 
- 
-    /** 
-     * Create output 
-     */ 
-    function render($mode, &$renderer, $data) { 
-        if($mode == 'xhtml'){ 
-            $renderer->doc .= '<div id="plugin_xfortune">'; 
-            $renderer->doc .= $this->_getCookie($data['cookie']); 
-            $renderer->doc .= '</div>'; 
-            $renderer->doc .= $this->_script($data['cookie'],$data['time']); 
-            return true; 
-        } 
-        return false; 
-    } 
- 
-    function _script($cookie,$time){ 
-        $str  = '<script type="text/javascript" language="javascript">'; 
-        $str .= 'var plugin_xfortune_time = '.($time*1000).';'; 
-        $str .= 'var plugin_xfortune_cookie = \''.$cookie."';"; 
-        $str .= "addEvent(window,'load',plugin_xfortune);"; 
-        $str .= '</script>'; 
-        return $str; 
-    } 
- 
-    /** 
-     * Returns one random cookie 
-     * 
-     * @author Andreas Gohr <andi@splitbrain.org> 
-     */ 
-    function _getCookie($cookie){ 
-        $file = mediaFN($cookie); 
-        if(!@file_exists($file)) return 'ERROR: cookie file not found'; 
- 
-        $dim = filesize($file); 
-        if($dim < 2) return "ERROR: invalid cookie file $file"; 
-        mt_srand( (double) microtime() * 1000000); 
-        $rnd = mt_rand(0,$dim); 
- 
-        $fd = fopen($file, 'r'); 
-        if (!$fd) return "ERROR: reading cookie file $file failed"; 
- 
-        // jump to random place in file 
-        fseek($fd, $rnd); 
- 
-        $text   = ''; 
-        $line   = ''; 
-        $cookie = false; 
-        $test   = 0; 
-        while(true){ 
-            $seek = ftell($fd); 
-            $line = fgets($fd, 1024); 
- 
-            if($seek == 0){ 
-                // start of file always starts a cookie 
-                $cookie = true; 
-                if($line == "%\n"){ 
-                    // ignore delimiter if exists 
-                    continue; 
-                }else{ 
-                    // part of the cookie 
-                    $text .= htmlspecialchars($line).'<br />'; 
-                    continue; 
-                } 
-            } 
- 
-            if(feof($fd)){ 
-                if($cookie){ 
-                    // we had a cookie already, stop here 
-                    break; 
-                }else{ 
-                    // no cookie yet, wrap around 
-                    fseek($fd,0); 
-                    continue; 
-                } 
-            } 
- 
-            if($line == "%\n"){ 
-                if($cookie){ 
-                    // we had a cookie already, stop here 
-                    break; 
-                }elseif($seek == $dim -2){ 
-                    // it's the end of file delimiter, wrap around 
-                    fseek($fd,0); 
-                    continue; 
-                }else{ 
-                    // start of the cookie 
-                    $cookie = true; 
-                    continue; 
-                } 
-            } 
- 
-            // part of the cookie? 
-            if($cookie){ 
-                $text .= htmlspecialchars($line).'<br />'; 
-            } 
-        } 
-        fclose($fd); 
- 
-        // if it is not valid UTF-8 assume it's latin1 
-        if(!utf8_check($text)) return utf8_encode($text); 
- 
-        return $text; 
-    } 
-} 
- 
-//Setup VIM: ex: et ts=4 enc=utf-8 : 
-</code> 
-==== ajax.php ==== 
- 
-This is the backend to handle the AJAX requests. Put it into ''lib/plugins/xfortune/ajax.php''. 
- 
-<code php ajax.php> 
-<?php 
-/** 
- * AJAX Backend Function for plugin_xfortune 
- * 
- * @author Andreas Gohr <andi@splitbrain.org> 
- */ 
- 
-//fix for Opera XMLHttpRequests 
-if(!count($_POST) && $HTTP_RAW_POST_DATA){ 
-  parse_str($HTTP_RAW_POST_DATA, $_POST); 
-} 
- 
-if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/'); 
-if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 
-require_once(DOKU_INC.'inc/init.php'); 
-//close sesseion 
-session_write_close(); 
- 
-require_once(DOKU_INC.'inc/pageutils.php'); 
-require_once(DOKU_INC.'inc/utf8.php'); 
-require_once(DOKU_PLUGIN.'xfortune/syntax.php'); 
- 
-header('Content-Type: text/html; charset=utf-8'); 
-$cookie = cleanID($_POST['cookie']); 
-print syntax_plugin_xfortune::_getCookie($cookie); 
- 
-?> 
-</code> 
-==== script.js ==== 
- 
-The needed JavaScript goes into ''lib/plugins/xfortune/script.js'' 
- 
-<code javascript> 
-/** 
- * Script for plugin_xfortune 
- * 
- * Fetches a new cookie 
- * 
- * @author Andreas Gohr <andi@splitbrain.org> 
- */ 
- 
-function plugin_xfortune(){ 
-    if(!document.getElementById){ 
-        return; 
-    } 
-    var obj = document.getElementById('plugin_xfortune'); 
-    if(obj === null){ 
-        return; 
-    } 
- 
-    // We use SACK to do the AJAX requests 
-    var ajax = new sack(DOKU_BASE+'lib/plugins/xfortune/ajax.php'); 
-    ajax_qsearch.sack.AjaxFailedAlert = ''; 
-    ajax_qsearch.sack.encodeURIString = false; 
- 
-    // define callback 
-    ajax.onCompletion = function(){ 
-        var data = this.response; 
-        if(data === ''){ return; } 
-        var out = document.getElementById('plugin_xfortune'); 
- 
-        out.style.visibility = 'hidden'; 
-        out.innerHTML = data; 
-        out.style.visibility = 'visible'; 
- 
-        // restart timer 
-        window.setTimeout("plugin_xfortune()",plugin_xfortune_time); 
-    }; 
- 
-    ajax.runAJAX('cookie='+encodeURI(plugin_xfortune_cookie)); 
-} 
-</code> 
- 
-==== style.css ==== 
- 
-Style your fortune cookies through this file: ''lib/plugins/xfortune/style.css'' 
- 
-<code css> 
-div#plugin_xfortune{ 
-  height: 120px; 
-  padding: 0.5em; 
-  margin: 1em; 
-  text-align: right; 
-  font-style: italic; 
-  overflow: auto; 
-} 
-</code> 
- 
-===== Known Shortcomings ===== 
- 
-  * If your input files are not in UTF-8 this plugin will assume they are in latin1 and tries to convert them to UTF-8. If you get any charset problems, make sure the file is valid UTF-8. 
-  * Using compiled fortune cookies may be more effective 
plugin/xfortune.txt · Last modified: 2023-10-30 23:43 by Klap-in

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki