Extension talk:ShowHide

1.15.1
Does anyone have this working in 1.15.1?--70.103.90.123 08:47, 22 January 2010 (UTC)

MediaWiki 1.11.0
If anyone has a working version of this code please paste the entire source here. Do not use that stupid patcher thing. Some of us still have to use windoze against our will.

Here is. This is nestable version, but it is used as normal. Note: Please edit content page to copy it. If you use this, named "diff file from mediawiki 1.11.0" (new patch of below), nest of tags is possible. (That file is just small diff file. remove lines are appeared with '-' symbol, and added lines are appeared with '+' symbol. @@ line contains start line number. Thus hand patching is a little pain.) -- a.i.


 * 6) This program is free software; you can redistribute it and/or modify
 * 7) it under the terms of the GNU General Public License as published by
 * 8) the Free Software Foundation; either version 2 of the License, or
 * 9) (at your option) any later version.
 * 10) To install, copy the extension to your extensions directory and add line
 * 11) include("extensions/ShowHide.php");
 * 12) to the bottom of your LocalSettings.php
 * 13) Example syntax:
 * 14) Some text (usually title) which will not be hidden
 * 15) Text which will be hidden
 * 16) If  tags are used instead of , the text will be
 * 17) shown by default
 * 18) For more information see its page at
 * 19) http://meta.wikimedia.org/wiki/ShowHide_Extension
 * 1) Some text (usually title) which will not be hidden
 * 2) Text which will be hidden
 * 3) If  tags are used instead of , the text will be
 * 4) shown by default
 * 5) For more information see its page at
 * 6) http://meta.wikimedia.org/wiki/ShowHide_Extension
 * 1) shown by default
 * 2) For more information see its page at
 * 3) http://meta.wikimedia.org/wiki/ShowHide_Extension
 * 1) http://meta.wikimedia.org/wiki/ShowHide_Extension

$wgExtensionFunctions[] = "wfShowHideExtension"; $wgHooks['ParserAfterTidy'][] = 'addShowHideScriptHook';

function wfShowHideExtension { $GLOBALS['wgParser']->setHook("showhide", "ShowHideExtension"); }

function ShowHideExtension($in, $params, &$parser) { static $numrun = 0; $run = $numrun++;

$out = $parser->recursiveTagParse($in); if(		( ($s = strpos($out, "&lt;show&gt;")) !== FALSE && strpos($out, "&lt;/show&gt;") > $s ) || (			($h = strpos($out, "&lt;hide&gt;")) !== FALSE && strpos($out, "&lt;/hide&gt;") > $h )	) {		$GLOBALS['showhidescript'] = true;
 * 1) 	$out = $parser->unstrip($parser->recursiveTagParse($in), $parser->mStripState);

if($s !== FALSE) $act = "show"; else $act = "hide";

if (isset($params['showbuttontext'])) $showbuttontext = $params['showbuttontext']; else $showbuttontext = wfMsg('showtoc');

if (isset($params['hidebuttontext'])) $hidebuttontext = $params['hidebuttontext']; else $hidebuttontext = wfMsg('hidetoc');

$hideline = 'showSHToggle("' . addslashes($showbuttontext) . '","' . addslashes($hidebuttontext) . '",' . $run . ') ';

$out = rtrim(substr($out, 0, strpos($out, "&lt;$act&gt;")), "\n\r"). $hideline. substr($out, strpos($out, "&lt;$act&gt;")); $out = str_replace(			array("&lt;$act&gt;", "&lt;/$act&gt;"),			array("", " "),			$out		); $out = "". trim($out). " ";		if($act == "hide") $out .= "toggleSH($run) "; }	return $out; }

function addShowHideScriptHook(&$parser, &$text) { if ($GLOBALS['showhidescript']) { $GLOBALS['showhidescript'] = false; $text = " ". $text; }

return true; } ?>

MediaWiki 1.8.2
The 'UNIQ' issue seems to exist for any ShowHide content now (tested in Firefox 2.x and IE 6.x).

