Extension:MantisIntegration

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
MantisIntegration
Release status: beta
ExtensionMantisintegration.jpg
Implementation Tag
Description Pretty links to the Mantis bug tracking system with a summary line
Author(s) Alexander Botero-Lowry <alex (at) foxybanana (dot) com>, Kevin Schulze
Latest version 0.3 (2008-11-06)
MediaWiki Works with MediaWiki 1.13.2
License No license specified
Download Extension:MantisIntegration#Code
Example see XMMS2 wiki
Translate the MantisIntegration extension if it is available at translatewiki.net
Check usage and version matrix.

I have added a parser tag to make pretty links to the Mantis bug tracking system complete with a summary line. We use this on the XMMS2 wiki To easily reference Bug reports like on this page: [1]


For Mantis 1.1.2 with LDAP Auth., this extension in the initial Release didnt work for me, so I changed to direct Database access (MySQL only!). See Code Version 0.3 for details.



org. Code from Alexander Botero-Lowry[edit]

The code:

<?php

/*
 * Mantis bug report summary extractor extension for MediaWiki.
 * by Alexander Botero-Lowry <alex@foxybanana.com>
 * some tweaks by Sham Chukoury <eleusis@xmms.org>
 * 2006-08-22
 */

$wgExtensionFunctions[] = "wfMantisExtension";

function wfMantisExtension() {
        global $wgParser;
        $wgParser->setHook( "mantis", "expandMantis" );
}

function expandMantis( $input, $argv ) {
 	global $wgOut;
	$mantis_base = "http://bugs.xmms2.xmms.se/view.php?id=";

	$bug_url = $mantis_base . $input;
	$data = implode("", file($bug_url));
	preg_match_all ("/<td colspan=\"5\">[\n\t ]+([^`]*?)[\t]+<\/td>/", $data, $matches);
	$our_match = $matches[1][1];
	return $wgOut->parse ("[$bug_url $our_match]", false);
}

/* below code expandMantis function for MediaWiki version >= 1.9
*  by kerimj <kerimj@poczta.fm>
*  2008-05-09
*/
function expandMantis( $input, $argv, &$parser) {
   $mantis_base = "http://bugs.xmms2.xmms.se/view.php?id=";
   $bug_url = $mantis_base . $input;
   $data = implode("", file($bug_url));
   preg_match_all ("/<td colspan=\"5\">[\n\t ]+([^`]*?)[\t]+<\/td>/", $data, $matches);
   $our_match = $matches[1][1];
   
   $output = $parser->parse("[$bug_url $our_match]", $parser->mTitle, $parser->mOptions, false, false);
   return $output->getText();   
}

Code[edit]

Version 0.3

<?php
 /*
 * Installation:
 * 	require_once("extensions/MantisIntegration/MantisIntegration.php"); in LocalSettings.php
 *      update  $mantisDBSERVER,  $mantisDBUSER,  $mantisDBUSERPW,  $mantisDBNAME, $mantis_home
 * Usage:
 *      <mantis>#bugid</mantis>
 *
 * @version 0.2 schke 2008-10-31
 *      -change data input from php implode of bugsite to direct database access
 *      -added state information to output
 *      -added meta information for Spezial:Version
 *
 * @version 0.3 schke 2008-11-06
 *      -bug with specialcharacters resolved
 */

if( !defined( 'MEDIAWIKI' ) ) {
        echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" );
        die( -1 );
}

// Extension credits that will show up on Special:Version
$wgExtensionCredits['parserhook']['MantisExtension'] = array(
        'name'         => 'MantisExtension',
        'version'      => '0.3',
        'author'       => 'Alexander Botero-Lowry, [http://www.mediawiki.org/wiki/User:Schke Kevin Schulze]',
        'description'  => 'Link to Mantis Bug Report in MediaWiki',
        'url'          => 'http://www.mediawiki.org/w/index.php?title=Extension:MantisIntegration'
);


$wgExtensionFunctions[] = "wfMantisExtension";

function wfMantisExtension() {
        global $wgParser;
        $wgParser->setHook( "mantis", "showMantis" );
}

function showMantis( $input, $argv, &$parser) {

   $mantisDBSERVER="";
   $mantisDBUSER="";
   $mantisDBUSERPW="";
   $mantisDBNAME="";
   $mantisDBPrefix="mantis_";

   $mantis_home = "http://mantis.xxxxxx.de/mantis/view.php?id=";

    # --------------------------------------------

   $bug_page = $mantis_home . $input;

    $link = mysql_connect($mantisDBSERVER,$mantisDBUSER ,$mantisDBUSERPW);
    if ( !$link ) { return "<hr><b>Error while connecting to host \"$mantisDBSERVER\" !</b><hr>"; }

    $db_selected = mysql_select_db($mantisDBNAME, $link);
    if (!$db_selected) {return "<hr><b>Error while selecting database \"$mantisDBNAME\" !</b><hr>"; }

    $sql = sprintf("SELECT id, convert(convert(summary using utf8), binary), status FROM ".$mantisDBPrefix."bug_table where id=%d",
                             mysql_real_escape_string($input));

    $qryres = mysql_query($sql);
    if (!$qryres) { return "<hr><b>Error while MySQL Query :". mysql_error() ;}

    if ($row = mysql_fetch_row($qryres)) {
      switch ($row[2]) {
          case 10:
               $state_msg="new";
               break;
          case 20:
               $state_msg="feedback";
               break;
          case 30:
               $state_msg="acknowledged";
               break;
          case 40:
               $state_msg="confirmed";
               break;
          case 50:
               $state_msg="assigned";
               break;
          case 60:
               $state_msg="working on";
               break;
          case 80:
               $state_msg="resolved";
               break;
          case 90:
               $state_msg="closed";
               break;
          default:
               $state_msg="unkown";
      }
      $res = "Bug ID:".$row[0]." (".$state_msg.") : ".$row[1];
    }else{
      $res = "No MANTIS Entry found!!!";
    }

    // Note: You may need to add a line here to select the mediawiki database. Also, I had to remove the mysql_close line below.
    //       Reason: This was causing Mediawiki code to try to access the mantis DB, and somehow after reselecting the wiki db, it was closing the main mediawiki db connection --~~~~
    mysql_free_result($qryres);
    mysql_close($link);

    $output = $parser->parse("[$bug_page $res]", $parser->mTitle, $parser->mOptions, false, false);
    return $output->getText();
}

Suggested Change: The current version does some odd text handling if used in lists. I altered the last few lines where the output is "wikified" to match a newer method illustrated in the mediawiki dev manual.

// change this:
$output = $parser->parse("[$bug_page $res]", $parser->mTitle, $parser->mOptions, false, false);

// to this:
$output = $parser->recursiveTagParse("[$bug_page $res]");

// and this: 
return $output->getText();

// to this:
return $output;