Extension:ExtensionInstaller

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
Extension Installer

Release status: beta

Implementation User interface
Description An extension that allows bundled extensions to be included with a MediaWiki distribution and installed via the installer.
Author(s) Christian Neubauer (cneubauer Talk)
Version 0.4 (2008-01-18)
MediaWiki 1.12 r29916
Download Copy this code

Contents

[edit] What can this extension do?

This is an extension to the MediaWiki installer (/config/index.php). The extension allows other extensions to be included with a MediaWiki distribution and gives the user installing MediaWiki the option to enable or disabled bundled extensions and configure some basic extension global variables. Once the wiki is installed, this extension doesn't do anything.

[edit] Usage

[edit] MediaWiki Distributer

  1. Download and install this extension.
  2. Create a configuration section for the extensions you would like to distribute in /extensions/ExtensionInstaller/ExtensionSettings.php.
  3. Add the extensions to the extensions folder.
  4. Zip the wiki up.
  5. Distribute.

[edit] MediaWiki Installer

  1. Unzip wiki as normal.
  2. Browse to wiki and click "setup this wiki" as normal.
  3. On the installer screen, look for the section on extensions. Enable and configure extensions as desired.
  4. Click install as normal.

[edit] Download

Please cut and paste the code found below and place it in $IP/extension/ExtensionInstaller/ExtensionInstaller.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Installation

To install this extension, patch config/index.php with the patch found below. You will also need to create a settings file at $IP/extension/ExtensionInstaller/ExtensionSettings.php. A sample settings file can be found below as well.

[edit] Code

<?php
/**
 * Extension Installer
 *
 * This is a MediaWiki extension to allow someone to bundle additional extensions
 * with MediaWiki which can then be installed during the wiki setup process.  To
 * use the extension, add it along with any other extensions that should be
 * available for installation into the extensions directory and edit
 * extensions/ExtensionInstaller/ExtensionSettings.php with an entry for each
 * bundled extension.  Syntax for extension configuration:
 *
 * $wgExtensionConfig[] = array(
 *     'name' => 'NameOfExtension',
 *     'label' => 'Name of Extension',
 *     'description' => 'A description of the extension.',
 *     'file' => 'path/to/NameOfExtension.php', # relative to the extensions directory
 *     'config' => array(
 *         '$egVar1' => array( '"value"',        'Description of var1'),
 *         '$egVar2' => array( '$variableValue', 'Description of var2'),
 *         '$egVarWithNoDescription' => 5
 *     ),
 *     'download' => 'http://www.google.com'
 * );
 *
 * Sample configuration for ParserFunctions.php:
 *
 * $wgExtensionConfig[] = array(
 *     'name' => 'ParserFunctions',
 *     'label' => 'Parser Functions',
 *     'description' => 'You may choose to install the <a href="http://meta.wikimedia.org/wiki/Help:ParserFunctions">ParserFunctions</a> extension by enabling this option. The ParserFunctions extension is installed on all Wikimedia Foundation wikis (such as the Wikipedia), and installing them allows more flexible control over page output.',
 *     'file' => 'ParserFunctions/ParserFunctions.php',
 *     'config' => '',
 *     'download' => 'http://meta.wikimedia.org/wiki/Help:ParserFunctions'
 * );
 *
 * @package MediaWiki
 * @subpackage Extensions
 *
 * @author Christian Neubauer
 * @link http://www.mediawiki.org/wiki/Extension:ExtensionInstaller
 * @copyright Copyright © 2008, Christian Neubauer
 * @license http://creativecommons.org/licenses/by/3.0/ Creative Commons By 3.0
 *
 * @changes v0.1
 * - Adapted code to bundle ParserFunctions by Ryan Schmidt into a generic extension installer (http://bugzilla.wikimedia.org/attachment.cgi?id=4472)
 * @changes v0.2
 * - Made into a seperate extension instead of just a modification to the installer: config/index.php.
 * - Renamed and added comments
 * @changes v0.3
 * - Changed layout of global configuration to make extension variables editable
 * @changes v0.4
 * - Added eval check for new LocalSettings before writing
 * - Changed function parameters to include error variable.  Most functions that
 *      do something now return true/false on success/failure and fill an error
 *      variable with an error/success message.
 * - Added explicit public modifiers to public functions
 */
