Pligg Download Counter

The other day I was curious to figure out how many downloads Pligg has had, or at least how many we were able to officially track. I ended up writing a PHP script to track the official Pligg downloads from the forums and to add in some old download data from Sourceforge. What saddens me is that I can’t track a lot of third-party sites out there and one-click installs provided by web hosts, so this number might be off by quite a bit. Some of you might find it interesting on how we are now tracking our downloads and how you can use our download count, so I’ll be documenting all of that on this post.First, let me explain how the stats work by giving you some example URLs that you can use to fetch download counts. The main url http://www.pligg.com/download_count.php gives you the total number of downloads that we are able to track. Now if you add some parameters to this URL you can get the stats for the average downloads per hour, week, day or year. Here’s an example of the day average url: http://www.pligg.com/download_count.php?stat=day.The numbers being generated here are updated a couple times an hour, so you won’t see the download count increasing with each page refresh. I bet your asking yourself how this number is being generated, well it’s a mix of hard-coded numbers and numbers that are continually being fetched from the vBulletin forum MySQL database. We used to use a downloads section of the site for hosting Pligg over a year ago, but since we switched that part of the site off the old versions of Pligg are no longer available. Since users can’t download them we know that the number for those versions isn’t going to change so we added up those numbers. Then we wrote a script that connects to the MySQL database and finds the download count for all of the working downloads from the Current Version forum. Then we add all of those numbers up into one figure representing Pligg.com downloads. I then hard coded some download figures provided by sourceforge.net and softpedia.com for some of our older versions and added them to the total figure. The end result of all of these numbers is our total download count.To generate an average daily count I wanted to discard a lot of the older download data since it wasn’t tracked very well and decided to only calculate the number of downloads from Pligg RC1 or newer. I took the total number of release candidate versions and then divided them by the number of days between the current date and the release date for RC1. The end result gives you an average daily download number (rounded to the nearest whole number) which the average hourly, monthly and yearly stats are based on.
<?php
$downloadcount = file_get_contents('http://www.pligg.com/download_count.php');
echo $downloadcount;
?>
Above is a quick example of how you could use PHP to grab the download number inside of a PHP script to display the number. We are using this method to display the Download Stats currently found at the top right of the Pligg Blog. If you are interested in the complete source code for this download counter script designed for vBulletin sites, please view the full article URL for the PHP source.
<?php
	
// This download counter grabs the number of downloads tracked by 
// the Pligg Forums, Sourceforge and other sites to be added in later
// It must manually be updated whenever a new download is added to
// the forums.

// First we need to connect to the vBulletin forum MySQL database
$conn = mysql_connect("localhost", "USERNAME", "PASSWORD");
if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}
if (!mysql_select_db("DATABASENAME")) {
    echo "Unable to select database name: " . mysql_error();
    exit;
}

// Now lets get the download numbers for each version of Pligg.
// Use postid for posts that only have 1 download, attachmentid for posts with more than 1 attachment.

// Beta 4
$B4 = "SELECT counter
        FROM   attachment
        WHERE  postid = 4";
$B4result = mysql_query($B4);
while ($row = mysql_fetch_assoc($B4result)) {
    $B4count = $row["counter"];
}
mysql_free_result($B4result);

// Beta 5
$B5 = "SELECT counter
        FROM   attachment
        WHERE  postid = 13";
$B5result = mysql_query($B5);
while ($row = mysql_fetch_assoc($B5result)) {
    $B5count = $row["counter"];
}
mysql_free_result($B5result);

// Beta 6.01
$B601 = "SELECT counter
        FROM   attachment
        WHERE  postid = 1058";
$B601result = mysql_query($B601);
while ($row = mysql_fetch_assoc($B601result)) {
    $B601count = $row["counter"];
}
mysql_free_result($B601result);

// Beta 7
$B7 = "SELECT counter
        FROM   attachment
        WHERE  postid = 1888";
$B7result = mysql_query($B7);
while ($row = mysql_fetch_assoc($B7result)) {
    $B7count = $row["counter"];
}
mysql_free_result($B7result);


// Beta 9.8.1
$B981 = "SELECT counter
        FROM   attachment
        WHERE  postid = 40434";
$B981result = mysql_query($B981);
while ($row = mysql_fetch_assoc($B981result)) {
    $B981count = $row["counter"];
}
mysql_free_result($B981result);

// Beta 9.9.5
$B995 = "SELECT counter
        FROM   attachment
        WHERE  attachmentid = 1759";
$B995result = mysql_query($B995);
while ($row = mysql_fetch_assoc($B995result)) {
    $B995count = $row["counter"];
}
mysql_free_result($B995result);

// 1.0 RC1
$rc1 = "SELECT counter
        FROM   attachment
        WHERE  attachmentid = 1759";
$rc1result = mysql_query($rc1);
while ($row = mysql_fetch_assoc($rc1result)) {
    $rc1count = $row["counter"];
}
mysql_free_result($rc1result);

// 1.0 RC2
$rc2 = "SELECT counter
        FROM   attachment
        WHERE  attachmentid = 1798";
$rc2result = mysql_query($rc2);
while ($row = mysql_fetch_assoc($rc2result)) {
    $rc2count = $row["counter"];
}
mysql_free_result($rc2result);

// 1.0 RC3
$rc3 = "SELECT counter
        FROM   attachment
        WHERE  attachmentid = 1859";
$rc3result = mysql_query($rc3);
while ($row = mysql_fetch_assoc($rc3result)) {
    $rc3count = $row["counter"];
}
mysql_free_result($rc3result);

// 1.0 RC4
$rc4 = "SELECT counter
        FROM   attachment
        WHERE  attachmentid = 1876";
