/** * @see SMWOrderedListPage::initParameters() * @note We use a smaller limit here; property pages might become large. */ protected function initParameters() { global $smwgPropertyPagingLimit; $this->limit = $smwgPropertyPagingLimit; $this->mProperty = SMWDIProperty::newFromUserLabel($this->mTitle->getText()); return true; }
protected function parseUserValue($value) { global $wgContLang; $this->m_diProperties = array(); $stringValue = ''; $valueList = preg_split('/[\\s]*;[\\s]*/u', trim($value)); foreach ($valueList as $propertyName) { $propertyNameParts = explode(':', $propertyName, 2); if (count($propertyNameParts) > 1) { $namespace = smwfNormalTitleText($propertyNameParts[0]); $propertyName = $propertyNameParts[1]; $propertyNamespace = $wgContLang->getNsText(SMW_NS_PROPERTY); if ($namespace != $propertyNamespace) { $this->addError(wfMessage('smw_wrong_namespace', $propertyNamespace)->inContentLanguage()->text()); } } $propertyName = smwfNormalTitleText($propertyName); try { $diProperty = SMWDIProperty::newFromUserLabel($propertyName); } catch (SMWDataItemException $e) { $diProperty = new SMWDIProperty('Error'); $this->addError(wfMessage('smw_noproperty', $propertyName)->inContentLanguage()->text()); } $this->m_diProperties[] = $diProperty; $stringValue .= ($stringValue ? ';' : '') . $diProperty->getKey(); } $this->m_dataitem = new SMWDIBlob($stringValue); }
function setTypeAndPossibleValues() { // The presence of "-" at the beginning of a property name // (which happens if SF tries to parse an inverse query) // leads to an error in SMW - just exit if that's the case. if (strpos($this->mSemanticProperty, '-') === 0) { return; } $proptitle = Title::makeTitleSafe(SMW_NS_PROPERTY, $this->mSemanticProperty); if ($proptitle === null) { return; } $store = SFUtils::getSMWStore(); // this returns an array of objects $allowed_values = SFUtils::getSMWPropertyValues($store, $proptitle, "Allows value"); $label_formats = SFUtils::getSMWPropertyValues($store, $proptitle, "Has field label format"); $propValue = SMWDIProperty::newFromUserLabel($this->mSemanticProperty); $this->mPropertyType = $propValue->findPropertyTypeID(); foreach ($allowed_values as $allowed_value) { // HTML-unencode each value $this->mPossibleValues[] = html_entity_decode($allowed_value); if (count($label_formats) > 0) { $label_format = $label_formats[0]; $prop_instance = SMWDataValueFactory::findTypeID($this->mPropertyType); $label_value = SMWDataValueFactory::newTypeIDValue($prop_instance, $wiki_value); $label_value->setOutputFormat($label_format); $this->mValueLabels[$wiki_value] = html_entity_decode($label_value->getWikiValue()); } } // HACK - if there were any possible values, set the property // type to be 'enumeration', regardless of what the actual type is if (count($this->mPossibleValues) > 0) { $this->mPropertyType = 'enumeration'; } }
function setTypeAndPossibleValues() { $proptitle = Title::makeTitleSafe(SMW_NS_PROPERTY, $this->mSemanticProperty); if ($proptitle === null) { return; } $store = smwfGetStore(); // this returns an array of objects $allowed_values = SFUtils::getSMWPropertyValues($store, $proptitle, "Allows value"); $label_formats = SFUtils::getSMWPropertyValues($store, $proptitle, "Has field label format"); $propValue = SMWDIProperty::newFromUserLabel($this->mSemanticProperty); $this->mPropertyType = $propValue->findPropertyTypeID(); foreach ($allowed_values as $allowed_value) { // HTML-unencode each value $this->mPossibleValues[] = html_entity_decode($allowed_value); if (count($label_formats) > 0) { $label_format = $label_formats[0]; $prop_instance = SMWDataValueFactory::findTypeID($this->mPropertyType); $label_value = SMWDataValueFactory::newTypeIDValue($prop_instance, $wiki_value); $label_value->setOutputFormat($label_format); $this->mValueLabels[$wiki_value] = html_entity_decode($label_value->getWikiValue()); } } // HACK - if there were any possible values, set the property // type to be 'enumeration', regardless of what the actual type is if (count($this->mPossibleValues) > 0) { $this->mPropertyType = 'enumeration'; } }
/** * @see SMWOrderedListPage::initParameters() * @note We use a smaller limit here; property pages might become large. */ protected function initParameters() { global $smwgPropertyPagingLimit; $this->limit = $smwgPropertyPagingLimit; $this->mProperty = SMWDIProperty::newFromUserLabel($this->mTitle->getText()); $this->store = ApplicationFactory::getInstance()->getStore(); return true; }
public function addPropertyAndValue( $propName, $value ) { // SMW 1.6+ if ( class_exists( 'SMWDIProperty' ) ) { $property = SMWDIProperty::newFromUserLabel( $propName ); } else { $property = SMWPropertyValue::makeUserProperty( $propName ); } $dataValue = SMWDataValueFactory::newPropertyObjectValue( $property, $value ); if ( $dataValue->isValid() ) { $this->mPropertyValuePairs[] = array( $property, $dataValue ); } // else - show an error message? }
/** * Write (or delte, if $delete is set to true) the data in the object * variables, to the wiki page corresponding to this page handler * @param boolean $delete */ public function writeOrDeleteDataToWiki( $delete = false ) { if ( $delete ) { if ( $this->checkWikiPageExists() ) { $this->initSMWWriter( $delete = true ); } else { return; } } else { $this->ensureWikiPageExists(); $this->initSMWWriter(); } $properties = $this->m_properties; foreach ( $properties as $cur_prop ) { $propertystring = $cur_prop['p']; // TODO: Remove old code: // $property = SMWPropertyValue::makeUserProperty( $propertystring ); $property_di = SMWDIProperty::newFromUserLabel($propertystring); $valuestring = RDFIOUtils::sanitizeSMWValue( $cur_prop['v'] ); $value = SMWDataValueFactory::newPropertyObjectValue( $property_di, $valuestring ); $propertyErrorText = $property->getErrorText(); $propertyHasError = ( $propertyErrorText != '' ); if ( $propertyHasError ) { $this->addError( "<p>In RDFIOPageHandler::writeOrDeleteDataToWiki(): " . $property->getErrorText() . "</p>" ); } $valueErrorText = $value->getErrorText(); $valueHasError = ( $valueErrorText != '' ); if ( $valueHasError ) { $this->addError( "<p>Error creating property value object in RDFIOPageHandler::writeOrDeleteDataToWiki():</p><p>" . $value->getErrorText() . "</p>" ); } if ( $delete ) { $this->m_smwwriter_remove->addPropertyObjectValue( $property, $value ); $editmessage = "Deleting properties. Last property delete: " . $propertystring . " : " . $valuestring; } else { $this->m_smwwriter_add->addPropertyObjectValue( $property, $value ); $editmessage = "Importing properties. Last property added: " . $propertystring . " : " . $valuestring; } } $this->m_smwwriter->update( $this->m_smwwriter_remove, $this->m_smwwriter_add, $editmessage ); $smwWriterError = $this->m_smwwriter->getError(); $smwWriterHasError = ( $smwWriterError != '' ); if ( $smwWriterHasError ) { $this->addError( "<p>SMWWriter Error: " . $smwWriterError . "</p>" ); } }
/** * Get an array key => value of genealogical properties as SMWDIProperty * @throws MWException */ public static function getProperties() { static $properties; if ($properties !== null) { return $properties; } global $wgGenealogicalProperties, $wgOut; $properties = array(); if (!is_array($wgGenealogicalProperties)) { throw new MWException('Configuration variable $wgGenealogicalProperties must be an array !'); } foreach ($wgGenealogicalProperties as $key => $value) { if ($value) { $properties[$key] = SMWDIProperty::newFromUserLabel($value); } } return $properties; }
/** * Helper function to handle getPropertyValues() in both SMW 1.6 * and earlier versions. */ public static function getSMWPropertyValues($store, $subject, $propID, $requestOptions = null) { // SMWDIProperty was added in SMW 1.6 if (class_exists('SMWDIProperty')) { if (is_null($subject)) { $page = null; } else { $page = SMWDIWikiPage::newFromTitle($subject); } $property = SMWDIProperty::newFromUserLabel($propID); $res = $store->getPropertyValues($page, $property, $requestOptions); $values = array(); foreach ($res as $value) { if ($value instanceof SMWDIUri) { $values[] = $value->getURI(); } else { // getSortKey() seems to return the // correct value for all the other // data types. $values[] = str_replace('_', ' ', $value->getSortKey()); } } return $values; } else { $property = SMWPropertyValue::makeProperty($propID); $res = $store->getPropertyValues($subject, $property, $requestOptions); $values = array(); foreach ($res as $value) { if (method_exists($value, 'getTitle')) { $valueTitle = $value->getTitle(); if (!is_null($valueTitle)) { $values[] = $valueTitle->getText(); } } else { $values[] = str_replace('_', ' ', $value->getWikiValue()); } } return array_unique($values); } }
/** * Helper function to handle getPropertyValues(). */ public static function getSMWPropertyValues($store, $subject, $propID, $requestOptions = null) { if (is_null($subject)) { $page = null; } else { $page = SMWDIWikiPage::newFromTitle($subject); } $property = SMWDIProperty::newFromUserLabel($propID); $res = $store->getPropertyValues($page, $property, $requestOptions); $values = array(); foreach ($res as $value) { if ($value instanceof SMWDIUri) { $values[] = $value->getURI(); } else { // getSortKey() seems to return the // correct value for all the other // data types. $values[] = str_replace('_', ' ', $value->getSortKey()); } } return $values; }
/** * Helper function to handle getPropertyValues(). */ public static function getSMWPropertyValues($store, $subject, $propID, $requestOptions = null) { // If SMW is not installed, exit out. if (!class_exists('SMWDIWikiPage')) { return array(); } if (is_null($subject)) { $page = null; } else { $page = SMWDIWikiPage::newFromTitle($subject); } $property = SMWDIProperty::newFromUserLabel($propID); $res = $store->getPropertyValues($page, $property, $requestOptions); $values = array(); foreach ($res as $value) { if ($value instanceof SMWDIUri) { $values[] = $value->getURI(); } elseif ($value instanceof SMWDIWikiPage) { $realValue = str_replace('_', ' ', $value->getDBKey()); if ($value->getNamespace() != 0) { $realValue = MWNamespace::getCanonicalName($value->getNamespace()) . ":{$realValue}"; } $values[] = $realValue; } else { // getSortKey() seems to return the correct // value for all the other data types. $values[] = str_replace('_', ' ', $value->getSortKey()); } } return $values; }
/** * Return serialised results in specified format. */ protected function getResultText(SMWQueryResult $res, $outputmode) { if ($this->mTreeProp === null || $this->mTreeProp === '') { $res->addErrors(array(wfMessage('srf-noparentprop')->inContentLanguage()->text())); return ''; } $store = $res->getStore(); // first put everything in a list // elements appearing more than once will be inserted more than once, // but only one instance will be inserted with the hash // only this instance will be considered as a parent element in the tree $list = array(); while ($row = $res->getNext()) { $hash = $row[0]->getResultSubject()->getSerialization(); if (array_key_exists($hash, $list)) { $list[] = new SRFTreeElement($row); } else { $list[$hash] = new SRFTreeElement($row); } } // transfer the listelements into the tree // elements with more than one parent will be cloned for each parent $tree = array(); foreach ($list as $hash => $listElem) { $parents = $store->getPropertyValues($listElem->mRow[0]->getResultSubject(), SMWDIProperty::newFromUserLabel($this->mTreeProp)); // transfer element from list to tree foreach ($parents as $parent) { $parentPageHash = $parent->getSerialization(); if ($hash !== null) { if (array_key_exists($parentPageHash, $list)) { $listElem->mParent = $parentPageHash; } $tree[$hash] = $listElem; $hash = null; } else { $treeElem = clone $listElem; if (array_key_exists($parentPageHash, $list)) { $treeElem->mParent = $parentPageHash; } else { $treeElem->mParent = null; } $tree[] = $treeElem; } } } foreach ($tree as $hash => $value) { } // build pointers from parants to children foreach ($tree as $hash => $treeElem) { if ($treeElem->mParent != null) { $tree[$treeElem->mParent]->mChildren[] = $treeElem; } } // remove children from toplevel foreach ($tree as $hash => $treeElem) { if ($treeElem->mParent != null) { unset($tree[$hash]); } } $result = ''; $rownum = 0; foreach ($tree as $hash => $treeElem) { $this->printElement($result, $treeElem, $row); } return $result; }
static function getXMLForPage($title, $simplified_format, $groupings, $depth = 0) { if ($depth > 5) { return ""; } global $wgContLang, $dtgContLang; $namespace_labels = $wgContLang->getNamespaces(); $template_label = $namespace_labels[NS_TEMPLATE]; $namespace_str = str_replace(' ', '_', wfMsgForContent('dt_xml_namespace')); $page_str = str_replace(' ', '_', wfMsgForContent('dt_xml_page')); $field_str = str_replace(' ', '_', wfMsgForContent('dt_xml_field')); $name_str = str_replace(' ', '_', wfMsgForContent('dt_xml_name')); $title_str = str_replace(' ', '_', wfMsgForContent('dt_xml_title')); $id_str = str_replace(' ', '_', wfMsgForContent('dt_xml_id')); $free_text_str = str_replace(' ', '_', wfMsgForContent('dt_xml_freetext')); // if this page belongs to the exclusion category, exit $parent_categories = $title->getParentCategoryTree(array()); $dt_props = $dtgContLang->getPropertyLabels(); // $exclusion_category = $title->newFromText($dt_props[DT_SP_IS_EXCLUDED_FROM_XML], NS_CATEGORY); $exclusion_category = $wgContLang->getNSText(NS_CATEGORY) . ':' . str_replace(' ', '_', $dt_props[DT_SP_IS_EXCLUDED_FROM_XML]); if (self::treeContainsElement($parent_categories, $exclusion_category)) { return ""; } $article = new Article($title); $page_title = str_replace('"', '"', $title->getText()); $page_title = str_replace('&', '&', $page_title); $page_namespace = $title->getNamespace(); if ($simplified_format) { $text = "<{$page_str}><{$id_str}>{$article->getID()}</{$id_str}><{$title_str}>{$page_title}</{$title_str}>\n"; } else { $text = "<{$page_str} {$id_str}=\"" . $article->getID() . "\" {$title_str}=\"" . $page_title . '" >'; } // traverse the page contents, one character at a time $uncompleted_curly_brackets = 0; $page_contents = $article->getContent(); // escape out variables like "{{PAGENAME}}" $page_contents = str_replace('{{PAGENAME}}', '{{PAGENAME}}', $page_contents); // escape out parser functions $page_contents = preg_replace('/{{(#.+)}}/', '{{$1}}', $page_contents); // escape out transclusions $page_contents = preg_replace('/{{(:.+)}}/', '{{$1}}', $page_contents); // escape out variable names $page_contents = str_replace('{{{', '{{{', $page_contents); $page_contents = str_replace('}}}', '}}}', $page_contents); // escape out tables $page_contents = str_replace('{|', '{|', $page_contents); $page_contents = str_replace('|}', '|}', $page_contents); $free_text = ""; $free_text_id = 1; $template_name = ""; $field_name = ""; $field_value = ""; $field_has_name = false; for ($i = 0; $i < strlen($page_contents); $i++) { $c = $page_contents[$i]; if ($uncompleted_curly_brackets == 0) { if ($c == "{" || $i == strlen($page_contents) - 1) { if ($i == strlen($page_contents) - 1) { $free_text .= $c; } $uncompleted_curly_brackets++; $free_text = trim($free_text); $free_text = str_replace('&', '&', $free_text); $free_text = str_replace('[', '[', $free_text); $free_text = str_replace(']', ']', $free_text); $free_text = str_replace('<', '<', $free_text); $free_text = str_replace('>', '>', $free_text); if ($free_text != "") { $text .= "<{$free_text_str} id=\"{$free_text_id}\">{$free_text}</{$free_text_str}>"; $free_text = ""; $free_text_id++; } } elseif ($c == "{") { // do nothing } else { $free_text .= $c; } } elseif ($uncompleted_curly_brackets == 1) { if ($c == "{") { $uncompleted_curly_brackets++; $creating_template_name = true; } elseif ($c == "}") { $uncompleted_curly_brackets--; // is this needed? // if ($field_name != "") { // $field_name = ""; // } if ($page_contents[$i - 1] == '}') { if ($simplified_format) { $text .= "</" . $template_name . ">"; } else { $text .= "</{$template_label}>"; } } $template_name = ""; } } else { // 2 or greater - probably 2 if ($c == "}") { $uncompleted_curly_brackets--; } if ($c == "{") { $uncompleted_curly_brackets++; } else { if ($creating_template_name) { if ($c == "|" || $c == "}") { $template_name = str_replace(' ', '_', trim($template_name)); $template_name = str_replace('&', '&', $template_name); if ($simplified_format) { $text .= "<" . $template_name . ">"; } else { $text .= "<{$template_label} {$name_str}=\"{$template_name}\">"; } $creating_template_name = false; $creating_field_name = true; $field_id = 1; } else { $template_name .= $c; } } else { if ($c == "|" || $c == "}") { if ($field_has_name) { $field_value = str_replace('&', '&', $field_value); if ($simplified_format) { $field_name = str_replace(' ', '_', trim($field_name)); $text .= "<" . $field_name . ">"; $text .= trim($field_value); $text .= "</" . $field_name . ">"; } else { $text .= "<{$field_str} {$name_str}=\"" . trim($field_name) . "\">"; $text .= trim($field_value); $text .= "</{$field_str}>"; } $field_value = ""; $field_has_name = false; } else { // "field_name" is actually the value if ($simplified_format) { $field_name = str_replace(' ', '_', $field_name); // add "Field" to the beginning of the file name, since // XML tags that are simply numbers aren't allowed $text .= "<" . $field_str . '_' . $field_id . ">"; $text .= trim($field_name); $text .= "</" . $field_str . '_' . $field_id . ">"; } else { $text .= "<{$field_str} {$name_str}=\"{$field_id}\">"; $text .= trim($field_name); $text .= "</{$field_str}>"; } $field_id++; } $creating_field_name = true; $field_name = ""; } elseif ($c == "=") { // handle case of = in value if (!$creating_field_name) { $field_value .= $c; } else { $creating_field_name = false; $field_has_name = true; } } elseif ($creating_field_name) { $field_name .= $c; } else { $field_value .= $c; } } } } } // handle groupings, if any apply here; first check if SMW is installed global $smwgIP; if (isset($smwgIP)) { $store = smwfGetStore(); foreach ($groupings as $pair) { list($property_page, $grouping_label) = $pair; $options = new SMWRequestOptions(); $options->sort = "subject_title"; // get actual property from the wiki-page of the property if (class_exists('SMWDIProperty')) { $wiki_page = new SMWDIWikiPage($page_title, $page_namespace, null); $property = SMWDIProperty::newFromUserLabel($property_page->getTitle()->getText()); } else { $wiki_page = SMWDataValueFactory::newTypeIDValue('_wpg', $page_title); $property = SMWPropertyValue::makeProperty($property_page->getTitle()->getText()); } $res = $store->getPropertySubjects($property, $wiki_page, $options); $num = count($res); if ($num > 0) { $grouping_label = str_replace(' ', '_', $grouping_label); $text .= "<{$grouping_label}>\n"; foreach ($res as $subject) { $subject_title = $subject->getTitle(); $text .= self::getXMLForPage($subject_title, $simplified_format, $groupings, $depth + 1); } $text .= "</{$grouping_label}>\n"; } } } $text .= "</{$page_str}>\n"; // escape back the curly brackets that were escaped out at the beginning $text = str_replace('&#123;', '{', $text); $text = str_replace('&#125;', '}', $text); return $text; }
/** * * Similar to getSemanticData(), but in this case regarding the Semantic Web. * @param String $uri */ private function getSemanticWebData( $graph, $uri ) { // Several possibilities: URI with redirect to RDF, URL with RDFa (but talking about what?),... // $data is of type SMWSemanticData $semanticDataResult = new SMWSemanticData( $this->subject->getDataItem() ); // I want to show all incoming and outcoming links // ...ideally in the same style // Get the representation of the URI $theResource = $graph->resource( $uri ); // Outgoing $theOutgoingProperties = $graph->propertyUris( $theResource ); // for each, ask for the objects foreach ( $theOutgoingProperties as $outProp ) { $outPropResult = $this->getArrayObjects( $graph, $theResource, $outProp ); // now, we have the subject, the property, the object (uri/literal) foreach ( $outPropResult as $outPropObject ) { /* * The question now is, what kind of propert. * If there is a page in the wiki, we simply use it as property. * Otherwise, we need to invent a new page with the URI as name */ $uriPageName = $this->getInternalMapping( $outProp ); $dataProperty = null; if ( !isset( $uriPageName ) || $uriPageName == null) { // There is no, we create a new property page /* * TODO: maybe register new property type that can display the property more * conveniently, e.g., with browse further: smwInitProperties */ $dataProperty = SMWDIProperty::newFromUserLabel( $outProp ); } else { $dataProperty = SMWDIProperty::newFromUserLabel( $uriPageName ); } // SMWDataItem, we only distinguish uri and literal // TODO: Maybe distinguish more, later, e.g., language $dataValue = null; if ( $outPropObject["type"] == "uri" ) { /* * If there is a page in the wiki with the value as equivalent URI, we * just use this page. */ $uriPageName = $this->getInternalMapping( $outPropObject["value"] ); if ( !isset( $uriPageName ) && $uriPageName == null ) { // URI value $dataValue = SMWDataValueFactory::newTypeIDValue( '_rur', $outPropObject["value"], $property = $dataProperty ); } else { $dataValue = SMWDataValueFactory::newTypeIDValue( '_wpg', $uriPageName, $property = $dataProperty ); } } else { // literal $this->debug($outPropObject["value"],"vis:"); $dataValue = SMWDataValueFactory::newTypeIDValue( '_txt', $outPropObject["value"], $property = $dataProperty ); //$dataItem = new SMWDIString($outPropObject["value"]); } // some objects have invalid type and print warning triangle instead of object info //in this case object has class SMWDIError // in this case this object wouldn't be printed if( !( get_class( $dataValue->getDataItem() ) == "SMWDIError" ) ){ $semanticDataResult->addPropertyObjectValue( $dataProperty, $dataValue->getDataItem() ); } } } return $semanticDataResult; }
static function getXMLForPage($title, $simplified_format, $groupings, $depth = 0) { if ($depth > 5) { return ""; } global $wgContLang, $dtgContLang; // if this page belongs to the exclusion category, exit $parent_categories = $title->getParentCategoryTree(array()); $dt_props = $dtgContLang->getPropertyLabels(); // $exclusion_category = $title->newFromText($dt_props[DT_SP_IS_EXCLUDED_FROM_XML], NS_CATEGORY); $exclusion_category = $wgContLang->getNSText(NS_CATEGORY) . ':' . str_replace(' ', '_', $dt_props[DT_SP_IS_EXCLUDED_FROM_XML]); if (self::treeContainsElement($parent_categories, $exclusion_category)) { return ""; } $pageStructure = DTPageStructure::newFromTitle($title); $text = $pageStructure->toXML($simplified_format); // handle groupings, if any apply here; first check if SMW is installed global $smwgIP; if (isset($smwgIP)) { $store = smwfGetStore(); $page_title = $title->getText(); $page_namespace = $title->getNamespace(); // Escaping is needed for SMWSQLStore3 - this may be a bug in SMW. $escaped_page_title = str_replace(' ', '_', $page_title); foreach ($groupings as $pair) { list($property_page, $grouping_label) = $pair; $options = new SMWRequestOptions(); $options->sort = "subject_title"; // get actual property from the wiki-page of the property if (class_exists('SMWDIProperty')) { $wiki_page = new SMWDIWikiPage($escaped_page_title, $page_namespace, null); $property = SMWDIProperty::newFromUserLabel($property_page->getTitle()->getText()); } else { $wiki_page = SMWDataValueFactory::newTypeIDValue('_wpg', $escaped_page_title); $property = SMWPropertyValue::makeProperty($property_page->getTitle()->getText()); } $res = $store->getPropertySubjects($property, $wiki_page, $options); $num = count($res); if ($num > 0) { $grouping_label = str_replace(' ', '_', $grouping_label); $text .= "<{$grouping_label}>\n"; foreach ($res as $subject) { $subject_title = $subject->getTitle(); $text .= self::getXMLForPage($subject_title, $simplified_format, $groupings, $depth + 1); } $text .= "</{$grouping_label}>\n"; } } } // escape back the curly brackets that were escaped out at the beginning $text = str_replace('&#123;', '{', $text); $text = str_replace('&#125;', '}', $text); return $text; }
/** * Extended parsing function to first check whether value refers to pre-defined * property, resolve aliases, and set internal property id accordingly. * @todo Accept/enforce property namespace. */ protected function parseUserValue($value) { $this->mPropTypeValue = null; $this->m_wikipage = null; if ($this->m_caption === false) { // always use this as caption $this->m_caption = $value; } $propertyName = smwfNormalTitleText(ltrim(rtrim($value, ' ]'), ' [')); // slightly normalise label $inverse = false; if ($propertyName !== '' && $propertyName[0] == '-') { // property refers to an inverse $propertyName = smwfNormalTitleText((string) substr($value, 1)); /// NOTE The cast is necessary at least in PHP 5.3.3 to get string '' instead of boolean false. /// NOTE It is necessary to normalize again here, since normalization may uppercase the first letter. $inverse = true; } try { $this->m_dataitem = SMWDIProperty::newFromUserLabel($propertyName, $inverse, $this->m_typeid); } catch (SMWDataItemException $e) { // happens, e.g., when trying to sort queries by property "-" $this->addError(wfMessage('smw_noproperty', $value)->inContentLanguage()->text()); $this->m_dataitem = new SMWDIProperty('ERROR', false); // just to have something } }
/** * Return serialised results in specified format. */ protected function getResultText(SMWQueryResult $res, $outputmode) { if ($this->mTreeProp === '') { $res->addErrors(array(wfMessage('srf-noparentprop')->inContentLanguage()->text())); return ''; } $store = $res->getStore(); // put everything in a list and set parent hashes // elements appearing more than once will be inserted more than once and // elements with more than one parent will be cloned for each parent, // but only one instance will ever be inserted with the hash and // only this instance will later be considered as a parent element in the tree $tree = array(); while ($row = $res->getNext()) { $element = new SRFTreeElement($row); $hash = $row[0]->getResultSubject()->getSerialization(); if (array_key_exists($hash, $tree)) { $hash = null; } $parents = $store->getPropertyValues($element->mRow[0]->getResultSubject(), SMWDIProperty::newFromUserLabel($this->mTreeProp)); if (empty($parents)) { // no parents: copy into tree as root level item if ($hash !== null) { $tree[$hash] = $element; } else { $tree[] = $element; } } else { // one or more parents: copy one copy per parent into tree foreach ($parents as $parent) { if ($hash !== null) { $tree[$hash] = $element; $hash = null; } else { $element = clone $element; $tree[] = $element; } $element->mParent = $parent->getSerialization(); } } } $rootElements = array(); // build pointers from parents to children and remove pointers to parents that don't exist in the tree foreach ($tree as $hash => $element) { if ($element->mParent !== null) { if (array_key_exists($element->mParent, $tree)) { $tree[$element->mParent]->mChildren[] = $element; } else { $element->mParent = null; $rootElements[$hash] = $element; } } else { $rootElements[$hash] = $element; } } $result = ''; $rownum = 0; // if a specific page was specified as root element of the tree if ($this->mRoot !== '') { // get the title object of the root page $rootTitle = Title::newFromText($this->mRoot); if ($rootTitle === null) { $res->addErrors(array(wfMessage('srf-rootinvalid')->params($this->mRoot)->inContentLanguage()->text())); return ''; } $rootSerialization = SMWDIWikiPage::newFromTitle($rootTitle)->getSerialization(); // find the root page in the tree and print it and its subtree if (array_key_exists($rootSerialization, $tree)) { $this->printElement($result, $tree[$rootSerialization], $rownum, $this->mStartLevel); } } else { // iterate through all tree elements foreach ($rootElements as $hash => $element) { // print current root element and its subtree $this->printElement($result, $element, $rownum, $this->mStartLevel); } } return $result; }