/** * This method will be called before an article is displayed or previewed. * For display and preview we strip out the semantic properties and append them * at the end of the article. * * @param Parser $parser * @param string $text */ static public function onInternalParseBeforeLinks( &$parser, &$text ) { global $smwgStoreAnnotations, $smwgLinksInValues; SMWParseData::stripMagicWords( $text, $parser ); // Store the results if enabled (we have to parse them in any case, // in order to clean the wiki source for further processing). $smwgStoreAnnotations = smwfIsSemanticsProcessed( $parser->getTitle()->getNamespace() ); SMWParserExtensions::$mTempStoreAnnotations = true; // used for [[SMW::on]] and [[SMW:off]] // Process redirects, if any (it seems that there is indeed no more direct way of getting this info from MW) if ( $smwgStoreAnnotations ) { $rt = Title::newFromRedirect( $text ); if ( !is_null( $rt ) ) { $p = new SMWDIProperty( '_REDI' ); $di = SMWDIWikiPage::newFromTitle( $rt, '__red' ); SMWParseData::getSMWData( $parser )->addPropertyObjectValue( $p, $di ); } } // only used in subsequent callbacks, forgotten afterwards SMWParserExtensions::$mTempParser = $parser; // In the regexp matches below, leading ':' escapes the markup, as known for Categories. // Parse links to extract semantic properties. if ( $smwgLinksInValues ) { // More complex regexp -- lib PCRE may cause segfaults if text is long :-( $semanticLinkPattern = '/\[\[ # Beginning of the link (?:([^:][^]]*):[=:])+ # Property name (or a list of those) ( # After that: (?:[^|\[\]] # either normal text (without |, [ or ]) |\[\[[^]]*\]\] # or a [[link]] |\[[^]]*\] # or an [external link] )*) # all this zero or more times (?:\|([^]]*))? # Display text (like "text" in [[link|text]]), optional \]\] # End of link /xu'; $text = preg_replace_callback( $semanticLinkPattern, array( 'SMWParserExtensions', 'parsePropertiesCallback' ), $text ); } else { // Simpler regexps -- no segfaults found for those, but no links in values. $semanticLinkPattern = '/\[\[ # Beginning of the link (?:([^:][^]]*):[=:])+ # Property name (or a list of those) ([^\[\]]*) # content: anything but [, |, ] \]\] # End of link /xu'; $text = preg_replace_callback( $semanticLinkPattern, array( 'SMWParserExtensions', 'simpleParsePropertiesCallback' ), $text ); } // Add link to RDF to HTML header. // TODO: do escaping via Html or Xml class. SMWOutputs::requireHeadItem( 'smw_rdf', '<link rel="alternate" type="application/rdf+xml" title="' . htmlspecialchars( $parser->getTitle()->getPrefixedText() ) . '" href="' . htmlspecialchars( SpecialPage::getTitleFor( 'ExportRDF', $parser->getTitle()->getPrefixedText() )->getLocalUrl( 'xmlmime=rdf' ) ) . "\" />" ); SMWOutputs::commitToParser( $parser ); return true; // always return true, in order not to stop MW's hook processing! }
/** * Method for handling the subobject parser function. * * @since 1.7 * * @param Parser $parser */ public static function render(Parser &$parser) { self::$m_errors = array(); $params = func_get_args(); array_shift($params); // We already know the $parser ... $subobjectName = str_replace(' ', '_', trim(array_shift($params))); // For objects that don't come with there own idenifier, use a value // dependant md4 hash key if ($subobjectName === '' || $subobjectName === '-') { $subobjectName = '_' . hash('md4', implode('|', $params), false); } $mainSemanticData = SMWParseData::getSMWData($parser); $subject = $mainSemanticData->getSubject(); $diSubWikiPage = new SMWDIWikiPage($subject->getDBkey(), $subject->getNamespace(), $subject->getInterwiki(), $subobjectName); $semanticData = new SMWContainerSemanticData($diSubWikiPage); $previousPropertyDi = null; foreach ($params as $param) { $parts = explode('=', trim($param), 2); // Only add the property when there is both a name // and a non-empty value. if (count($parts) == 2 && $parts[1] != '') { $previousPropertyDi = self::addPropertyValueToSemanticData($parts[0], $parts[1], $semanticData); } elseif (count($parts) == 1 && !is_null($previousPropertyDi)) { self::addPropertyDiValueToSemanticData($previousPropertyDi, $parts[0], $semanticData); } else { //self::$m_errors[] = wfMessage( 'smw_noinvannot' )->inContentLanguage()->text(); } } $propertyDi = new SMWDIProperty('_SOBJ'); $subObjectDi = new SMWDIContainer($semanticData); SMWParseData::getSMWData($parser)->addPropertyObjectValue($propertyDi, $subObjectDi); return smwfEncodeMessages(self::$m_errors); }
/** * Method for handling the subobject parser function. * * @since 1.7 * * @param Parser $parser */ public static function render(Parser &$parser) { self::$m_errors = array(); $params = func_get_args(); array_shift($params); // We already know the $parser ... $subobjectName = str_replace(' ', '_', trim(array_shift($params))); $mainSemanticData = SMWParseData::getSMWData($parser); $subject = $mainSemanticData->getSubject(); $diSubWikiPage = new SMWDIWikiPage($subject->getDBkey(), $subject->getNamespace(), $subject->getInterwiki(), $subobjectName); $semanticData = new SMWContainerSemanticData($diSubWikiPage); foreach ($params as $param) { $parts = explode('=', trim($param), 2); // Only add the property when there is both a name // and a non-empty value. if (count($parts) == 2 && $parts[1] != '') { self::addPropertyValueToSemanticData($parts[0], $parts[1], $semanticData); } else { //self::$m_errors[] = wfMsgForContent( 'smw_noinvannot' ); } } $propertyDi = new SMWDIProperty('_SOBJ'); $subObjectDi = new SMWDIContainer($semanticData); SMWParseData::getSMWData($parser)->addPropertyObjectValue($propertyDi, $subObjectDi); return smwfEncodeMessages(self::$m_errors); }
/** * 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('>', '<'), 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('[', '[', $concept_text) . "</pre>\n</div>"; if (!is_null($wgTitle) && $wgTitle->isSpecialPage()) { global $wgOut; SMWOutputs::commitToOutputPage($wgOut); } else { SMWOutputs::commitToParser($parser); } return $result; }
/** * Add data about the query and its parameters to the semantic data of * the given parser. The $queryKey is a string key that uniquely * identifies the query; this is difficult to create in a stable way * from the processed query object and parameters, but easy to get from * the raw user input. * * @param string $queryKey * @param SMWQuery $query * @param array $params * @param Parser $parser * * @since 1.8 */ public static function addQueryData($queryKey, SMWQuery $query, array $params, Parser $parser) { $mainSemanticData = SMWParseData::getSMWData($parser); $subject = $mainSemanticData->getSubject(); $diSubWikiPage = new SMWDIWikiPage($subject->getDBkey(), $subject->getNamespace(), $subject->getInterwiki(), "_QUERY" . $queryKey); $semanticData = new SMWContainerSemanticData($diSubWikiPage); $description = $query->getDescription(); // Add query string $propertyDi = new SMWDIProperty('_ASKST'); $valueDi = new SMWDIBlob($description->getQueryString()); $semanticData->addPropertyObjectValue($propertyDi, $valueDi); // Add query size $propertyDi = new SMWDIProperty('_ASKSI'); $valueDi = new SMWDINumber($description->getSize()); $semanticData->addPropertyObjectValue($propertyDi, $valueDi); // Add query depth $propertyDi = new SMWDIProperty('_ASKDE'); $valueDi = new SMWDINumber($description->getDepth()); $semanticData->addPropertyObjectValue($propertyDi, $valueDi); // Add query format $propertyDi = new SMWDIProperty('_ASKFO'); $valueDi = new SMWDIString($params['format']->getValue()); $semanticData->addPropertyObjectValue($propertyDi, $valueDi); $propertyDi = new SMWDIProperty('_ASK'); $subObjectDi = new SMWDIContainer($semanticData); SMWParseData::getSMWData($parser)->addPropertyObjectValue($propertyDi, $subObjectDi); }