$wgExtensionCredits['other'][] = array(
        'name' => 'Extension Installer',
        'author' => 'Christian Neubauer',
        'url' => 'http://www.mediawiki.org/wiki/Extension:Extension_Installer',
        'version' => '0.4',
        'description' => 'An extension that can be bundled with MediaWiki to allow other bundled extensions to be installed during wiki setup'
);
 
if(@file_exists("$IP/extensions/ExtensionInstaller/ExtensionSettings.php")) {
    require_once( "$IP/extensions/ExtensionInstaller/ExtensionSettings.php" );
}
 
class ExtensionInstaller {
    var $conf;
    var $extensions;
 
    const ENABLED = 0;
    const DISABLED = 1;
    const NOTFOUND = 2;
 
    public static function styles() {
        return <<<EOT
              fieldset.extension .config-section label.column {
                        clear: left;
                        font-weight: bold;
                        width: 18em;
                        float: left;
                        text-align: right;
                        padding-right: 1em;
                        padding-top: .2em;
                }
                fieldset.extension .iput-text {
                        width: 24em;
                        margin-right: 1em;
                }
EOT;
    }
 
    function __construct($conf = null) {
        global $wgExtensionConfig;
        if($conf) {
            $this->conf = $conf;
        } else {
            $this->conf = array();
        }
        $this->extensions = array();
        if(!$wgExtensionConfig) {
            $wgExtensionConfig = array();
        }
    }
 
    public function loadConfiguration() {
        global $wgExtensionConfig;
        foreach($wgExtensionConfig as $extension) {
            $this->conf->$extension['name']  = importRequest($extension['name'], $extension['name'].'_disabled');
            if( isset($extension['config']) && is_array($extension['config']) ) {
                foreach( $extension['config'] as $name => $config ) {
                    if( is_array($config) ) {
                        list($value, ) = $config;
                    } else {
                        $value = $config;
                    }
                    $name = str_replace('$', ':', $extension['name'] . $name);
                    if( $value === false ) { $value = 'false'; }
                    if( $value === true )  { $value = 'true'; }
                    $this->conf->$name  = importRequest($name, $value);
                }
            }
        }
    }
 
    public function outputForm() {
        global $wgExtensionConfig;
        if( count($wgExtensionConfig) > 0 ) {
            ?><h2>Extensions</h2><?php
 
            foreach($wgExtensionConfig as $extension) {
                ?><fieldset class="extension"><legend><?php echo $extension['name'] ?> specific options</legend>
                <div class="config-section">
                        <div class="config-input">
                                <label class='column'><?php echo $extension['label'] ?>:</label>
                        <ul class="plain">
                                <li><?php aField( $this->conf, $extension['name'], "Disabled", "radio", $extension['name']."_disabled" ); ?></li>
                                <li><?php aField( $this->conf, $extension['name'], "Enabled", "radio", $extension['name']."_enabled" ); ?></li>
                        </ul>
                        </div><?php
                    if( isset($extension['description']) && $extension['description'] != '' ) {
                        ?><p class="config-desc"><?php echo $extension['description'] ?></p><?php
                    }
                    if( isset($extension['config']) && is_array($extension['config']) ) {
                        foreach( $extension['config'] as $name => $config ) {
                            if( is_array($config) ) {
                                list(, $desc) = $config;
                            } else {
                                $desc = '';
                            }
                            $id = str_replace('$', ':', $extension['name'].$name);
                            ?><div class="config-input">
                                        <?php aField( $this->conf, $id, $name.':', "text" ); ?>
                                </div><?php
                            if( $desc != '' ) {
                                ?><p class="config-desc"><?php echo $desc ?></p><?php
                            }
                        }
                    }
                ?></div></fieldset><?php
            }
        }
    }
 
