Difference between revisions of "Extension:DisplayFilter.php"

From Organic Design wiki
m
m
 
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:
<?php
+
{{legacy}}
# Extension:Display Filter{{Category:Extensions|DisplayFilter}}{{#Security:*|dev}}{{#Security:view|*}}{{php}}
+
<php><?php
 +
# Extension:Display Filter
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
# - Author: [http://www.organicdesign.co.nz/nad User:Nad]{{Category:Extensions created with Template:Extension}}
+
# - Author: [http://www.organicdesign.co.nz/nad User:Nad]
 
   
 
   
$wgDisplayFilterVersion        = "0.0.0/2007-05-07";
+
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 +
 +
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',
'author' => '[http://www.organicdesign.co.nz/nad User:Nad]',
+
'author'     => '[http://www.organicdesign.co.nz/nad User:Nad]',
'url'   => 'http://www.mediawiki.org/wiki/Extension:Display_Filter'
+
'description' => 'Create links which control visibility of areas by CSS class',
 +
'url'         => 'http://www.mediawiki.org/wiki/Extension:Display_Filter',
 +
'version'    => DISPLAYFILTER_VERSION
 
);
 
);
+
 
 +
# Keep track of JavaScript added to page to avoid doubleups
 +
if (!isset($wgJS)) $wgJS = array();
 +
 
 
class DisplayFilter {
 
class DisplayFilter {
 
# Properties
 
var $prop1: 'default value';
 
var $prop2: 'default value';
 
 
   
 
   
 
# 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'));
 
 
# This allows JS functions to be added to the global $wgJavaScriptFunctions array for inclusion in the output page
 
if (!is_array($wgJavaScriptFunctions)) {
 
$wgJavaScriptFunctions = array();
 
$wgHooks['OutputPageBeforeHTML'][] = $this;
 
}
 
 
}
 
}
 
   
 
   
 
# Expand the filter-magic
 
# Expand the filter-magic
function magicFilter(&$parser) {
+
function filter(&$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)) {
 +
$arg = ereg_replace('[\\\\\'"]','',$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);
# Build text of expaded result
+
$atts = array();
$args = '';
+
$atts['href'] = "javascript:filter(['".join("','",$argv)."'])";
foreach ($argv as $k => $v) $args .= "*'''$k:''' ''$v''\n";
+
$link = '<a';
$text = "=== magicFilter(): ===\n$args";
+
foreach ($atts as $k => $v) $link .= " $k=\"$v\"";
+
$link .= ">$anchor</a>";
# Return result with available parser flags
+
return array($link,'noparse' => true, 'isHTML' => true);
return array(
 
$text,
 
found  => true,
 
nowiki  => false,
 
noparse => false,
 
noargs  => false,
 
isHTML  => false
 
);
 
 
 
}
 
}
 
   
 
   
# Add any JS functions to the output page
+
# Add the javascript to the output object if not added yet and there is at least one tree
function onOutputPageBeforeHTML(&$out) {
+
function addJS() {
global $wgJavaScriptFunctions;
+
global $wgOut,$wgJS,$wgJsMimeType;
foreach ($wgJavaScriptFunctions as $js) $out->addScript("\n<script type=\"text/javascript\">$js</script>\n");
+
if (isset($wgJS['DisplayFilter'])) return;
return true;
+
$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
 
# Needed in some versions to prevent Special:Version from breaking
 
function __toString() { return 'Display Filter'; }
 
function __toString() { return 'Display Filter'; }
Line 78: Line 71:
 
function wfSetupDisplayFilter() {
 
function wfSetupDisplayFilter() {
 
global $wgDisplayFilter;
 
global $wgDisplayFilter;
$wgDisplay Filter = new DisplayFilter();
+
$wgDisplayFilter = new DisplayFilter();
 +
$wgDisplayFilter->addJS(); # addScript doesn't seem to work from within parser
 
}
 
}
 
   
 
   
Line 88: Line 82:
 
}
 
}
 
?>
 
?>
 +
</php>
 +
[[Category:Legacy Extensions|DisplayFilter]]

Latest revision as of 14:55, 22 October 2014

Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, this is only useful for a historic record of work done. You may find a link to the currently used concept or function in this article, if not you can contact the author to find out what has taken the place of this legacy item.

<php><?php

  1. Extension:Display Filter
  2. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  3. - Author: User:Nad

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 {

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

# Expand the filter-magic function filter(&$parser) { $this->addJS(); $argv = array(); foreach (func_get_args() as $arg) if (!is_object($arg)) { $arg = ereg_replace('[\\\\\'"]',,$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("','",$argv)."'])"; $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['DisplayFilter'])) 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(); $wgDisplayFilter->addJS(); # addScript doesn't seem to work from within parser }

  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; } ?> </php>