User:Lecid/Downloads/Downloads.php

Downloads.php v 0.1 edit

<?php
# Author : Romain GEORGES
# Date : 03/31/2006
# type : MediaWiki Pluggins
# name : Downloads.php
# topic : Downloads list in table from filesystem
# derivated from 'FileSystemListing' by Javier Castro (jac) - javier.alejandro.castro@gmail.com
#     http://meta.wikimedia.org/wiki/FileSystemListing
# usage : <downloads path=value url=value></downloads>
# argument are NOT optionals
# Install by copy in "$MEDIAWIKI_PATH/extensions/" the Downloads.php file
# and add require_once(" extensions/Downloads.php "); in $MEDIAWIKI_PATH/LocalSettings.php
$wgExtensionCredits['parserhook'][] = array(
                                       'name' => 'Downloads',
                                       'description' => 'Downloads : Provide a downloads Aera on your wiki fom FS with comments and icons',
                                       'author' => 'Romain GEORGES',
                                       'url' => 'http://www.diablotins.org',
                                       'version'=>'0.1'
                                       );


$wgExtensionFunctions[] = "wfDownloads";
function wfDownloads() {
  global $wgParser;
  $wgParser->setHook( "downloads", "renderDownloads" );
}

# The callback function for converting the input text to HTML output
function renderDownloads( $input, $argv )
{
  $path = $argv['path'];
  $url = $argv['url'];
  if ($path !== "")
    {
      $result = readDirContents($path);
      return renderDirContents($result, $path, $url);
    }
  return "";
}

function iconexist($path,$ext){
  if ($path{strlen($path)-1} !== '/')
    $path .= '/';
  $im = $path .".icons/". $ext .".gif";
  if (file_exists($im)) {
    return true;
  }
  else
  {
    return false;
  }
}

function renderDirContents($FileArray, $PathName, $url=null) {
  $output = '<table align=center style="border:1px dotted grey;background-color:lightgrey;text-align:left;color:black;border-spacing: 1em;-moz-border-radius: 0.6em;">';

  foreach ($FileArray as $value)
    {
      if ($value['content'] !== null)
        {
          $output .= "<li><h3>".$value['name']."</h3></li>";
          $output .= renderDirContents($value['content'], $PathName, $url);
        }
      else
        {
          if ($url)
            {
              if ($PathName{strlen($PathName)-1} !== '/')
                $PathName .= '/';
              if ($url{strlen($url)-1} !== '/')
                $url .= '/';
              $pathToFile = substr($value['path'], strlen($PathName));
              $href = $url . $pathToFile;
              $output .= '<tr><td style="border:1px dotted red;background-color:white;width=90%;color:black;-moz-border-radius: 0.6em;padding: 1em 1em 1em 1em ">';
              $tb = explode('.',$pathToFile);
              $ext = array_pop($tb);
              $file = implode('.',$tb);
              $info = $file.".info";
              $image = $url . ".icons/". $ext . ".gif";
              if (iconexist($PathName,$ext))
                $output .= "<ul style='list-style-image: none;'><li><img src='$image'>";
              $output .= "  <b>Fichier</b> : <a href='$href'>".$value['name']."</a></li>\n";
              if (file_exists($PathName.".data/".$info)) {
                $tb = file($PathName.".data/".$info);
                $output .= "<li><b>  Description</b> : ".$tb[0]."</li>\n";
                $output .= "<li><b>  Plateforme</b> : ".iconv("ISO-8859-1","UTF-8",$tb[1])."</li>\n";
              }
              $output .= "</td></tr>\n";
            }
          else
            {
              $output .= "<tr><td>".$value['name']."</td></tr>\n";
            }
        }
    }
  $output .= "</ul></td></tr></table>";
  return $output;
}

function readDirContents($dir, $sort=true)
{
  if ($dir{strlen($dir)-1} !== '/')
    $dir .= '/';
  $a = array();
  $gd = opendir($dir);
  $i=0;
  while (($fileName = readdir($gd)) !== false)
    {
      if ($fileName == "." || $fileName == ".." || $fileName == ".data" || $fileName == ".icons")
        continue;
      if (is_dir($dir.$fileName))
        $a[$i++] = array("path" => $dir.$fileName, "name" => $fileName, "content" => readDirContents($dir.$fileName));
      else
        $a[$i++] = array("path" => $dir.$fileName, "name" => $fileName, "content" => null);
    }
  closedir($gd);
  if ($sort)
    {
      sort($a);
    }
  return $a;
}

?>