Extension:Include

Description
This extension lets you include external static content from the file system or remote URL. If the external text is source code then it can be optionally colorized with syntax highlighting. By default the text is automatically wrapped in a &lt;pre&gt;&lt;/pre&gt; tag and all HTML entities are escaped. This can be turned off if you want to include raw text or HTML.

Note that syntax coloring requires the Pear Text_Highlighter module. The extension will run without Text_Highlighter, but the 'highlight' attribute will be disabled. If you try to use it without installing Text_Highlighter include will gracefully return an error message.

Installation
Put this script on your server in your MediaWiki extensions directory: "$IP/extensions/include.php" where $IP is the install path of your MediaWiki. Then add this line to LocalSettings.php: require_once("$IP/extensions/include.php");

Example Usage in a wikipage
This would include plain text from the given src URL.  The previous example would be rendered in MediaWiki something like this: Network Working Group                                    T. Berners-Lee Request for Comments: 1945                                      MIT/LCS Category: Informational                                     R. Fielding UC Irvine H. Frystyk MIT/LCS May 1996

Hypertext Transfer Protocol -- HTTP/1.0

Status of This Memo

This memo provides information for the Internet community. This memo does not specify an Internet standard of any kind. Distribution of  this memo is unlimited.

IESG Note:

The IESG has concerns about this protocol, and expects this document to be replaced relatively soon by a standards track document.

Abstract

The Hypertext Transfer Protocol (HTTP) is an application-level protocol with the lightness and speed necessary for distributed, collaborative, hypermedia information systems. It is a generic, stateless, object-oriented protocol which can be used for many tasks, such as name servers and distributed object management systems, through extension of its request methods (commands). A feature of  HTTP is the typing of data representation, allowing systems to be   built independently of the data being transferred.

HTTP has been in use by the World-Wide Web global information initiative since 1990. This specification reflects common usage of  the protocol referred to as "HTTP/1.0".

The following example includes the contents of a PHP script. The src points to a local file system path. This could be useful for documenting the script in a wiki. The advantage here is that you could include the script that is actually being used. 

Since you are including PHP source code for display you could also turn on syntax highlighting for PHP. 

Source Code
&lt;?php /** * NAME * *      &lt;include/&gt; * * SYNOPSIS * *     &lt;include src=&quot;[URL]&quot; [noesc] [nopre] [highlight=&quot;[URL]&quot;] [csshref=&quot;[URL]&quot;] /&gt; * * INSTALL *       *      Put this script on your server in your MediaWiki extensions directory: *         &quot;$IP/extensions/include.php&quot; * *     where $IP is the Install Path of your MediaWiki. *     Then add this line to LocalSettings.php: *         require_once(&quot;$IP/extensions/include.php&quot;); * * DESCRIPTION * *     This extension allows you to include the contents of remote and local files *     in a wiki article. * *     This extension should almost certainly make you concerned about security! *     By default this extension is not very secure. I use it mainly on a site *     where I am the only authorized editor. There are notes in the source code *     that explain how you might improve security if you wanted to allow others *     to use this extension. See the render_include function. * *     Note that external content is only refreshed when you save the wiki page that *     contains the &lt;include/&gt;. Changing the external file WILL NOT update the wiki *     page until the wiki page is edited and saved (not merely refreshed in the browser). *     You can also instruct the server to refresh the page by using the refresh action. *     See http://en.wikipedia.org/wiki/Wikipedia:Bypass_your_cache#Server_cache *     You can add &lt;code&gt;&lt;/code&gt; *     to the wiki page to make it easier to clear the cache. * * ATTRIBUTES * *     The &lt;include/&gt; tag must always include at least have a 'src' attribute. *     *      src=&quot;[URL]&quot; *         You must include 'src' to specify the URL of the file to import. *         The URL may be the URL to a remote file or it may be a local file system path. *     *      noesc *         By default &lt;include&gt; will escape all html entities in the included text. *         You may turn this off by adding the 'noesc' attribute. *         It does not take any value. *     *      nopre *         By default &lt;include&gt; will add &lt;pre&gt;&lt;/pre&gt; tags around the included text. *         You may turn this off by adding the 'nopre' attribute. *         It does not take any value. *     *      highlight=&quot;[SYNTAX]&quot; *         You may colorize the text of any file that you import. *         The value of SYNTAX must be one of the following: * *             CPP *             CSS *             diff *             DTD *             HTML *             Java *             Javascript *             MySQL *             Perl *             PHP *             Python *             Ruby *             SQL *             XML * *     csshref=&quot;[URL]&quot; * *         If you set 'highlight' then by default it includes an inline CSS. *         You may override this and link to the style sheet of your choice. *         Set &quot;URL&quot; to point to the CSS you want to use. * * EXAMPLES *          *      Include a file from the local file system: *         &lt;include src=&quot;/var/www/htdocs/README&quot; /&gt; *     Include a remote file: *         &lt;include src=&quot;http://www.gogle.com/search?q=noah.org&quot; nopre noesc /&gt; * *     Include a local fragment of HTML: *         &lt;include src=&quot;/var/www/htdocs/header.html&quot; nopre noesc /&gt; *     Include a local file with syntax highlighting: *         &lt;include src=&quot;/home/svn/checkout/trunk/include.php&quot; highlight=&quot;php&quot; /&gt; * * * DEPENDENCIES * *     For highlight support you will need to install PEAR Text_Highlighter. For example: *         pear install --alldeps http://download.pear.php.net/package/Text_Highlighter-0.6.9.tgz * * AUTHOR * *     Noah Spurrier &lt;noah@noah.org&gt; * * @package extensions * @version 2 * @copyright Copyright 2007 * @author Noah Spurrier * @license public domain -- free of any licenses and restrictions * * vi:ts=4:sw=4:expandtab:ft=php: */

