<?
define("WS_ADMIN",true);
require_once( "../common/WSInit.php" );
function grabNamespaceInfo( $dbname, $domain, $resultList, $nameField, $type, &$info ) {
foreach ($resultList as $m) {
if (!isset($m[$nameField])) continue;
$n= (int)$m['id'];
$ns= array(
'dbname' => $dbname,
'domain' => $domain,
);
$ns['ns_id']= $n;
$ns['ns_name']= $m[$nameField];
$ns['ns_type']= $type;
$info[]= $ns;
}
}
function makeNamespaceInfo( $wiki, $aliases = false ) {
$dbname = $wiki->dbname;
$domain = $wiki->domain;
$u= "http://$domain{$wiki->script_path}api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases&format=php";
$text= @file_get_contents($u);
if (!$text) {
trigger_error("failed to fetch namespace info from $u", E_USER_WARNING);
return false;
}
$data = @unserialize($text);
if (!$data) {
if (strpos($text, '<b>$wgEnableAPI=true;</b>') !== false) return false; #XXX: hack. API disabled #TODO: use Special:Export
trigger_error("failed to unserialize namespace info from $u", E_USER_WARNING);
return false;
}
if (isset($data['error'])) {
if ($data['error']['code'] == 'readapidenied') return false; #private wiki, no read permission
trigger_error("API call $u returned an error: ".$data['error']['code'].": ".$data['error']['info'], E_USER_WARNING);
return false;
}
if (isset($data['warnings']['query']['*'])) {
trigger_error("API call $u returned a warning: ".$data['warnings']['query']['*'], E_USER_WARNING);
return false;
}
if (!isset($data['query'])) {
trigger_error("API call $u returned strange data: ".var_export($data, true), E_USER_WARNING);
return false;
}
$info= array();
# Primary names
grabNamespaceInfo($dbname, $domain, $data['query']['namespaces'], "*", "primary", $info);
if ($aliases) {
# Canonical names
grabNamespaceInfo($dbname, $domain, $data['query']['namespaces'], "canonical", "canonical", $info);
# Aliases
grabNamespaceInfo($dbname, $domain, $data['query']['namespacealiases'], "*", "alias", $info);
}
return $info;
}
function printNamespaceInfo( $info ) {
print $info['dbname'];
print "\t";
print $info['domain'];
print "\t";
print $info['ns_id'];
print "\t";
print $info['ns_name'];
if ( isset($info['ns_type'] ) ) {
print "\t";
print $info['ns_type'];
}
print "\n";
}
function updateNamespaceInfo( &$db, $info, $type = false, $override = flase ) {
global $table;
if (!$type) unset($info['ns_type']);
$info= wsfStripIntKeys($info);
$values= '';
foreach ($info as $k => $v) {
if ($values) $values.= ', ';
$values.= "$k = ";
if (is_int($v) || is_float($v)) $values.= $v;
else if (is_bool($v)) $values.= $v ? '1' : '0';
else $values.= $db->addQuotes($v);
}
if ($override) $sql = "REPLACE INTO $table SET $values;";
else $sql = "INSERT IGNORE INTO $table SET $values;";
$res= $db->query($sql,'NamespaceList::updateNamespaceInfo');
#if ($res) $db->freeResult($res);
return $res;
}
#--------------------------------------------------------
$csv= isset($options['csv']);
$aliases= isset($options['aliases']);
$override= isset($options['override']) || isset($options['replace']);
$update= isset( $options['update']) || $override;
$show= isset( $options['show'] );
$truncate= isset( $options['truncate'] );
$targetDB= $wsgWikiListDB;
if ( $aliases ) $table= 'namespacename';
else $table= 'namespace';
if (isset($options['db'])) $targetDB= $options['db'];
if (isset($options['table'])) $table= $options['table'];
if (!@$options['test']) {
$u= $targetDB;
if (strpos($u,':')===false && strpos($u, '%')!==0) $u= "$wsgWikiDB/$targetDB";
$db= openConnection($u);
if (!$db) {
wsfLog("failed to connect to database!",LL_ERROR);
die();
}
} else {
$db = false;
}
if (!isset($table)) $table = $aliases ? $wsgNamespaceNameTable : $wsgCustomNamespaceTable;
if ($show) {
foreach ($args as $w) {
$wiki = getWikiInfoFromDomain($w);
$sql= "select * from $table where domain = ".$db->addQuotes($wiki->domain)." order by ns_id";
$res= $db->query($sql,'NamespaceList: show entry');
while ( $row= $db->fetchRow($res) ) {
printNamespaceInfo($row);
}
$db->freeResult($res);
}
}
else if (@$options['test']) {
foreach ($args as $w) {
$wiki = getWikiInfoFromDomain($w);
$info= makeNamespaceInfo($wiki);
foreach ($info as $ns) {
printNamespaceInfo($ns);
}
}
}
else if ($update || $csv) {
if (!$args) {
if ($truncate && $db) {
wsfLog("truncating $table", LL_INFO);
$db->query("TRUNCATE $table");
}
$sql= "select * from $wsgWikiListTable";
$res= $db->query($sql,'NamespaceList: list namespaces');
} else {
$res = false;
}
$i = 0;
while ( true ) {
if ($res) {
$row= $db->fetchObject($res);
if (!$row) break;
} else {
if ($i >= count($args)) break;
$n = $args[$i];
$i += 1;
$row = getWikiInfoFromDomain($n);
if (!$row) {
if (!$csv) wsfLog("bad wiki name: ".$n,LL_WARN);
continue;
}
}
if (!$row->domain) continue;
if ($row->is_closed) continue;
$info= makeNamespaceInfo($row, $aliases);
if ($info===false) {
if (!$csv) wsfLog("failed to access: {$row->domain}",LL_WARN);
continue;
} if (!$info) {
if (!$csv) wsfLog("no custom namespaces: {$row->domain}",LL_INFO);
continue;
}
foreach ($info as $ns) {
if ($csv) printNamespaceInfo($ns);
else {
updateNamespaceInfo($db,$ns, $aliases, $override);
wsfLog("updated {$row->domain}: {$ns['ns_name']}",LL_INFO);
}
}
}
if ($res) $db->freeResult($res);
}
else {
wsfLog("nothing to do. Use --show, --update, or --csv",LL_WARN);
}
$db->close();
?>NamespaceList.php
application/x-php, 6033 bytes (load raw)

