Difference between revisions of "Extension:WikiaInfo.php"

From Organic Design wiki
(sortkey)
(1.0.7 - add DB/table info)
Line 1: Line 1:
 
<?php
 
<?php
# Extension:WikiaInfo{{Category:Extensions|WIkiaInfo}}{{php}}{{Category:Extensions created with Template:SpecialPage}}{{Category:OD2}}
+
# Extension:WikiaInfo{{Category:Extensions}}{{php}}{{Category:Extensions created with Template:SpecialPage}}{{Category:OD2}}
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Author: [http://www.organicdesign.co.nz/nad User:Nad]
 
# - Author: [http://www.organicdesign.co.nz/nad User:Nad]
Line 7: Line 7:
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
  
define('WIKIAINFO_VERSION','1.0.6, 2008-03-11');
+
define('WIKIAINFO_VERSION', '1.0.7, 2008-05-27');
 +
 
 +
$egWikiaInfoDataDir = '/var/lib/mysql';
  
 
$wgExtensionFunctions[] = 'wfSetupWikiaInfo';
 
$wgExtensionFunctions[] = 'wfSetupWikiaInfo';
  
 
$wgExtensionCredits['specialpage'][] = array(
 
$wgExtensionCredits['specialpage'][] = array(
'name'       => 'Special:WikiaInfo',
+
'name' => 'Special:WikiaInfo',
 
'author'      => '[http://www.organicdesign.co.nz/nad User:Nad]',
 
'author'      => '[http://www.organicdesign.co.nz/nad User:Nad]',
 
'description' => 'List the currently running wikia and the domains pointing to them',
 
'description' => 'List the currently running wikia and the domains pointing to them',
'url'         => 'http://www.organicdesign.co.nz/Extension:WikiaInfo.php',
+
'url' => 'http://www.organicdesign.co.nz/Extension:WikiaInfo.php',
 
'version'    => WIKIAINFO_VERSION
 
'version'    => WIKIAINFO_VERSION
 
);
 
);
Line 21: Line 23:
 
require_once "$IP/includes/SpecialPage.php";
 
require_once "$IP/includes/SpecialPage.php";
  
# Define a new class based on the SpecialPage class
+
/**
 +
* Define a new class based on the SpecialPage class
 +
*/
 
class SpecialWikiaInfo extends SpecialPage {
 
class SpecialWikiaInfo extends SpecialPage {
  
# Constructor
+
/**
 +
* Constructor
 +
*/
 
function SpecialWikiaInfo() {
 
function SpecialWikiaInfo() {
SpecialPage::SpecialPage('WikiaInfo','sysop',true,false,false,false);
+
SpecialPage::SpecialPage('WikiaInfo', 'sysop', true, false, false, false);
}
+
}
 +
 
 +
/**
 +
* Return passed size as Bytes, KB, MB, GB
 +
*/
 +
function friendlySize($size) {
 +
$suffix = 'Bytes';
 +
if ($size >> 30) { $size >>= 30; $suffix = 'GB'; }
 +
elseif ($size >> 20) { $size >>= 20; $suffix = 'MB'; }
 +
elseif ($size >> 10) { $size >>= 10; $suffix = 'KB'; }
 +
return "$size $suffix";
 +
}
  
# Override SpecialPage::execute()
+
/**
 +
* Override SpecialPage::execute()
 +
*/
 
function execute() {
 
function execute() {
global $wgOut,$domains,$settings;
+
global $wgOut, $domains, $settings, $egWikiaInfoDataDir;
 
$this->setHeaders();
 
$this->setHeaders();
 
$wgOut->addWikiText("\n\n");
 
$wgOut->addWikiText("\n\n");
 +
 +
# Render tree of databases
 +
if ($egWikiaInfoDataDir) {
 +
$dbcount  = 0;
 +
$alltotal  = 0;
 +
$alltable  = 0;
 +
$tree      = "*'''Databases'''\n";
 +
$databases = array();
 +
foreach (glob("$egWikiaInfoDataDir/*") as $db) if (is_dir($db)) {
 +
$dbcount++;
 +
$tables = array();
 +
$total  = 0;
 +
foreach (glob("$db/*.???") as $file) {
 +
$stat  = stat($file);
 +
$size  = $stat[7];
 +
$total += $size;
 +
$table  = preg_replace("|^(.*/)?(.+?)\\....$|", "$2", $file);
 +
$tables[$table] = isset($tables[$table]) ? $tables[$table]+$size : $size;
 +
}
 +
$alltotal += $total;
 +
$ntable = count($tables);
 +
$alltable += $ntable;
 +
$total = $this->friendlySize($total);
 +
$tree .= "**".basename($db)." ($ntable tables, $total)\n";
 +
#foreach ($tables as $table => $size) $tree .= "***$table (".$this->friendlySize($size).")\n";
 +
}
 +
$alltotal = $this->friendlySize($alltotal);
 +
$tree  = "{"."{#tree:root=<big><b>$dbcount Databases ($alltable tables, $alltotal)</b></big>|openlevels=1|id=databasetree|\n$tree";
 +
$tree .= "}"."}\n";
 +
$wgOut->addWikiText($tree);
 +
}
 +
  
 
$codebases = array();
 
$codebases = array();
Line 39: Line 90:
 
$stat = stat($codebase);
 
$stat = stat($codebase);
 
$codebases[$stat[1]] = basename($codebase);
 
$codebases[$stat[1]] = basename($codebase);
}
+
}
  
 
# Get list of domain symlinks by inode
 
# Get list of domain symlinks by inode
Line 48: Line 99:
 
if (is_array($dlist[$inode])) $dlist[$inode][] = basename($link);
 
if (is_array($dlist[$inode])) $dlist[$inode][] = basename($link);
 
else $dlist[$inode] = array(basename($link));
 
else $dlist[$inode] = array(basename($link));
}
+
}
  
 
# Loop through settings files associating with domains
 
# Loop through settings files associating with domains
Line 55: Line 106:
 
$stat = stat($file);
 
$stat = stat($file);
 
$wikia[basename($file)] = $dlist[$stat[1]];
 
$wikia[basename($file)] = $dlist[$stat[1]];
}
+
}
 
