Extension:PatchOutput

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
PatchOutput

Release status: beta

Implementation Extended syntax
Description patch MediaWiki output before displaying
Author(s) RV1971 Talk
Version 0.2 (2007-07-19)
MediaWiki >=1.10.0
Download #Source
#Change Log

Contents

[edit] Purpose

This extension applies some patches to the HTML code of a page before it is displayed. It can be seen as a demo for using the OutputPageBeforeHTML hook. You're likely to customize it for your needs.

The current version achieves the following:

  • External links are automatically opened in a new browser window.
  • The server name "localhost" is translated to the current server. This is useful if your MediaWiki site can be addressed by different IP addresses or names, and you want to comfortably access other resources on the same webserver. The normal meaning of "localhost" (the machine where the client is running) would not have any sense on a MediaWiki page, anyway.

[edit] Warnings

  • The result is achieved via crude string search-and-replace on the html code. The search patterns are likely to be subject to change in future MediaWiki versions.

[edit] Installation

  • Save the #Source to a file extensions/PatchOutput/PatchOutput.php.
  • Add the following line to your LocalSettings.php:
require_once( "extensions/PatchOutput/PatchOutput.php" );
  • The search-and-replace patterns are stored in a static class member ExtPatchOutput::$mTable. It contains an array where keys are the terms to search and values the corresponding replacements. You can redefine this array or add entries in your LocalSettings.php.

Even though this extension has been tested on MediaWiki 1.10, it should work on MediaWiki >= 1.6.0.

[edit] Source

<?php
 
$wgExtensionCredits['other'][] = array(
  'name' => 'PatchOutput',
  'version' => ExtPatchOutput::VERSION,
  'author' => '[http://www.mediawiki.org/wiki/User:RV1971 RV1971]',
  'url' => 'http://www.mediawiki.org/wiki/Extension:PatchOutput',
  'description' => 'patch html output'
);
 
// default settings
ExtPatchOutput::$mTable = array(
  /* open external links in new window */
  'class="external' => 'target="_blank" class="external',
  /* translate localhost to MediaWiki server */
  '://localhost' => '://' . $_SERVER[ 'SERVER_NAME' ],
  /* do not show mailto: in mailto links */
  'rel="nofollow">mailto:' => '>' );
 
$wgExtPatchOutput = new ExtPatchOutput();
 
$wgHooks['OutputPageBeforeHTML'][] = array( &$wgExtPatchOutput, 
                                            'onOutputPageBeforeHTML' );
 
class ExtPatchOutput
{
  const VERSION = '0.2';
 
  public static $mTable;
 
  public function onOutputPageBeforeHTML( &$out, &$text ) {
    foreach( self::$mTable as $from => $to )
      $text =& str_replace( $from, $to, $text );
 
    return true;
  }
}

[edit] Change Log

0.2 
  • The configuration parameter now a class member.
0.1 
  • First version published.

[edit] See also

Personal tools