<?
define("WS_CONSOLE",true);
require_once( "../common/WSInit.php" );
require_once( "WikiAccess.php" );
define('XM_LIMIT', 500);
#$wgLogLevel = 100;
$takeout_id = 0;
function preg_takeout_callback($text, &$data) {
global $takeout_id;
$takeout_id += 1;
$id = $takeout_id;
$id = "\0\0<<$id>>\0\0";
$data[$id] = $text;
#print "$id\n";
return $id;
}
function preg_takeout($pattern, $replace, $s, &$data) {
if (preg_match('/[^\w\d]\w*e\w*$/', $pattern)) {
return preg_replace($pattern, "preg_takeout_callback($replace, &\$data)", $s);
}
else {
$s = str_replace('$', '\$', $s);
$replace = str_replace("'", "\\'", $replace);
return preg_replace($pattern.'e', "preg_takeout_callback(\"$replace\", &\$data)", $s);
}
}
function preg_putback($s, &$data) {
$k = array_keys($data);
$v = array_values($data);
return str_replace($k, $v, $s);
}
function extractExtensionDetails( $text ) {
$details = array();
preg_match_all('!\{\{\s*([^|{}]+)\s*(.*?)\}\}!smi', $text, $templates, PREG_SET_ORDER);
$alerts = array();
$info = NULL;
foreach ($templates as $t) {
$name = trim($t[1]);
$params = trim($t[2]);
$params = preg_replace('!<nowiki>(.*?)</nowiki>!esmi', 'wfEscapeWikiText("\1")', $params);
$name = preg_replace('/^template:/i', '', $name);
$lcname = strtolower($name);
if ($lcname == 'extension') {
$info = $params;
}
else if (preg_match('/(^|[ _])alert$/', $lcname)) {
$alerts[] = $name;
}
}
$details['alerts'] = $alerts;
if (!$info) {
return false;
}
$outtakes = array();
$info = preg_takeout('!\[\[[^\{\}\[\]\r\n]+?\]\]!', '\0', $info, $outtakes);
#print "\n\n$text\n\n";
$params = preg_split('!\s*\|\s*!smi', $info);
foreach ($params as $p) {
$x = preg_split('!\s*=\s*!smi', $p, 2);
if (sizeof($x)<2) continue;
$k = preg_putback($x[0], $outtakes);
$v = preg_putback($x[1], $outtakes);
$details[$k] = sanitizeParamValue( $v );
}
return $details;
}
function sanitizeParamValue( $v ) {
$v = trim(preg_replace('!\r\n!', ' ', $v));
$v = preg_replace('!\[\[\s*([^:\[\]]+)\s*:!i', '[[:\1:', $v);
$v = preg_replace('!\{\{|\}\}!', '<nowiki>\0</nowiki>', $v);
return $v;
}
function makeExtensionRow( $details ) {
global $dateformat;
extract($details);
$types = array();
if (@$type) $types[]= str_replace(' ', '_', $type);
if (@$type1) $types[]= str_replace(' ', '_', $type1);
if (@$type2) $types[]= str_replace(' ', '_', $type2);
if (@$type3) $types[]= str_replace(' ', '_', $type3);
if (!@$status) $status = 'unknown';
if (!$types) $types[]= 'unknown';
$statusmsg = $status;
foreach ($alerts as $alert) {
$statusmsg.= ' / <span class="xm-alert">[[Template:' . $alert . '|' . $alert . ']]</span> ';
}
if (@$version && @$date) $version = "$version ($update)";
else if (@$date) $version = $date;
if (@$mediawiki) $mediawiki = "MW $mediawiki";
$links = '';
if (@"$download$readme$changelog$example" !== '')
$links = @"<span class=\"xm-links\">(Links: $download $readme $changelog $example)</span>";
@$s = '';
@$s .= "| class=\"xm-name\" | [[$_page_|$name]]\n";
@$s .= "| class=\"xm-status xm-status-$status\"| $statusmsg\n";
@$s .= "| class=\"xm-type xm-status-".implode(" xm-status-", $types)."\" | ".implode(", ", $types)."\n";
@$s .= "| class=\"xm-mediawiki\"| $mediawiki\n";
@$s .= "| class=\"xm-version\"| $version \n";
@$s .= "| class=\"xm-author\"| $author \n";
#@$s .= "| class=\"xm-links\"| $download $readme $changelog $example \n";
@$s .= "|--- valign=\"top\"\n";
@$s .= "| class=\"xm-updated\"| {{#time:$dateformat|$_timestamp_}}\n";
@$s .= "| class=\"xm-description\" colspan=\"5\"| $description $links\n";
return $s;
}
function titlecomp(&$a, &$b) {
$s = $a['title'];
$t = $b['title'];
if ($s>$t) return 1;
else if ($s<$t) return -1;
else return 0;
}
$limit = XM_LIMIT;
if (isset($options['limit'])) $limit = $options['limit'];
$limit = min($limit, XM_LIMIT);
$test = @$options['test'];
$update = @$options['update'];
$wiki = @$options['wiki'];
if (!$wiki) $wiki = 'www.mediawiki.org';
$wiki = WikiAccess::newInstance($wiki);
#if ($wiki->wikiDB) $wiki->wikiDB->close();
#$wiki->wikiDB = NULL;
$wiki->initTranslations();
$dateformat = $wiki->translations->getDefaultDateFormat();
$dateformat = $wiki->translations->dateFormats["$dateformat date"];
if ($update) {
print "will post to $update\n";
print "listing extension pages\n";
}
if ($test) $pages = array( array( 'title' => $test ) );
else $pages = $wiki->webApiList('Template:Extension', 'embeddedin', array('eititle'=>'Template:Extension', 'eilimit' => $limit, 'einamespace' => '102'), 'eicontinue');
if ($update) {
print "found ".sizeof($pages)." extension pages\n";
print "analyzing extension pages...\n";
ob_start();
}
print "{| class=\"xm-table\" border=\"1\" cellpadding=\"2\" cellspacing=\"2\" |\n";
print "! class=\"xm-name\" | Page\n";
print "! class=\"xm-status\"| Status\n";
print "! class=\"xm-type\" | Type\n";
print "! class=\"xm-mediawiki\"| MW Version\n";
print "! class=\"xm-version\"| Version (Date)\n";
print "! class=\"xm-author\"| Author\n";
print "|--- valign=\"top\"\n";
print "! class=\"xm-updated\"| Pg. Updated\n";
print "! class=\"xm-description\" colspan=\"5\"| Description <span class=\"xm-links\">(Links)</span>\n";
print "|--- valign=\"top\"\n";
usort($pages, 'titlecomp');
$stoplist = array();
foreach ($pages as $p) {
$t = $p['title'];
$lnk= $wiki->makeLinkEntry($t);
$name = $lnk['link'];
$ns = $lnk['namespacenum'];
$n = explode('/', $name);
if (sizeof($n)>1) {
if (isset($stoplist[$ns.':'.$n[0]])) continue; //skip subpages if parent page was already processed
}
$stoplist["$ns:$name"] = true;
$pg = $wiki->loadInfo( $name, $ns );
if (!$pg) {
print "| colspan=\"6\" style=\"color:red;\" | WARNING: failed to fetch [[$t]]\n";
print "|--- valign=\"top\"\n";
continue;
}
$details = extractExtensionDetails($pg['text']);
if (!$details) {
print "| colspan=\"6\" style=\"color:red;\" | WARNING: failed to parse [[$t]]\n";
print "|--- valign=\"top\"\n";
continue;
}
$details['_page_'] = $t;
$details['_timestamp_'] = $pg['timestamp'];
print makeExtensionRow($details);
print "|--- valign=\"top\"\n";
}
print "|}\n";
if ($update) {
$text = ob_get_contents();
ob_end_clean();
print "updating $update\n";
require_once('WikiBot.php');
$bot = new WikiBot($wiki);
$bot->traceLog = @$options['httplog'];
if ($bot->assertLogin()) {
$bot->edit( $update, $text, "automatic matrix update",
'<!--\s*MATRIX END.*?-->', '<!--\s*MATRIX START.*?-->',
NULL, NULL, true );
}
else {
print "unable to log in\n";
}
}
$wiki->close();
?>
ExtensionMatrix.php
application/x-php, 6658 bytes (load raw)

