Difference between revisions of "Extension:DisplayFilter.php"

From Organic Design wiki
m ({{AddJavaScript}}, const ver etc)
(general pf struct)
Line 6: Line 6:
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
   
 
   
define('DISPLAYFILTER_VERSION','0.0.1, 2007-05-10');
+
define('DISPLAYFILTER_VERSION','0.0.2, 2007-05-15');
  
 
$wgDisplayFilterMagic          = "filter";
 
$wgDisplayFilterMagic          = "filter";
 
$wgExtensionFunctions[]        = 'wfSetupDisplayFilter';
 
$wgExtensionFunctions[]        = 'wfSetupDisplayFilter';
 
$wgHooks['LanguageGetMagic'][] = 'wfDisplayFilterLanguageGetMagic';
 
$wgHooks['LanguageGetMagic'][] = 'wfDisplayFilterLanguageGetMagic';
+
 
 
$wgExtensionCredits['parserhook'][] = array(
 
$wgExtensionCredits['parserhook'][] = array(
 
'name'        => 'Display Filter',
 
'name'        => 'Display Filter',
Line 19: Line 19:
 
'version'    => DISPLAYFILTER_VERSION
 
'version'    => DISPLAYFILTER_VERSION
 
);
 
);
+
 
 +
# Keep track of JavaScript added to page to avoid doubleups
 +
if (!isset($wgJS)) $wgJS = array();
 +
 
 
class DisplayFilter {
 
class DisplayFilter {
 
   
 
   
Line 28: Line 31:
 
# Constructor
 
# Constructor
 
function DisplayFilter() {
 
function DisplayFilter() {
global $wgHooks,$wgParser,$wgDisplayFilterMagic,$wgDisplay FilterTag,$wgJavaScriptFunctions;
+
global $wgParser,$wgDisplayFilterMagic;
+
$wgParser->setFunctionHook($wgDisplayFilterMagic,array($this,'filter'));
# Add the parser-function
 
$wgParser->setFunctionHook($wgDisplayFilterMagic,array($this,'magicFilter'));
 
 
# Add the tagHook
 
$wgParser->setHook($wgDisplayFilterTag,array($this,'tagFoo'));
 
 
 
}
 
}
 
   
 
   
 
# Expand the filter-magic
 
# Expand the filter-magic
 
function magicFilter(&$parser) {
 
function magicFilter(&$parser) {
+
$this->addJS();
# Populate $argv with both named and numeric parameters
 
 
$argv = array();
 
$argv = array();
 
foreach (func_get_args() as $arg) if (!is_object($arg)) {
 
foreach (func_get_args() as $arg) if (!is_object($arg)) {
 
if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]] = $match[2]; else $argv[] = $arg;
 
if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]] = $match[2]; else $argv[] = $arg;
 
}
 
}
 +
$anchor = array_shift($argv);
 +
$atts = array();
 +
$atts['href'] = 'javascript:filter(['.join(',',$arg).'])';
 +
$link = '<a';
 +
foreach ($atts as $k => $v) $link .= "$k=\"$v\"";
 +
$link .= ">$anchor</a>";
 +
return array($link,'noparse' => true, 'isHTML'  => true);
 +
}
 
   
 
   
# Build text of expaded result
+
# Add the javascript to the output object if not added yet and there is at least one tree
$args = '';
+
function addJS() {
foreach ($argv as $k => $v) $args .= "*'''$k:''' ''$v''\n";
+
global $wgOut,$wgJS,$wgJsMimeType;
$text = "=== magicFilter(): ===\n$args";
+
if (isset($wgJS['TreeView'])) return;
+
$wgJS['DisplayFilter'] = true;
# Return result with available parser flags
+
$wgOut->addScript('<script type="'.$wgJsMimeType.'">
return array(
+
function filter(args) {
$text,
+
if (args.length == 1) {
found  => true,
+
}
nowiki  => false,
+
else {
noparse => false,
+
}
noargs  => false,
+
}
isHTML  => false
+
</script>');
);
 
 
 
}
 
}
+
 
 
# Needed in some versions to prevent Special:Version from breaking
 
# Needed in some versions to prevent Special:Version from breaking
 
function __toString() { return 'Display Filter'; }
 
function __toString() { return 'Display Filter'; }
Line 72: Line 74:
 
global $wgDisplayFilter;
 
global $wgDisplayFilter;
 
$wgDisplayFilter = new DisplayFilter();
 
$wgDisplayFilter = new DisplayFilter();
 +
if (version_compare($wgVersion,'1.9.0')<0) $wgTreeView->addJS(); # addScript doesn't seem to work from within parser on MW versions < 1.9.x
 
}
 
}
 
   
 
   
Line 80: Line 83:
 
return true;
 
return true;
 
}
 
}
 
{{AddJavaScript}}
 
 
?>
 
?>

Revision as of 06:34, 15 May 2007

<?php

  1. Extension:Display Filter
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

{{#Security:*|dev}}{{#Security:view|*}}Template:Php

  1. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  2. - Author: User:NadCategory:Extensions created with Template:Extension

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('DISPLAYFILTER_VERSION','0.0.2, 2007-05-15');

$wgDisplayFilterMagic = "filter"; $wgExtensionFunctions[] = 'wfSetupDisplayFilter'; $wgHooks['LanguageGetMagic'][] = 'wfDisplayFilterLanguageGetMagic';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'Display Filter', 'author' => 'User:Nad', 'description' => 'Create links which control visibility of areas by CSS class', 'url' => 'http://www.mediawiki.org/wiki/Extension:Display_Filter', 'version' => DISPLAYFILTER_VERSION );

  1. Keep track of JavaScript added to page to avoid doubleups

if (!isset($wgJS)) $wgJS = array();

class DisplayFilter {

# Properties var $prop1: 'default value'; var $prop2: 'default value';

# Constructor function DisplayFilter() { global $wgParser,$wgDisplayFilterMagic; $wgParser->setFunctionHook($wgDisplayFilterMagic,array($this,'filter')); }

# Expand the filter-magic function magicFilter(&$parser) { $this->addJS(); $argv = array(); foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]] = $match[2]; else $argv[] = $arg; } $anchor = array_shift($argv); $atts = array(); $atts['href'] = 'javascript:filter(['.join(',',$arg).'])'; $link = '<a'; foreach ($atts as $k => $v) $link .= "$k=\"$v\""; $link .= ">$anchor</a>"; return array($link,'noparse' => true, 'isHTML' => true); }

# Add the javascript to the output object if not added yet and there is at least one tree function addJS() { global $wgOut,$wgJS,$wgJsMimeType; if (isset($wgJS['TreeView'])) return; $wgJS['DisplayFilter'] = true; $wgOut->addScript('<script type="'.$wgJsMimeType.'"> function filter(args) { if (args.length == 1) { } else { } } </script>'); }

# Needed in some versions to prevent Special:Version from breaking function __toString() { return 'Display Filter'; }

	}

  1. Called from $wgExtensionFunctions array when initialising extensions

function wfSetupDisplayFilter() { global $wgDisplayFilter; $wgDisplayFilter = new DisplayFilter(); if (version_compare($wgVersion,'1.9.0')<0) $wgTreeView->addJS(); # addScript doesn't seem to work from within parser on MW versions < 1.9.x }

  1. Needed in MediaWiki >1.8.0 for magic word hooks to work properly

function wfDisplayFilterLanguageGetMagic(&$magicWords,$langCode = 0) { global $wgDisplayFilterMagic; $magicWords[$wgDisplayFilterMagic] = array(0,$wgDisplayFilterMagic); return true; } ?>