Difference between pages "URL Whitelist MediaWiki Modification" and "ConsoleOutput MediaWiki Extension"

(Difference between pages)
(Created page with "The following code was created by Daniel Robbins to provide URL Whitelist functionality for MediaWiki. This code allows you to create a page named "URLWhitelist" which contain...")
 
 
Line 1: Line 1:
The following code was created by Daniel Robbins to provide URL Whitelist functionality for MediaWiki. This code allows you to create a page named "URLWhitelist" which contains lists of domains that users can link against. All domains not listed on the [[URLWhitelist]] page will result in a Spam Protection error when saving any edits to a page.
+
The ConsoleOutput MediaWiki extension was created by Daniel Robbins to provide highlighting of user input for interactive terminal session blocks. To use it, surround user input with  <tt>&lt;console&gt;</tt> opening and closing tags, and put a <tt>##i##</tt> input code immediately before user input on each line. This will cause all text from the <tt>##i##</tt> point onwards to be in a darker color. The rest of the text will be in a lighter grey, allowing it to be distinguished more easily from user input. Here are other sequences that can be used:
  
On the URLWhitelist page, the wikitext should contain one domain per line, and sub-paths are fine, such as this:
+
* <tt>##b##</tt> - highlight the rest of the line in bold.
 +
* <tt>##b## text here ##!b##</tt> - highlight the text between both markers in bold.
 +
* <tt>##i## text here ##!i##</tt> - highlight the text between both markers in a darker color.
  
<pre>
+
This extension can be further extended to add color support, as desired, and some of the CSS for color support can already be found in the code below.
google.com
+
foo.bar.com/oni
+
</pre>
+
  
Above, the URLWhitelist will allow http(s)://[domain.]google.com/* and http(s)://[domain.]foo.bar.com/oni[/optional-path]
+
Here is an example of ConsoleOutput output:
  
The following patch will apply against MediaWiki-1.19.1:
+
<console>
 +
www@www-smw ~/public_html $ ##i##ls
 +
COPYING  LocalSettings.php    api.php  ##b##extensions##!b##    index.php  maintenance          redirect.php    skins              thumb_handler.php5
 +
CREDITS  README                api.php5  images        index.php5  mw-config            redirect.php5  tests              wiki.phtml
 +
FAQ      RELEASE-NOTES-1.19   bin      img_auth.php  languages  opensearch_desc.php  redirect.phtml  thumb.php
 +
HISTORY  StartProfiler.sample  cache    img_auth.php5  load.php    opensearch_desc.php5  resources      thumb.php5
 +
INSTALL  UPGRADE              docs      includes      load.php5  profileinfo.php      serialized      thumb_handler.php
 +
www@www-smw ~/public_html $ ##i##cd extensions/
 +
</console>
 +
To install, make the following modifications to your skin:
  
<pre>
+
<syntaxhighlight lang="css">
--- mediawiki-1.19.1/includes/EditPage.php      2012-06-13 18:22:38.000000000 +0000
+
--- mediawiki-1.19.1/skins/vector/screen.css    2012-06-13 18:22:39.000000000 +0000
+++ public_html/includes/EditPage.php  2012-08-27 04:53:33.073996581 +0000
+
+++ public_html/skins/vector/screen.css 2012-08-27 04:34:47.507912892 +0000
@@ -1081,7 +1081,9 @@
+
@@ -683,10 +683,47 @@
                # Check for spam
+
        list-style-image: url(images/bullet-icon.png);
                $match = self::matchSummarySpamRegex( $this->summary );
+
}
                if ( $match === false ) {
+
-                       $match = self::matchSpamRegex( $this->textbox1 );
+
+                      /* Don't run spam regex matching on the URLWhitelist page itself: */
+
+                      if ($this->mTitle->getText() != "URLWhitelist")
+
+                              $match = self::matchSpamRegex( $this->textbox1 );
+
                }
+
                if ( $match !== false ) {
+
                        $result['spam'] = $match;
+
@@ -1558,7 +1560,7 @@
+
                global $wgSpamRegex;
+
                // For back compatibility, $wgSpamRegex may be a single string or an array of regexes.
+
                $regexes = (array)$wgSpamRegex;
+
-              return self::matchSpamRegexInternal( $text, $regexes );
+
+              return self::matchURLWhitelistInternal( $text, $regexes );
+
        }
+
 
   
 
   
        /**
+
-pre {
@@ -1571,7 +1573,7 @@
+
-      line-height: 1.3em;
        public static function matchSummarySpamRegex( $text ) {
+
+/* ConsoleOutput.php start */
                global $wgSummarySpamRegex;
+
+
                $regexes = (array)$wgSummarySpamRegex;
+
+.shell, pre, code, tt, div.mw-geshi {
-               return self::matchSpamRegexInternal( $text, $regexes );
+
+       font-size: 12px;
+               return self::matchURLWhitelistInternal( $text, $regexes );
+
+        font-family: Consolas, 'andale mono','lucida console', monospace;
        }
