Extension:DownloadCounter

From MediaWiki.org
Jump to: navigation, search


MediaWiki extensions manual - list
Crystal Clear action run.png
DownloadCounter

Release status: beta

Description Add some functions for counting user's downloaded file(s)
Author(s) Eric Petit (SurfzoidTalk)
Last version 0.1 (November 13, 2009)
MediaWiki 1.11+
License GPL
Download see below

Check usage (experimental)

Contents

[edit] What can this extension do?

This extensions can display the total number and the last date of downloaded files, which are recorded in MySql database throught "download.php"

[edit] Usage

In your Wiki article, you will register an download like that :

[http://exemple.com/mediawiki/download.php?f=DataCollectorEdit/Setup-DataCollectorEdit.2.0.exe Download this great software here]

And display the recorded data like that.

 <AfficheDetailsTelechargements name="DataCollectorEdit/Setup-DataCollectorEdit.2.0.exe" | type="total"></AfficheDetailsTelechargements> total download.
 Last download was : <AfficheDetailsTelechargements name="DataCollectorEdit/Setup-DataCollectorEdit.2.0.exe" | type="date"></AfficheDetailsTelechargements>
 

[edit] Installation

  • 1. Add this in the LocalSetting.php:
   #add configuration parameters here
   require "$IP/extensions/downloadcount.php";
  • 2. Create a file download.php in the root directory of your mediawiki installation
  • 3. Create a file downloadcount.php in the root/extensions directory of your mediawiki installation
  • 4. Create the following MySql Table in wikidb database
  -- Create the table : 'downloads_files'
 
  CREATE TABLE downloads_files (
    filename      VARCHAR(255) NOT NULL, -- I guess, file name is unique ;o)
    downloaded    INT(5)       NOT NULL DEFAULT 0, -- By default, downloaded 0 time (could be 1)
    last_download INT(5)       NOT NULL DEFAULT 0, -- By default, downloaded 0 time (could be 1)
 
    PRIMARY KEY(filename)
  ) Type = MyISAM;
  • 5. Setup correctly MySql conection in the extension ("downloadcount.php") and in the php download file ("download.php")
  • 6. Setup correctly the root directory of file to download

[edit] Requirements

  • MediaWiki version 1.11+
  • MySQL 5+

[edit] download.php code

<?php
#
# Author: Eric Petit (Surfzoid) - surfzoid@gmail.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# http://www.gnu.org/copyleft/gpl.html

//http://www.php-astux.info/script-compteur-telechargements.php
 
    require_once( dirname('.') . '/includes/AutoLoader.php' );
        $filesdir = 'Download/'; // // Path where the files to download are stored
    require_once( dirname('.') . '/includes/WebStart.php' );
/**
 * Protect against register_globals vulnerabilities.
 * This line must be present before any global variable is referenced.
 */
 
        // Get the filename argument
        $filename = (isset($_GET['f'])) ? trim(sprintf("%s", $_GET['f'])) : '';
 
        if ($filename != '') // It is not empty, okay.
        {
                // WARNING : Check if the file exist
                if ((file_exists($filesdir . $filename)) && (is_file($filesdir . $filename)))
                {
                        // File is here, increment the counter
                        $req_augmenterdownload = "INSERT INTO `downloads_files` (
            `filename` ,
            `downloaded` ,
            `last_download`
            )
            VALUES ('".$filename."', '(downloaded+1)', '".time()."')
                         ON DUPLICATE KEY UPDATE 
                                                        downloaded = (downloaded+1),
                                                        last_download = '".time()."',
                                                        filename='".$filename."';";
 
 
                        // // Now execute the query
        $db = wfGetDB(DB_MASTER);
                $FileDetails = $db->doQuery($req_augmenterdownload);// or die($req_FileDetails.'<br />'.mysql_error());
 
                        // Query finish, send the file to the user
                        header("Location: ".$filesdir . $filename);
                        exit();
                };
        };
 
?>

[edit] extensions/downloadcount.php code

<?php
#
# Author: Eric Petit (Surfzoid) - surfzoid@gmail.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# http://www.gnu.org/copyleft/gpl.html

//http://www.php-astux.info/script-compteur-telechargements.php
        // Mini config
 
    global $IP; 
    require_once("$IP/includes/AutoLoader.php");
        $filesdir = 'Download/'; // Path where the files to download are stored
 
/**
 * Protect against register_globals vulnerabilities.
 * This line must be present before any global variable is referenced.
 */
if (!defined('MEDIAWIKI')) die();
 
        function AfficheDetailsTelechargements($input, $argv, $parser)
        {
    $parser->disableCache();
        $file = $argv['name'];
    $details_type = $argv['type'];
 
                $req_FileDetails = "SELECT
                                                {CHAMP}
                                        FROM
                                                downloads_files
                                        WHERE
                                                filename='".$file."';";
 
 
                // Read the number or the last date of a downloaded file
                $req_FileDetails = ($details_type == 'total') ?
                        str_replace('{CHAMP}', 'downloaded', $req_FileDetails) :
                        str_replace('{CHAMP}', 'last_download', $req_FileDetails);
 
 
 
                // Execute the query and return the result
                //$FileDetails = DatabaseMysql.query($req_FileDetails);//mysql_query($req_FileDetails);// or die($req_FileDetails.'<br />'.mysql_error());
        $db = wfGetDB(DB_MASTER);
                $FileDetails = $db->doQuery($req_FileDetails);// or die($req_FileDetails.'<br />'.mysql_error());
 
                if (mysql_num_rows($FileDetails) != 1) // File not found
                {
                        return 0;
                }
                else // File found
                {
                        $rs = mysql_fetch_array($FileDetails);
 
                        return ($details_type == 'total') ? $rs['downloaded'] : date("d/m/Y H:i:s",$rs['last_download']);
                };
        };
 
//Avoid unstubbing $wgParser on setHook() too early on modern (1.12+) MW versions, as per r35980
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
        $wgHooks['ParserFirstCallInit'][] = 'wfDownloadCounter';
} else {
        $wgExtensionFunctions[] = 'wfDownloadCounter';
}
 
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'DownloadCounter',
        'version' => '0.1',
        'author' => 'Eric Petit',
        'description' => 'Allows the display of total and last download, Read download counter value',
        'url' => 'http://www.mediawiki.org/wiki/Extension:DownloadCounter',
);
 
function wfDownloadCounter() {
        global $wgParser;
        # register the extension with the WikiText parser
 # the first parameter is the name of the new tag.
 # In this case it defines the tag <dirlist> ... </dirlist>
 # the second parameter is the callback function for
 # processing the text between the tags
 $wgParser->setHook( 'AfficheDetailsTelechargements', 'AfficheDetailsTelechargements' );
        $wgParser->setHook( 'DownloadFs', 'DownloadFs' );
        return true;
}
 
?>
Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox