Difference between revisions of "Extension:ImageHistoryClear"

From Organic Design wiki
(tidy up style)
m (Reverted edits by Jack (Talk); changed back to last version by Nad)
Line 1: Line 1:
 
<?php
 
<?php
 
/**
 
/**
  * ImageHistoryClear extension - An extension to strip TOC, history and links information from image page views by anonymous users. Made with [http://www.organicdesign.co.nz/Template:Extension Template:Extension].
+
  * ImageHistoryClear extension - A extension made with [http://www.organicdesign.co.nz/Template:Extension Template:Extension] to strip the image history and links from image pages.
 
  *{{php}}{{Category:Extensions|ImageHistoryClear}}{{Category:Jack}}
 
  *{{php}}{{Category:Extensions|ImageHistoryClear}}{{Category:Jack}}
 
  * See http://www.mediawiki.org/wiki/Extension:ImageHistoryClear for installation and usage details
 
  * See http://www.mediawiki.org/wiki/Extension:ImageHistoryClear for installation and usage details
Line 13: Line 13:
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
   
 
   
define ('IMAGEHISTORYCLEAR_VERSION', '1.0.0, 2008-10-21');
+
define('IMAGEHISTORYCLEAR_VERSION', '1.0.0, 2008-10-21');
 
   
 
   
 +
$egImageHistoryClearMagic            = "ImageHistoryClear";
 +
$egImageHistoryClearTag              = "IHistoryClear";
 
$wgExtensionFunctions[]        = 'efSetupImageHistoryClear';
 
$wgExtensionFunctions[]        = 'efSetupImageHistoryClear';
 +
$wgHooks['LanguageGetMagic'][] = 'efImageHistoryClearLanguageGetMagic';
  
$wgExtensionCredits['parserhook'][] = array (
+
global $wgRequest,$wgUser,$wgGroupPermissions,$wgHooks;
 +
 
 +
$title = Title::newFromText($_REQUEST['title']);
 +
if (is_object($title) && ($title->getNamespace() == NS_IMAGE) && $wgUser->isAllowed('edit'))
 +
$wgHooks['OutputPageBeforeHTML'][] = 'egImageHistoryClear';
 +
 
 +
$wgExtensionCredits['parserhook'][] = array(
 
'name'        => 'ImageHistoryClear',
 
'name'        => 'ImageHistoryClear',
 
'author'      => '[http://www.organicdesign.co.nz/wiki/User:Jack User:Jack]',
 
'author'      => '[http://www.organicdesign.co.nz/wiki/User:Jack User:Jack]',
'description' => 'An extension to strip TOC, history and links information from image page views by anonymous users. Made with [http://www.organicdesign.co.nz/Template:Extension Template:Extension].',
+
'description' => 'A extension made with [http://www.organicdesign.co.nz/Template:Extension Template:Extension] to strip the image history and links from image pages.',
 
'url'        => 'http://www.organicdesign.co.nz/Extension:ImageHistoryClear',
 
'url'        => 'http://www.organicdesign.co.nz/Extension:ImageHistoryClear',
 
'version'    => IMAGEHISTORYCLEAR_VERSION
 
'version'    => IMAGEHISTORYCLEAR_VERSION
 
);
 
);
 +
 +
class ImageHistoryClear {
 +
 +
/**
 +
* Properties
 +
*/
 +
var $prop1 = 'default value';
 +
var $prop2 = 'default value';
 +
 +
/**
 +
* Constructor
 +
*/
 +
function __construct() {
 +
global $wgOut, $wgHooks, $wgParser, $egImageHistoryClearMagic, $egImageHistoryClearTag, $jktext;
 +
 +
# Add the parser-function
 +
$wgParser->setFunctionHook($egImageHistoryClearMagic, array($this, 'magicImageHistoryClear'));
 +
 +
# Add the tagHook
 +
$wgParser->setHook($egImageHistoryClearTag, array($this, 'tagIHistoryClear'));
 +
 +
$jktext = $wgOut->mBodytext;
 +
print_r($jktext);
 +
 +
}
 +
 +
/**
 +
* Expand the ImageHistoryClear-magic
 +
*/
 +
function magicImageHistoryClear(&$parser) {
 +
global $egImageHistoryClearMagic;
 +
# Populate $argv with both named and numeric parameters
 +
$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;
 +
}
 +
 +
# Build text of expanded result
 +
$args = '';
 +
foreach ($argv as $k => $v) $args .= "*'''$k:''' ''$v''\n";
 +
        $args =  htmlspecialchars($args);
 +
$text = "=== Magic $egImageHistoryClearMagic(): ===\n$args";
 +
 +
# Return result with available parser flags
 +
return array(
 +
$text,
 +
'found'  => true,
 +
'nowiki'  => false,
 +
'noparse' => false,
 +
'noargs'  => false,
 +
'isHTML'  => false
 +
);
 +
 +
}
 +
 +
/**
 +
* Convert the <IHistoryClear> tags to HTML
 +
*/
 +
function tagIHistoryClear($text,$argv,&$parser) {
 +
global $egImageHistoryClearTag;
 +
$args = '';
 +
foreach ($argv as $k => $v) {
 +
$k = htmlspecialchars($k);
 +
$v = htmlspecialchars($v);
 +
$args .= "<li><b>$k:</b> <i>$v</i></li>\n";
 +
}
 +
$text =  htmlspecialchars($text);
 +
return "<h3>Tag $egImageHistoryClearTag():</h3>\n<ul>$args<li>Content:</b>$text</ul>\n";
 +
}
 +
 +
