<?php

if (isset($GET_)) {
    echo "This file cannot be run from the web.\n";
    die(1);
}
if (getenv('MW_INSTALL_PATH')) {
    $IP = getenv('MW_INSTALL_PATH');
} else {
    $dir = dirname(__FILE__);
    if (file_exists("{$dir}/../../LocalSettings.php")) {
        $IP = "{$dir}/../..";
    } elseif (file_exists("{$dir}/../../../LocalSettings.php")) {
        $IP = "{$dir}/../../..";
    } elseif (file_exists("{$dir}/../../phase3/LocalSettings.php")) {
        $IP = "{$dir}/../../phase3";
    } elseif (file_exists("{$dir}/../../../phase3/LocalSettings.php")) {
        $IP = "{$dir}/../../../phase3";
    } else {
        $IP = $dir;
    }
}
require_once "{$IP}/maintenance/commandLine.inc";
$dir = dirname(__FILE__);
$dtbase = dirname(realpath($dir));
$src = $args[0];
$field = $args[1];
$value = $args[2];
$source = DataTransclusionHandler::getDataSource($src);
$data = $source->fetchRecord($field, $value, null);
print_r($data);
 function testCachedFetchRecord()
 {
     global $wgDataTransclusionSources;
     $data[] = array("name" => "foo", "id" => 3, "info" => 'test 1');
     $data[] = array("name" => "bar", "id" => 5, "info" => 'test 2');
     $spec = array('class' => 'FakeDataTransclusionSource', 'data' => $data, 'keyFields' => 'name,id', 'fieldNames' => 'id,name,info', 'cacheDuration' => 2, 'cache' => new HashBagOStuff());
     $wgDataTransclusionSources['FOO'] = $spec;
     $src = DataTransclusionHandler::getDataSource('FOO');
     $this->assertTrue($src instanceof CachingDataTransclusionSource);
     // get original version
     $rec = $src->fetchRecord('id', 3);
     $this->assertEquals(3, $rec['id']);
     $this->assertEquals('foo', $rec['name']);
     $this->assertEquals('test 1', $rec['info']);
     // change record
     $rec = array("name" => "foo", "id" => 3, "info" => 'test X');
     $src->source->putRecord($rec);
     // fetch record - should be the cached version
     $rec = $src->fetchRecord('id', 3);
     $this->assertEquals('test 1', $rec['info']);
     sleep(3);
     // fetch record - cached version should have expired
     $rec = $src->fetchRecord('id', 3);
     $this->assertEquals('test X', $rec['info']);
 }
 public function execute()
 {
     global $wgDataTransclusionSources;
     $this->debug = $this->hasOption('debug');
     $this->noblob = $this->hasOption('noblob');
     $recursive = $this->hasOption('recursive');
     $limit = (int) $this->getOption('limit');
     $this->recordSeparator = $this->getOption('record-separator');
     $this->multiRecord = $this->recordSeparator || $this->hasOption('multi-record');
     $this->idListField = $this->getOption('id-list-field');
     $this->idListFile = $this->getOption('id-list-file');
     $this->idList = null;
     $src = $this->mArgs[0];
     $dir = $this->mArgs[1];
     if (!isset($wgDataTransclusionSources[$src])) {
         throw new MWException("unknown transclusion data source '{$src}', not found in \$wgDataTransclusionSources");
     }
     $this->output("using settings for data transclusion source \"{$src}\".\n");
     $this->source = DataTransclusionHandler::getDataSource($src);
     if (!$this->source instanceof DBDataTransclusionSource) {
         throw new MWException("bad data source '{$src}': not compatible with DBDataTransclusionSource");
     }
     $this->blob_table = $this->mArgs[2];
     $this->index_table = $this->mArgs[3];
     if ($this->hasOption('prefix')) {
         $prefix = $this->getOption("prefix");
         $this->blob_table = $prefix . $this->blob_table;
         $this->index_table = $prefix . $this->index_table;
     } else {
         $db = wfGetDB(DB_MASTER);
         # we'll need the master anyway later
         $this->blob_table = $db->tableName($this->blob_table);
         $this->index_table = $db->tableName($this->index_table);
     }
     if (!$this->debug) {
         $this->output("using tables {$this->blob_table} and {$this->index_table}.\n");
         if ($this->hasOption('create')) {
             $this->output("creating tables if neccessary.\n");
             $this->createTables($this->blob_table, $this->index_table);
         }
         if ($this->hasOption('truncate')) {
             $this->output("truncating tables.\n");
             $this->truncateTables($this->blob_table, $this->index_table);
         }
     }
     $this->id_map = array();
     foreach ($this->source->keyFields as $key) {
         $this->id_map[$key] = MAB2RecordTransformer::getMABFields($key);
         if (!$this->id_map[$key]) {
             $this->error("unknown key field '{$key}', no MAB fields mapped.\n");
         }
     }
     if ($this->idListFile) {
         $this->output("loading id list from {$this->idListFile}.\n");
         $this->idList = $this->loadList($this->idListFile, $this->idListField);
         if ($this->idList === false) {
             $this->error("failed to load id list from {$this->idListFile}.\n");
             return;
         }
     }
     if ($this->idList && $this->idListField) {
         $this->output("filtering by {$this->idListField} from {$this->idListFile}.\n");
     }
     $dir = "php://stdin";
     if (is_dir($dir)) {
         $this->importDir($dir, $recursive, $limit);
     } else {
         $this->importMabFile($dir);
     }
 }
 /**
  * Fetches a records and renders it, according to the given array of parameters.
  * Common implementation for parser tag and parser function.
  */
 static function handleRecordTransclusion($template, $argv, $parser, $asHTML, $templateText = null)
 {
     // find out which data source to use...
     if (empty($argv['source'])) {
         if (empty($argv[1])) {
             wfDebugLog('DataTransclusion', "no source specified\n");
             return DataTransclusionHandler::errorMessage('datatransclusion-missing-source', $asHTML);
         } else {
             $sourceName = $argv[1];
         }
     } else {
         $sourceName = $argv['source'];
     }
     $source = DataTransclusionHandler::getDataSource($sourceName);
     if (empty($source)) {
         wfDebugLog('DataTransclusion', "unknown source: {$sourceName}\n");
         return DataTransclusionHandler::errorMessage('datatransclusion-unknown-source', $asHTML, $sourceName);
     }
     // find out how to render the record
     if (!empty($argv['template'])) {
         $template = $argv['template'];
     } elseif ($template === null || $template === false) {
         if (empty($argv[0])) {
             wfDebugLog('DataTransclusion', "missing 'template' argument\n");
             return DataTransclusionHandler::errorMessage('datatransclusion-missing-argument-template', $asHTML);
         } else {
             $template = $argv[0];
         }
     }
     // find key
     $by = false;
     $key = false;
     $keyFields = $source->getKeyFields();
     foreach ($keyFields as $k) {
         if (isset($argv[$k])) {
             $by = $k;
             $key = $argv[$k];
             break;
             //XXX: could keep running and complain about multiple keys
         }
     }
     if (!$by) {
         global $wgContLang;
         wfDebugLog('DataTransclusion', "no key specified\n");
         return DataTransclusionHandler::errorMessage('datatransclusion-missing-key', $asHTML, $sourceName, $wgContLang->commaList($keyFields), count($keyFields));
     }
     // collect options
     $options = array();
     $optionNames = $source->getOptionNames();
     if ($optionNames) {
         foreach ($optionNames as $n) {
             if (isset($argv[$n])) {
                 $options[$n] = $argv[$n];
             }
         }
     }
     // load the record
     $record = $source->fetchRecord($by, $key, $options);
     if (empty($record)) {
         wfDebugLog('DataTransclusion', "no record found matching {$by}={$key} in {$sourceName}\n");
         return DataTransclusionHandler::errorMessage('datatransclusion-record-not-found', $asHTML, $sourceName, $by, $key);
     }
     // render the record into wiki text
     if ($template === "#dump") {
         $t = null;
     } else {
         $t = Title::newFromText($template, NS_TEMPLATE);
         if (empty($t)) {
             wfDebugLog('DataTransclusion', "illegal template name: {$template}\n");
             return DataTransclusionHandler::errorMessage('datatransclusion-bad-template-name', $asHTML, $template);
         }
     }
     $handler = new DataTransclusionHandler($parser, $source, $t, $templateText);
     $record = $handler->normalizeRecord($record, $argv);
     $text = $handler->render($record);
     if ($text === false) {
         wfDebugLog('DataTransclusion', "template not found: {$template}\n");
         return DataTransclusionHandler::errorMessage('datatransclusion-unknown-template', $asHTML, $template);
     }
     // set parser output expiry
     $expire = $source->getCacheDuration();
     if ($expire !== false && $expire !== null) {
         $parser->getOutput()->updateCacheExpiry($expire);
         // NOTE: this works only since r67185 //TESTME (how?)
     }
     if ($asHTML && $parser) {
         // render into HTML if desired
         $html = $parser->recursiveTagParse($text);
         return $html;
     } else {
         return $text;
     }
 }