+
+}
 +
+
 +
+.shell, pre, div.mw-geshi {
 +
+        background-color: #F8F8FF;
 +
+        line-height: 15px;
 +
+        padding: 10px;
 +
+        border: none;
 +
+        border-top: 2px solid #C6C9E0;
 +
+        border-bottom: 2px solid #C6C9E0;
 +
+       margin: 0;
 +
+        overflow-x: auto;
 +
+        overflow-y: hidden;
 +
+}
 +
+
 +
+.code {
 +
+        color: #666;
 +
+}
 +
+
 +
+.code_input {
 +
+        color: #000;
 +
}
 
   
 
   
        /**
+
+.code_red {
@@ -1589,6 +1591,46 @@
+
+       color: #f00;
                return false;
+
+}
        }
+
+
+      protected static function matchURLWhitelistInternal( $text, $regexes ) {
+
+              global $wgUser;
+
+              /*if ( in_array('sysop', $wgUser->getGroups())) {
+
+                      return false;
+
+              }*/
+
+              $a = new Article( Title::newFromText('URLWhitelist'));
+
+              $stuff = $a->getContent();
+
+              $foo = preg_split('/\s+/', $stuff);
+
+              /*$httpreg = ",http://[^\s/]*(?!";*/
+
+              $httpreg = ",https?://(?!";
+
+              $count = 0;
+
+              foreach($foo as $reg) {
+
+                      $reg = str_replace(".","\.",$reg);
+
+                      $reg = "\S*" . $reg;
+
+                      if ($count != 0 )
+
+                              $httpreg .= "|";
+
+                      $httpreg .= $reg;
+
+                      $count = $count + 1;
+
+              }
+
+              $httpreg .= "),";
+
+              $hmatches = array();
+
+              /* URL match with whitelist */
+
+              if ( preg_match( $httpreg, $text, $hmatches ) ) {
+
+                       return "non-whitelisted http/https URL";
+
+              }
+
+              /*
+
+              for testing:
+
+              if ( $wgUser->getName() == "Drobbins" )
+
+                      return $httpreg;
+
+              */
+
+              foreach( $regexes as $regex ) {
+
+                      $matches = array();
+
+                      if( preg_match( $regex, $text, $matches ) ) {
+
+                              return $matches[0];
+
+                      }
+
+              }
+
+              return false;
+
+       }
+
 
+
 
+
 +
+.code_blue {
 +
+        color: #00f;
 +
+}
 
+
 
+
         function setHeaders() {
+
+.shell_green {
                global $wgOut, $wgUser;
+
+        color: #080;
 +
+}
 +
+
 +
+/* ConsoleOutput.php end */
 +
+
 +
/* Site Notice (includes notices from CentralNotice extension) */
 +
#siteNotice {
 +
         font-size: 0.8em;
 +
</syntaxhighlight>
 +
 
 +
Then install the following code in your <tt>extensions</tt> directory and include it with a <tt>require_once( "$IP/extensions/ConsoleOutput.php" );</tt> in <tt>LocalSettings.php</tt>:
 +
 
 +
<syntaxhighlight lang="php">
 +
<?php
 +
$wgExtensionCredits['validextensionclass'][] = array(
 +
    'name' => 'ConsoleOutput',
 +
    'author' => 'Daniel Robbins',
 +
    'url' => 'https://github.com/danielrobbins/mediawiki-consoleoutput',
 +
    'description' => 'This extension allows you to display colorized console output in mediawiki'
 +
);
 +
 
 +
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
 +
        $wgHooks['ParserFirstCallInit'][] = 'consoleOutputSetup';
 +
} else {
 +
        $wgExtensionFunctions[] = 'consoleOutputSetup';
 +
}
 +
 
 +
function consoleOutputSetup( $data )
 +
{
 +
    global $wgParser;
 +
    $wgParser->setHook('console', 'consoleRender');
 +
    return true;
 +
}
 +
 
 +
function consoleRender($input, $args, $parser)
 +
{
 +
    if (count($args))
 +
    {
 +
        return "<strong class='error'>" .
 +
              "ConsoleOutput: arguments not supported" .
 +
              "</strong>";
 +
    }
 +
 
 +
    # Display < and > as literals, so escape them:
 +
 
 +
    $input = preg_replace('/>/','&gt;', $input);
 +
    $input = preg_replace('/</','&lt;', $input);
 +
 
 +
    # http://www.perlmonks.org/?node_id=518444
 +
    # See "Matching a pattern that doesn't include another pattern:
  
</pre>
+
    $input = preg_replace('/##i##((?:(?!##!i##).)*)##!i##/','<span class="code_input">$1</span>', $input);
 +
    $input = preg_replace('/##i##(.*)/','<span class="code_input">$1</span>', $input);
 +
    $input = preg_replace('/##b##((?:(?!##!b##).)*)##!b##/','<b>$1</b>', $input);
 +
    $input = preg_replace('/##b##(.*)/','<b>$1</b>', $input);
 +
    return "<pre class=\"code\">" . $input . "&lt;/pre>";
 +
}
 +
