DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:fortune

fortune Plugin

Compatible with DokuWiki

No compatibility info given!

plugin Show QOTD (Fortune cookie)

Last updated on
2005-08-01
Provides
Syntax

This extension has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues.

Similar to xfortune

Tagged with quotes, random

Description

This plugin Show a Quote of the Day on your page. It should change every time the page is rendered again.

The plugin uses code written by Henrik Aasted Sorensen, henrik@aasted.org Read more at http://www.aasted.org/quote

Fortune files can be found e.g. from freshmeat
and one nice from here http://www.splitbrain.org/Fortunes/hitchhiker

Usage

After installation (below) you can insert the code

{{fortune}}

in your page to display a random quote.

Installation

Using Plugin Manager from here

Fixed link to correct archive.

or

Create a new folder lib/plugin/fortune/ and place the following file in it: syntax.php.

Create a new folder lib/plugin/fortune/dat/ and set it to be writable for the web server.

From the cookie file, put the file containing the text (not the .dat file) to the created fortune/dat/ directory. The dat file will be generated automatically.

Plugin

lib/plugin/fortune/syntax.php:

<?php
/**
 * Include Plugin: displays a wiki page within another
 * Usage:
 * {{fortune}}
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Otto Vainio <oiv-plugins@valjakko.net>
 */
 
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');
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_fortune extends DokuWiki_Syntax_Plugin {
 
    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Otto Vainio',
            'email'  => 'oiv-plugins@valjakko.net',
            'date'   => '2005-10-14',
            'name'   => 'Fortune Plugin',
            'desc'   => 'Displays qotd (Quote Of The Day)',
            'url'    => 'http://www.dokuwiki.org/plugin:fortune',
        );
    }
 
    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'substition';
    }
 
    /**
     * Where to sort in?
     */
    function getSort(){
        return 56;
    }
 
    /**
     * Paragraph Type
     */
    function getPType(){
        return 'block';
    }
 
 
    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
      $this->Lexer->addSpecialPattern("{{fortune}}",$mode,'plugin_fortune');
    }
 
    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){
        return $match;
    }
 
    /**
     * Create output
     */
    function render($mode, &$renderer, $data) {
 
        if($mode == 'xhtml'){
//           include_once "fortuneclass.php";
            $f = new Fortune;
            $fortunecookie = $f->quoteFromDir(DOKU_PLUGIN . "fortune/dat/");
            $renderer->doc .= "<div class=\"fortune\">";
            $renderer->doc .= nl2br(htmlspecialchars($fortunecookie));
            $renderer->doc .= "</div>";
 
            return true;
        }
        return false;
    }
}
/*
Main methods to use:
 quoteFromDir($dir):
   Quotes from any of the fortune-files in the dir.
 getRandomQuote($file):
   Quotes from the specific file.
 
 Written by Henrik Aasted Sorensen, henrik@aasted.org
 Read more at http://www.aasted.org/quote
 Edited by Otto Vainio
*/
 
class Fortune {
  function quoteFromDir($dir) {
    $amount = 0;
    $index = 0;
    clearstatcache();
    if ( $handle = opendir($dir) ) {
      while (false !== ($file = readdir($handle))) {
        if (strlen($file)>2 && $file!="_dummy") {
          if (substr($file,-4) == ".dat"){
            if (@filemtime(substr($file,0,-4))>@filemtime($dir.$file)) {
                $this->createIndexFile($dir.substr($file,0,-4));
            }
            $number = $this->getNumberOfQuotes($dir . $file);
            $amount += $number;
            $quotes[$index] = $amount;
            $files[$index] = $file;
            $index++;
          } else {
            if (!is_file($dir.$file.".dat") || (@filemtime($dir.$file)>@filemtime($dir.$file.".dat"))) {
                $this->createIndexFile($dir.$file);
                $number = $this->getNumberOfQuotes($dir . $file . ".dat");
                $amount += $number;
                $quotes[$index] = $amount;
                $files[$index] = $file.".dat";
                $index++;
            }
          }
        }
      }
      srand((double)microtime()*1000000);
      $index = rand(0, $amount);
      $i = 0;
      while ($quotes[$i] < $index)  {
        $i++;
      }
      return $this->getRandomQuote($dir .$files[$i]);
    }
    return "";
  }
  /*
   Reads the number of quotes in the file.
  */
  function getNumberOfQuotes($file) {
    $len=0;
    if ($fd = @fopen($file, "rb")) {
      $this->readLong($fd); // Just move over the first long. Might as well be fseek.
      $len =  $this->readLong($fd);
      fclose($fd);
    }
    return $len;
  }
  /*
   Picks quote number $index from the dat-file in $file.
  */
  function getExactQuote($file, $index) {
    if (is_file($file) == FALSE) {
      return;
    }
 
    if ( ($fd = fopen($file, "rb")) == FALSE ) {
      return;
    }
    fseek($fd, 24 + 4 * $index);
 
    $phys_index = $this->readLong($fd);
 
    fclose($fd);
 
    $quotefile = substr($file, 0, strlen($file) - 4);
 
    if ( ($fd = fopen($quotefile, "rb")) == FALSE ) {
      return;
    }
 
    $res = $this->getQuote($fd, $phys_index);
    fclose($fd);
 
    return $res;
  }
 