There is some "Other" patch code posted here that is claimed to work with 1.8.2. I haven't been able to find the code that v1.0patch3 is supposed to patch. There was a version jump from v0.1/v0.1.1 to v1.0patch3. This might have been a typo or the v1.0 code may have been mistakenly left unposted. The patch3 and patch4 versions both seem to have been contributed by "210.253.93.243" Jasonbrewer 15:45, 8 January 2007 (UTC)

Sorry my typo in version number. This should be corrected v0.1+patch3. Please apply these pathces as patch ShowHide.php ShowHide.php.v0.1+patch3 patch ShowHide.php ShowHide.php.v0.1+patch4 or by hand where ShowHide.php is version 0.1. These patches are still work on mediawiki 1.9.0 -- a.i.

patch and patch2 (Though this file is not exists in content page) are not needed. -- a.i.

On display some symbols are automatically changed from original source code by wiki. Please edit content page to copy the patches as other extention's source code. --a.i.

How exactly do I patch?
Here are the md5sums I'm getting before I try the first patch: ShowHide.php (original v0.1): 14ae799c863dfe4412e1907e3ae13dd7 ShowHide.php.v0.1+patch3: 48ea43296c1966d5e98bf4424af97f55

Here is my output from the first patch commmand: #patch ShowHide.php ShowHide.php.v0.1+patch3 patching file ShowHide.php Hunk #2 FAILED at 36. Hunk #3 FAILED at 91. 2 out of 3 hunks FAILED -- saving rejects to file ShowHide.php.rej

What am I doing wrong?

Jasonbrewer 20:51, 19 January 2007 (UTC)

a.i., can you post your final/patched code somewhere or email it to me? My email address is in my profile.

Jasonbrewer 20:59, 19 January 2007 (UTC)

OK, here is my patched (patch3 and patch4) code in running. -- a.i.


 * 6) This program is free software; you can redistribute it and/or modify
 * 7) it under the terms of the GNU General Public License as published by
 * 8) the Free Software Foundation; either version 2 of the License, or
 * 9) (at your option) any later version.
 * 10) To install, copy the extension to your extensions directory and add line
 * 11) include("extensions/ShowHide.php");
 * 12) to the bottom of your LocalSettings.php
 * 13) Example syntax:
 * 14) Some text (usually title) which will not be hidden
 * 15) Text which will be hidden
 * 16) If  tags are used instead of , the text will be
 * 17) shown by default
 * 18) For more information see its page at
 * 19) http://meta.wikimedia.org/wiki/ShowHide_Extension
 * 1) Some text (usually title) which will not be hidden
 * 2) Text which will be hidden
 * 3) If  tags are used instead of , the text will be
 * 4) shown by default
 * 5) For more information see its page at
 * 6) http://meta.wikimedia.org/wiki/ShowHide_Extension
 * 1) shown by default
 * 2) For more information see its page at
 * 3) http://meta.wikimedia.org/wiki/ShowHide_Extension
 * 1) http://meta.wikimedia.org/wiki/ShowHide_Extension

$wgExtensionFunctions[]="wfShowHideExtension";

function wfShowHideExtension {	$GLOBALS['wgParser']->setHook("showhide","ShowHideExtension"); }

function ShowHideExtension($in,$params,&$parser) {	static $numrun=0;

$out = $parser->unstrip($parser->recursiveTagParse($in),$parser->mStripState); if(		( ($s=strpos($out,"&lt;show&gt;"))!==FALSE && strpos($out,"&lt;/show&gt;")>$s ) || (			($h=strpos($out,"&lt;hide&gt;"))!==FALSE && strpos($out,"&lt;/hide&gt;")>$h )	) {		if($numrun==0) { $out= " ".$out; }		$numrun++;

if($s!==FALSE) $act="show"; else $act="hide";

if (isset($params['showbuttontext'])) $showbuttontext = $params['showbuttontext']; else $showbuttontext = wfMsg('showtoc');

if (isset($params['hidebuttontext'])) $hidebuttontext = $params['hidebuttontext']; else $hidebuttontext = wfMsg('hidetoc');

$hideline = ' showSHToggle("' . addslashes($showbuttontext) . '","' . addslashes($hidebuttontext) . '",' . $numrun . ') ';

$out=rtrim(substr($out,0,strpos($out,"&lt;$act&gt;")),"\n\r"). $hideline. substr($out,strpos($out,"&lt;$act&gt;")); $out=str_replace(			array("&lt;$act&gt;",               "&lt;/$act&gt;"),			array(""," "),			$out		); $out="$out "; if($act=="hide") $out.="toggleSH($numrun) "; }	return $out; } ?>

