Esempio n. 1
0
 /**
  * Method for handling the ask concept function.
  * 
  * @todo The possible use of this in an HTML or Specal page context needs to be revisited. The code mentions it, but can this actually happen?
  * @todo The escaping of symbols in concept queries needs to be revisited.
  * 
  * @since 1.5.3
  * 
  * @param Parser $parser
  */
 public static function render(Parser &$parser)
 {
     global $wgContLang, $wgTitle;
     $title = $parser->getTitle();
     $pconc = new SMWDIProperty('_CONC');
     if ($title->getNamespace() != SMW_NS_CONCEPT) {
         $result = smwfEncodeMessages(array(wfMsgForContent('smw_no_concept_namespace')));
         SMWOutputs::commitToParser($parser);
         return $result;
     } elseif (count(SMWParseData::getSMWdata($parser)->getPropertyValues($pconc)) > 0) {
         $result = smwfEncodeMessages(array(wfMsgForContent('smw_multiple_concepts')));
         SMWOutputs::commitToParser($parser);
         return $result;
     }
     // process input:
     $params = func_get_args();
     array_shift($params);
     // We already know the $parser ...
     // Use first parameter as concept (query) string.
     $concept_input = str_replace(array('&gt;', '&lt;'), array('>', '<'), array_shift($params));
     // second parameter, if any, might be a description
     $concept_docu = array_shift($params);
     // NOTE: the str_replace above is required in MediaWiki 1.11, but not in MediaWiki 1.14
     $query = SMWQueryProcessor::createQuery($concept_input, SMWQueryProcessor::getProcessedParams(array('limit' => 20, 'format' => 'list')), SMWQueryProcessor::CONCEPT_DESC);
     $concept_text = $query->getDescription()->getQueryString();
     if (!is_null(SMWParseData::getSMWData($parser))) {
         $diConcept = new SMWDIConcept($concept_text, $concept_docu, $query->getDescription()->getQueryFeatures(), $query->getDescription()->getSize(), $query->getDescription()->getDepth());
         SMWParseData::getSMWData($parser)->addPropertyObjectValue($pconc, $diConcept);
     }
     // display concept box:
     $rdflink = SMWInfolink::newInternalLink(wfMsgForContent('smw_viewasrdf'), $wgContLang->getNsText(NS_SPECIAL) . ':ExportRDF/' . $title->getPrefixedText(), 'rdflink');
     SMWOutputs::requireResource('ext.smw.style');
     // TODO: escape output, preferably via Html or Xml class.
     $result = '<div class="smwfact"><span class="smwfactboxhead">' . wfMsgForContent('smw_concept_description', $title->getText()) . (count($query->getErrors()) > 0 ? ' ' . smwfEncodeMessages($query->getErrors()) : '') . '</span>' . '<span class="smwrdflink">' . $rdflink->getWikiText() . '</span>' . '<br />' . ($concept_docu ? "<p>{$concept_docu}</p>" : '') . '<pre>' . str_replace('[', '&#x005B;', $concept_text) . "</pre>\n</div>";
     if (!is_null($wgTitle) && $wgTitle->isSpecialPage()) {
         global $wgOut;
         SMWOutputs::commitToOutputPage($wgOut);
     } else {
         SMWOutputs::commitToParser($parser);
     }
     return $result;
 }
	static function getOutputData( $title, $rid = 0 ) {
		// normal page
		global $wgTitle, $wgUser;
		$wgTitle = $title;
		$revision = Revision::newFromTitle( $title, $rid );
		if ( $revision === NULL ) {
			throw new MWException( __METHOD__ . ": Page not exist '{$page_name} ({$rid})'" );
		}
		$text = $revision->getText();
		$wom = WOMProcessor::parseToWOM( $text );

		global $wgOMOutputHookedParserFunctions;
		$pfs = $wom->getObjectsByTypeID( WOM_TYPE_PARSERFUNCTION );
		foreach ( $pfs as $id => $obj ) {
			self::removeSubWOMIds( $obj );
			foreach ( $wgOMOutputHookedParserFunctions as $function_key ) {
				if ( $obj->getFunctionKey() == $function_key ) {
					// add wom id to parser function, with specified parameter 'wom_id'
					$param = new WOMParameterModel( 'wom_id' );
					$pv = new WOMParamValueModel();
					$param->insertObject( $pv );
					$pv->insertObject( new WOMTextModel( $id ) );
					$obj->insertObject( $param );
				}
			}
		}
		// FIXME: template values may have object ids
		$tmpls = $wom->getObjectsByTypeID( WOM_TYPE_TEMPLATE );
		foreach ( $tmpls as $id => $obj ) {
			self::removeSubWOMIds( $obj );
		}

		global $wgParser, $wgOut, $wgWOMOutputHooked;
		$wgWOMOutputHooked = true;
		$options = ParserOptions::newFromUser( $wgUser );

		self::$queryProps = array();
		self::$queryId = 1;
		$wgParser->parse( $wom->getWikiText(), $title, $options );

		$output = SMWParseData::getSMWdata( $wgParser );

		if ( !isset( $output ) ) {
			$semdata = smwfGetStore()->getSemanticData( $title );
		} else {
			$semdata = $output;
		}

		$tmp_id = 0;
		// fill in semantic properties
		$properties = array();
		foreach ( $semdata->getProperties() as $property ) {
			$label = '';
			if ( !$property->isShown() ) { // showing this is not desired, hide
				continue;
			} elseif ( $property->isUserDefined() ) { // user defined property
				if ( version_compare ( SMW_VERSION, '1.6', '>=' ) ) {
					$label = $property->getLabel();
				} else {
					$property->setCaption( preg_replace( '/[ ]/u', '&nbsp;', $property->getWikiValue(), 2 ) );
					// / NOTE: the preg_replace is a slight hack to ensure that the left column does not get too narrow
					$label = $property->getWikiValue();
				}
			} else {
				if ( version_compare ( SMW_VERSION, '1.6', '>=' ) ) {
					$label = $property->getLabel();
				} else {
					if ( $property->isVisible() ) { // predefined property
						$label = $property->getWikiValue();
					} else { // predefined, internal property
						continue;
					}
				}
			}
			$properties[$label] = array();

			$propvalues = $semdata->getPropertyValues( $property );
			foreach ( $propvalues as $propvalue ) {
				if ( version_compare ( SMW_VERSION, '1.6', '>=' ) ) {
					if ( $propvalue->getDIType() == SMWDataItem::TYPE_ERROR ) continue;
					$properties[$label][$propvalue->getSerialization()] = false;
				} else {
					$properties[$label][$propvalue->getWikiValue()] = false;
				}
			}
		}
		$props = $wom->getObjectsByTypeID( WOM_TYPE_PROPERTY );
		foreach ( $props as $prop ) {
			$id = $prop->getObjectID();
			if ( $id == '' ) continue;
			$name = $prop->getPropertyName();
			$value = $prop->getPropertyValue();
			if ( !isset( $properties[$name][$value] ) ) {
				// remove category not match to output
				$wom->removePageObject( $id );
			} else {
				$properties[$name][$value] = true;
			}
		}
		foreach ( $properties as $name => $values ) {
			foreach ( $values as $val => $flag ) {
				if ( $flag !== true ) {
					$p = new WOMPropertyModel( $name, $val );
					$p->setObjectID( 'output' . ( $tmp_id ++ ) );
					$wom->insertObject( $p );
					$wom->addToPageObjectSet( $p );
				}
			}
		}

		// fill in ask query results
		foreach ( self::$queryProps as $wom_id => $queries ) {
			$query_res = new WOMQueryResult();
			if ( $wom_id != '' ) {
				$parent = $wom->getObject( $wom_id );
			} else {
				$parent = new WOMParserFunctionModel( 'ask' );
				$parent->setObjectID( 'output' . ( $tmp_id ++ ) );
				$wom->insertObject( $parent );
				$wom->addToPageObjectSet( $parent );
			}
			$parent->insertObject( $query_res );
			$query_res->setObjectID( 'output' . ( $tmp_id ++ ) );
			$wom->addToPageObjectSet( $query_res );
			foreach ( $queries as $label => $vals ) {
				$vals = array_unique( $vals );
				foreach ( $vals as $val ) {
					$query_res->insertObject( new WOMPropertyModel( $label, $val ) );
				}
			}
		}

		// fill in categories
		$categories = $title->getParentCategories();
		$cates = $wom->getObjectsByTypeID( WOM_TYPE_CATEGORY );
		foreach ( $cates as $cate ) {
			$id = $cate->getObjectID();
			if ( $id == '' ) continue;
			$name = Title::newFromText( $cate->getName(), NS_CATEGORY )->getFullText();
			if ( !isset( $categories[$name] ) ) {
				// remove category not match to output
				$wom->removePageObject( $id );
			} else {
				$categories[$name] = true;
			}
		}
		foreach ( $categories as $cate => $flag ) {
			if ( $flag !== true ) {
				$c = new WOMCategoryModel( Title::newFromText( $cate )->getText() );
				$c->setObjectID( 'output' . ( $tmp_id ++ ) );
				$wom->insertObject( $c );
				$wom->addToPageObjectSet( $c );
			}
		}

		return $wom;
	}