RandomQuote-OldVersion

Goal

Insert a random quote into a wiki page.

Solution

randomquote.zipΔ

randomquote.zip unpacks into cookbook/quote.php.

add the following line to config.php

include_once("$farmD/cookbook/quote.php");

If your site doesn't use wikifarms, the correct include statement is

include_once('cookbook/quote.php');

(Note: I had trouble getting this script to work until I deleted the Main.Quotes file that was included in the zip. I then created Main.Quotes using the normal procedure for creating a new wikipage.)

Discussion

Randomquote adds the markup (:randquote:) which will be replaced by a randomly chosen line from quotes.txt.

The format for quotes.txt is one quote per line with pipes ( | ) used to denote line breaks within the quote. Each pipe will be replaced with <br> before the quote is inserted.

You can see it in action at: http://www.hostgrabber.de

Contributor

  • Dave Noonan ~ original Author ( http://wiki.thunderbunny.org/GeekStuff/RandomQuote )
  • Jens Rosenfeld ~ Update to PmWiki 2.x
  • Tiago Luis Bonamigo ~ updated to use a pmwiki page as the source for quotes
  • Carlos Alberto Bonamigo ~ Minor change, the first asterisk gives you an empty quote on the array. Fixed now.
  • Daniel Scheibler ~ updated the pmwiki page version to be configurable

See also

RandomQuoteWiki


If you want to avoid wikiword processing you may use the keep()? function:

 // the original code
 //	return "<div id=\"randquote\">$RandomQuote2</div>";
 // adding the keep() function
    return keep("<div id=\"randquote\">$RandomQuote2</div>");

-- Guillaume (2005-02-07)


I liked this plugin so much, that i decided to migrate it to PmWiki 2.x -- Jens Rosenfeld (2005-01-08)


Tiago Luis Bonamigo - Here is my update

 // Script: Copyright 2004 Dave Noonan, DaveNoonan@ThunderBunny.org
 // 2004-02-15
 // Update for PmWiki 2.x needs
 //
 // 2005-01-08 by Jens Rosenfeld, webmaster[at]hostgrabber.de
 // Tested with PmWiki 2.0 Beta 14
 //
 //Changes made by Tiago Luis Bonamigo 
 //2005-01-08
 //Tested with pmwiki-2.0.beta14
 // 
 //=======================
 // To make this script work, extract it in your pmwiki/cookbook directory.
 // Then add the following line to config.php:
 //
 //      include("cookbook/quote.php");
 //
 // Insert (:randquote:) as a markup to your wikipage.
 // The markup will be replaced with the quote  
 //
 //=======================
 // Configuration:
 // 
 // Fill a pmwiki page with quotes and start every quote with an asterisk.
 // Put (:randquote:) inside every other page in pmwiki and get it replaced with a random quote.
 //
 //Changelog:
 //
 // Now  you can choose a pmwiki page, full of quotes separated by an asterisk.
 // We assume the new line will always be "2" exponencial, if you know what I mean.
 // Changed the markup to be (:randquote:).
 // That's all folks!!! 




 // Function to get a random line out of text file and sub <br> for | with a 
 // div named randquote around it.

 function f_RandomQuote() {

	$QuoteFile = "wiki.d/Main.Quotes";

	$Quotes = fopen($QuoteFile, "r");
	$conteudo = fread($Quotes, filesize ($QuoteFile));
	fclose ($Quotes);

	$arrConteudo = explode("\n",$conteudo);

	foreach($arrConteudo as $linha) 
		if(substr($linha,0,6) == "text=*") {
			$arrQuotes = substr($linha,6);
			$arrQuotes = str_replace("2","<BR>",$arrQuotes);// have to change the first  
                                                                        // position on str_replace, 
                                                                        // to be "2" exponential.
			$arrQuotes = explode("*",$arrQuotes);			
		}


	srand( (double)microtime() * 1000000 );
	shuffle($arrQuotes);
	return "<div id=\"randquote\">$arrQuotes[0]</div>";	

}

 // Pointer to the function
 $randQuote =& f_RandomQuote();

 // Add (:randquote:) markup that will be replaced with the quote
 Markup("randquote","directives","/\\(:randquote:\\)/",$randQuote);

I wrote it slightly differently, as I like the idea to separate the quotes by "*", the reason being that the quote page then displays nicely as a bulleted list with one quote per bullet. To do this, I used a "**" to introduce a line break inside a quote. The code goes like this: Philippe? - 11/01/2005

 foreach($arrConteudo as $linha) 
 	if(substr($linha,0,6) == "text=*") {
 		$arrQuotes = substr($linha,6);
 		$arrQuotes = str_replace("**","<BR>",$arrQuotes);
 		$arrQuotes = explode("2*",$arrQuotes);			
 	}

Replacing the 2 as an exponential 2, of course. This way, the quotes list can be like this:

  • quote 1**author 1
  • quote 2 - Line 1**Line 2**author 2
  • quote 3**author 3

What do you think ?


I like it a lot as well (the version above), but I have a problem in that it still interprets wiki words, but does not interpret wiki markups (in particular those used to prevent interpretation :) ). Does anyone have a solution for this ? Philippe? - 11/01/2005


