Extension:Pingbox

From MediaWiki.org

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

Release status: unknown

Implementation Tag
Description
Author(s) Brice Vandeputte
MediaWiki 1.5+
License No license specified
Download See below

The pingbox extension is a MediaWiki extension by Brice Vandeputte to get the result of a ping command into a wiki page. It was originally created for the purpose of checking server state in a private company. It requires MediaWiki 1.5 and an extensions script (see Installation below).

NOTE: host paramater is not escaped, leading to allow arbitrary command excecution by any user capable of editing. Use with caution.

Contents

[edit] General syntax

Pingboxes are constructed like this:

 <pingbox>
 host=hostname_to_ping (mandatory)
 defaultok=You ping it (default:"OK")
 defaultko=No answer (default:"KO")
 labelinfo=-info- (default:"(?)")
 </pingbox>

The host parameter is mandatory. All other parameters are optional (they've a default value.

The aim is to ping the host hostname_to_ping (your MediaWiki server must be able to ping to use this extension)

[edit] Parameters

  • host : hostname to ping
  • defaultok : label used to say that the ping is a success
  • defaultko : label used to say that the ping failed
  • labelinfo : (optional) label used to create unused link after defaultok or defaultko with result of the ping command in "title" attribute.

[edit] Installation

  1. Copy and save the file pingbox.php into the extensions folder of your MediaWiki installation.
  2. Open and edit the LocalSettings.php file by adding the following line near the bottom:
require_once("extensions/pingbox.php");

NOTE: Make sure that it is still above the closing php tag (if there is any) so that it looks like this:

require_once("extensions/pingbox.php");
?>

[edit] File "pingbox.php"

<?php
 
/**
 * This file contains the main include file for the Pingbox extension of
 * MediaWiki.
 *
 * Usage: require_once("path/to/pingbox.php"); in LocalSettings.php
 *
 * This extension requires MediaWiki 1.5 or higher.
 * Based on the "inputbox" extension by Erik Moller
 *
 * @author Brice Vandeputte <Boly38 - AT - yahoo - DOT - fr>
 * @copyright Public domain
 * @license Public domain
 * @package MediaWikiExtensions
 * @version 0.1
 */
 
/**
 * Register the Pingbox extension with MediaWiki
 */
$wgExtensionFunctions[] = 'registerPingboxExtension';
$wgExtensionCredits['parserhook'][] = array(
'name' => 'Pingbox',
'author' => 'Brice Vandeputte',
'url' => 'http://www.mediawiki.org/wiki/Extension:Pingbox',
);
 
/**
 * Sets the tag that this extension looks for and the function by which it
 * operates
 */
function registerPingboxExtension()
{
    global $wgParser;
    $wgParser->setHook('pingbox', 'renderPingbox');
}
 
 
 
 
/**
 * Renders an pingbox based on information provided by $input.
 */
function renderPingbox($input, $params) // , &$parser)
{
        $pingbox=new Pingbox();
        getPBoxOption($pingbox->host,$input,'host');
        getPBoxOption($pingbox->defaultok,$input,'defaultok');
        getPBoxOption($pingbox->defaultko,$input,'defaultko');
        getPBoxOption($pingbox->labelinfo,$input,'labelinfo');
        $pingbox->checkDefault();
        $pingbox->checkLabelInfo();
 
        $boxhtml=$pingbox->render();
        if($boxhtml) {
                return $boxhtml;
        } else {
                return "<br /> <font color='red'>Ping box host = '{$pingbox->host}' not defined.</font>";
        }
}
 
 
function getPBoxOption(&$value,&$input,$name,$isNumber=false) {
 
      if(preg_match("/^\s*$name\s*=\s*(.*)/mi",$input,$matches)) {
                if($isNumber) {
                        $value=intval($matches[1]);
                } else {
                        $value=htmlspecialchars($matches[1]);
                }
        }
}
 
class Pingbox {
        var $host,$width;
        var $defaultok, $defaultko;
        var $labelinfo;
        var $result;
 
        function render() {
                if($this->host!='') {
                        return $this->getSimpleForm();
                } else {
                        return false;
                }
        }
 
        function getSimpleForm() {
                global $wgScript;
 
                $action = htmlspecialchars( $wgScript );
                $arrResult = array();
                $intR = exec('ping -c 2 ' . $this->host, &$arrResult);
                $this->result = "Resultat du ping -c2 " . $this->host . " (".$intR."): ";
                foreach($arrResult as $ind => $val)
                {
                        $this->result .= $this->result . "\r\n" . $val;
                }
                if (count($arrResult) > 0)
                        $pingform .= $this->defaultok;
                else
                        $pingform .= $this->defaultko;
                $pingform .= " <a href=\"#\" title=\"" . $this->result . "\">" . $this->labelinfo . "</a>";
                return $pingform;
        }
 
        function checkDefault() {
                if( !$this->defaultok || trim( $this->defaultok ) == '' )
                        $this->defaultok = "OK";
                if( !$this->defaultko || trim( $this->defaultko ) == '' )
                        $this->defaultko = "KO";
        }
 
        function checkLabelInfo() {
                if( !$this->labelinfo || trim( $this->labelinfo ) == '' )
                        $this->labelinfo = "(?)";
        }
}
?>
Personal tools