Manual:Wiki family/Drupal-style LocalSettings.php

<?php 
/**
 * Taken from Drupal code to tap multisite configuration.
 */

$confdir = '/home/web/mediawiki/sites';

/**
 * Find the appropriate configuration directory.
 *
 * Try finding a matching configuration directory by stripping the website's
 * hostname from left to right and pathname from right to left. The first
 * configuration file found will be used, the remaining will ignored. If no
 * configuration file is found, return a default value '$confdir/default'.
 *
 * Example for a fictitious site installed at
 * http://www.drupal.org:8080/mysite/test/ the 'settings.php' is searched in
 * the following directories:
 *
 *  1. $confdir/8080.www.drupal.org.mysite.test
 *  2. $confdir/www.drupal.org.mysite.test
 *  3. $confdir/drupal.org.mysite.test
 *  4. $confdir/org.mysite.test
 *  5. $confdir/mysite.test
 *
 *  6. $confdir/8080.www.drupal.org.mysite
 *  7. $confdir/www.drupal.org.mysite
 *  8. $confdir/drupal.org.mysite
 *  9. $confdir/org.mysite
 * 10. $confdir/mysite
 *
 * 11. $confdir/8080.www.drupal.org
 * 12. $confdir/www.drupal.org
 * 13. $confdir/drupal.org
 * 14. $confdir/org
 *
 * 15. $confdir/default
 */
  static $conf = '';

  $uri = explode('/', $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_FILENAME']);
  $server = explode('.', implode('.', array_reverse(explode(':', rtrim($_SERVER['HTTP_HOST'], '.')))));

  // Ensure that only one file is loaded - otherwise fr.drupal.org matches fr.drupal.org and drupal.org
  $done = false;
  $i = count($uri);
  while (!$done && $i > 0) {
    $j = count($server);
    while (!$done && $j >= 0) {
      if ($j > 0) {
        $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
      } else {
        // If we have run out of URL segments, look for just directory
        $dir = implode('.', array_slice($uri, 1, $i));
      }
      if (file_exists("$confdir/$dir/LocalSettings.php")) {
        $done = true;
        $conf = "$confdir/$dir";
        require_once( $conf . '/LocalSettings.php' );
      }
      $j--;
    }
    $i--;
  }

  if (file_exists("$confdir/default/LocalSettings.php")) {
    $conf = "$confdir/default";
    require_once( $conf . '/LocalSettings.php' );
  }

if (!$conf) {
    $IP = '.';
    require_once( './includes/DefaultSettings.php' ); # used for printing the version
    require_once( './includes/NoLocalSettings.php' );
    die();
}