    public function processExtensions() {
        global $wgExtensionConfig, $IP;
        for($i = 0; $i < count($wgExtensionConfig); $i++) {
            $extension = $wgExtensionConfig[$i];
                if ( $this->conf->$extension['name'] == $extension['name'].'_enabled' ) {
                if( @file_exists($IP.'/extensions/'.$extension['file']) ) {
                    print "<li>Installing " . $IP.'/extensions/'.$extension['file']."</li>\r\n";
                    $wgExtensionConfig[$i]['enable'] = ExtensionInstaller::ENABLED;
                } else {
                    print "<li><strong>Warning:</strong> Skipping install for <tt>" . $IP.'/extensions/'.$extension['file']."</tt>. Extension not found.</li>\r\n";
                    $wgExtensionConfig[$i]['enable'] = ExtensionInstaller::NOTFOUND;
                }
                if( isset($extension['config']) && is_array($extension['config']) ) {
                    foreach( $extension['config'] as $name => $config ) {
                        $id = str_replace('$', ':', $extension['name'].$name);
                        if( is_array($config) ) {
                            $wgExtensionConfig[$i]['config'][$name][0] = $this->conf->$id;
                        } else {
                            $wgExtensionConfig[$i]['config'][$name] = $this->conf->$id;
                        }
                    }
                }
                } else {
                print "<li>Not installing <tt>" . $IP.'/extensions/'.$extension['file']."</tt></li>\n";
                $wgExtensionConfig[$i]['enable'] = ExtensionInstaller::DISABLED;
                }
        }
    }
 
    public function outputExtensionConfiguration() {
        global $wgExtensionConfig;
        $localsettings = "\r\n############# Extensions: #############\r\n\r\n";
        foreach($wgExtensionConfig as $extension) {
            $localsettings .= $this->_processConfig($extension);
        }
        return $localsettings;
    }
 
    private function _processConfig($extension) {
        $file = 'require_once("$IP/extensions/'.$extension['file'].'");';
        if( $extension['enable'] == ExtensionInstaller::DISABLED ) { # if the extension is disabled, do nothing
                return '';
        }
        $string = '';
        if( isset($extension['config']) && is_array($extension['config']) ) {
            foreach( $extension['config'] as $name => $config ) {
                if( is_array($config) ) {
                    $string .= $name.' = '.$config[0].'; # '.$config[1]."\r\n";
                } else {
                    $string .= $name.' = '.$config.';'."\r\n";
                }
            }
        }
        if( $extension['enable'] == ExtensionInstaller::NOTFOUND ) { # if the extension file is not found, write the config info commented out.
                $prepend = '# Please download this extension from '.$extension['download']." and then uncomment the following lines to enable it.\r\n/*\r\n";
            $config = $string == '' ? '*/' : $string.'*/'."\r\n";
        } else {
            $prepend = '';
            $config = $string == '' ? "\r\n" : $string."\r\n";
        }
        return $prepend.$file."\r\n".$config;
    }
}

[edit] Sample Settings File

<?php
$wgExtensionConfig = array();
 
$wgExtensionConfig[] = array(
    'name' => 'ParserFunctions',
    'label' => 'Parser Functions',
    'description' => 'You may choose to install the <a href="http://meta.wikimedia.org/wiki/Help:ParserFunctions">ParserFunctions</a> extension by enabling this option. The ParserFunctions extension is installed on all Wikimedia Foundation wikis (such as the Wikipedia), and installing them allows more flexible control over page output.',
    'file' => 'ParserFunctions/ParserFunctions.php',
    'config' => '',
    'download' => 'http://meta.wikimedia.org/wiki/Help:ParserFunctions'
);
 
