/** * Try to find an SMWDataItem that the given SMWExpElement might * represent. Returns null if this attempt failed. * * @param SMWExpElement $expElement * @return SMWDataItem or null */ public static function findDataItemForExpElement(SMWExpElement $expElement) { global $wgContLang; $dataItem = null; if ($expElement instanceof SMWExpResource) { $uri = $expElement->getUri(); $wikiNamespace = self::getNamespaceUri('wiki'); if (strpos($uri, $wikiNamespace) === 0) { $localName = substr($uri, strlen($wikiNamespace)); $dbKey = urldecode(self::decodeURI($localName)); $parts = explode('-23', $dbKey, 2); if (count($parts) == 2) { $subobjectname = $parts[1]; } else { $subobjectname = ''; } $parts = explode(':', $dbKey, 2); if (count($parts) == 1) { $dataItem = new SMWDIWikiPage($dbKey, NS_MAIN, '', $subobjectname); } else { // try the by far most common cases directly before using Title $namespaceName = str_replace('_', ' ', $parts[0]); $namespaceId = -1; foreach (array(SMW_NS_PROPERTY, NS_CATEGORY, NS_USER, NS_HELP) as $nsId) { if ($namespaceName == $wgContLang->getNsText($nsId)) { $namespaceId = $nsId; break; } } if ($namespaceId != -1) { $dataItem = new SMWDIWikiPage($parts[1], $namespaceId, '', $subobjectname); } else { $title = Title::newFromDBkey($dbKey); if (!is_null($title)) { $dataItem = new SMWDIWikiPage($title->getDBkey(), $title->getNamespace(), $title->getInterwiki(), $subobjectname); } } } } // else: not in wiki namespace -- TODO: this could be an imported URI } else { // TODO (currently not needed, but will be useful for displaying external SPARQL results) } return $dataItem; }
/** * Get the Turtle serialization string for the given SMWExpElement. The * method just computes a name, and does not serialize triples, so the * parameter must be an SMWExpResource or SMWExpLiteral, no SMWExpData. * * @param $expElement SMWExpElement being SMWExpLiteral or SMWExpResource * @return string */ public static function getTurtleNameForExpElement(SMWExpElement $expElement) { if ($expElement instanceof SMWExpResource) { if ($expElement->isBlankNode()) { return '[]'; } elseif ($expElement instanceof SMWExpNsResource && $expElement->hasAllowedLocalName()) { return $expElement->getQName(); } else { return '<' . str_replace('>', '\\>', SMWExporter::getInstance()->expandURI($expElement->getUri())) . '>'; } } elseif ($expElement instanceof SMWExpLiteral) { $dataType = $expElement->getDatatype(); $lexicalForm = self::getCorrectLexicalForm($expElement); if ($dataType !== '' && $dataType != 'http://www.w3.org/2001/XMLSchema#string') { $count = 0; $newdt = str_replace('http://www.w3.org/2001/XMLSchema#', 'xsd:', $dataType, $count); return $count == 1 ? "{$lexicalForm}^^{$newdt}" : "{$lexicalForm}^^<{$dataType}>"; } else { return $lexicalForm; } } else { throw new InvalidArgumentException('The method can only serialize atomic elements of type SMWExpResource or SMWExpLiteral.'); } }
/** * Constructor. The given lexical form should be the plain string for * representing the literal without datatype or language information. * It must not use any escaping or abbreviation mechanisms. * * @param string $lexicalForm lexical form * @param string $datatype Data type URI or empty for untyped literals * @param SMWDataItem|null $dataItem * * @throws InvalidArgumentException */ public function __construct($lexicalForm, $datatype = '', SMWDataItem $dataItem = null) { if (!is_string($lexicalForm)) { throw new InvalidArgumentException('$lexicalForm needs to be a string'); } if (!is_string($datatype)) { throw new InvalidArgumentException('$datatype needs to be a string'); } parent::__construct($dataItem); $this->lexicalForm = $lexicalForm; $this->datatype = $datatype; }
/** * Constructor. $subject is the SMWExpResource for the * subject about which this SMWExpData is. */ public function __construct(SMWExpResource $subject) { parent::__construct($subject->getDataItem()); $this->m_subject = $subject; }
/** * Constructor. The given lexical form should be the plain string for * representing the literal without datatype or language information. * It must not use any escaping or abbrevition mechanisms. * * @param $lexicalForm string lexical form * @param $datatype string datatype URI or empty for untyped literals * @param $dataItem SMWDataItem or null */ public function __construct($lexicalForm, $datatype = '', $dataItem = null) { parent::__construct($dataItem); $this->m_lexicalForm = $lexicalForm; $this->m_datatype = $datatype; }
/** * @dataProvider instanceProvider */ public function testGetDataItem(\SMWExpElement $element) { $this->assertTypeOrValue('SMWDataItem', $element->getDataItem(), null); }