MediaWiki 1.7.1
All TeX--Pngs before the -section look like "�UNIQ2b16b3743d2879b7-math-5522ebf95c92a-QINU" All -sections in and after the -section work. All What to do?

it gives me the same UNIQ-QINU, with a few different extensions that use tags and  code has a bug on comment treatment. Please see below for fixed version. -- a. i.	function extractTagsAndParams( $elements, $text, &$matches, $uniq_prefix = '' ) { static $n = 1; $stripped = ''; $matches = array;

$taglist = implode( '|', $elements ); $start = "/<($taglist)(\\s+[^>]*?|\\s*?)(\/?>)|<(!--)/i";

while ( '' != $text ) { $p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE ); $stripped .= $p[0][0]; if( count( $p ) < 5 ) break;

if( count( $p ) > 5 ) { // comment $element   = $p[4][0]; $attributes = ''; $close     = ''; $inside    = $p[5][0]; } else { // tag $element   = $p[1][0]; $attributes = $p[2][0]; $close     = $p[3][0]; $inside    = $p[4][0]; }

$marker = "$uniq_prefix-$element-". sprintf('%08X', $n++). '-QINU'; $stripped .= $marker;

if ( $close === '/>' ) { // Empty element tag, $content = null; $text = $inside; $tail = null; } else { if( $element == '!--' ) $q = preg_split( '/(-->)/', $inside, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE ); else { $nest = 1; $length = 0; do { // search end tag if ( count( $q = preg_split( "/()/i", $inside, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE ) ) < 3 ) break;

// search start tag $r = preg_split( "/(<$element(\\s+[^>]*?|\\s*?)(\/?)>)|/', $r[5][0], 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE ) ) < 3 )								break;

$inside = $q[2][0]; $length += $q[2][1]; } else if ( count( $r ) < 5 or $q[1][1] < $r[1][1] ) {	// the next tag is an end tag $nest--; $inside = $q[2][0]; $length += $q[1][1]; if ( $nest > 0 )	// the last tag is not content $length += $q[2][1] - $q[1][1]; } else {	// the next tag is a start tag $nest++; $inside = $r[4][0]; $length += $r[4][1]; }					} while ( $nest > 0 );

$content = count( $q ) < 3 ? $p[4][0] : substr( $text, $p[4][1], $length ); }

if( count( $q ) < 3 ) { # No end tag -- let it run out to the end of the text. $tail = ''; $text = ''; } else { $tail = $q[1][0]; $text = $q[2][0]; }			}

$matches[$marker] = array( $element,				$content,				Sanitizer::decodeTagAttributes( $attributes ),				"<$element$attributes$close$content$tail" ); }		return $stripped; }

I recognized that the method of insert javascript in  function of this code is not work in some condition. Please see below for fixed version. -- a. i. 
 * 6) This program is free software; you can redistribute it and/or modify
 * 7) it under the terms of the GNU General Public License as published by
 * 8) the Free Software Foundation; either version 2 of the License, or
 * 9) (at your option) any later version.
 * 10) To install, copy the extension to your extensions directory and add line
 * 11) include("extensions/ShowHide.php");
 * 12) to the bottom of your LocalSettings.php
 * 13) Example syntax:
 * 14) Some text (usually title) which will not be hidden
 * 15) Text which will be hidden
 * 16) If  tags are used instead of , the text will be
 * 17) shown by default
 * 18) For more information see its page at
 * 19) http://meta.wikimedia.org/wiki/ShowHide_Extension
 * 1) Some text (usually title) which will not be hidden
 * 2) Text which will be hidden
 * 3) If  tags are used instead of , the text will be
 * 4) shown by default
 * 5) For more information see its page at
 * 6) http://meta.wikimedia.org/wiki/ShowHide_Extension
 * 1) shown by default
 * 2) For more information see its page at
 * 3) http://meta.wikimedia.org/wiki/ShowHide_Extension
 * 1) http://meta.wikimedia.org/wiki/ShowHide_Extension