/**
 +
* Needed in some versions to prevent Special:Version from breaking
 +
*/
 +
function __toString() { return __CLASS__; }
 +
}
 
   
 
   
 
/**
 
/**
  * Function called from the hook BeforePageDisplay, with a regular expression to replace TOC, history and image information.
+
  * Called from $wgExtensionFunctions array when initialising extensions
 
  */
 
  */
 
+
function efSetupImageHistoryClear() {
function efImageHistoryClear (&$out) {
+
global $egImageHistoryClear;
$out->mBodytext = preg_replace ('%(<ul id="filetoc">.+?</ul>)(.*)(<h2 id="filehistory".+$)%s', '\2', $out->mBodytext);
+
$egImageHistoryClear = new ImageHistoryClear();
return true;
 
 
}
 
}
 
   
 
   
 
/**
 
/**
  * Setup function specifies a condition for the user being anonymous and the page being an image page
+
  * Needed in MediaWiki >1.8.0 for magic word hooks to work properly
 
  */
 
  */
+
function efImageHistoryClearLanguageGetMagic(&$magicWords, $langCode = 0) {
function efSetupImageHistoryClear () {
+
global $egImageHistoryClearMagic;
global $wgHooks, $wgUser, $wgRequest;
+
$magicWords[$egImageHistoryClearMagic] = array($langCode, $egImageHistoryClearMagic);
$title = Title::newFromText ($wgRequest->getText('title'));
+
return true;
if ($title->getNamespace () == NS_IMAGE && $wgUser->isAnon ())
 
$wgHooks['BeforePageDisplay'][] = 'efImageHistoryClear';
 
 
}
 
}

Revision as of 22:38, 12 November 2008

<?php /**

* ImageHistoryClear extension - A extension made with Template:Extension to strip the image history and links from image pages.
*Template:Php
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.
* See http://www.mediawiki.org/wiki/Extension:ImageHistoryClear for installation and usage details
*
* @package MediaWiki
* @subpackage Extensions
* @author User:Jack
* @copyright © 2008 User:Jack
* @licence GNU General Public Licence 2.0 or later
*/

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

define('IMAGEHISTORYCLEAR_VERSION', '1.0.0, 2008-10-21');

$egImageHistoryClearMagic = "ImageHistoryClear"; $egImageHistoryClearTag = "IHistoryClear"; $wgExtensionFunctions[] = 'efSetupImageHistoryClear'; $wgHooks['LanguageGetMagic'][] = 'efImageHistoryClearLanguageGetMagic';

global $wgRequest,$wgUser,$wgGroupPermissions,$wgHooks;

$title = Title::newFromText($_REQUEST['title']); if (is_object($title) && ($title->getNamespace() == NS_IMAGE) && $wgUser->isAllowed('edit')) $wgHooks['OutputPageBeforeHTML'][] = 'egImageHistoryClear';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'ImageHistoryClear', 'author' => 'User:Jack', 'description' => 'A extension made with Template:Extension to strip the image history and links from image pages.', 'url' => 'http://www.organicdesign.co.nz/Extension:ImageHistoryClear', 'version' => IMAGEHISTORYCLEAR_VERSION );

class ImageHistoryClear {

/** * Properties */ var $prop1 = 'default value'; var $prop2 = 'default value';

/** * Constructor */ function __construct() { global $wgOut, $wgHooks, $wgParser, $egImageHistoryClearMagic, $egImageHistoryClearTag, $jktext;

# Add the parser-function $wgParser->setFunctionHook($egImageHistoryClearMagic, array($this, 'magicImageHistoryClear'));

# Add the tagHook $wgParser->setHook($egImageHistoryClearTag, array($this, 'tagIHistoryClear'));

		$jktext = $wgOut->mBodytext;
		print_r($jktext);

}

/** * Expand the ImageHistoryClear-magic */ function magicImageHistoryClear(&$parser) { global $egImageHistoryClearMagic; # Populate $argv with both named and numeric parameters $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; }

# Build text of expanded result $args = ; foreach ($argv as $k => $v) $args .= "*$k: $v\n"; $args = htmlspecialchars($args); $text = "=== Magic $egImageHistoryClearMagic(): ===\n$args";

# Return result with available parser flags return array( $text, 'found' => true, 'nowiki' => false, 'noparse' => false, 'noargs' => false, 'isHTML' => false );

}

/** * Convert the <IHistoryClear> tags to HTML */ function tagIHistoryClear($text,$argv,&$parser) { global $egImageHistoryClearTag; $args = ; foreach ($argv as $k => $v) { $k = htmlspecialchars($k); $v = htmlspecialchars($v);

$args .= "

  • $k: $v
  • \n";

    } $text = htmlspecialchars($text);

    return "

    Tag $egImageHistoryClearTag():

    \n

      $args
    • Content:$text

    \n";

    }

    /** * Needed in some versions to prevent Special:Version from breaking */ function __toString() { return __CLASS__; }

    }
    
    

    /**

    * Called from $wgExtensionFunctions array when initialising extensions
    */
    

    function efSetupImageHistoryClear() { global $egImageHistoryClear; $egImageHistoryClear = new ImageHistoryClear(); }

    /**

    * Needed in MediaWiki >1.8.0 for magic word hooks to work properly
    */
    

    function efImageHistoryClearLanguageGetMagic(&$magicWords, $langCode = 0) { global $egImageHistoryClearMagic; $magicWords[$egImageHistoryClearMagic] = array($langCode, $egImageHistoryClearMagic); return true; }