// We want highlighting, but it isn't required. // Set a flag to show if it's available or not. include 'Text/Highlighter.php'; if (class_exists('Text_Highlighter')) {   $highlighter_package = True; } else {   $highlighter_package = False; }

$wgExtensionFunctions[] = &quot;wf_include&quot;; $wgExtensionCredits['other'][] = array (   'name' =&gt; 'include',    'author' =&gt; 'Noah Spurrier',    'url' =&gt; 'http://mediawiki.org/wiki/Extension:include',    'description' =&gt; 'This lets you include static content from the local file system or a remote URL.', );

function wf_include {   global $wgParser; $wgParser-&gt;setHook( &quot;include&quot;, &quot;render_include&quot; ); }

$inline_css='&lt;style type=&quot;text/css&quot;&gt;

.hl-default {color:Black;} .hl-code {color:Gray;} .hl-brackets {color:Olive;} .hl-comment {color:Orange;} .hl-quotes {color:Darkred;} .hl-string {color:Red;} .hl-identifier {color:Blue;} .hl-builtin {color:Teal;} .hl-reserved {color:Green;} .hl-inlinedoc {color:Blue;} .hl-var {color:Darkblue;} .hl-url {color:Blue;} .hl-special {color:Navy;} .hl-number {color:Maroon;} .hl-inlinetags {color:Blue;} .hl-main {background-color:White;} .hl-gutter {background-color:#999999; color:White} .hl-table {font-family:courier; font-size:12px; border:solid 1px Lightgrey;} &lt;/style&gt; ';

/** * render_include * * This is called automatically by the MediaWiki parser extension system. * This does the work of loading a file and returning the text content. * $argv is an associative array of arguments passed in the &lt;include&gt; tag as * attributes. * * @param mixed $input unused * @param mixed $argv associative array * @param mixed $parser unused * @access public * @return string */ function render_include ( $input, $argv, &amp;$parser ) {   global $inline_css, $highlighter_package; if (!isset($argv['src'])) return &quot;ERROR: &lt;include&gt; tag is missing 'src' attribute.&quot;;

// You can add this to restrict contents to a given path or DOCUMENT_ROOT: //   if (is_file(realpath($argv['src'])) &amp;&amp; strlen(strstr(realpath($argv['src']), realpath($_SERVER['DOCUMENT_ROOT']))) &lt;= 0) //       return &quot;ERROR: src to local path is not under DOCUMENT_ROOT.&quot;; // Or you could use this to disallow local files altogether: //   if (is_file(realpath($argv['src']))) //       return &quot;ERROR: Local files are not allowed.&quot;; // You can use similar tricks to restrict the src url.

// I was thinking about an iframe extension...       //    if (isset($argv['iframe'])) //       return '&lt;iframe src=&quot;'. $argv['src']. '&quot; width=&quot;100%&quot;&gt; &lt;/iframe&gt;&quot;'; $output=file_get_contents($argv['src']); if ($output === False) return &quot;ERROR: &lt;include&gt; could not read the given src URL.&quot;;

if (isset($argv['highlight'])) {       if ($highlighter_package === False) {           return &quot;ERROR: Text_Highlighter is not installed. You can't use 'highlight'.&quot;; }

$hl =&amp; Text_Highlighter::factory($argv['highlight']); $output = $hl-&gt;highlight($output); if (isset($argv['csshref'])) {           $output = '&lt;link rel=&quot;stylesheet&quot; href=&quot;'. $argv['csshref']. '&quot; type=&quot;text/css&quot; media=&quot;all&quot; /&gt;'. &quot;\n&quot;. $output; }       else {           $output = $inline_css. $output; }       return $output; }

if ( ! isset($argv['noesc'])) $output = htmlentities( $output ); if ( ! isset($argv['nopre'])) $output = &quot;&lt;pre&gt;&quot;. $output. &quot;&lt;/pre&gt;&quot;; return $output; }

?&gt;