<?php if (!defined('PmWiki')) exit();
/*
    Automatic spelling corrections (typing errors)
  COPYRIGHT
    Copyright 2004 by Thomas Pahl, tp > codework.de
  LICENSE
    Same as PmWiki, see www.pmwiki.org
  DESCRIPTION
    This PmWiki plugin helps fighting the many typical typing errors you (as me)
    will make in editing PmWiki entries.
    It applies simple text substitions to the text which is input in the page
    edit box, before it is saved or redisplayed for preview.

    The substitions can be defined in an array in local/config.php or, much more
    comfortable in maintenance, in a special Wiki page.

  IMPLEMENTATION
    The code intercepts the HandleEdit function (will properly stack if other 
    plugins do the same) and modifies the $_POST['text'] value but only if the
    "Save" or "Preview" button has been hit.
    It applies simple text substitutions defined in the $SpellWords[] array.
    The array can be built in two ways:
    1. In local/config.php, define substitutions values by
       $SpellWords['worng']=>'wrong';
       $SpellWords['gloabl']=>'global';
       $SpellWords['Oracel']=>'Oracle';
    2. More comfortable in maintenance, create a Wiki file
       "Main.SpellingCorrections" which contains lines of the format:
         worng=wrong
         gloabl=global
         Oracel=Oracle
       Note: all lines not containing a '=' are ignored, so they can be used as
       comments.
       Note: you may want to limit edit permission on this page.

    The code has been implemented in a couple of functions, in order to make
    local modifications easier, and to keep existing global variables untouched.
    It could be linear code.

  CONFIGURATION
    The following variables can be set before the script is included in
    local/config.php:
    $SpellCorrectPagename - full Wiki pagename of definition file
    $SpellWords - key=>value substitions (plain text, no regexps)

  INSTALLATION
    * Put this script in your PmWiki 'local' directory
    * Include the plugin from your 'config.php' file:
      include_once('local/spellcorrect.php');
    * Optionally configure the script
    * Optionally create a Wiki page 'Main.SpellingCorrections' (assuming 'Main'
      is your default group) - the pagename can be configured.

  REQUIREMENTS
    This has been tested with PmWiki 1.0.8 and PHP 4.3.
    I have not checked if there could be problems with setting $_POST in some
    PHP setups.
  ACKNOWLEDGEMENTS
    The code reading the Wiki page has been copied from some other script,
    I'm sorry I don't remember which one.
*/


#
# Define the Wiki page containing the substitutions.
# Set the value to null to disable the file.
#
SDV($SpellCorrectPagename, $DefaultGroup.'/SpellingCorrections');

#
# Optionally, you may also define substitutions here.
# Note that substitutions defined in the array have priority over those defined
# in the Wiki file. This is to give final word to the Admin :-)
#
##$SpellWords = array();
##$SpellWords['worng'] = 'wrong';

$SpellCorrectEditFunction = $HandleActions['edit'];
$HandleActions['edit'] = 'SpellCorrectHandleEdit';

##
# Load the substitions from the Wiki.
#
function loadSpellCorrections() {
	global $SpellWords;
	global $SpellCorrectPagename;

	if (!trim($SpellCorrectPagename))
		return;
	$page = ReadPage($SpellCorrectPagename);
	if (!$page) return;
	$text = $page['text'];
	##Dprint $text;
	if (!is_array($SpellWords))
		$SpellWords = array();
	foreach (explode("\n",$text) as $l) {
		$l = trim($l);
		if ($l and !(strpos($l, '=') === false)) {
			list($k, $v) = explode('=', $l);
			if ($v and !isset($SpellWords[$k]))
				$SpellWords[$k] = $v;
		}
	}
}

##
# Apply typing corrections
#
function SpellCorrect($text, $pagename) {
	global $SpellWords;
	global $SpellCorrectPagename;

	if (!$text)
		return $text;
	# do not process the substitution definition file
	if ($pagename == str_replace('/', '.', $SpellCorrectPagename))
		return $text;
	if ($SpellCorrectPagename)		# if defined, load substitution page
		loadSpellCorrections();
	if (!is_array($SpellWords))
		return $text;
	foreach ($SpellWords as $k=>$v) {
		#$text = preg_replace('!'.$k.'!', $v, $text);
		$text = str_replace($k, $v, $text);
	}
	return $text;
}

##
# 'HandleEdit' interceptor
#
function SpellCorrectHandleEdit($pagename) {
	global $SpellCorrectEditFunction;
	# modify text if "Save" or "Preview" button was used
	if ($_POST['post'] or $_POST['preview'])
		$_POST['text'] = SpellCorrect($_POST['text'], $pagename);
	# pass on to original HandleEdit
	$SpellCorrectEditFunction($pagename);
}

# vim:set ts=4 sw=4 ft=php:
?>