FileSystemListing: Difference between revisions
Content deleted Content added
No edit summary |
Jack Phoenix (talk | contribs) Replacing page with '{{MovedToMediaWiki|Extension:FileSystemListing}} Imported with full page histories. --~~~~' |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
{{MovedToMediaWiki|Extension:FileSystemListing}} |
|||
Imported with full page histories. --[[User:Roosa|<b>Roosa</b>]] <sub>([[User talk:Roosa|<font color="hotpink">Talk</font>]]) </sub> 16:40, 26 June 2007 (UTC) |
|||
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: |
|||
<pre> |
|||
<dirlist dir="/var/www/html/downloads"></dirlist> |
|||
</pre> |
|||
== 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! |
|||
<pre> |
|||
<?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; |
|||
} |
|||
?> |
|||
</pre> |
|||
== 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>. |
|||
[[Category:MediaWiki extensions]] |
Latest revision as of 16:40, 26 June 2007
This page has been moved to Extension:FileSystemListing on mediawiki.org.
Any modifications or additions to this page should be made on mediawiki.org instead. |
Imported with full page histories. --Roosa (Talk) 16:40, 26 June 2007 (UTC)