[Mediawiki-l] extensions/hooks/javascript help

ben wiseley wiseleyb at gmail.com
Sun Oct 9 16:28:20 UTC 2005


I wanted to do something very simple (or so I thought) on my recipe wiki (
http://mudabone.com/recipe ) in MediaWiki1.5

I wanted to have a simple form to allow users to add a recipe in a standard
format.

So, I grabbed the HTML from the form that appears when you add/edit a page.

I wrote an extension that outputs a modified format of this which is a
simple html form and made wpTextbox1 hidden (which is the large text box on
the add/edit form page).

The form, onsubmit, calls a javascript function which puts together the
format I wanted to support and sets it to the value in wpTextbox1

In theory this works but, for reasons I don't understand, tidy seems to
managle the javascript.

I read about the hooks thing (
http://meta.wikimedia.org/wiki/Write_your_own_MediaWiki_extension#Hooks )
and implemented a simple hack to get around tidy.

$wgHooks['ParserAfterTidy'][] = 'parse_addrecipe2' ;

function parse_addrecipe2(&$parser , &$text)
{
$text .= gethtml();
$text .= getjavascript();
$wgHooks['ParserAfterTidy'][] = null;
}

And that works great except I now have that form on every page in the wiki.

How do I remove a hook?. I obviously barely understand what I'm doing here.
Any help would be much appreciated.

Is there a better way to do this? I'm trying to idiot proof a few of the
basics so users wouldn't have to learn wiki-script (or at least not
initially, this would give them a starting point for editing).

-ben

------------------------ Code for extension if it helps
---------------------------------------------

<?
$wgExtensionFunctions[] = "wfFormsAddRecipe";

function wfFormsAddRecipe ()
{
global $wgParser ;
$wgParser->setHook ( "addrecipe" , parse_addrecipe ) ;
global $wgHooks ;
$wgHooks['ParserAfterTidy'][] = 'parse_addrecipe2' ;
}

function parse_addrecipe2(&$parser , &$text)
{
$text .= gethtml();
$text .= getjavascript();
$wgHooks['ParserAfterTidy'][] = null;
}
function parse_addrecipe($text)
{
//return gethtml();
}



function gethtml()
{
$ret ='
<form id="editform" name="editform" method="post" action="
/recipe/index.php"
enctype="multipart/form-data" onsubmit="validateRecipe(this)">
<input type="hidden" name="action" value="submit"/>

<table>
<tr><td>Recipe Name:</td><td><input type="text" name="title" value="New
Recipe"/></td></tr>
<tr><td>Original Recipe URL:</td><td><input type="text"
name="original_url"/></td></tr>
<tr><td>URL Text:</td><td><input type="text" name="url_text"/></td></tr>
<tr><td>Recipe By:</td><td><input type="text" name="recipe_by"/></td></tr>
<tr><td>Ingredients:<br>
<small>one ingredient per line with " "<br>
example:<br>
one
two</small></td><td><textarea name="ingredients" rows="15"
cols="60"/></textarea></td></tr>
<tr><td>Directions:</td><td><textarea name="directions" rows="15"
cols="60"/></textarea></td></tr>
</table>
<input type="hidden" name="wpTextbox1"/>
<br>

<br />Summary: <input tabindex="2" type="text" value="" name="wpSummary"
maxlength="200" size="60" /><br />
<input tabindex="3" type="checkbox" value="1" name="wpMinoredit"
accesskey="i" id="wpMinoredit" /><label for="wpMinoredit" title="Mark this
as a minor edit [alt-i]">This is a minor edit</label><input tabindex="4"
type="checkbox" name="wpWatchthis" accesskey="&lt;accesskey-watch&gt;"
id="wpWatchthis" /><label for="wpWatchthis" title="Add this page to your
watchlist [alt-w]">Watch this page</label><br />

<input tabindex="5" id="wpSave" type="submit" value="Save page"
name="wpSave" accesskey="s" title="Save your changes [alt-s]"
onclick="validateRecipe(this)"/>
<input tabindex="6" id="wpPreview" type="submit" value="Show preview"
name="wpPreview" accesskey="p" title="Preview your changes, please use this
before saving! [alt-p]"/>
<input tabindex="7" id="wpDiff" type="submit" value="Show changes"
name="wpDiff" accesskey="v" title="Show which changes you made to the text.
[alt-d]"/>
<em><a href="/recipe/index.php?title=Test" title="Test">Cancel</a></em> |
<em><a target="helpwindow"
href="/recipe/index.php?title=Help:Editing">Editing help</a> (opens in new
window)</em><div id="editpage-copywarn">
<p>Please note that all contributions to RecipeWiki
may be edited, altered, or removed by other contributors.
If you don"t want your writing to be edited mercilessly, then don"t submit
it here.<br />

You are also promising us that you wrote this yourself, or copied it from a
public domain or similar free resource (see <a
href="/recipe/index.php?title=RecipeWiki:Copyrights&amp;action=edit"
class="new" title="RecipeWiki:Copyrights">Project:Copyrights</a> for
details).
<strong>DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!</strong>
</p>
</div>

<input type="hidden" value="" name="wpSection" />
<input type="hidden" value="" name="wpEdittime" />

<input type="hidden" value="c032ebbd7fd87a6ac32f92368a8940aa"
name="wpEditToken" />
</form>
';
return $ret;
}
function getjavascript()
{
$ret ='
<script language="javascript">

function validateRecipe(form)
{
var rn = "\r\n";
var res = "[[Category:Recipe]]" + rn;

if (form.original_url.value != "")
{
if (form.url_text.value == "")
{
form.url_text.value = "Original Recipe";
}
res += "==Credits==" + rn + "[" + form.original_url.value + " " +
form.url_text.value + "] ";
}

if (form.recipe_by.value != "")
{
res += "by " + form.recipe_by.value;
}

res += rn + rn;
res += "==Ingredients==" + rn;
res += form.ingredients.value + rn;

res += "==Directions==" + rn + form.directions.value;

form.wpTextbox1.value = res;
return true;
//form.submit
}

</script>
';
return $ret;
}

?>



More information about the MediaWiki-l mailing list