Difference between revisions of "Extension:WikiaInfo.php"
(sortkey) |
(1.0.7 - add DB/table info) |
||
Line 1: | Line 1: | ||
<?php | <?php | ||
− | # Extension:WikiaInfo{{Category:Extensions | + | # 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. | + | define('WIKIAINFO_VERSION', '1.0.7, 2008-05-27'); |
+ | |||
+ | $egWikiaInfoDataDir = '/var/lib/mysql'; | ||
$wgExtensionFunctions[] = 'wfSetupWikiaInfo'; | $wgExtensionFunctions[] = 'wfSetupWikiaInfo'; | ||
$wgExtensionCredits['specialpage'][] = array( | $wgExtensionCredits['specialpage'][] = array( | ||
− | 'name' | + | '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' | + | '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 | ||
+ | */ | ||
class SpecialWikiaInfo extends SpecialPage { | class SpecialWikiaInfo extends SpecialPage { | ||
− | + | /** | |
+ | * 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() | ||
+ | */ | ||
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) | ||
− | $ | + | $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 = stat("$settings/$file/wiki"); |
− | $ver | + | $ver = $codebases[$stat[1]]; |
− | $ | + | $tree .= "*'''$file''' ($ver)\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"; |
− | $ | + | $tree .= "**[http://$domain $domain]\n"; |
− | $ | + | $tree .= "***[$wiki=Special:Recentchanges Recent changes]\n"; |
− | $ | + | $tree .= "***[$wiki=Special:Version Version]\n"; |
− | $ | + | $tree .= "***[$wiki=Special:Statistics Statistics]\n"; |
− | |||
} | } | ||
− | $ | + | } |
− | $wgOut->addWikiText($ | + | $tree .= "}"."}\n"; |
+ | $wgOut->addWikiText($tree); | ||
− | |||
− | |||
} | } | ||
− | + | ||
− | + | } | |
+ | |||
+ | /* | ||
+ | * 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
- Extension:WikiaInfo
Template:PhpCategory:Extensions created with Template:SpecialPage
- - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
- - Author: User:Nad
- - 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()); }