$wgExtensionConfig[] = array(
    'name' => 'SpamBlacklist',
    'label' => 'Spam Blacklist',
    'description' => 'Click here to install the <a href="http://www.mediawiki.org/wiki/Extension:SpamBlacklist">SpamBlacklist</a>extension. It downloads and installs a blacklist of websites that users of your
wiki will be forbidden to link to.',
    'file' => 'SpamBlacklist/SpamBlacklist.php',
    'config' => '',
    'download' => 'http://www.mediawiki.org/wiki/Extension:SpamBlacklist'
);
 
$wgExtensionConfig[] = array(
    'name' => 'LdapAuthentication',
    'label' => 'LDAP Authentication',
    'description' => 'This extension allows users to log in with credentials stored in an LDAP repository.',
    'file' => 'LdapAuthentication/LdapAuthentication.php',
    'config' => array(
        '$wgLDAPDomainNames' => "array( 'exampleNonADDomain' )",
        '$wgLDAPServerNames' => "array( 'exampleNonADDomain' => 'exampleldapserver.example.com exampleldapserver2.example.com', )",
        '$wgLDAPSearchStrings' => "array( 'exampleNonADDomain' => 'uid=USER-NAME,ou=people,dc=exampledomain,dc=example,dc=com', )",
        '$wgLDAPEncryptionType' => "array( 'exampleNonADDomain' => 'ssl', )",
        '$wgMinimalPasswordLength' => 1
     ),
    'download' => 'http://www.mediawiki.org/wiki/Extension:LDAP_Authentication'
);

[edit] Patch

Index: config/index.php
===================================================================
--- config/index.php  (revision 29916)
+++ config/index.php  (working copy)
@@ -47,6 +47,8 @@
 require_once( "$IP/includes/ProfilerStub.php" );
 require_once( "$IP/includes/GlobalFunctions.php" );
 require_once( "$IP/includes/Hooks.php" );
+# Add extension for installing extensions
+require_once( "$IP/extensions/ExtensionInstaller/ExtensionInstaller.php" );
 
 # If we get an exception, the user needs to know
 # all the details
@@ -158,7 +160,10 @@
                .success-box {
                        font-size: 130%;
                }
-
+        <?php 
+        # Add extension specific styles
+        echo ExtensionInstaller::styles();
+        ?>
        </style>
        <script type="text/javascript">
        <!--
@@ -702,6 +705,10 @@
 $conf->Enotif    = importRequest("Enotif", "enotif_allpages");
 $conf->Eauthent  = importRequest("Eauthent", "eauthent_enabled");
 
+/* load defaults for extensions */
+$extensionInstaller = new ExtensionInstaller($conf);
+$extensionInstaller->loadConfiguration();
+
 if( $conf->posted && ( 0 == count( $errs ) ) ) {
        do { /* So we can 'continue' to end prematurely */
                $conf->Root = ($conf->RootPW != "");
@@ -1307,6 +1308,8 @@
 
 </div>
 
+<?php $extensionInstaller->outputForm(); ?>
+
 <h2>Database config</h2>
 
 <div class="config-section">
@@ -1541,6 +1540,9 @@
                $enotifusertalk = 'false';
                $enotifwatchlist = 'false';
        }
+    
+    global $extensionInstaller;
+    $extensionInstaller->processExtensions();
 
        $file = @fopen( "/dev/urandom", "r" );
        if ( $file ) {
@@ -1685,7 +1687,10 @@
 # When you make changes to this configuration file, this will make
 # sure that cached pages are cleared.
 \$wgCacheEpoch = max( \$wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
-"; ## End of setting the $localsettings string
+     "; ## End of setting the $localsettings string
+
+    global $extensionInstaller;
+    $localsettings .= $extensionInstaller->outputExtensionConfiguration();
 
        // Keep things in Unix line endings internally;
        // the system will write out as local text type.
Personal tools