Extension:DirList

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.svg
DirList

Release status:Extension status stable

Dirlist v0.1.png
ImplementationTemplate:Extension#type Tag
DescriptionTemplate:Extension#description lists the content of a directory
Author(s)Template:Extension#username Andreas Hagmann (Ahtalk)
Latest versionTemplate:Extension#version 1.0
MediaWikiTemplate:Extension#mediawiki tested with 1.12, not working with 1.22
LicenseTemplate:Extension#license GPL
Download Extension:DirList#Code
ExampleTemplate:Extension#example Extension:DirList#Usage

Translate the DirList extension if it is available at translatewiki.net

Check usage and version matrix.

Overview[edit]

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

Installation[edit]

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

require_once("extensions/DirList.php");

Code[edit]

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'=>isset($info['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']/1000, 2) . "kB"."</td></tr>";
	}
	$output .= "</table>";
	return $output;
}

Usage[edit]

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


Output[edit]

Dirlist v0.1.png