function testNormalizeRecord() { global $wgParser; $spec = array('name' => 'FOO', 'keyFields' => 'name,id', 'fieldNames' => 'id,name,info', 'sourceInfo' => array('x' => 43, 'quux' => 'xyzzy')); $data[] = $rec = array("name" => "foo", "id" => 3, "info" => '{{test}}=[[x]] 1&2 ', 'stuff' => 'bla bla bla'); $source = new FakeDataTransclusionSource($spec, $data); $handler = new DataTransclusionHandler($wgParser, $source, null); $args = array("name" => "cruft", "more" => "stuff"); $rec = $handler->normalizeRecord($rec, $args); $this->assertEquals('FOO', $rec['source-name']); $this->assertEquals('xyzzy', $rec['quux']); $this->assertEquals(43, $rec['x']); $this->assertEquals('stuff', $rec['more']); $this->assertEquals('foo', $rec['name']); $this->assertEquals('3', $rec['id']); $this->assertEquals('{{test}}=[[x]] 1&2 ', $rec['info']); $this->assertTrue(!isset($rec['stuff'])); $this->assertTrue(!isset($rec['name.keyFields'])); }
/** * 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; } }