The answer to your problem is that the quotes are extracted from a pmwiki page without passing through pmwiki markup processing, just the "find & link" one. Obvious?! I know... CarlosAB


OK, thanks for the explanation, any way around this ? Philippe? - 13/01/2005


Great Idea, Tiago. That makes things much more easier! -- Jens Rosenfeld (2005-01-08)


Daniel Scheibler (2005-01-15):

Changes:
  • change "2*" into "$Newline" to use wiki-specific newline character
  • change "<BR>" into "<br />" for HTML 4 Compatibility
  • meaning of config variable $QuoteFile changed; could now be changed in local.php
  • insetion of a config variable $QuoteLineDelimiter, which will be changed in <br />

important: variable configuration has to be done before include_once("cookbook/quote.php");

updated source:

 <?php
 // Script: Copyright 2004 Dave Noonan, DaveNoonan@ThunderBunny.org
 // 2004-02-15
 //
 // Update for PmWiki 2.x needs
 // 2005-01-08 by Jens Rosenfeld, webmaster[at]hostgrabber.de
 // Tested with PmWiki 2.0 Beta 14         
 //
 //=======================
 // To make this script work, extract it in your pmwiki/cookbook directory.
 // Then add the following line to config.php:
 //
 //      include("cookbook/quote.php");
 //
 // Insert (:randquote:) as a markup to your wikipage.
 // The markup will be replaced with the quote 
 //
 //=======================
 // Configuration:
 // 
 // Fill a pmwiki page with quotes and start every quote with an asterisk.
 // Put (:randquote:) inside every other page in pmwiki and get it replaced with a random quote.
 //
 //Changelog:
 //
 // Now  you can choose a pmwiki page, full of quotes separated by an asterisk.
 // We assume the new line will always be "". 

 // Changed the markup to be (:randquote:).
 // That's all folks!!! 

 // ============== Changes by Daniel Scheibler <pmwiki@scheiby.de> <[(approve links)
  edit
  diff]
 // * change "2*" into "$Newline" to use wiki-specific newline character
 // * insertion of a config variable $QuoteRemoveItemize to change between $Newline 
 //   and $Newline followed by an *
 // * change "<BR>" into "<br />" for HTML 4 Compatibility
 // * meaning of config variable $QuoteFile changed; could now be changed in local.php
 // * insetion of a config variable $QuoteLineDelimiter, which will be changed in <br />

 // Function to get a random line out of text file and sub <br> for | with a 
 // div named randquote around it.

 SDV($QuoteFile, "Main.Quotes");
 SDV($QuoteLineDelimiter, "**");
 SDV($QuoteRemoveItemize, false);

 function f_RandomQuote()
 {
   global $Newline;
   global $QuoteFile;
   global $WorkDir;
   global $QuoteLineDelimiter;
   global $QuoteRemoveItemize;

   $Quotes = fopen($WorkDir . "/" . $QuoteFile, "r");
   $conteudo = fread($Quotes, filesize ($WorkDir . "/" . $QuoteFile));
   fclose ($Quotes);

   $arrConteudo = explode("\n", $conteudo);

   foreach($arrConteudo as $linha) {
     if(substr($linha,0,5) == "text=") {
       $arrQuotes = substr($linha, 5);
       $arrQuotes = str_replace($QuoteLineDelimiter, "<br />", $arrQuotes); 

       if ($QuoteRemoveItemize) {
         $arrQuotes = explode($Newline . "*", $arrQuotes);
       } else {
         $arrQuotes = explode($Newline, $arrQuotes);
       }
     }
   }


   srand( (double)microtime() * 1000000 );
   shuffle($arrQuotes);
   return "<div id=\"randquote\">$arrQuotes[0]</div>";
 }

 // Pointer to the function
 $randQuote =& f_RandomQuote();

 // Add [:randquote:] markup that will be replaced with the quote
 Markup("randquote", "directives", "/\\ as a markup to your wikipage.
 // The markup will be replaced with the quote 
 //
 //=======================
 // Configuration:
 // 
 // Fill a pmwiki page with quotes and start every quote with an asterisk.
 // Put (:randquote:) inside every other page in pmwiki and get it replaced with a random quote.
 //
 //Changelog:
 //
 // Now  you can choose a pmwiki page, full of quotes separated by an asterisk.
 // We assume the new line will always be "". 
 //
 // Changed the markup to be (:randquote:).
 // That's all folks!!! 
 //
 // ============== Changes by Daniel Scheibler <pmwiki@scheiby.de> <[(approve links)
  edit
  diff]
 // * change "2*" into "$Newline" to use wiki-specific newline character
 // * insertion of a config variable $QuoteRemoveItemize to change between $Newline 
 //   and $Newline followed by an *
 // * change "<BR>" into "<br />" for HTML 4 Compatibility
 // * meaning of config variable $QuoteFile changed; could now be changed in local.php
 // * insetion of a config variable $QuoteLineDelimiter, which will be changed in <br /> 
 //
 // ============== Changes by Daniel Scheibler <pmwiki@scheiby.de> <[(approve links)
  edit
  diff]
 // * because of errors in cooperation with file storage in GZ format changing reading from $QuoteFile
 //   into standard PmWiki ReadPage function
 //
 // Function to get a random line out of text file and sub <br> for | with a 
 // div named randquote around it.

 SDV($QuoteFile, "Main.Quotes");
 SDV($QuoteLineDelimiter, "**");
 SDV($QuoteRemoveItemize, false);

 function f_RandomQuote()
 {
   global $QuoteFile;
   global $QuoteLineDelimiter;
   global $QuoteRemoveItemize;

   $conteudo = ReadPage($QuoteFile);

   $arrQuotes = $conteudo[text];
   $arrQuotes = str_replace($QuoteLineDelimiter, "<br />", $arrQuotes); 

   if ($QuoteRemoveItemize) {
     $arrQuotes = explode("\n*", $arrQuotes);
     $arrQuotes[0] = substr($arrQuotes[0], 1);
   } else {
     $arrQuotes = explode("\n", $arrQuotes);
   }


   srand( (double)microtime() * 1000000 );
   if (isset($arrQuotes)) {
     shuffle($arrQuotes);
     return "<div id=\"randquote\">$arrQuotes[0]</div>";
   } else {
     return "<div id=\"randquote\"></div>";
   }
 }

 // Pointer to the function
 $randQuote =& f_RandomQuote();

 // Add [:randquote:] markup that will be replaced with the quote
 Markup("randquote", "directives", "/
<?php if (!defined('PmWiki')) exit();
// Script: Copyright 2004 Dave Noonan, DaveNoonan@ThunderBunny.org
// 2004-02-15
//
// Update for PmWiki 2.x needs
// 2005-01-08 by Jens Rosenfeld, webmaster[at]hostgrabber.de
// Tested with PmWiki 2.0 Beta 14         
//
// Tested with PmWiki 2.0 Beta 32 - 2005-04-21 by Daniel Scheibler <pmwiki@scheiby.de>
//
//=======================
// To make this script work, extract it in your pmwiki/cookbook directory.
// Then add the following line to config.php:
//
//      include("$farmD/cookbook/quote.php");
//
// Insert (:randquote:) as a markup to your wikipage.
// The markup will be replaced with the quote 
//
//=======================
// Configuration:
// 
// Fill a pmwiki page with quotes and start every quote with an asterisk.
// Put (:randquote:) inside every other page in pmwiki and get it replaced with a random quote.
//
//Changelog:
//
// Now  you can choose a pmwiki page, full of quotes separated by an asterisk.
// We assume the new line will always be "". 

// Changed the markup to be (:randquote:).
// That's all folks!!! 

// ============== Changes by Daniel Scheibler <pmwiki@scheiby.de> <http://www.scheiby.org>
// * change "2*" into "$Newline" to use wiki-specific newline character
// * insertion of a config variable $QuoteRemoveItemize to change between $Newline 
//   and $Newline followed by an *
// * change "<BR>" into "<br />" for HTML 4 Compatibility
// * meaning of config variable $QuoteFile changed; could now be changed in local.php
// * insetion of a config variable $QuoteLineDelimiter, which will be changed in <br />

// ============== Changes by Daniel Scheibler <pmwiki@scheiby.de> <http://www.scheiby.org>
// * because of errors in cooperation with file storage in GZ format changing reading from $QuoteFile
//   into standard PmWiki ReadPage function

// Function to get a random line out of text file and sub <br> for | with a 
// div named randquote around it.

//=======================
//Changes by Feral at Tue, 27  Dec  2005 22:10:19 PST
// I have added the ability to specify a page via page=group.pagename, i.e. (:randquote page=Quotes.Einstein:)

SDV($QuoteFile, "Main.Quotes");
SDV($QuoteLineDelimiter, "**");
SDV($QuoteRemoveItemize, false);

function f_RandomQuote($opts)
{
  global $QuoteFile;
  global $QuoteLineDelimiter;
  global $QuoteRemoveItemize;

  $defaults = array(
    'page'=>''
  );
  $args = array_merge($defaults, ParseArgs($opts));
  $page = $args['page'];
  if ($page == '') {
    $page = $QuoteFile;
  }

  $conteudo = ReadPage($page);

  $arrQuotes = $conteudo[text];
  $arrQuotes = str_replace($QuoteLineDelimiter, "<br />", $arrQuotes); 

  if ($QuoteRemoveItemize) {
    $arrQuotes = explode("\n*", $arrQuotes);
    $arrQuotes[0] = substr($arrQuotes[0], 1);
  } else {
    $arrQuotes = explode("\n", $arrQuotes);
  }


  srand( (double)microtime() * 1000000 );
  if (isset($arrQuotes)) {
    shuffle($arrQuotes);
    return "<div id=\"randquote\">$arrQuotes[0]</div>";
  } else {
    return "<div id=\"randquote\">This space for rent.</div>";
  }
}

// Pointer to the function
// [Feral:361/05@22:07] Feral sees no reason to do this, so she isn't (=
//$randQuote =& f_RandomQuote();

// Add (:randquote:) markup that will be replaced with the quote
// [Feral:361/05@22:07] Added ability to specify page via page=group.pagename, i.e. (:randquote page=Quotes.Einstein:)
Markup("randquote", "directives", "/\\(:randquote\\s*(.*?):\\)/e", "f_RandomQuote(PSS('$1'))");


?>

I have been using this with pmwiki-2.1.beta17 for a while with no unexpected results. ... I think we all know how that works though! (=

Just a side note, but when you have this set as:

$QuoteLineDelimiter = "[[<<]]";
$QuoteRemoveItemize = true;

You can have entries like so:

*"Do, or do not. There is no 'try'"[[<<]]~ Yoda
*"The impossible is only a little bit harder."[[<<]]~ My Grandmother.
*"When you're going through hell, keep going."[[<<]]~ Winston Churchill
*"It is better to be hated for what you are than to be loved for what you are not."[[<<]]~ Andre Gide

Which makes directly viewing the quote page(s) considerably easier. (=

-- Feral January 06, 2006, at 09:38 PM
Perhaps this page should be redone, so it could be a little easier to read, and some of the outdated information can be deleted. I also had some problems with the zip file not extract properly. Would be better to just put up the php script and then give instructions on this page to create Main.Quotes and perhaps some sample text to cut and paste into that page. Caveman