Extension:ViewFiles

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

Release status: stable

px
Implementation Special page
Description Allows users to view the contents of a limited set of files made available by the system administrator.
Author(s) (Leucostictetalk)
Latest version 1.0.2
MediaWiki 1.19+
License GPL
Download No link
Example http://meta.inclumedia.org/wiki/Special:ViewFiles

Translate the ViewFiles extension if possible

Check usage and version matrix; code metrics

The ViewFiles extension allows users to view the contents of a limited set of files made available by the system administrator. A possible use for this extension would be to enable your users to see the current contents of your configuration files (e.g. LocalSettings.php and InitialiseSettings.php) or customized skin files so that they could better assist with troubleshooting and make recommendations for configuration changes. Don't forget, if you're going to use this to display files such as LocalSettings.php that contain sensitive data, you'll want to get that data out of webroot before exposing those files to the world.

Installation[edit | edit source]

Download SyntaxHighlight_GeSHi[edit | edit source]

Download ViewFiles[edit | edit source]

  • Download the latest version of ViewFiles
  • Create a folder in the extensions folder named ViewFiles
  • Move the files to the extensions/ViewFiles/ folder

Install SyntaxHighlight_GeSHi and ViewFiles[edit | edit source]

  • Edit LocalSettings.php in the root of your MediaWiki installation, and add the following line near the bottom:
require_once("$IP/extensions/ViewFiles/ViewFiles.php");
require_once("$IP/extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.php");
require_once("$IP/extensions/SyntaxHighlight_GeSHi/geshi/geshi.php");

See Extension:SyntaxHighlight_GeSHi#Default_Source_Language for information on how to set a default source language (optional; will not affect how files are displayed by ViewFiles).

Configuration[edit | edit source]

The only required configuration setting is $wgViewFilesFilePathList.

$wgViewFilesIntro[edit | edit source]

What to begin the page with; e.g.

$wgViewFilesIntro = "__FORCETOC__\n";

$wgViewFilesBegin[edit | edit source]

What to begin each file listing with; defaults to:

$wgViewFilesBegin = '<source lang="$1">' . "\n";

$wgViewFilesEnd[edit | edit source]

What to end each file listing with; defaults to:

$wgViewFilesEnd = '</source>' . "\n";

$wgViewFilesFileLangList[edit | edit source]

This list of filenames and associated languages overrides GeSHi's extension lookup. Defaults to:

$wgViewFilesFileLangList = array (
	'.htaccess' => 'apache',
	'robots.txt' => 'robots',
);

$wgViewFilesFilePathList[edit | edit source]

Create a list of files and their paths, e.g.:

$wgViewFilesPathList = array(
	'LocalSettings.php' => "$IP/LocalSettings.php",
	'robots.txt' => "$IP/../robots.txt"
);

Files[edit | edit source]

ViewFiles.php[edit | edit source]

<?php
/**
 * ViewFiles MediaWiki extension.
 *
 * This extension allows users to view the contents of a limited set of files.
 *
 * Written by Leucosticte
 * http://www.mediawiki.org/wiki/User:Leucosticte
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Extensions
 */
 
# Alert the user that this is not a valid entry point to MediaWiki if the user tries to access the
# extension file directly.
if (!defined('MEDIAWIKI')) {
   die( 'This file is a MediaWiki extension. It is not a valid entry point' );
}
 
$wgExtensionCredits['specialpage'][] = array(
   'path' => __FILE__,
   'name' => 'ViewFiles',
   'author' => '[https://www.mediawiki.org/wiki/User:Leucosticte Leucosticte]',
   'url' => 'https://www.mediawiki.org/wiki/Extension:ViewFiles',
   'descriptionmsg' => 'viewfiles-desc',
   'version' => '1.0.2',
);
 
$dir = dirname(__FILE__) . '/';
 
$wgAutoloadClasses['SpecialViewFiles'] = $dir . 'SpecialViewFiles.php';
$wgExtensionMessagesFiles['ViewFiles'] = $dir . 'ViewFiles.i18n.php';
$wgExtensionMessagesFiles['ViewFilesAlias'] = $dir . 'ViewFiles.alias.php';
$wgSpecialPages['ViewFiles'] = 'SpecialViewFiles';
$wgSpecialPageGroups['ViewFiles'] = 'other';
 
// Display this before any of the files
$wgViewFilesIntro = '';
// Display this before each file
$wgViewFilesBegin = '<source lang="$1">' . "\n";
// Display this after each file
$wgViewFilesEnd = '</source>' . "\n";
// This list of filenames and associated languages overrides GeSHi's extension lookup
$wgViewFilesFileLangList = array (
   '.htaccess' => 'apache',
   'robots.txt' => 'robots',
);
// Change this to an array with 'filenames' as keys and 'paths/to/filenames' as values
$wgViewFilesFilePathList = array();