$wgExtensionFunctions[]="wfShowHideExtension";

function wfShowHideExtension {	$GLOBALS['wgParser']->setHook("showhide","ShowHideExtension"); }

function ShowHideExtension($in,$params,&$parser) {	static $numrun=0;

$out=''; if($numrun==0) { $out= " "; }	$run = $numrun++;

$out .= $parser->unstrip($parser->recursiveTagParse($in),$parser->mStripState); if(		( ($s=strpos($out,"&lt;show&gt;"))!==FALSE && strpos($out,"&lt;/show&gt;")>$s ) || (			($h=strpos($out,"&lt;hide&gt;"))!==FALSE && strpos($out,"&lt;/hide&gt;")>$h )	) {		if($s!==FALSE) $act="show"; else $act="hide";

if (isset($params['showbuttontext'])) $showbuttontext = $params['showbuttontext']; else $showbuttontext = wfMsg('showtoc');

if (isset($params['hidebuttontext'])) $hidebuttontext = $params['hidebuttontext']; else $hidebuttontext = wfMsg('hidetoc');

$hideline = 'showSHToggle("' . addslashes($showbuttontext) . '","' . addslashes($hidebuttontext) . '",' . $run . ') ';

$out=rtrim(substr($out,0,strpos($out,"&lt;$act&gt;")),"\n\r"). $hideline. substr($out,strpos($out,"&lt;$act&gt;")); $out=str_replace(			array("&lt;$act&gt;","&lt;/$act&gt;"),			array("<div id=\"shinside$run\">"," "),			$out		); $out="<span id=\"showhide$run\">".trim($out)." "; if($act=="hide") $out.="<script type=\"text/javascript\">toggleSH($run) "; }	return $out; } ?>

Bugs fixed codes of nesting

This  function is fixed a bug on comment treatment. -- a. i.	function extractTagsAndParams( $elements, $text, &$matches, $uniq_prefix = '' ) { static $n = 1; $stripped = ''; $matches = array;

$taglist = implode( '|', $elements ); $start = "/<($taglist)(\\s+[^>]*?|\\s*?)(\/?>)|<(!--)/i";

while ( '' != $text ) { $p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE ); $stripped .= $p[0][0]; if( count( $p ) < 5 ) break;

if( count( $p ) > 5 ) { // comment $element   = $p[4][0]; $attributes = ''; $close     = ''; $inside    = $p[5][0]; } else { // tag $element   = $p[1][0]; $attributes = $p[2][0]; $close     = $p[3][0]; $inside    = $p[4][0]; }

$marker = "$uniq_prefix-$element-". sprintf('%08X', $n++). '-QINU'; $stripped .= $marker;

if ( $close === '/>' ) { // Empty element tag, $content = null; $text = $inside; $tail = null; } else { if( $element == '!--' ) { $q = preg_split( '/(-->)/', $inside, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE ); $content = $q[0][0]; } else { $nest = 1; $length = 0; do { // search end tag if ( count( $q = preg_split( "/(<\\/$element\\s*>)/i", $inside, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE ) ) < 3 ) break;

// search start tag $r = preg_split( "/(<$element(\\s+[^>]*?|\\s*?)(\/?)>)|/', $r[5][0], 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE ) ) < 3 )								break;

$inside = $q[2][0]; $length += $q[2][1]; } else if ( count( $r ) < 5 or $q[1][1] < $r[1][1] ) {	// the next tag is an end tag $nest--; $inside = $q[2][0]; $length += $q[1][1]; if ( $nest > 0 )	// the last tag is not content $length += $q[2][1] - $q[1][1]; } else {	// the next tag is a start tag $nest++; $inside = $r[4][0]; $length += $r[4][1]; }					} while ( $nest > 0 );

$content = count( $q ) < 3 ? $p[4][0] : substr( $text, $p[4][1], $length ); }

if( count( $q ) < 3 ) { # No end tag -- let it run out to the end of the text. $tail = ''; $text = ''; } else { $tail = $q[1][0]; $text = $q[2][0]; }			}

$matches[$marker] = array( $element,				$content,				Sanitizer::decodeTagAttributes( $attributes ),				"<$element$attributes$close$content$tail" ); }		return $stripped; }

This ShowHideExtension is fixed a bug on insert javascript in some condition. -- a. i. <?php
 * 1) WikiMedia ShowHide extension v0.1
 * 2) Based on example code from
 * 3) http://meta.wikimedia.org/wiki/Write_your_own_MediaWiki_extension
 * 4) Contains code from MediaWiki's Skin.php and wikibits.js
 * 5) All other code is copyright © 2005 Nikola Smolenski <smolensk@eunet.yu>
 * 6) This program is free software; you can redistribute it and/or modify
 * 7) it under the terms of the GNU General Public License as published by
 * 8) the Free Software Foundation; either version 2 of the License, or
 * 9) (at your option) any later version.
 * 10) To install, copy the extension to your extensions directory and add line
 * 11) include("extensions/ShowHide.php");
 * 12) to the bottom of your LocalSettings.php
 * 13) Example syntax:
 * 14) Some text (usually title) which will not be hidden
 * 15) Text which will be hidden
 * 16) If  tags are used instead of , the text will be
 * 17) shown by default
 * 18) For more information see its page at
 * 19) http://meta.wikimedia.org/wiki/ShowHide_Extension
 * 1) Some text (usually title) which will not be hidden
 * 2) Text which will be hidden
 * 3) If  tags are used instead of , the text will be
 * 4) shown by default
 * 5) For more information see its page at
 * 6) http://meta.wikimedia.org/wiki/ShowHide_Extension
 * 1) shown by default
 * 2) For more information see its page at
 * 3) http://meta.wikimedia.org/wiki/ShowHide_Extension
 * 1) http://meta.wikimedia.org/wiki/ShowHide_Extension

