2 rcs_id('$Id: SystemInfo.php,v 1.16 2004-05-06 20:30:47 rurban Exp $');
4 Copyright (C) 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Usage: <?plugin SystemInfo all ?>
25 * or <?plugin SystemInfo pagestats cachestats discspace hitstats ?>
26 * or <?plugin SystemInfo version ?>
27 * or <?plugin SystemInfo current_theme ?>
28 * or <?plugin SystemInfo PHPWIKI_DIR ?>
30 * Provide access to phpwiki's lower level system information.
32 * version, charset, pagestats, SERVER_NAME, database, discspace,
33 * cachestats, userstats, linkstats, accessstats, hitstats,
34 * revisionstats, interwikilinks, imageextensions, wikiwordregexp,
35 * availableplugins, downloadurl, ...
37 * In spirit to http://www.ecyrd.com/JSPWiki/SystemInfo.jsp
39 * Todo: Some calculations are heavy (~5-8 secs), so we should cache
40 * the result. In the page or with WikiPluginCached?
42 //require_once "lib/WikiPluginCached.php";
44 class WikiPlugin_SystemInfo
45 //extends WikiPluginCached
48 function getPluginType() {
49 return PLUGIN_CACHED_HTML;
52 return _("SystemInfo");
55 function getDescription() {
56 return _("Provides access to PhpWiki's lower level system information.");
59 function getVersion() {
60 return preg_replace("/[Revision: $]/", '',
61 "\$Revision: 1.16 $");
64 function getExpire($dbi, $argarray, $request) {
65 return '+1800'; // 30 minutes
67 function getHtml($dbi, $argarray, $request, $basepage) {
68 $loader = new WikiPluginLoader;
69 return $loader->expandPI('<?plugin SystemInfo '
70 . WikiPluginCached::glueArgs($argarray) // all
71 . ' ?>', $request, $this, $basepage);
74 function getDefaultArguments() {
76 'seperator' => ' ', // on multiple args
82 global $DBParams, $request;
83 $s = _("db type:") . " {$DBParams['dbtype']}, ";
84 switch ($DBParams['dbtype']) {
87 $dsn = $DBParams['dsn'];
88 $s .= _("db backend:") . " ";
89 $s .= ($DBParams['dbtype'] == 'SQL') ? 'PearDB' : 'ADODB';
90 if (preg_match('/^(\w+):/', $dsn, $m)) {
96 $s .= _("dba handler:") . " {$DBParams['dba_handler']}, ";
99 // $s .= "cvs stuff: , ";
102 // $s .= "flatfile stuff: , ";
105 // hack: suppress error when using sql, so no timeout
106 @$s .= _("timeout:") . " {$DBParams['timeout']}";
109 function cachestats() {
110 global $DBParams, $request;
111 if (! defined('USECACHE') or !USECACHE)
112 return _("no cache used");
113 $dbi = $request->getDbh();
114 $cache = $dbi->_cache;
115 $s = _("cached pagedata:") . " " . count($cache->_pagedata_cache);
116 $s .= ", " . _("cached versiondata:");
117 $s .= " " . count($cache->_versiondata_cache);
118 //$s .= ", glv size: " . count($cache->_glv_cache);
119 //$s .= ", cache hits: ?";
120 //$s .= ", cache misses: ?";
123 function ExpireParams() {
124 global $ExpireParams;
125 $s = sprintf(_("Keep up to %d major edits, but keep them no longer than %d days."),
126 $ExpireParams['major']['keep'],
127 $ExpireParams['major']['max_age']);
128 $s .= sprintf(_(" Keep up to %d minor edits, but keep them no longer than %d days."),
129 $ExpireParams['minor']['keep'],
130 $ExpireParams['minor']['max_age']);
131 $s .= sprintf(_(" Keep the latest contributions of the last %d authors up to %d days."),
132 $ExpireParams['author']['keep'], $ExpireParams['author']['max_age']);
133 $s .= sprintf(_(" Additionally, try to keep the latest contributions of all authors in the last %d days (even if there are more than %d of them,) but in no case keep more than %d unique author revisions."),
134 $ExpireParams['author']['min_age'],
135 $ExpireParams['author']['keep'],
136 $ExpireParams['author']['max_keep']);
139 function pagestats() {
143 $dbi = $request->getDbh();
144 $include_empty = true;
145 $iter = $dbi->getAllPages($include_empty);
146 while ($page = $iter->next())
148 $s = sprintf(_("%d pages"), $e);
149 $include_empty = false;
150 $iter = $dbi->getAllPages($include_empty);
151 while ($page = $iter->next())
153 $s .= ", " . sprintf(_("%d not-empty pages"), $a);
155 // $s .= ", " . sprintf(_("earliest page from %s"), $earliestdate);
156 // $s .= ", " . sprintf(_("latest page from %s"), $latestdate);
157 // $s .= ", " . sprintf(_("latest pagerevision from %s"), $latestrevdate);
160 //What kind of link statistics?
161 // total links in, total links out, mean links per page, ...
162 // Any useful numbers similar to a VisualWiki interestmap?
163 function linkstats() {
167 // number of homepages: easy
168 // number of anonymous users?
169 // calc this from accesslog info?
170 // number of anonymous edits?
171 // easy. related to the view/edit rate in accessstats.
172 function userstats() {
174 $dbi = $request->getDbh();
176 $page_iter = $dbi->getAllPages(true);
177 while ($page = $page_iter->next()) {
178 if ($page->isUserPage(true)) // check if the admin is there. if not add him to the authusers.
181 $s = sprintf(_("%d homepages"), $h);
182 // $s .= ", " . sprintf(_("%d anonymous users"), $au); // ??
183 // $s .= ", " . sprintf(_("%d anonymous edits"), $ae); // see recentchanges
184 // $s .= ", " . sprintf(_("%d authenticated users"), $auth); // users with password set
185 // $s .= ", " . sprintf(_("%d externally authenticated users"), $extauth); // query AuthDB?
188 //only from logging info possible. = hitstats per time.
189 // total hits per day/month/year
191 function accessstats() {
195 // only absolute numbers, not for any time interval. see accessstats
196 // some useful number derived from the curve of the hit stats.
197 // total, max, mean, median, stddev;
198 // %d pages less than 3 hits (<10%) <10% percent of the leastpopular
199 // %d pages more than 100 hits (>90%) >90% percent of the mostpopular
200 function hitstats() {
202 $dbi = $request->getDbh();
206 $page_iter = $dbi->getAllPages(true);
207 while ($page = $page_iter->next()) {
208 if (($current = $page->getCurrentRevision())
209 && (! $current->hasDefaultContents())) {
210 $h = $page->get('hits');
213 $max = max($h, $max);
219 $median_i = (int) $n / 2;
221 $median = $hits[$median_i];
223 $median = $hits[$median_i];
224 $stddev = stddev($hits, $total);
226 $s = sprintf(_("total hits: %d"), $total);
227 $s .= ", " . sprintf(_("max: %d"), $max);
228 $s .= ", " . sprintf(_("mean: %2.3f"), $total / $n);
229 $s .= ", " . sprintf(_("median: %d"), $median);
230 $s .= ", " . sprintf(_("stddev: %2.3f"), $stddev);
232 $mintreshold = $max * $percentage / 100.0; // lower than 10% of the hits
234 $nmin = $hits[0] < $mintreshold ? 1 : 0;
235 while (next($hits) < $mintreshold)
237 $maxtreshold = $max - $mintreshold; // more than 90% of the hits
240 while (prev($hits) > $maxtreshold)
242 $s .= "; " . sprintf(_("%d pages with less than %d hits (<%d%%)."),
243 $nmin, $mintreshold, $percentage);
244 $s .= " " . sprintf(_("%d page(s) with more than %d hits (>%d%%)."),
245 $nmax, $maxtreshold, 100 - $percentage);
248 function revisionstats() {
250 $dbi = $request->getDbh();
254 $page_iter = $dbi->getAllPages(true);
255 while ($page = $page_iter->next()) {
256 if ($current = $page->getCurrentRevision()
257 && (! $current->hasDefaultContents())) {
258 //$ma = $page->get('major');
259 //$mi = $page->get('minor');
265 // size of databases/files/cvs are possible plus the known size of the app.
266 // Todo: cache this costly operation!
267 function discspace() {
269 $dir = defined('PHPWIKI_DIR') ? PHPWIKI_DIR : '.';
270 $appsize = `du -s $dir | cut -f1`;
272 if (in_array($DBParams['dbtype'], array('SQL', 'ADODB'))) {
274 } elseif ($DBParams['dbtype'] == 'dba') {
276 $dbdir = $DBParams['directory'];
277 if ($DBParams['dba_handler'] == 'db3')
278 $pagesize = filesize($DBParams['directory']
279 . "/wiki_pagedb.db3") / 1024;
280 // if issubdirof($dbdir, $dir) $appsize -= $pagesize;
281 } else { // flatfile, cvs
282 $dbdir = $DBParams['directory'];
283 $pagesize = `du -s $dbdir`;
284 // if issubdirof($dbdir, $dir) $appsize -= $pagesize;
286 $s = sprintf(_("Application size: %d Kb"), $appsize);
288 $s .= ", " . sprintf(_("Pagedata size: %d Kb", $pagesize));
292 function inlineimages () {
293 return implode(' ', explode('|', INLINE_IMAGES));
295 function wikinameregexp () {
296 return $GLOBALS['WikiNameRegexp'];
298 function allowedprotocols () {
299 return implode(' ', explode('|', ALLOWED_PROTOCOLS));
301 function available_plugins () {
302 $fileset = new FileSet(FindFile('lib/plugin'), '*.php');
303 $list = $fileset->getFiles();
306 return sprintf(_("Total %d plugins: "), count($list))
307 . implode(', ', array_map(create_function('$f',
308 'return substr($f,0,-4);'),
311 function supported_languages () {
312 $available_languages = listAvailableLanguages();
313 natcasesort($available_languages);
315 return sprintf(_("Total of %d languages: "),
316 count($available_languages))
317 . implode(', ', $available_languages) . ". "
318 . sprintf(_("Current language: '%s'"), $GLOBALS['LANG'])
319 . ((DEFAULT_LANGUAGE != $GLOBALS['LANG'])
320 ? ". " . sprintf(_("Default language: '%s'"), DEFAULT_LANGUAGE)
324 function supported_themes () {
326 $available_themes = listAvailableThemes();
327 natcasesort($available_themes);
328 return sprintf(_("Total of %d themes: "), count($available_themes))
329 . implode(', ',$available_themes) . ". "
330 . sprintf(_("Current theme: '%s'"), $Theme->_name)
331 . ((THEME != $Theme->_name)
332 ? ". " . sprintf(_("Default theme: '%s'"), THEME)
336 function call ($arg, &$availableargs) {
337 if (!empty($availableargs[$arg]))
338 return $availableargs[$arg]();
339 elseif (method_exists($this, $arg)) // any defined SystemInfo->method()
340 return call_user_func_array(array(&$this, $arg), '');
341 elseif (defined($arg) && $arg != 'ADMIN_PASSWD') // any defined constant
342 return constant($arg);
344 return $this->error(sprintf(_("unknown argument '%s' to SystemInfo"), $arg));
347 function run($dbi, $argstr, &$request, $basepage) {
348 // don't parse argstr for name=value pairs. instead we use just 'name'
349 //$args = $this->getArgs($argstr, $request);
350 $args['seperator'] = ' ';
351 $availableargs = // name => callback + 0 args
352 array ('appname' => create_function('',"return 'PhpWiki';"),
353 'version' => create_function('',"return sprintf('%s', PHPWIKI_VERSION);"),
354 'LANG' => create_function('','return $GLOBALS["LANG"];'),
355 'LC_ALL' => create_function('','return setlocale(LC_ALL, 0);'),
356 'current_language' => create_function('','return $GLOBALS["LANG"];'),
357 'system_language' => create_function('','return DEFAULT_LANGUAGE;'),
358 'current_theme' => create_function('','return $GLOBALS["Theme"]->_name;'),
359 'system_theme' => create_function('','return THEME;'),
360 // more here or as method.
361 '' => create_function('',"return 'dummy';")
363 // split the argument string by any number of commas or space
364 // characters, which include " ", \r, \t, \n and \f
365 $allargs = preg_split("/[\s,]+/", $argstr, -1, PREG_SPLIT_NO_EMPTY);
366 if (in_array('all', $allargs) || in_array('table', $allargs)) {
367 $allargs = array('appname' => _("Application name"),
368 'version' => _("PhpWiki engine version"),
369 'database' => _("Database"),
370 'cachestats' => _("Cache statistics"),
371 'pagestats' => _("Page statistics"),
372 //'revisionstats' => _("Page revision statistics"),
373 //'linkstats' => _("Link statistics"),
374 'userstats' => _("User statistics"),
375 //'accessstats' => _("Access statistics"),
376 'hitstats' => _("Hit statistics"),
377 //'discspace' => _("Harddisc usage"),
378 'expireparams' => _("Expiry parameters"),
379 'wikinameregexp' => _("Wikiname regexp"),
380 'allowedprotocols' => _("Allowed protocols"),
381 'inlineimages' => _("Inline images"),
382 'available_plugins' => _("Available plugins"),
383 'supported_languages' => _("Supported languages"),
384 'supported_themes' => _("Supported themes"),
388 $table = HTML::table(array('border' => 1,'cellspacing' => 3,
389 'cellpadding' => 3));
390 foreach ($allargs as $arg => $desc) {
395 $table->pushContent(HTML::tr(HTML::td(HTML::strong($desc . ':')),
396 HTML::td(HTML($this->call($arg, $availableargs)))));
401 foreach ($allargs as $arg) {
402 $o = $this->call($arg, $availableargs);
406 $output .= ($o . $args['seperator']);
408 // if more than one arg, remove the trailing seperator
409 if ($output) $output = substr($output, 0,
410 - strlen($args['seperator']));
411 return HTML($output);
416 /* // autolisp stdlib
417 ;;; Median of the sorted list of numbers. 50% is above and 50% below
418 ;;; "center of a distribution"
419 ;;; Ex: (std-median (std-make-list 100 std-%random)) => 0.5 +- epsilon
420 ;;; (std-median (std-make-list 100 (lambda () (std-random 10))))
421 ;;; => 4.0-5.0 [0..9]
422 ;;; (std-median (std-make-list 99 (lambda () (std-random 10))))
424 ;;; (std-median '(0 0 2 4 12)) => 2
425 ;;; (std-median '(0 0 4 12)) => 2.0
426 (defun STD-MEDIAN (numlst / l)
427 (setq numlst (std-sort numlst '<)) ; don't remove duplicates
428 (if (= 0 (rem (setq l (length numlst)) 2)) ; if even length
429 (* 0.5 (+ (nth (/ l 2) numlst) ; force float!
430 (nth (1- (/ l 2)) numlst))) ; fixed by Serge Pashkov
431 (nth (/ l 2) numlst)))
434 function median($hits) {
438 $median = (int) $n / 2;
439 if (! ($n % 2)) // proper rounding on even length
440 return ($hits[$median] + $hits[$median-1]) * 0.5;
442 return $hits[$median];
445 function rsum($a, $b) {
449 function mean(&$hits, $total = false) {
452 $total = array_reduce($hits, 'rsum');
453 return (float) $total / ($n * 1.0);
455 function gensym($prefix = "_gensym") {
457 while (isset($GLOBALS[$prefix . $i]))
462 /* // autolisp stdlib
463 (defun STD-STANDARD-DEVIATION (numlst / n _dev_m r)
464 (setq n (length numlst)
465 _dev_m (std-mean numlst)
466 r (mapcar (function (lambda (x) (std-sqr (- x _dev_m)))) numlst))
467 (sqrt (* (std-mean r) (/ n (float (- n 1))))))
469 function stddev(&$hits, $total = false) {
471 if (!$total) $total = array_reduce($hits, 'rsum');
472 $GLOBALS['mean'] = $total / $n;
473 $r = array_map(create_function('$i', 'global $mean; return ($i-$mean)*($i-$mean);'),
475 unset($GLOBALS['mean']);
476 return (float)sqrt(mean($r, $total) * ($n / (float)($n -1)));
479 // $Log: not supported by cvs2svn $
480 // Revision 1.15 2004/05/03 11:40:42 rurban
481 // put listAvailableLanguages() and listAvailableThemes() from SystemInfo and
482 // UserPreferences into Themes.php
484 // Revision 1.14 2004/04/19 23:13:04 zorloc
485 // Connect the rest of PhpWiki to the IniConfig system. Also the keyword regular expression is not a config setting
487 // Revision 1.13 2004/04/18 01:11:52 rurban
488 // more numeric pagename fixes.
489 // fixed action=upload with merge conflict warnings.
490 // charset changed from constant to global (dynamic utf-8 switching)
492 // Revision 1.12 2004/03/14 16:26:21 rurban
495 // Revision 1.11 2004/03/13 19:24:21 rurban
496 // fixed supported_languages() and supported_themes()
498 // Revision 1.10 2004/03/08 18:17:10 rurban
499 // added more WikiGroup::getMembersOf methods, esp. for special groups
500 // fixed $LDAP_SET_OPTIONS
501 // fixed _AuthInfo group methods
503 // Revision 1.9 2004/02/17 12:11:36 rurban
504 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
506 // Revision 1.8 2003/02/24 01:36:26 dairiki
507 // Don't use PHPWIKI_DIR unless it's defined.
508 // (Also typo/bugfix in SystemInfo plugin.)
510 // Revision 1.7 2003/02/22 20:49:56 dairiki
511 // Fixes for "Call-time pass by reference has been deprecated" errors.
513 // Revision 1.6 2003/02/21 23:01:11 dairiki
514 // Fixes to support new $basepage argument of WikiPlugin::run().
516 // Revision 1.5 2003/01/18 22:08:01 carstenklapp
518 // Reformatting & tabs to spaces;
519 // Added copyleft, getVersion, getDescription, rcs_id.
526 // c-hanging-comment-ender-p: nil
527 // indent-tabs-mode: nil