ksort($wikia);
 
ksort($wikia);
  
 
# Render the list (don't show naked domains if a www also exists)
 
# Render the list (don't show naked domains if a www also exists)
$table = "{"."{#tree:root=<big><b>Currently installed wikia</b></big>|id=wikiatree|openlevels=1|\n";
+
$tree = "{"."{#tree:root=<big><b>Currently Installed Wikia</b></big>|openlevels=1|id=wikiatree|\n";
 
foreach ($wikia as $file => $dlist) if (is_array($dlist)) {
 
foreach ($wikia as $file => $dlist) if (is_array($dlist)) {
$stat   = stat("$settings/$file/wiki");
+
$stat = stat("$settings/$file/wiki");
$ver   = $codebases[$stat[1]];
+
$ver   = $codebases[$stat[1]];
$table .= "*'''$file''' ($ver)\n";
+
$tree .= "*'''$file''' ($ver)\n";
$table .= "**[[Config:$file/LocalSettings.php|LocalSettings.php]]\n";
+
$tree .= "**[[Config:$file/LocalSettings.php|LocalSettings.php]]\n";
 
sort($dlist);
 
sort($dlist);
 
foreach ($dlist as $domain) {
 
foreach ($dlist as $domain) {
$wiki = "http://$domain/wiki/index.php?title";
+
$wiki = "http://$domain/wiki/index.php?title";
$table .= "**[http://$domain $domain]\n";
+
$tree .= "**[http://$domain $domain]\n";
$table .= "***[$wiki=Special:Recentchanges Recent changes]\n";
+
$tree .= "***[$wiki=Special:Recentchanges Recent changes]\n";
$table .= "***[$wiki=Special:Version Version]\n";
+
$tree .= "***[$wiki=Special:Version Version]\n";
$table .= "***[$wiki=Special:Statistics Statistics]\n";
+
$tree .= "***[$wiki=Special:Statistics Statistics]\n";
}
 
 
}
 
}
$table .= "}"."}\n";
+
}
$wgOut->addWikiText($table);
+
$tree .= "}"."}\n";
 +
$wgOut->addWikiText($tree);
  
}
 
 
 
}
 
}
   
+
 
# Called from $wgExtensionFunctions array when initialising extensions
+
}
 +
 
 +
/*
 +
  * Called from $wgExtensionFunctions array when initialising extensions
 +
*/
 
function wfSetupWikiaInfo() {
 
function wfSetupWikiaInfo() {
 
global $wgLanguageCode,$wgMessageCache;
 
global $wgLanguageCode,$wgMessageCache;
 
$wgMessageCache->addMessages(array('wikiainfo' => 'Current Wikia Information'));
 
$wgMessageCache->addMessages(array('wikiainfo' => 'Current Wikia Information'));
 
SpecialPage::addPage(new SpecialWikiaInfo());
 
SpecialPage::addPage(new SpecialWikiaInfo());
}
+
}
?>
 

Revision as of 09:37, 27 May 2008

