Extension talk:BugMilestone

From mediawiki.org

This is the MediaWiki 1.6 version :

<?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 Bertrand Gorge <bertrand@epistema.com>
 * @copyright Copyright . 2008, Bertrand Gorge
 * @license http://creativecommons.org/licenses/by-sa/2.5/ cc-by-sa 2.5 or later
 * @version $Rev: 45 $
 */

	$wgExtensionCredits['other'][] = array(
		'name' => "BugMilestone",
		'author' => "Bertrand Gorge",
		'description' => "for showing a Tracs like milestone dashboard",
		'url' => "http://www.epistema.com/",
	);

	$wgExtensionFunctions[] = "wfBugMilestone";

	function wfBugMilestone()
	{
		global $wgHooks;

		// Application hooks
		$wgHooks['ParserAfterStrip'][] = "wfBugMilestone_ParserAfterStrip";
		$wgHooks['ParserAfterTidy'][] = "wfBugMilestone_ParserAfterTidy";

	}

	$wfBugMilestone_markerList = array();

	function wfBugMilestone_ParserAfterStrip(&$parser, &$text, &$strip_state)
	{
		$text = preg_replace('/(\[\[Milestone:(.*)\]\])/ei',
							 "wfInsertMilestoneDashboard('\\2')",
							 $text);

		return true;
	}

	function wfBugMilestone_ParserAfterTidy(&$parser, &$text)
	{
		// find markers in $text
		// replace markers with actual output
		global $wfBugMilestone_markerList;
		for ($i=0;$i<count($wfBugMilestone_markerList);$i++)
			$text = preg_replace('/xx-marker'.$i.'-xx/',$wfBugMilestone_markerList[$i],$text);
		return true;
	}

	define("pBugMilestone_NoConnection", 0);
	define("pBugMilestone_QueryError", 1);
	define("pBugMilestone_DBNotFound", 2);

	function wfInsertMilestoneDashboard($Milestone)
	{
		global $wgDBserver, $wgDBname, $wgDBUser, $wgDBPassword;

		$Source = $GLOBALS['conf']['BugzillaMilestone'];

		if (empty($Source['host']))
			$Source['host'] = $wgDBserver;
		if (empty($Source['database']))
			$Source['database'] = $wgDBname;
		if (empty($Source['user']))
			$Source['user'] = $wgDBUser;
		if (empty($Source['password']))
			$Source['password'] = $wgDBPassword;
		if (empty($Source['prefix']))
			$Source['prefix'] = "";

		$Link = @mysql_connect($Source['host'], $Source['user'], $Source['password'], true);
		if (!$Link)
			return pBugMilestone_NoConnection;

		if (!mysql_select_db($Source['database'], $Link)) {
			mysql_close($Link);
			return pBugMilestone_DBNotFound;
		}

		if (empty($Source['bugzilla_url']))
			$Source['bugzilla_url'] = 'http://yourbugzilla.installation.com/';

		$openURL =  $Source['bugzilla_url'] . 'buglist.cgi?query_format=advanced&target_milestone='.rawurlencode($Milestone).'&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED';
		$closedURL = $Source['bugzilla_url'] . 'buglist.cgi?query_format=advanced&target_milestone='.rawurlencode($Milestone).'&bug_status=RESOLVED&bug_status=VERIFIED&bug_status=CLOSED';

		$OpenBugCountSQL = "SELECT COUNT(1) FROM " . $Source['prefix'] . "bugs WHERE bug_status IN ('UNCONFIRMED', 'NEW', 'ASSIGNED', 'REOPENED') AND target_milestone='".mysql_escape_string($Milestone)."'";
		$Result = @mysql_query($OpenBugCountSQL, $Link);
		if (!$Result)
		{
			mysql_close($Link);
			return pBugMilestone_QueryError;
		}
		if ($Row = mysql_fetch_row($Result))
			$OpenBugCount = $Row[0];

		$ClosedBugCountSQL = "SELECT COUNT(1) FROM " . $Source['prefix'] . "bugs WHERE bug_status IN ('RESOLVED', 'VERIFIED', 'CLOSED') AND target_milestone='".mysql_escape_string($Milestone)."'";
		$Result = @mysql_query($ClosedBugCountSQL, $Link);
		if (!$Result)
		{
			mysql_close($Link);
			return pBugMilestone_QueryError;
		}
		if ($Row = mysql_fetch_row($Result))
			$ClosedBugCount = $Row[0];

		mysql_free_result($Result);
		mysql_close($Link);

		$TotalBugsInMilestone = $ClosedBugCount + $OpenBugCount;
		if ($TotalBugsInMilestone > 0)
			$ClosedPercent = round($ClosedBugCount / $TotalBugsInMilestone * 100);
		else
			$ClosedPercent = 100;

		$strMiletone = '';

		if (empty($GLOBALS['BugMilestoneCSS']))
		{
			$GLOBALS['BugMilestoneCSS'] = true;
			$strMiletone .= '<style type="text/css" media="print, screen, all">' . "\n";
			$strMiletone .= 'table.progress {' . "\n";
			$strMiletone .= ' border: 1px solid #d7d7d7;' . "\n";
			$strMiletone .= ' border-collapse: collapse;' . "\n";
			$strMiletone .= ' border-spacing: 0;' . "\n";
			$strMiletone .= ' float: left;' . "\n";
			$strMiletone .= ' margin: 0;' . "\n";
			$strMiletone .= ' padding: 0;' . "\n";
			$strMiletone .= ' empty-cells: show;' . "\n";
			$strMiletone .= '}' . "\n";
			$strMiletone .= 'table.progress a, table.progress :link, table.progress :visited,' . "\n";
			$strMiletone .= 'table.progress :link:hover, table.progress :visited:hover {' . "\n";
			$strMiletone .= ' border: none;' . "\n";
			$strMiletone .= ' display: block;' . "\n";
			$strMiletone .= ' width: 100%;' . "\n";
			$strMiletone .= ' height: 1.2em;' . "\n";
			$strMiletone .= ' padding: 0;' . "\n";
			$strMiletone .= ' margin: 0;' . "\n";
			$strMiletone .= ' text-decoration: none' . "\n";
			$strMiletone .= '}' . "\n";
			$strMiletone .= 'table.progress td { background: #fff; padding: 0 }' . "\n";
			$strMiletone .= 'table.progress td.closed { background: #bae0ba }' . "\n";
			$strMiletone .= 'table.progress td :hover { background: none }' . "\n";
			$strMiletone .= 'p.percent { font-size: 10px; line-height: 2.4em; margin: 0.9em 0 0 }' . "\n";
			$strMiletone .= 'ul.milestones { margin: 2em 0 0; padding: 0 }' . "\n";
			$strMiletone .= 'li.milestone { list-style: none; margin-bottom: 4em }' . "\n";
			$strMiletone .= '.milestone .info { white-space: nowrap }' . "\n";
			$strMiletone .= '.milestone .info h2 {' . "\n";
			$strMiletone .= ' background: #f7f7f7;' . "\n";
			$strMiletone .= ' border-bottom: 1px solid #d7d7d7;' . "\n";
			$strMiletone .= ' margin: 0;' . "\n";
			$strMiletone .= '}' . "\n";
			$strMiletone .= '.milestone .info .progress { margin: 1em 1em 0; width: 40em; max-width: 70% }' . "\n";
			$strMiletone .= '.milestone .info dl {' . "\n";
			$strMiletone .= ' font-size: 10px;' . "\n";
			$strMiletone .= ' font-style: italic;' . "\n";
			$strMiletone .= ' margin: 0 1em 2em;' . "\n";
			$strMiletone .= ' white-space: nowrap;' . "\n";
			$strMiletone .= ' clear: both;' . "\n";
			$strMiletone .= '}' . "\n";
			$strMiletone .= '.milestone .info dt { display: inline; margin-left: .5em }' . "\n";
			$strMiletone .= '.milestone .info dd { display: inline; margin: 0 1em 0 .5em }' . "\n";
			$strMiletone .= '</style>' . "\n";
		}

		$strMiletone .= '<ul class="milestones">  <li class="milestone">   <div class="info">' . "\n";
		$strMiletone .= '<table class="progress">' . "\n";
		$strMiletone .= ' <tr>' . "\n";
		$strMiletone .= '  <td class="closed" style="width: '.$ClosedPercent.'%;"><a target="_blank" href="'.$closedURL.'" title="'.$ClosedBugCount.' of '.$TotalBugsInMilestone.' tickets closed"></a></td>' . "\n";
		$strMiletone .= '  <td class="open" style="width: '.(100-$ClosedPercent).'%;"><a target="_blank" href="'.$openURL.'" title="'.$OpenBugCount.' of '.$TotalBugsInMilestone.' tickets active"></a></td>' . "\n";
		$strMiletone .= ' </tr>' . "\n";
		$strMiletone .= '</table>' . "\n";
		$strMiletone .= '<p class="percent">'.$ClosedPercent.'%</p>' . "\n";
		$strMiletone .= '<dl>' . "\n";
		$strMiletone .= ' <dt>Closed tickets:</dt>' . "\n";
		$strMiletone .= ' <dd><a target="_blank" href="'.$closedURL.'">'.$ClosedBugCount.'</a></dd>' . "\n";
		$strMiletone .= ' <dt>Active tickets:</dt>' . "\n";
		$strMiletone .= ' <dd><a target="_blank" href="'.$openURL.'">'.$OpenBugCount.'</a></dd>' . "\n";
		$strMiletone .= '</dl>' . "\n";
		$strMiletone .= '</div> </li>  </ul>' . "\n";

		global $wfBugMilestone_markerList;
    $makercount =  count($wfBugMilestone_markerList);
    $marker = "xx-marker".$makercount."-xx";
    $wfBugMilestone_markerList[$makercount] = $strMiletone;

		return $marker;
	}