Extension:Pingbox

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
Pingbox
Release status: beta
Implementation Tag
Description Gets the result of a ping command into a wiki page.
Author(s) User:Boly38
MediaWiki 1.5+
Database changes No
License Public Domain
Download See below
<pingbox>
Translate the Pingbox extension if it is available at translatewiki.net
Check usage and version matrix.

The Pingbox extension is a MediaWiki extension by Boly38 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 parameter is not escaped, leading to allow arbitrary command excecution by any user capable of editing. Use with caution.

General syntax[edit]

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)

Parameters[edit]

  • 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.

Installation[edit]

  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");

File "pingbox.php"[edit]

<?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 Boly38 - AT - gmail - DOT - com
 * @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' => 'Boly38',
'url' => 'https://www.mediawiki.org/wiki/Extension:Pingbox',
);

/**
 * Checks if this extension is being called directly
 */
if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is an extension to MediaWiki and thus not a valid entry point.' );
}

/**
 * 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 = "(?)";
        }
}