allwiki首页  
天下维客 你可以修改的网络知识库
首页最近更改优秀条目专题展示电脑科技词典软件学习网络知识电脑安全明星时尚天下百科
 

MediaWiki扩展:文件系统列表

天下维客,你可以修改的网络知识库

Jump to: navigation, search

本文取自元维基,欢迎共同翻译、整理与大家共享,促进中文wiki发展^_^

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 it's 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;
}

?>

原文参见: FileSystemListing

Personal tools
工具
金银币拍卖 金币拍卖预展  金银币网店 熊猫金银币 生肖金银币