Important: This is a development wiki, so things might be a little unstable. If something doesn't work properly, try refreshing the page. If that fails, come back later!

BugSquish

From TestWiki

Jump to: navigation, search

This extension checks your Bugzilla installation and adds a strikethrough to interwiki links for closed bugs.

Contents

Installation

Simply copy and paste the code (below) into a new file in your 'extensions' directory and include() it from your LocalSettings.php file in the usual way.

Requirements

Firstly, links to your Bugzilla installation need to be setup in your interwiki table. For example you might setup an interwiki link called bug with the URL http://www.example.com/bugzilla/show_bug.cgi?id=$1. This means you can enter the wiki code [[bug:2314]] to create a link to bug 2314 in your Bugzilla install (resulting in a link such as: bug:2314).

For BugSquish to work are you need to have an interwiki prefix that accepts just a single number, and that the DB containing the bugs must be accessible locally (though the code is designed to allow for alternative access to the Bugzilla data (e.g. via WebDAV)... so long as someone fills in the necessary code!).

Multiple interwiki prefixes to different Bugzillas can be defined, but currently the code only works with Bugzilla installations - no other bugtrack systems are currently supported.

Configuration

For each interwiki link that you want to use, you need to add an entry into $wgBugSquishSources. Each entry has the following required elements:

  • interwiki => The interwiki prefix this definition applies to.
  • type => The type of connection. Currently only BUGSQUISH_DBConnect is valid for this field.


The following elements are only required if necessary:

  • prefix => DB prefix (as setup in your Bugzilla installation - can be omitted if no prefix used).


The following optional elements can be used to define the database connection. If any of the elements are omitted, then the values used for your MediaWiki installation are used. Therefore if Bugzilla shares a database with MediaWiki (and the MediaWiki user has appropriate access privileges) then you can omit all of the settings. If not, then fill in anything that differs from your MediaWiki install.

  • host => The database host.
  • database => The database name.
  • user => Username for DB connection.
  • password => Password for DB connection.

Styling

You may wish to add the following styles to your common.css file (available on your wiki at MediaWiki:Common.css). Note that this has not been tested with all browsers - if you spot and fix any problems, please feel free to update the CSS below.

.bsBugLink a {
  font-size: 0.9em;
  border: 1px solid #E0E0FF;
  background-color: #F0F0FF !important;
 
  margin: 0 0.2em;
  padding: 0 0.5em !important;
 
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
}
 
.bsBugLink a:hover {
  border-color: #9999FF;
  background-color: #D0E0FF !important;
  color: #0000FF !important;
  text-decoration: none;
}
 
s.bsFixedBug {
  text-decoration: none !important;
}
 
.bsFixedBug a, .bsFixedBug a:hover {
  text-decoration: line-through;
}

Examples

Example 1 - shared DB

If MediaWiki and Bugzilla are installed in a single DB, then you only need to specify a minimal set of components:

include("extensions/BugSquish.php");
 
$wgBugSquishSources[] = array(
	'interwiki'	=> "bug",
	'type'		=> BUGSQUISH_DBConnect,
	'prefix'	=> "bugzilla_",
);

Example 2 - separate DB

If Bugzilla is stored in a separate DB, then you need to supply details. In most cases 'host' will be the same as for MediaWiki, in which case it can be omitted (as it has been here). Also if no table prefix is used in your Bugzilla install, then that field can be omitted.

include("extensions/BugSquish.php");
 
$wgBugSquishSources[] = array(
	'interwiki' 	=> "bug",
	'type'		=> BUGSQUISH_DBConnect,
	'database'	=> "dbname",
	'user'		=> "dbuser",
	'password'	=> "dbpass",
);

Known issues

  • Doesn't handle interwiki links created dynamically via templates, (e.g. [[bug:{{{1}}}]]. It should handle links that don't contain variables though - let me know if you spot any other problems with transclusion, however!
  • In MediaWiki v1.6 the 'Buginese' language was added, which has the ISO code 'bug'. This means that in the default configuration for version 1.6 upwards, you can't use 'bug' for the links (or rather, you can, but they appear in the left side-bar rather than in the text as expected). There are two work-arounds to this.
    1. Use a different interwiki prefix.
    2. If you don't use inter-language links (and if you're unsure, you probably don't) then you can disable them by setting $wgInterwikiMagic to false in your LocalSettings.php file.

Source Code

Live sourcecode viewer: BugSquish.php
Last modified: 2008-10-23 09:49:47

