Extension:DirList
From MediaWiki.org
|
DirList Release status: stable |
|
|---|---|
| Implementation | Tag |
| Description | lists the content of a directory |
| Author(s) | Andreas Hagmann (AhTalk) |
| Last version | 1.0 |
| MediaWiki | tested with 1.12, might work with others too |
| License | GPL |
| Download | Extension:DirList#Code |
| Example | Extension:DirList#Usage |
|
Check usage (experimental) |
|
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 sortby='timestamp'> * directory_with_files * </dirlist> * * One can sort by * * name * * filename * * fullpath * * extension * * timestamp * * size * */ if(! defined( 'MEDIAWIKI' ) ) { echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" ); die( -1 ); } define('EXTENSION_VERSION','1.0'); $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' ); } // sort function from // http://blog.jachim.be/2009/09/php-msort-multidimensional-array-sort/ // modified for case insensitive sort function msort($array, $key, $sort_flags = SORT_REGULAR) { if (is_array($array) && count($array) > 0) { if (!empty($key)) { $mapping = array(); foreach ($array as $k => $v) { $sort_key = ''; if (!is_array($key)) { $sort_key = $v[$key]; } else { // @TODO This should be fixed, now it will be sorted as string foreach ($key as $key_key) { $sort_key .= $v[$key_key]; } $sort_flags = SORT_STRING; } $mapping[$k] = $sort_key; } natcasesort($mapping); $sorted = array(); foreach ($mapping as $k => $v) { $sorted[] = $array[$k]; } return $sorted; } } return $array; } function dirListRender( $input, $args, $parser ) { global $IP; $parser->disableCache(); $path = $parser->recursiveTagParse(trim($input)); $fullpath = "$IP/". $path; 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'=>filesize ($fullpath), 'timestamp'=>$date ); } } // sort $files = msort($files, $args['sortby']); //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;'>".round($file['size']/1024, 2) . "KB"."</td></tr>"; } $output .= "</table>"; return $output; }
[edit] Usage
Put the directory name (relative to $IP) between <dirlist> and </dirlist>
<dirlist sortby='timestamp'>public</dirlist>
One can sort by:
- name
- filename
- fullpath
- extension
- timestamp
- size

