Open main menu

Extension:LocalFileSystem

MediaWiki extensions manual
OOjs UI icon advanced.svg
LocalFileSystem
Release status: experimental
Implementation Data extraction, Special page
Description Displays files of one directory of the local file system
Author(s) Florian Konnertz (groovehuntertalk)
Latest version 0.1 (2008-02-20)
MediaWiki 1.8
License No license specified
Download No link
Example no example public currently
Translate the LocalFileSystem extension if it is available at translatewiki.net
Check usage and version matrix.

What can this extension do?Edit

It displays (currently only) restructured text files from a directory of the local file system (rendered to HTML of course) as a wiki page. It first checks if a page title (called by current request) exists in the wiki/DB and if this is not the case checks if available in the directory (which has to be set in the extension's code). All pages in the dir can be see on Special:LocalFileSystem

Currently this is a simple hack, I aim to extend it to check for different file formats, html, stx, txt, which on as default. I have to dive in further in the parser code and hooks. Anyone wanting to help is welcome.

The idea of the extension is simple and i think it's useful. My motivation was to make my old wiki pages available in my Mediawiki - unfortunately they are not linked internally - because they're CamelCased. Another thing which should be done.

This is my first extension and i am quite new to PHP so my code and style is not the best, sorry for that.

UsageEdit

Link pages and folders known to be in the filesystem stored into the FSDIR parameter ("./extensions/" by default).

InstallationEdit

  • Download and install rst2html.py on your system, if not yet installed. It's part of docutils
  • Download and place the file(s) in a directory called LocalFileSystem in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/LocalFileSystem/LocalFileSystem.php";
    
  •   Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

User rightsEdit

To view the special page i invent the permission 'localfilesystem' which per default everybody has.

CodeEdit

<?php
// LocalFileSystem extension
// requires rst2html.py installed 
if (!defined('MEDIAWIKI')) die();
 
 
define('FSDIR', './extensions/');
$rst2html = '/usr/bin/rst2html.py';     // rst parser executable
 
 
$wgHooks['ArticleFromTitle'][]      = 'LFS_ArticleFromTitle';
$wgHooks['OutputPageBeforeHTML'][]  = 'LFS_addContent';
 
$wgExtensionCredits['specialpage'][] = array(
       'name' => 'LocalFileSystem extension',
       'version' => '1.0.0',
       'author' => 'Florian Konnertz',
       'description' => 'Displays files of one directory of the local filesystem into [[Special:LocalFileSystem]]',
       'url' => 'http://openspirit.de/mediawiki/'
);
 
$wgExtensionFunctions[] = "wfSetupLocalFileSystem";
$wgAvailableRights[] = 'localfilesystem';
$wgGroupPermissions['*']   ['localfilesystem'] = true;
 
function wfSetupLocalFileSystem() {
    // FIXME title of specialpage is not rendered correctly
    require_once( 'SpecialPage.php' );
    SpecialPage::addPage( new SpecialPage( 'LocalFileSystem', 'localfilesystem',
      /*listed*/ true, /*function*/ false, /*file*/ false )  );
}
 
function wfSpecialLocalFileSystem() {
    global $wgOut;
    $files = scandir(FSDIR);
    $files = array_slice($files, 2);        // remove . and .. from scandir's list
    foreach ($files as $k=>$fn) {
        $line = '<a href="/index.php?title='.$fn.'">'.$fn.'</a><br>';   // FIXME: replace with mw globals
        $line .= "\n";
        $wgOut->addHTML($line);
    }
 
}
 
 
$fstitle = '';
 
function LFS_ArticleFromTitle(&$title, &$article) {
    if ( $title->exists() ) {
        return true;
    }
    global $fstitle;
    $fn = FSDIR.$title;
    if (file_exists($fn)) {
        $fstitle = $fn;
    }
    return true;
}
 
function LFS_addContent(&$out, &$text) {
    global $fstitle;
    global $wgOut;
    if ($fstitle) {
        $t = file_get_contents($fstitle);
        $html = rstRstRender($t);
        $text = $html;
    }
    return true;
}
 
function rstRstRender($input) {
    // credits: ReStructured text extension
    // FIXME warnings and errors from rst2html are displayed on the page, not wanted.
    global $rst2html;
 
    if (!is_executable($rst2html))
    {
        return "<strong class='error'>" .
               "reStructureText extension: No executable at $rst2html" .
               "</strong>";
    }
 
    # If pipe errors are reported, enable output to the file.
    # But make certain the file doesn't already exist or else
    # the webserver may not have permission to create it.
    $io_desc = array(
            0 => array('pipe', 'r'),
            1 => array('pipe', 'w'),
            /* 2 => array('file', '/tmp/error-output.txt', 'a') */);
 
    $res = proc_open($rst2html . " --stylesheet-path='' " .
                     "--initial-header-level=2 --no-doc-title " .
                     "--no-file-insertion --no-raw",
                     $io_desc, $pipes, '/tmp', NULL);
    if (is_resource($res))
    {
        fwrite($pipes[0], $input);
        fclose($pipes[0]);
 
        $html = stream_get_contents($pipes[1]);
        fclose($pipes[1]);
 
        $html = preg_replace('/.*<body>\n(.*)<\/body>.*/s', '${1}', $html);
    }
    else
    {
        $html = "<strong class='error'>" .
                "reStructureText extension: error opening pipe" .
                "</strong>";
    }
 
    return $html;
}

See alsoEdit

Not sure, some extension might be related ie.,