<?php

  1. Extension:WikiaInfo
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:PhpCategory:Extensions created with Template:SpecialPage

  1. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  2. - Author: User:Nad
  3. - Started: 2007-08-27

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('WIKIAINFO_VERSION', '1.0.7, 2008-05-27');

$egWikiaInfoDataDir = '/var/lib/mysql';

$wgExtensionFunctions[] = 'wfSetupWikiaInfo';

$wgExtensionCredits['specialpage'][] = array( 'name' => 'Special:WikiaInfo', 'author' => 'User:Nad', 'description' => 'List the currently running wikia and the domains pointing to them', 'url' => 'http://www.organicdesign.co.nz/Extension:WikiaInfo.php', 'version' => WIKIAINFO_VERSION );

require_once "$IP/includes/SpecialPage.php";

/**

* Define a new class based on the SpecialPage class
*/

class SpecialWikiaInfo extends SpecialPage {

/** * Constructor */ function SpecialWikiaInfo() { SpecialPage::SpecialPage('WikiaInfo', 'sysop', true, false, false, false); }

/** * Return passed size as Bytes, KB, MB, GB */ function friendlySize($size) { $suffix = 'Bytes'; if ($size >> 30) { $size >>= 30; $suffix = 'GB'; } elseif ($size >> 20) { $size >>= 20; $suffix = 'MB'; } elseif ($size >> 10) { $size >>= 10; $suffix = 'KB'; } return "$size $suffix"; }

/** * Override SpecialPage::execute() */ function execute() { global $wgOut, $domains, $settings, $egWikiaInfoDataDir; $this->setHeaders(); $wgOut->addWikiText("\n\n");

# Render tree of databases if ($egWikiaInfoDataDir) { $dbcount = 0; $alltotal = 0; $alltable = 0; $tree = "*Databases\n"; $databases = array(); foreach (glob("$egWikiaInfoDataDir/*") as $db) if (is_dir($db)) { $dbcount++; $tables = array(); $total = 0; foreach (glob("$db/*.???") as $file) { $stat = stat($file); $size = $stat[7]; $total += $size; $table = preg_replace("|^(.*/)?(.+?)\\....$|", "$2", $file); $tables[$table] = isset($tables[$table]) ? $tables[$table]+$size : $size; } $alltotal += $total; $ntable = count($tables); $alltable += $ntable; $total = $this->friendlySize($total); $tree .= "**".basename($db)." ($ntable tables, $total)\n"; #foreach ($tables as $table => $size) $tree .= "***$table (".$this->friendlySize($size).")\n"; } $alltotal = $this->friendlySize($alltotal); $tree = "{"."{#tree:root=$dbcount Databases ($alltable tables, $alltotal)|openlevels=1|id=databasetree|\n$tree"; $tree .= "}"."}\n"; $wgOut->addWikiText($tree); }


$codebases = array(); foreach (glob('/var/www/mediawiki-*') as $codebase) { $stat = stat($codebase); $codebases[$stat[1]] = basename($codebase); }

# Get list of domain symlinks by inode $dlist = array(); foreach (glob("$domains/*") as $link) { $stat = stat($link); $inode = $stat[1]; if (is_array($dlist[$inode])) $dlist[$inode][] = basename($link); else $dlist[$inode] = array(basename($link)); }

# Loop through settings files associating with domains $wikia = array(); foreach (glob("$settings/*") as $file) { $stat = stat($file); $wikia[basename($file)] = $dlist[$stat[1]]; } ksort($wikia);

# Render the list (don't show naked domains if a www also exists) $tree = "{"."{#tree:root=Currently Installed Wikia|openlevels=1|id=wikiatree|\n"; foreach ($wikia as $file => $dlist) if (is_array($dlist)) { $stat = stat("$settings/$file/wiki"); $ver = $codebases[$stat[1]]; $tree .= "*$file ($ver)\n"; $tree .= "**LocalSettings.php\n"; sort($dlist); foreach ($dlist as $domain) { $wiki = "http://$domain/wiki/index.php?title"; $tree .= "**$domain\n"; $tree .= "***[$wiki=Special:Recentchanges Recent changes]\n"; $tree .= "***[$wiki=Special:Version Version]\n"; $tree .= "***[$wiki=Special:Statistics Statistics]\n"; } } $tree .= "}"."}\n"; $wgOut->addWikiText($tree);

}

}

/*

* Called from $wgExtensionFunctions array when initialising extensions
*/

function wfSetupWikiaInfo() { global $wgLanguageCode,$wgMessageCache; $wgMessageCache->addMessages(array('wikiainfo' => 'Current Wikia Information')); SpecialPage::addPage(new SpecialWikiaInfo()); }