$rc4result = mysql_query($rc4);
while ($row = mysql_fetch_assoc($rc4result)) {
    $rc4count = $row["counter"];
}
mysql_free_result($rc4result);

/*
echo 'Pligg Forum Downloads <br />';
echo 'Beta 4:' .$B4count. '<br />';
echo 'Beta 5:' .$B5count. '<br />';
echo 'Beta 6.0.1:' .$B601count. '<br />';
echo 'Beta 7:' .$B7count. '<br />';
echo 'Beta 9.9.5:' .$B995count. '<br />';
echo '1.0 RC1:' .$rc1count. '<br />';
echo '1.0 RC2:' .$rc2count. '<br />';
echo '1.0 RC3:' .$rc3count. '<br />';
echo '1.0 RC4:' .$rc4count. '<br />';
*/

// Add up all of the Pligg Forum Downloads
// The number values are from the retired downloads section that we used to use, which are no longer active
$subtotal = $B4count + $B5count + 993 + $B601count + $B7count + 1003 + 1414 + 3382 + 4109 + 10494 + 21342 + $B995count + $rc1count + $rc2count + $rc3count + $rc4count ;
// echo 'Subtotal: ' .$subtotal. '<br />';

// Add the SourceForge totals as of 04/14/2009
$sftotal = 7744 + 14184 + 9393 + 8341 + 15295 + 14305 + 8043 + 2516 + 1901 ;
// echo 'Sourceforge: ' .$sftotal. '<br />';

// misc represents softpedia.com
$misc = 1115 + 50 ;
// echo 'Other: ' .$misc. '<br />';

// Add up all of the subtotals
$totaldl = $subtotal + $sftotal + $misc;

// Now it checks if the url specifies a source for the download number
// URL values are ?source=sourceforge , ?source=forums and ?source=misc
if(!isset($_GET['stat']))	{
	// Add commas to the number every 3 digits in from the right
	$formatted_total = number_format($totaldl);
	// Output the final total number
	echo $formatted_total;
} else {
	$stat = $_GET['stat'];
	if ( $stat == "sourceforge" ) {
		// Add commas to the number every 3 digits in from the right
		$formatted_sourceforge = number_format($sftotal);
		echo $formatted_sourceforge;
	}
	if ( $stat == "home" ) {
		// Add commas to the number every 3 digits in from the right
		$formatted_subtotal = number_format($subtotal);
		echo $formatted_subtotal;
	}
	if ( $stat == "misc" ) {
		// Add commas to the number every 3 digits in from the right
		$formatted_misc = number_format($misc);
		echo $formatted_misc;
	}
	if ( $stat == "hour" ) {
		// Finds the average number of downloads per hour from Pligg 1.0 RC start date
		$pliggstart = strtotime( "2009/01/30" );
		$today = strtotime( date("Y/m/d") );
		$timediff = abs(($today - $pliggstart) / 3600 / 24);
		// Release candidate versions
		$version1 = $rc1count + $rc2count + $rc3count + $rc4count ;
		$averagehour = $version1 / $timediff / 24;
		// echo round($averagehour, 2);
		// Add commas to the number every 3 digits in from the right
		$formatted_averagehour = number_format($averagehour);
		echo $formatted_averagehour;
	}
	if ( $stat == "day" ) {
		$pliggstart = strtotime( "2009/01/30" );
		$today = strtotime( date("Y/m/d") );
		$timediff = abs(($today - $pliggstart) / 3600 / 24);
		// Release candidate versions
		$version1 = $rc1count + $rc2count + $rc3count + $rc4count ;
		$averageday = $version1 / $timediff;
		// echo round($averageday, 2);
		// Add commas to the number every 3 digits in from the right
		$formatted_averageday = number_format($averageday);
		echo $formatted_averageday;
	}
	if ( $stat == "week" ) {
		$pliggstart = strtotime( "2009/01/30" );
		$today = strtotime( date("Y/m/d") );
		$timediff = abs(($today - $pliggstart) / 3600 / 24);
		// Release candidate versions
		$version1 = $rc1count + $rc2count + $rc3count + $rc4count ;
		$averageweek = $version1 / $timediff * 7;
		// echo round($averageweek, 2);
		// Add commas to the number every 3 digits in from the right
		$formatted_averageweek = number_format($averageweek);
		echo $formatted_averageweek;
	}
	if ( $stat == "month" ) {
		$pliggstart = strtotime( "2009/01/30" );
		$today = strtotime( date("Y/m/d") );
		$timediff = abs(($today - $pliggstart) / 3600 / 24);
		// Release candidate versions
		$version1 = $rc1count + $rc2count + $rc3count + $rc4count ;
		$averagemonth = $version1 / $timediff * 30;
		// echo round($averagemonth, 2);
		// Add commas to the number every 3 digits in from the right
		$formatted_averagemonth = number_format($averagemonth);
		echo $formatted_averagemonth;
	}
	if ( $stat == "year" ) {
		$pliggstart = strtotime( "2009/01/30" );
		$today = strtotime( date("Y/m/d") );
		$timediff = abs(($today - $pliggstart) / 3600 / 24);
		// Release candidate versions
		$version1 = $rc1count + $rc2count + $rc3count + $rc4count ;
		$averageyear = $version1 / $timediff * 365;
		// echo round($averageyear, 2);
		// Add commas to the number every 3 digits in from the right
		$formatted_averageyear = number_format($averageyear);
		echo $formatted_averageyear;
	}
}

?>

2 thoughts on “Pligg Download Counter

  1. Pingback: Back to Sourceforge | Pligg CMS

  2. I would be download 208,680 … if only the Sourceforge download would work properly. I have tried several times from different computers and browsers. It keeps telling me there is “no mirror for this file,” whatever that means. Any advice? Thanks.

Comments are closed.