SpecialViewFiles.php[edit | edit source]

<?php
if ( !defined( 'MEDIAWIKI' ) ) {
   die( 'This file is a MediaWiki extension. It is not a valid entry point' );
}
 
class SpecialViewFiles extends SpecialPage {
   function __construct() {
       parent::__construct( 'ViewFiles' );
   }
 
   function execute( $par ) {
       global $wgViewFilesIntro, $wgViewFilesBegin, $wgViewFilesEnd,
           $wgViewFilesFileLangList, $wgViewFilesFilePathList;
 
       $this->setHeaders();
       $viewOutput->setRobotPolicy ( 'index,follow' );
       $viewOutput = $this->getOutput();
 
       // Bail if SyntaxHighlight isn't installed
       if ( !class_exists ( 'SyntaxHighlight_GeSHi' )
           || !class_exists ( 'GeSHi' ) ) {
           $viewOutput->addWikiMsg ( 'viewfiles-no-geshi' );
           $viewOutput->addWikiText ( "\n" . '<pre>' . "\n"
               . 'require_once("$IP/extensions/SyntaxHighlight_GeSHi/'
                   . 'SyntaxHighlight_GeSHi.php")' . "\n"
               . 'require_once("$IP/extensions/SyntaxHighlight_GeSHi/'
                   . 'geshi/geshi.php")' . "\n" . '</pre>' );
           return;
       }
 
       // Bail if no files have been made available
       if ( !$wgViewFilesFilePathList ) {
           $viewOutput->addWikiMsg ( 'viewfiles-no-files-available' );
           return;
       }
 
       // Display introductory material
       $output = $wgViewFilesIntro;
       // Iterate through the list of files
       foreach ( $wgViewFilesFilePathList as $filename => $pathFilename ) {
           // Display the filename
           $output .= "== $filename ==\n";
           // Set whatever file display format is appropriate given the filename
           if ( isset ( $wgViewFilesFileLangList[ $filename ] ) ) {
               $lang = $wgViewFilesFileLangList[ $filename ];
           } else {
               // If this particular filename has no selected format, then
               // go with what Geshi suggests, given the extension
               $geshi = new GeSHi;
               $lang = $geshi->get_language_name_from_extension(
                   pathinfo ( $filename, PATHINFO_EXTENSION ) );
           }
           $langOutput = str_replace ( '$1', $lang, $wgViewFilesBegin );
           // Read and display the file, if it exists
           if ( !file_exists ( $pathFilename ) ) {
               $output .= $this->msg( 'filenotfound',
                   $filename ) . "\n";
           } else {
               $output .= $langOutput;
               $handle = fopen( $pathFilename, "r" );
               $contents = file_get_contents ( $pathFilename );
               if ( $contents ) {
                   $output .= $contents;
               }
               $output .= $wgViewFilesEnd;
           }
       }
       $viewOutput->addWikiText( $output );
   }
}

ViewFiles.i18n.php[edit | edit source]

<?php
/**
 * Internationalisation for ViewFiles
 *
 * @file
 * @ingroup Extensions
 */
$messages = array();
 
/** English
 * @author Leucosticte
 */
$messages['en'] = array(
   'viewfiles' => 'View files',
   'viewfiles-desc' => 'Adds a [[Special:ViewFiles|special page]] to view the current contents of a limited set of files',
   'viewfiles-no-files-available' => 'No files have been made available for viewing.',
   'viewfiles-no-geshi' => 'Error: You need to download and install the [https://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi GeSHi SyntaxHighlight extension].
Be sure to add both these lines to your LocalSettings.php file:',
);
 
/** Message documentation
 * @author Leucosticte
 */
$messages['qqq'] = array(
   'viewfiles-desc' => '{{desc}}',
   'viewfiles-no-files-available' => 'This is the message the user gets if no files have been made available by the system administrator for viewing.',
   'viewfiles-no-geshi' => 'This is the message the user gets if the SyntaxHighlight GeSHi extension, upon which the ViewFiles extension depends, has not been installed properly.
Following this message, the two require_once lines that the system administrator needs to add to LocalSettings.php are displayed.',
);

ViewFiles.alias.php[edit | edit source]

<?php
/**
 * Aliases for ViewFiles
 *
 * @file
 * @ingroup Extensions
 */
 
$specialPageAliases = array();
 
/** English
 * @author Leucosticte
 */
$specialPageAliases['en'] = array(
   'ViewFiles' => array( 'ViewFiles', 'View files' ),
);