$wgExtensionFunctions[] = "wfShowHideExtension"; $wgHooks['ParserAfterTidy'][] = 'addShowHideScriptHook';

function wfShowHideExtension { $GLOBALS['wgParser']->setHook("showhide", "ShowHideExtension"); }

function ShowHideExtension($in, $params, &$parser) { static $numrun = 0; $run = $numrun++;

$out = $parser->unstrip($parser->recursiveTagParse($in), $parser->mStripState); if(		( ($s = strpos($out, "&lt;show&gt;")) !== FALSE && strpos($out, "&lt;/show&gt;") > $s ) || (			($h = strpos($out, "&lt;hide&gt;")) !== FALSE && strpos($out, "&lt;/hide&gt;") > $h )	) {		$GLOBALS['showhidescript'] = true;

if($s !== FALSE) $act = "show"; else $act = "hide";

if (isset($params['showbuttontext'])) $showbuttontext = $params['showbuttontext']; else $showbuttontext = wfMsg('showtoc');

if (isset($params['hidebuttontext'])) $hidebuttontext = $params['hidebuttontext']; else $hidebuttontext = wfMsg('hidetoc');

$hideline = '<script type="text/javascript">showSHToggle("' . addslashes($showbuttontext) . '","' . addslashes($hidebuttontext) . '",' . $run . ') ';

$out = rtrim(substr($out, 0, strpos($out, "&lt;$act&gt;")), "\n\r"). $hideline. substr($out, strpos($out, "&lt;$act&gt;")); $out = str_replace(			array("&lt;$act&gt;", "&lt;/$act&gt;"),			array("<div id=\"shinside$run\">", " "),			$out		); $out = "<span id=\"showhide$run\">". trim($out). " ";		if($act == "hide") $out .= "<script type=\"text/javascript\">toggleSH($run) "; }	return $out; }

function addShowHideScriptHook(&$parser, &$text) { if ($GLOBALS['showhidescript']) { $GLOBALS['showhidescript'] = false; $text = "<script type=\"text/javascript\"> ". $text; } } ?>

An example is A B C D E

Similar extension
If I understood Extension:ShowHide correctly, Extension:ToggleDisplay offers a similar functionality with some more configuration options. RV1971 17:18, 4 September 2007 (UTC)