<?php
if (!defined('MEDIAWIKI')) die();
/**
 * An extension that checks your Bugzilla installation
 * and adds a strikethrough to interwiki links for closed bugs.
 *
 * @package MediaWiki
 * @subpackage Extensions
 *
 * @author Mark Clements <mclements at kennel17 dot co dot uk>
 * @copyright copyright © 2006, Mark Clements
 * @license http://creativecommons.org/licenses/by-sa/2.5/ cc-by-sa 2.5 or later
 * @version $Rev: 165 $
 */
 
	$wgExtensionCredits['other'][] = array(
		'name' => "BugSquish",
		'author' => "Mark Clements",
		'description' => "for striking-out closed bugs",
		'url' => "http://www.kennel17.co.uk/testwiki/",
	);
 
	$wgExtensionFunctions[] = "wfBugSquish";
 
	if (!isset($wgBugSquishSources))
		$wgBugSquishSources = array();
 
// Constants for the 'type' variable in the sources array.
// Only 1 so far.
	define("BUGSQUISH_DBConnect", 1);
 
	function wfBugSquish() {
		global $wgHooks;
 
	// Application hooks
		$wgHooks['ParserAfterStrip'][] = "wfBugSquish_ParserAfterStrip";		
	}
 
 
	function wfBugSquish_ParserAfterStrip(&$parser, &$text, &$strip_state) {
		global $wgBugSquishSources;
 
		foreach ($wgBugSquishSources as $Key => $Value) {
			$text = preg_replace('/(\[\[' . $Value['interwiki'] . ':)([^]|]*)((?:|.*)?\]\])/ei', 
								 "wfSquishBug('\\1','\\2','\\3','" . $Key . "')", 
								 $text);
		}
 
		return true;
	}
 
 
	define("pBUGSQUISH_NoConnection", 	0);
	define("pBUGSQUISH_QueryError", 	1);
	define("pBUGSQUISH_BugNotFound", 	2);
	define("pBUGSQUISH_BadType", 		3);
	define("pBUGSQUISH_DBNotFound", 	4);
 
	function wfSquishBug($Pre, $ID, $Post, $Key) {
		global $wgBugSquishSources;
 
		$Source = $wgBugSquishSources[$Key];
		if (!isset($Source['type']))
			$Source['type'] = "";
 
		switch ($Source['type']) {
			case BUGSQUISH_DBConnect:
				$BugData = wfSquishBug_GetDataFromDB($ID, $Key);
				break;
			default: 
				$BugData = pBUGSQUISH_BadType;
				break;
		}
 
		$Result = $Pre . $ID . $Post;
		$Class = "bsBugLink";
 
	// Error occurred.  Indicate this in the CSS class.
		if (!is_array($BugData))
			$Class .= " bsError";
 
	// Otherwise, cross out the fixed bugs, and add the current
	// bug status to the CSS class.
		else {
			if ($BugData['IsClosed'])
				 $Result = '<s class="bsFixedBug">' . $Result . '</s>';
 
			$Class .= " bsStatus_" . $BugData['Status'];
			if (!IsBlank($BugData['Resolution']))
				$Class .= " bsResolution_" . $BugData['Resolution'];
		}
 
	// Return the result wrapped in a span tag, with the appropriate classes.
		return '<span class="' . $Class . '">' . $Result . '</span>';
	}
 
	function wfSquishBug_GetDataFromDB($ID, $Key) {
		global $wgDBserver, $wgDBname, $wgDBUser, $wgDBPassword;
		global $wgBugSquishSources;
 
		$Source = $wgBugSquishSources[$Key];
 
		if (!isset($Source['host']) || $Source['host'] == "")
			$Source['host'] = $wgDBserver;
		if (!isset($Source['database']) || $Source['database'] == "")
			$Source['database'] = $wgDBname;
		if (!isset($Source['user']) || $Source['user'] == "")
			$Source['user'] = $wgDBUser;
		if (!isset($Source['password']) || $Source['password'] == "")
			$Source['password'] = $wgDBPassword;
		if (!isset($Source['prefix']))
			$Source['prefix'] = "";
 
		$Link = @mysql_connect($Source['host'], $Source['user'], $Source['password']);
		if (!$Link)
			return pBUGSQUISH_NoConnection;
 
		if (!mysql_select_db($Source['database'], $Link)) {
			mysql_close($Link);
			return pBUGSQUISH_DBNotFound;
		}
 
		$Result = @mysql_query("SELECT resolution, short_desc, bug_status FROM " 
				. $Source['prefix'] . "bugs WHERE bug_id = " . $ID . ";", $Link);
 
		if (!$Result) {
			mysql_close($Link);
			return pBUGSQUISH_QueryError;
		}
 
		if ($Row = mysql_fetch_assoc($Result)) {
			$ReturnVal = array();
 
			if ($Row['resolution'] == "")
				$ReturnVal['IsClosed'] = false;
			else
				$ReturnVal['IsClosed'] = true;
 
			$ReturnVal['Summary'] = $Row['short_desc'];
			$ReturnVal['Status'] = $Row['bug_status'];
			$ReturnVal['Resolution'] = $Row['resolution'];
		}
		else
			$ReturnVal = pBUGSQUISH_BugNotFound;
 
		mysql_free_result($Result);
		mysql_close($Link);
 
		return $ReturnVal;
	}
Personal tools