?>
 +
</syntaxhighlight>
  
 
[[Category:MediaWiki Hacks]]
 
[[Category:MediaWiki Hacks]]

Latest revision as of 01:48, 21 December 2012

The ConsoleOutput MediaWiki extension was created by Daniel Robbins to provide highlighting of user input for interactive terminal session blocks. To use it, surround user input with <console> opening and closing tags, and put a ##i## input code immediately before user input on each line. This will cause all text from the ##i## point onwards to be in a darker color. The rest of the text will be in a lighter grey, allowing it to be distinguished more easily from user input. Here are other sequences that can be used:

  • ##b## - highlight the rest of the line in bold.
  • ##b## text here ##!b## - highlight the text between both markers in bold.
  • ##i## text here ##!i## - highlight the text between both markers in a darker color.

This extension can be further extended to add color support, as desired, and some of the CSS for color support can already be found in the code below.

Here is an example of ConsoleOutput output:

www@www-smw ~/public_html $ ls
COPYING  LocalSettings.php     api.php   extensions     index.php   maintenance           redirect.php    skins              thumb_handler.php5
CREDITS  README                api.php5  images         index.php5  mw-config             redirect.php5   tests              wiki.phtml
FAQ      RELEASE-NOTES-1.19    bin       img_auth.php   languages   opensearch_desc.php   redirect.phtml  thumb.php
HISTORY  StartProfiler.sample  cache     img_auth.php5  load.php    opensearch_desc.php5  resources       thumb.php5
INSTALL  UPGRADE               docs      includes       load.php5   profileinfo.php       serialized      thumb_handler.php
www@www-smw ~/public_html $ cd extensions/

To install, make the following modifications to your skin:

--- mediawiki-1.19.1/skins/vector/screen.css    2012-06-13 18:22:39.000000000 +0000
+++ public_html/skins/vector/screen.css 2012-08-27 04:34:47.507912892 +0000
@@ -683,10 +683,47 @@
        list-style-image: url(images/bullet-icon.png);
 }
 
-pre {
-       line-height: 1.3em;
+/* ConsoleOutput.php start */
+
+.shell, pre, code, tt, div.mw-geshi {
+        font-size: 12px;
+        font-family: Consolas, 'andale mono','lucida console', monospace;
+}
+
+.shell, pre, div.mw-geshi {
+        background-color: #F8F8FF;
+        line-height: 15px;
+        padding: 10px;
+        border: none;
+        border-top: 2px solid #C6C9E0;
+        border-bottom: 2px solid #C6C9E0;
+        margin: 0;
+        overflow-x: auto;
+        overflow-y: hidden;
+}
+
+.code {
+        color: #666;
+}
+
+.code_input {
+        color: #000;
 }
 
+.code_red {
+        color: #f00;
+}
+
+.code_blue {
+        color: #00f;
+}
+
+.shell_green {
+        color: #080;
+}
+
+/* ConsoleOutput.php end */
+
 /* Site Notice (includes notices from CentralNotice extension) */
 #siteNotice {
        font-size: 0.8em;

Then install the following code in your extensions directory and include it with a require_once( "$IP/extensions/ConsoleOutput.php" ); in LocalSettings.php:

<?php
$wgExtensionCredits['validextensionclass'][] = array(
    'name' => 'ConsoleOutput',
    'author' => 'Daniel Robbins',
    'url' => 'https://github.com/danielrobbins/mediawiki-consoleoutput',
    'description' => 'This extension allows you to display colorized console output in mediawiki'
);
 
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
        $wgHooks['ParserFirstCallInit'][] = 'consoleOutputSetup';
} else {
        $wgExtensionFunctions[] = 'consoleOutputSetup';
}
 
function consoleOutputSetup( $data )
{
    global $wgParser;
    $wgParser->setHook('console', 'consoleRender');
    return true;
}
 
function consoleRender($input, $args, $parser)
{
    if (count($args))
    {
        return "<strong class='error'>" .
               "ConsoleOutput: arguments not supported" .
               "</strong>";
    }
 
    # Display < and > as literals, so escape them:

    $input = preg_replace('/>/','&gt;', $input);
    $input = preg_replace('/</','&lt;', $input);
 
    # http://www.perlmonks.org/?node_id=518444
    # See "Matching a pattern that doesn't include another pattern:

    $input = preg_replace('/##i##((?:(?!##!i##).)*)##!i##/','<span class="code_input">$1</span>', $input);
    $input = preg_replace('/##i##(.*)/','<span class="code_input">$1</span>', $input);
    $input = preg_replace('/##b##((?:(?!##!b##).)*)##!b##/','<b>$1</b>', $input);
    $input = preg_replace('/##b##(.*)/','<b>$1</b>', $input);
    return "<pre class=\"code\">" . $input . "&lt;/pre>";
}
?>