Extension:DirList

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
DirList

Release status: stable

Implementation Tag
Description lists the content of a directory
Author(s) Andreas Hagmann (Ah Talk)
Version 0.1
MediaWiki tested with 1.12, might work with others too
License GPL
Download Extension:DirList#Code
Example Extension:DirList#Usage

Contents

[edit] Overview

This extension lists the content of a directory in a table and creates links for downloading files.

[edit] Installation

Create the file with the Code and add the following to your LocalSettings.php:

require_once("extensions/DirList.php");

[edit] Code

Put the following into $IP/extensions/DirList.php:

<?php
/**
 * DirList.php
 * This extension lists the content of a directory
 * written by Andreas Hagmann
 * andreas@hagmann.name
 * To activate the functionality of this extension include the following in your
 * LocalSettings.php file:
 * require_once('$IP/extensions/DirList.php');
 *
 * === Example ===
 * <dirlist>
 * directory_with_files
 * </dirlist>
 */
 
if(! defined( 'MEDIAWIKI' ) ) {
   echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" );
   die( -1 );
}
 
define('EXTENSION_VERSION','0.1');
 
$wgExtensionCredits['other'][] = array(
  'name'         => 'DirList',
  'author'       =>'Andreas Hagmann <andreas@hagmann.name>', 
  'url'          => 'http://www.mediawiki.org/wiki/Extension:DirList',
  'description'  => 'This extension lists the content of a directory',
  'version'      => EXTENSION_VERSION
);
 
$wgExtensionFunctions[] = 'dirListSetup';
 
function dirListSetup() {
    global $wgParser;
    $wgParser->setHook( 'dirlist', 'dirListRender' );
}
 
function dirListRender( $input, $args, $parser ) {
        global $IP;
 
        $parser->disableCache();
        $path = trim($input);
        $fullpath = "$IP/".trim($input);
        if (!is_dir($path))      return ("'" . htmlspecialchars($path) . "' not found!<br />");
 
        //clearstatcache ();
 
        //parse dir
        $files = array();
        $handle = opendir ($fullpath);
        while ($file=readdir ($handle)){
                if ($file!="."  &&  $file!="..") {
                        $fullpath = "$path/$file";
                        $date = filemtime($fullpath);
                        $info = $info=pathinfo($file);
                        $punkt=strrpos($file, ".");
                        $name=substr($file, 0, $punkt);
                        $files[]=array(        'name'=>$name,
                                        'extension'=>strtolower($info['extension']),
                                        'filename'=>$file,
                                        'fullpath'=>$fullpath,
                                        'size'=>(round(filesize ($fullpath)/1024, 2) . "KB"),
                                        'timestamp'=>$date
                                        );
                }
        }
 
        //create html output
        $output = "<style>tr.body:hover {background-color: #DDDDDD;}</style>";
        $output .= "<table style='width: 80%; border-collapse: collapse;'>";
        //Table Head
        $output .= "<tr><th>Datei</th><th>Datum</th><th>Größe</th></tr>";
        foreach ($files as $file) {
                $link = "<a href='".$file['fullpath']."'>".$file['filename']."</a>";
                $output .= "<tr class='body' style='border-top: 1px solid black;'><td>".$link."</td><td style='width: 150px; text-align: right;'>".date ("d.m.Y H:i", $file['timestamp'])."</td><td style='width: 100px; text-align: right;'>".$file['size']."</td></tr>";
        }
        $output .= "</table>";
        return $output;
}

[edit] Usage

Put the directory name (relative to $IP) between <dirlist> and </dirlist>

<dirlist>public</dirlist>

[edit] Output

Image:dirlist_v0.1.png

Personal tools