  /*
   Returns a random quote from $file.
  */
  function getRandomQuote($file) {
    $number = $this->getNumberOfQuotes($file);
 
    $index = rand(0, $number - 1);
 
    return $this->getExactQuote($file, $index);
  }
 
  /*
   Reads a quote from the specified index.
  */
  function getQuote($fd, $index) {
   fseek($fd, $index);
   $line=""; $res = "";
   do {
    $res = $res . $line;
    $line = fgets($fd, 1024);
   } while ( ($line[0] != "%") && (!feof($fd)) );
 
   return $res;
  }
 
  /*
   Gets indexes from the file pointed to by the filedescriptor $fd.
  */
  function getIndices($fd) {
    fseek($fd, 24, SEEK_SET);
    $i = 0;
 
    while ( feof($fd) == FALSE ) {
      $res[$i] = readLong($fd);
      $i++;
    }
    return $res;
  }
 
  function readLong($fd) {
    $res = fread($fd, 4);
    $l = ord($res[3]);
    $l += ord($res[2]) << 8;
    $l += ord($res[1]) << 16;
    $l += ord($res[0]) << 24;
    return $l;
  }
  function createIndexFile($file) {
    $fd = fopen($file, "r");
    if ($fd == false) {
//                  echo "File error!";
      exit;
    }
 
    $i = 1;
    $length = 0;
    $longest = 0;
    $shortest = 100000;
    $indices[0] = 0;
    while (!feof($fd)) {
      $line = fgets($fd);
      if ($line == "%\n") {
        $indices[$i] = ftell($fd);
        $i++;
        if ($length > $longest)
           $longest = $length;
        if ($length < $shortest)
           $shortest = $length;
        $length = 0;
      } else {
        $length = $length + strlen($line);
      }
    }
    fclose($fd);
    $fd = @fopen($file . ".dat", "w");
    if ($fd == false) {
//                  echo "<!-- createIndexFile: Could not write to file....-->";
      exit;
    }
 
    // Write header.
    $this->writeLong($fd, 2);
    $this->writeLong($fd, count($indices));
    $this->writeLong($fd, $longest);
    $this->writeLong($fd, $shortest);
    $this->writeLong($fd, 0);
    $this->writeLong($fd, 37 << 24);
 
    for ($i = 0 ; $i < count($indices) ; $i++) {
      $this->writeLong($fd, $indices[$i]);
    }
 
    fclose($fd);
  }
 
  function writeLong($fd, $l) {
    fwrite($fd, chr ( ($l >> 24) & 255));
    fwrite($fd, chr ( ($l >> 16) & 255));
    fwrite($fd, chr ( ($l >> 8) & 255));
    fwrite($fd, chr ( $l & 255));
  }
}// End of class
?>

Discussion

The link to the archive for the plugin manager is pointing to the archive for your ifauth plugin! — ChristopherArndt 2005-10-17 13:05
Fixed — Otto Vainio 2005-10-18 11:26
How is this plugin used? — M. O'Neill 2005-12-09 06:03
Good question ;-) I totally forgot this. I now added a section usageOtto Vainio 2005-12-09 10:20
Good Idea. Instead of using normal fortune files you also could use it to show a hint of the day for DokuWiki, like used in many software-applications. The only problem would be to collect some hints. Who would be interested? — Konrad Bauckmeier 2005-12-09 11:39
I'm using fortune for some time now and I'm wondering if there is a smart possibility of using different sets of data for several namespaces or by triggering it in the code like: {{fortune:[namespace]}}, so I could use quotes concerning health in a different namespace than quotes from some movie or those concerning sports or politics. The [namespace] trigger should not be mandatory but an option. Any idea, how this could be solved best? — eModul 2007-02-18 14:11
I added a style sheet to this. Wanted to set the FORTUNE off a bit form the rest of the page. Just make a 'style.css' file in the fortune directory.
.fortune {
    border: 1px black solid;

    margin-left: 40px;
    margin-right: 40px;
    margin-bottom: 40px;

    padding: 10px;
    
    font-style: italic;
}

Walter 2009-01-11 03:36

Walter, you're from the future (2009? wow!)! Great plugin! Could this be extended to read a quote from a different file, such as: {{fortune:[dat_filename]}}. This may complement/solve/be-the-same-as eModul's question from above… Juice 2008-06-03

Same fortune every time

How does it update?

Ditto. Is there a way to tell Dokuwiki not to cache this particular element?

⇒ one can deactivate cache with the directive

~~NOCACHE~~ 
plugin/fortune.txt · Last modified: 2014-05-30 14:05 by oiv