Extension:FileSystemListing

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
FileSystemListing

Release status: stable

Implementation Tag
Description Provides an easy way to list filesystem contents on your webserver.
Author(s) Javier Castro
Last version 1.0.0
License GPL
Download see below

Check usage (experimental)

The FileSystemListing 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

[edit] How to use

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

For the Files/Directorys list:
 <dirlist dir="/var/www/html/downloads"></dirlist>

for the DateTime of an file :
<font color=red>'''<FsDateTime name="/var/www/html/downloads/DataCollector/Setup.2.0.exe">
</FsDateTime>'''</font>

[edit] The code

Here is the source code. If somebody has more time to see how can be added to the SVN 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

//Avoid unstubbing $wgParser on setHook() too early on modern (1.12+) MW versions, as per r35980
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
        $wgHooks['ParserFirstCallInit'][] = 'wfListDirectory';
} else {
        $wgExtensionFunctions[] = 'wfListDirectory';
}
 
$wgExtensionCredits['parserhook'][] = array( 
        'name' => 'FileSystemListing',
        'version' => '1.0.0',
        'author' => 'Javier Castro',
        'description' => 'Provides an easy way to list filesystem contents on your webserver with <tt>&lt;dirlist&gt;</tt> tag',
        'url' => 'http://www.mediawiki.org/wiki/Extension:FileSystemListing'
);
 
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 <dirlist> ... </dirlist>
 # the second parameter is the callback function for
 # processing the text between the tags
 $wgParser->setHook( 'dirlist', 'renderDirList' );
        $wgParser->setHook( 'FsDateTime', 'fsDT' );
        return true;
}
 
# The callback function for converting the input text to HTML output
function renderDirList( $input, $argv, $parser ) { 
$parser->disableCache();
        $dir = $argv['name'];
        $filePrefix = $argv['fileprefix'];
        if ($dir !== "") {
                $result = readDirContents($dir);
                return renderDirContents($result, $dir, $filePrefix);
        }
        return "";
}
 
# The callback function for converting the input text to HTML output and return the date time of an file
function fsDT( $input, $argv, $parser ) {  
$parser->disableCache();
        $dir = $argv['name'];
        //$filePrefix = $argv['fileprefix'];
    $fsdate = date ("d/m/Y H:i",filemtime($dir.$fileName));
        if ($dir !== "") {
                return $fsdate;
        }
        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;
}

[edit] Use with FCKEditor

I have noticed that FCKEditor alters some HTML tags as they are entered. This proved to be the case with the custom tag <dirlist dir="">, where the dir="" would be removed.

By replacing $dir = $argv['dir']; with $dir = $argv['name'];

in the function function renderDirList( $input, $argv ) above, FCKeditor allows the argument in the tag to be preserved.

Usage then changes to <dirlist name="path/to/directory"></dirlist>.

Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox