FileSystemListing: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
Esteffect (talk | contribs)
correct category
mNo edit summary
Line 3: Line 3:
This extension provides an easy way to list filesystem contents on your webserver.
This extension provides an easy way to list filesystem contents on your webserver.


The initial intention was to easily map a link for each file provided for download on a website that uses mediawiki as it's backend, and render directory contents nicely inside an article called 'Downloads'.
The initial intention was to easily map a link for each file provided for download on a website that uses mediawiki as its backend, and render directory contents nicely inside an article called 'Downloads'.


The author hopes it will be useful, and also extended to provide more functionality
The author hopes it will be useful, and also extended to provide more functionality

Revision as of 15:15, 1 August 2006

What is FileSystemListing

This extension provides an easy way to list filesystem contents on your webserver.

The initial intention was to easily map a link for each file provided for download on a website that uses mediawiki as its backend, and render directory contents nicely inside an article called 'Downloads'.

The author hopes it will be useful, and also extended to provide more functionality

How to use

To use, simply put on your wiki page something like:

 <dirlist dir="/var/www/html/downloads"></dirlist>

The code

Here is the source code. If somebody has more time to see how can be added to the CVS repository, it'll be nice!


<?php
#
# Author: Javier Castro (jac) - javier.alejandro.castro@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

$wgExtensionFunctions[] = "wfListDirectory";

function wfListDirectory() {
    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 <example> ... </example>
    # the second parameter is the callback function for
    # processing the text between the tags
    $wgParser->setHook( "dirlist", "renderDirList" );
}

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

function renderDirContents($dirArray, $dirName, $prefix=null)
{
    $output = "<ul>";
    foreach ($dirArray as $value)
    {
	if ($value['content'] !== null)
	{
    	    $output .= "<li><h3>".$value['name']."</h3></li>";
	    $output .= renderDirContents($value['content'], $dirName, $prefix);
	}
	else
	{
	    if ($prefix)
	    {
		$pathToFile = substr($value['path'], strlen($dirName));
		$href = $prefix . $pathToFile;
    		$output .= "<li><a href='$href'>".$value['name']."</a></li>";
	    }
	    else
	    {
        	$output .= "<li>".$value['name']."</li>";
	    }
	}
    }
    $output .= "</ul>";
    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 == "..") 
	    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;
}

?>