r41483 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r41482‎ | r41483 | r41484 >
Date:15:19, 1 October 2008
Author:mkroetzsch
Status:old
Tags:
Comment:
Moved all parsing-related functions of SMW into a single file for better maintenance and readbility
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Data.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_GlobalFunctions.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Hooks.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_ParserExtensions.php (added) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_TemplateDeclare.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Data.php
@@ -32,33 +32,4 @@
3333 return true;
3434 }
3535
36 -/**
37 - * Parser function for data, that enables to use a parser function for annotations.
38 - * Using the following syntax:
39 - * {{#set:
40 - * population = 13000
41 - * | area = 396 km²
42 - * | sea = Adria
43 - * }}
44 - * This creates annotations with the properties as stated on the left side, and the
45 - * values on the right side.
46 - *
47 - * @param[in] &$parser Parser The current parser
48 - * @return nothing
49 - */
50 -function smwfDataEntry_Render( &$parser ) {
51 - $params = func_get_args();
52 - array_shift( $params ); // we already know the $parser ...
53 - foreach ($params as $p)
54 - if (trim($p) != "") {
55 - $parts = explode("=", trim($p));
56 - if (count($parts)==2) {
57 - $property = $parts[0];
58 - $subject = $parts[1];
59 - // Adds the fact to the factbox, which may be problematic in case
60 - // the parser gets called several times...
61 - SMWParseData::addProperty( $property, $subject, false, $parser, true );
62 - }
63 - }
64 - return;
65 -}
 36+
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_ParserExtensions.php
@@ -0,0 +1,348 @@
 2+<?php
 3+/**
 4+ * This file contains essentially all SMW code that affects parsing by reading some
 5+ * special SMW syntax.
 6+ * @file
 7+ * @ingroup SMW
 8+ * @author Markus Krötzsch
 9+ * @author Denny Vrandecic
 10+ */
 11+
 12+/**
 13+ * Static class to collect all functions related to parsing wiki text in SMW.
 14+ * It includes all parser function declarations and hooks.
 15+ * @ingroup SMW
 16+ */
 17+class SMWParserExtensions {
 18+
 19+ /**
 20+ * This method will be called before an article is displayed or previewed.
 21+ * For display and preview we strip out the semantic properties and append them
 22+ * at the end of the article.
 23+ */
 24+ static public function onInternalParseBeforeLinks(&$parser, &$text) {
 25+ global $smwgStoreAnnotations, $smwgTempStoreAnnotations, $smwgLinksInValues, $smwgTempParser;
 26+ SMWParseData::stripMagicWords($text, $parser);
 27+ // store the results if enabled (we have to parse them in any case, in order to
 28+ // clean the wiki source for further processing)
 29+ $smwgStoreAnnotations = smwfIsSemanticsProcessed($parser->getTitle()->getNamespace());
 30+ $smwgTempStoreAnnotations = true; // used for [[SMW::on]] and [[SMW:off]]
 31+
 32+ // process redirects, if any
 33+ // (it seems that there is indeed no more direct way of getting this info from MW)
 34+ $rt = Title::newFromRedirect($text);
 35+ if ($rt !== NULL) {
 36+ $dv = SMWDataValueFactory::newSpecialValue(SMW_SP_REDIRECTS_TO,$rt->getPrefixedText());
 37+ if ($smwgStoreAnnotations) {
 38+ SMWParseData::getSMWData($parser)->addSpecialValue(SMW_SP_REDIRECTS_TO,$dv);
 39+ }
 40+ }
 41+
 42+ $smwgTempParser = $parser; // only used in subsequent callbacks, forgotten afterwards
 43+ // In the regexp matches below, leading ':' escapes the markup, as
 44+ // known for Categories.
 45+ // Parse links to extract semantic properties
 46+ if ($smwgLinksInValues) { // more complex regexp -- lib PCRE may cause segfaults if text is long :-(
 47+ $semanticLinkPattern = '/\[\[ # Beginning of the link
 48+ (?:([^:][^]]*):[=:])+ # Property name (or a list of those)
 49+ ( # After that:
 50+ (?:[^|\[\]] # either normal text (without |, [ or ])
 51+ |\[\[[^]]*\]\] # or a [[link]]
 52+ |\[[^]]*\] # or an [external link]
 53+ )*) # all this zero or more times
 54+ (?:\|([^]]*))? # Display text (like "text" in [[link|text]]), optional
 55+ \]\] # End of link
 56+ /xu';
 57+ $text = preg_replace_callback($semanticLinkPattern, 'SMWParserExtensions::parsePropertiesCallback', $text);
 58+ } else { // simpler regexps -- no segfaults found for those, but no links in values
 59+ $semanticLinkPattern = '/\[\[ # Beginning of the link
 60+ (?:([^:][^]]*):[=:])+ # Property name (or a list of those)
 61+ ([^\[\]]*) # content: anything but [, |, ]
 62+ \]\] # End of link
 63+ /xu';
 64+ $text = preg_replace_callback($semanticLinkPattern, 'SMWParserExtensions::simpleParsePropertiesCallback', $text);
 65+ }
 66+
 67+ // add link to RDF to HTML header
 68+ smwfRequireHeadItem('smw_rdf', '<link rel="alternate" type="application/rdf+xml" title="' .
 69+ $parser->getTitle()->getPrefixedText() . '" href="' .
 70+ htmlspecialchars($parser->getOptions()->getSkin()->makeSpecialUrl(
 71+ 'ExportRDF/' . $parser->getTitle()->getPrefixedText(), 'xmlmime=rdf'
 72+ )) . "\" />");
 73+
 74+ return true; // always return true, in order not to stop MW's hook processing!
 75+ }
 76+
 77+ /**
 78+ * This callback function strips out the semantic attributes from a wiki
 79+ * link. Expected parameter: array(linktext, properties, value|caption)
 80+ * This function is a preprocessing for smwfParsePropertiesCallback, and
 81+ * takes care of separating value and caption (instead of leaving this to
 82+ * a more complex regexp).
 83+ */
 84+ static public function simpleParsePropertiesCallback($semanticLink) {
 85+ $value = '';
 86+ $caption = false;
 87+ if (array_key_exists(2,$semanticLink)) {
 88+ $parts = explode('|',$semanticLink[2]);
 89+ if (array_key_exists(0,$parts)) {
 90+ $value = $parts[0];
 91+ }
 92+ if (array_key_exists(1,$parts)) {
 93+ $caption = $parts[1];
 94+ }
 95+ }
 96+ if ($caption !== false) {
 97+ return SMWParserExtensions::parsePropertiesCallback(array($semanticLink[0],$semanticLink[1],$value,$caption));
 98+ } else {
 99+ return SMWParserExtensions::parsePropertiesCallback(array($semanticLink[0],$semanticLink[1],$value));
 100+ }
 101+ }
 102+
 103+ /**
 104+ * This callback function strips out the semantic attributes from a wiki
 105+ * link. Expected parameter: array(linktext, properties, value, caption)
 106+ */
 107+ static public function parsePropertiesCallback($semanticLink) {
 108+ global $smwgInlineErrors, $smwgStoreAnnotations, $smwgTempStoreAnnotations, $smwgTempParser;
 109+ wfProfileIn("smwfParsePropertiesCallback (SMW)");
 110+ if (array_key_exists(1,$semanticLink)) {
 111+ $property = $semanticLink[1];
 112+ } else { $property = ''; }
 113+ if (array_key_exists(2,$semanticLink)) {
 114+ $value = $semanticLink[2];
 115+ } else { $value = ''; }
 116+
 117+ if ($property == 'SMW') {
 118+ switch ($value) {
 119+ case 'on': $smwgTempStoreAnnotations = true; break;
 120+ case 'off': $smwgTempStoreAnnotations = false; break;
 121+ }
 122+ wfProfileOut("smwfParsePropertiesCallback (SMW)");
 123+ return '';
 124+ }
 125+
 126+ if (array_key_exists(3,$semanticLink)) {
 127+ $valueCaption = $semanticLink[3];
 128+ } else { $valueCaption = false; }
 129+
 130+ //extract annotations and create tooltip
 131+ $properties = preg_split('/:[=:]/u', $property);
 132+ foreach($properties as $singleprop) {
 133+ $dv = SMWParseData::addProperty($singleprop,$value,$valueCaption, $smwgTempParser, $smwgStoreAnnotations && $smwgTempStoreAnnotations);
 134+ }
 135+ $result = $dv->getShortWikitext(true);
 136+ if ( ($smwgInlineErrors && $smwgStoreAnnotations && $smwgTempStoreAnnotations) && (!$dv->isValid()) ) {
 137+ $result .= $dv->getErrorText();
 138+ }
 139+ wfProfileOut("smwfParsePropertiesCallback (SMW)");
 140+ return $result;
 141+ }
 142+
 143+ /**
 144+ * This hook registers parser functions and hooks to the given parser. It is
 145+ * called during SMW initialisation. Note that parser hooks are something different
 146+ * than MW hooks in general, which explains the two-level registration.
 147+ */
 148+ public static function registerParserFunctions(&$parser) {
 149+ $parser->setHook( 'ask', 'SMWParserExtensions::doAskHook' );
 150+ $parser->setFunctionHook( 'ask', 'SMWParserExtensions::doAsk' );
 151+ $parser->setFunctionHook( 'show', 'SMWParserExtensions::doShow' );
 152+ $parser->setFunctionHook( 'info', 'SMWParserExtensions::doInfo' );
 153+ $parser->setFunctionHook( 'concept', 'SMWParserExtensions::doConcept' );
 154+ $parser->setFunctionHook( 'set', 'SMWParserExtensions::doConcept' );
 155+ $parser->setFunctionHook( 'declare', 'SMWParserExtensions::doDeclare', SFH_OBJECT_ARGS );
 156+ return true; // always return true, in order not to stop MW's hook processing!
 157+ }
 158+
 159+ /**
 160+ * Function for handling the {{\#ask }} parser function. It triggers the execution of inline
 161+ * query processing and checks whether (further) inline queries are allowed.
 162+ */
 163+ static public function doAsk(&$parser) {
 164+ global $smwgQEnabled, $smwgIQRunningNumber;
 165+ if ($smwgQEnabled) {
 166+ $smwgIQRunningNumber++;
 167+ $params = func_get_args();
 168+ array_shift( $params ); // we already know the $parser ...
 169+ return SMWQueryProcessor::getResultFromFunctionParams($params,SMW_OUTPUT_WIKI);
 170+ } else {
 171+ wfLoadExtensionMessages('SemanticMediaWiki');
 172+ return smwfEncodeMessages(array(wfMsgForContent('smw_iq_disabled')));
 173+ }
 174+ }
 175+
 176+ /**
 177+ * The \<ask\> parser hook processing part. This has been replaced by the
 178+ * parser function \#ask and should no longer be used.
 179+ */
 180+ static public function doAskHook($querytext, $params, &$parser) {
 181+ global $smwgQEnabled, $smwgIQRunningNumber;
 182+ if ($smwgQEnabled) {
 183+ $smwgIQRunningNumber++;
 184+ return SMWQueryProcessor::getResultFromHookParams($querytext,$params,SMW_OUTPUT_HTML);
 185+ } else {
 186+ wfLoadExtensionMessages('SemanticMediaWiki');
 187+ return smwfEncodeMessages(array(wfMsgForContent('smw_iq_disabled')));
 188+ }
 189+ }
 190+
 191+ /**
 192+ * Function for handling the {{\#show }} parser function. The \#show function is
 193+ * similar to \#ask but merely prints some property value for a specified page.
 194+ */
 195+ static public function doShow(&$parser) {
 196+ global $smwgQEnabled, $smwgIQRunningNumber;
 197+ if ($smwgQEnabled) {
 198+ $smwgIQRunningNumber++;
 199+ $params = func_get_args();
 200+ array_shift( $params ); // we already know the $parser ...
 201+ return SMWQueryProcessor::getResultFromFunctionParams($params,SMW_OUTPUT_WIKI,SMWQueryProcessor::INLINE_QUERY,true);
 202+ } else {
 203+ wfLoadExtensionMessages('SemanticMediaWiki');
 204+ return smwfEncodeMessages(array(wfMsgForContent('smw_iq_disabled')));
 205+ }
 206+ }
 207+
 208+ /**
 209+ * Function for handling the {{\#concept }} parser function. This parser function provides a special input
 210+ * facility for defining concepts, and it displays the resulting concept description.
 211+ */
 212+ static public function doConcept(&$parser) {
 213+ global $smwgQDefaultNamespaces, $smwgQMaxSize, $smwgQMaxDepth, $smwgPreviousConcept, $wgContLang;
 214+ wfLoadExtensionMessages('SemanticMediaWiki');
 215+ // The global $smwgConceptText is used to pass information to the MW hooks for storing it,
 216+ // $smwgPreviousConcept is used to detect if we already have a concept defined for this page.
 217+ $title = $parser->getTitle();
 218+ if ($title->getNamespace() != SMW_NS_CONCEPT) {
 219+ return smwfEncodeMessages(array(wfMsgForContent('smw_no_concept_namespace')));
 220+ } elseif (isset($smwgPreviousConcept) && ($smwgPreviousConcept == $title->getText())) {
 221+ return smwfEncodeMessages(array(wfMsgForContent('smw_multiple_concepts')));
 222+ }
 223+ $smwgPreviousConcept = $title->getText();
 224+
 225+ // process input:
 226+ $params = func_get_args();
 227+ array_shift( $params ); // we already know the $parser ...
 228+ $concept_input = str_replace(array('&gt;','&lt;'),array('>','<'),array_shift( $params )); // use first parameter as concept (query) string
 229+ /// NOTE: the str_replace above is required in MediaWiki 1.11, but not in MediaWiki 1.14
 230+ $query = SMWQueryProcessor::createQuery($concept_input, array('limit' => 20, 'format' => 'list'), SMWQueryProcessor::CONCEPT_DESC);
 231+ $concept_text = $query->getDescription()->getQueryString();
 232+ $concept_docu = array_shift( $params ); // second parameter, if any, might be a description
 233+
 234+ $dv = SMWDataValueFactory::newSpecialValue(SMW_SP_CONCEPT_DESC);
 235+ $dv->setValues($concept_text, $concept_docu, $query->getDescription()->getQueryFeatures(), $query->getDescription()->getSize(), $query->getDescription()->getDepth());
 236+ if (SMWParseData::getSMWData($parser) !== NULL) {
 237+ SMWParseData::getSMWData($parser)->addSpecialValue(SMW_SP_CONCEPT_DESC,$dv);
 238+ }
 239+
 240+ // display concept box:
 241+ $rdflink = SMWInfolink::newInternalLink(wfMsgForContent('smw_viewasrdf'), $wgContLang->getNsText(NS_SPECIAL) . ':ExportRDF/' . $title->getPrefixedText(), 'rdflink');
 242+ smwfRequireHeadItem(SMW_HEADER_STYLE);
 243+
 244+ $result = '<div class="smwfact"><span class="smwfactboxhead">' . wfMsgForContent('smw_concept_description',$title->getText()) .
 245+ (count($query->getErrors())>0?' ' . smwfEncodeMessages($query->getErrors()):'') .
 246+ '</span>' . '<span class="smwrdflink">' . $rdflink->getWikiText() . '</span>' . '<br />' .
 247+ ($concept_docu?"<p>$concept_docu</p>":'') .
 248+ '<pre>' . str_replace('[', '&#x005B;', $concept_text) . "</pre>\n</div>";
 249+ return $result;
 250+ }
 251+
 252+ /**
 253+ * Function for handling the {{\#info }} parser function. This function creates a tooltip like
 254+ * the one used by SMW for giving hints.
 255+ * @note This feature is at risk and may vanish or change in future versions.
 256+ */
 257+ static public function doInfo(&$parser) {
 258+ $params = func_get_args();
 259+ array_shift( $params ); // we already know the $parser ...
 260+ $content = array_shift( $params ); // use only first parameter, ignore rest (may get meaning later)
 261+ return smwfEncodeMessages(array($content), 'info');
 262+ }
 263+
 264+ /**
 265+ * Function for handling the {{\#set }} parser function. This is used for adding annotations
 266+ * silently.
 267+ *
 268+ * Usage:
 269+ * {{\#set:
 270+ * population = 13000
 271+ * | area = 396 km²
 272+ * | sea = Adria
 273+ * }}
 274+ * This creates annotations with the properties as stated on the left side, and the
 275+ * values on the right side.
 276+ *
 277+ * @param[in] &$parser Parser The current parser
 278+ * @return nothing
 279+ */
 280+ static public function doSet( &$parser ) {
 281+ $params = func_get_args();
 282+ array_shift( $params ); // we already know the $parser ...
 283+ foreach ($params as $p)
 284+ if (trim($p) != "") {
 285+ $parts = explode("=", trim($p));
 286+ if (count($parts)==2) {
 287+ $property = $parts[0];
 288+ $subject = $parts[1];
 289+ SMWParseData::addProperty( $property, $subject, false, $parser, true );
 290+ }
 291+ }
 292+ return;
 293+ }
 294+
 295+ /**
 296+ * Function for handling the {{\#declare }} parser function. It is used for declaring template parameters
 297+ * that should automagically be annotated when the template is used.
 298+ *
 299+ * Usage:
 300+ * {{\#declare:Author=Author\#list|Publisher=editor}}
 301+ */
 302+ static public function doDeclare( Parser &$parser, PPFrame $frame, $args ) {
 303+ if ($frame->isTemplate()) {
 304+ foreach ($args as $arg)
 305+ if (trim($arg) != "") {
 306+ $expanded = trim( $frame->expand( $arg ));
 307+ $parts = explode("=", $expanded, 2);
 308+ if (count($parts)==1) {
 309+ $propertystring = $expanded;
 310+ $argumentname = $expanded;
 311+ } else {
 312+ $propertystring = $parts[0];
 313+ $argumentname = $parts[1];
 314+ }
 315+ $property = Title::newFromText( $propertystring, SMW_NS_PROPERTY );
 316+ //if ($property == null) continue;
 317+ $argument = $frame->getArgument($argumentname);
 318+ $valuestring = $frame->expand($argument);
 319+ if ($property != null) {
 320+ $type = SMWDataValueFactory::getPropertyObjectTypeID($property);
 321+ if ($type == "_wpg") {
 322+ $matches = array();
 323+ preg_match_all("/\[\[([^\[\]]*)\]\]/", $valuestring, $matches);
 324+ $objects = $matches[1];
 325+ if (count($objects) == 0) {
 326+ if (trim($valuestring) != '') {
 327+ SMWParseData::addProperty( $propertystring, $valuestring, false, $parser, true );
 328+ }
 329+ } else {
 330+ foreach ($objects as $object) {
 331+ SMWParseData::addProperty( $propertystring, $object, false, $parser, true );
 332+ }
 333+ }
 334+ } else {
 335+ if (trim($valuestring) != '') {
 336+ SMWParseData::addProperty( $propertystring, $valuestring, false, $parser, true );
 337+ }
 338+ }
 339+ $value = SMWDataValueFactory::newPropertyObjectValue($property, $valuestring);
 340+ //if (!$value->isValid()) continue;
 341+ }
 342+ }
 343+ } else {
 344+ // @todo Save as metadata
 345+ }
 346+ return;
 347+ }
 348+
 349+}
\ No newline at end of file
Property changes on: trunk/extensions/SemanticMediaWiki/includes/SMW_ParserExtensions.php
___________________________________________________________________
Added: svn:eol-style
1350 + native
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Hooks.php
@@ -10,134 +10,4 @@
1111
1212 //// Parsing annotations
1313
14 -/**
15 -* This method will be called before an article is displayed or previewed.
16 -* For display and preview we strip out the semantic properties and append them
17 -* at the end of the article.
18 -*/
19 -function smwfParserHook(&$parser, &$text) {
20 - global $smwgStoreAnnotations, $smwgTempStoreAnnotations, $smwgLinksInValues, $smwgTempParser;
21 - SMWParseData::stripMagicWords($text, $parser);
22 - // store the results if enabled (we have to parse them in any case, in order to
23 - // clean the wiki source for further processing)
24 - $smwgStoreAnnotations = smwfIsSemanticsProcessed($parser->getTitle()->getNamespace());
25 - $smwgTempStoreAnnotations = true; // used for [[SMW::on]] and [[SMW:off]]
2614
27 - // process redirects, if any
28 - // (it seems that there is indeed no more direct way of getting this info from MW)
29 - $rt = Title::newFromRedirect($text);
30 - if ($rt !== NULL) {
31 - $dv = SMWDataValueFactory::newSpecialValue(SMW_SP_REDIRECTS_TO,$rt->getPrefixedText());
32 - if ($smwgStoreAnnotations) {
33 - SMWParseData::getSMWData($parser)->addSpecialValue(SMW_SP_REDIRECTS_TO,$dv);
34 - }
35 - }
36 -
37 - $smwgTempParser = $parser; // only used in subsequent callbacks, forgotten afterwards
38 - // In the regexp matches below, leading ':' escapes the markup, as
39 - // known for Categories.
40 - // Parse links to extract semantic properties
41 - if ($smwgLinksInValues) { // more complex regexp -- lib PCRE may cause segfaults if text is long :-(
42 - $semanticLinkPattern = '/\[\[ # Beginning of the link
43 - (?:([^:][^]]*):[=:])+ # Property name (or a list of those)
44 - ( # After that:
45 - (?:[^|\[\]] # either normal text (without |, [ or ])
46 - |\[\[[^]]*\]\] # or a [[link]]
47 - |\[[^]]*\] # or an [external link]
48 - )*) # all this zero or more times
49 - (?:\|([^]]*))? # Display text (like "text" in [[link|text]]), optional
50 - \]\] # End of link
51 - /xu';
52 - $text = preg_replace_callback($semanticLinkPattern, 'smwfParsePropertiesCallback', $text);
53 - } else { // simpler regexps -- no segfaults found for those, but no links in values
54 - $semanticLinkPattern = '/\[\[ # Beginning of the link
55 - (?:([^:][^]]*):[=:])+ # Property name (or a list of those)
56 - ([^\[\]]*) # content: anything but [, |, ]
57 - \]\] # End of link
58 - /xu';
59 - $text = preg_replace_callback($semanticLinkPattern, 'smwfSimpleParsePropertiesCallback', $text);
60 - }
61 -// SMWFactbox::printFactbox($text, SMWParseData::getSMWData($parser));
62 -
63 - // add link to RDF to HTML header
64 - smwfRequireHeadItem('smw_rdf', '<link rel="alternate" type="application/rdf+xml" title="' .
65 - $parser->getTitle()->getPrefixedText() . '" href="' .
66 - htmlspecialchars($parser->getOptions()->getSkin()->makeSpecialUrl(
67 - 'ExportRDF/' . $parser->getTitle()->getPrefixedText(), 'xmlmime=rdf'
68 - )) . "\" />");
69 -
70 - return true; // always return true, in order not to stop MW's hook processing!
71 -}
72 -
73 -/**
74 -* This callback function strips out the semantic attributes from a wiki
75 -* link. Expected parameter: array(linktext, properties, value|caption)
76 -* This function is a preprocessing for smwfParsePropertiesCallback, and
77 -* takes care of separating value and caption (instead of leaving this to
78 -* a more complex regexp).
79 -*/
80 -function smwfSimpleParsePropertiesCallback($semanticLink) {
81 - $value = '';
82 - $caption = false;
83 - if (array_key_exists(2,$semanticLink)) {
84 - $parts = explode('|',$semanticLink[2]);
85 - if (array_key_exists(0,$parts)) {
86 - $value = $parts[0];
87 - }
88 - if (array_key_exists(1,$parts)) {
89 - $caption = $parts[1];
90 - }
91 - }
92 - if ($caption !== false) {
93 - return smwfParsePropertiesCallback(array($semanticLink[0],$semanticLink[1],$value,$caption));
94 - } else {
95 - return smwfParsePropertiesCallback(array($semanticLink[0],$semanticLink[1],$value));
96 - }
97 -}
98 -
99 -/**
100 -* This callback function strips out the semantic attributes from a wiki
101 -* link. Expected parameter: array(linktext, properties, value, caption)
102 -*/
103 -function smwfParsePropertiesCallback($semanticLink) {
104 - global $smwgInlineErrors, $smwgStoreAnnotations, $smwgTempStoreAnnotations, $smwgTempParser;
105 - wfProfileIn("smwfParsePropertiesCallback (SMW)");
106 - if (array_key_exists(1,$semanticLink)) {
107 - $property = $semanticLink[1];
108 - } else { $property = ''; }
109 - if (array_key_exists(2,$semanticLink)) {
110 - $value = $semanticLink[2];
111 - } else { $value = ''; }
112 -
113 - if ($property == 'SMW') {
114 - switch ($value) {
115 - case 'on': $smwgTempStoreAnnotations = true; break;
116 - case 'off': $smwgTempStoreAnnotations = false; break;
117 - }
118 - wfProfileOut("smwfParsePropertiesCallback (SMW)");
119 - return '';
120 - }
121 -
122 - if (array_key_exists(3,$semanticLink)) {
123 - $valueCaption = $semanticLink[3];
124 - } else { $valueCaption = false; }
125 -
126 - //extract annotations and create tooltip
127 - $properties = preg_split('/:[=:]/u', $property);
128 - foreach($properties as $singleprop) {
129 - $dv = SMWParseData::addProperty($singleprop,$value,$valueCaption, $smwgTempParser, $smwgStoreAnnotations && $smwgTempStoreAnnotations);
130 - }
131 - $result = $dv->getShortWikitext(true);
132 - if ( ($smwgInlineErrors && $smwgStoreAnnotations && $smwgTempStoreAnnotations) && (!$dv->isValid()) ) {
133 - $result .= $dv->getErrorText();
134 - }
135 - wfProfileOut("smwfParsePropertiesCallback (SMW)");
136 - return $result;
137 -}
138 -
139 -// Special display for certain types of pages
140 -
141 -
142 -
143 -
144 -
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_GlobalFunctions.php
@@ -109,6 +109,7 @@
110110 ///// Set up autoloading
111111 ///// All classes registered for autoloading here should be tagged with this information:
112112 ///// Add "@note AUTOLOADED" to their class documentation. This avoids useless includes.
 113+ $wgAutoloadClasses['SMWParserExtensions'] = $smwgIP . '/includes/SMW_ParserExtensions.php';
113114 $wgAutoloadClasses['SMWInfolink'] = $smwgIP . '/includes/SMW_Infolink.php';
114115 $wgAutoloadClasses['SMWFactbox'] = $smwgIP . '/includes/SMW_Factbox.php';
115116 $wgAutoloadClasses['SMWParseData'] = $smwgIP . '/includes/SMW_ParseData.php';
@@ -241,10 +242,7 @@
242243 require_once($smwgIP . '/includes/SMW_Hooks.php');
243244 require_once($smwgIP . '/includes/SMW_RefreshTab.php');
244245
245 - $wgHooks['InternalParseBeforeLinks'][] = 'smwfParserHook'; // parse annotations in [[link syntax]]
246 - $wgHooks['ParserAfterTidy'][] = 'smwfParserAfterTidy'; // add items to HTML header during parsing
247 - $wgHooks['BeforePageDisplay'][]='smwfAddHTMLHeadersOutput'; // add items to HTML header during output
248 -// $wgHooks['ArticleSave'][] = 'smwfPreSaveHook'; // check some settings here
 246+ $wgHooks['InternalParseBeforeLinks'][] = 'SMWParserExtensions::onInternalParseBeforeLinks'; // parse annotations in [[link syntax]]
249247 $wgHooks['ArticleDelete'][] = 'SMWParseData::onArticleDelete'; // delete annotations
250248 $wgHooks['TitleMoveComplete'][]='SMWParseData::onTitleMoveComplete'; // move annotations
251249 $wgHooks['LinksUpdateConstructed'][] = 'SMWParseData::onLinksUpdateConstructed'; // update data after template change and at safe
@@ -252,15 +250,17 @@
253251 $wgHooks['SkinAfterContent'][] = 'SMWFactbox::onSkinAfterContent'; // draw Factbox below categories
254252 // $wgHooks['OutputPageBeforeHTML'][] = 'SMWFactbox::onOutputPageBeforeHTML';// draw Factbox right below page content
255253
 254+ $wgHooks['ParserAfterTidy'][] = 'smwfParserAfterTidy'; // add items to HTML header during parsing
 255+ $wgHooks['BeforePageDisplay'][]='smwfAddHTMLHeadersOutput'; // add items to HTML header during output
256256 $wgHooks['ArticleFromTitle'][] = 'smwfShowListPage'; // special implementations for property/type articles
257257
258258 if( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
259 - $wgHooks['ParserFirstCallInit'][] = 'smwfRegisterParserFunctions';
 259+ $wgHooks['ParserFirstCallInit'][] = 'SMWParserExtensions::registerParserFunctions';
260260 } else {
261261 if ( class_exists( 'StubObject' ) && !StubObject::isRealObject( $wgParser ) ) {
262262 $wgParser->_unstub();
263263 }
264 - smwfRegisterParserFunctions( $wgParser );
 264+ SMWParserExtensions::registerParserFunctions( $wgParser );
265265 }
266266 if ($smwgToolboxBrowseLink) {
267267 if (version_compare($wgVersion,'1.13','>')) {
@@ -294,119 +294,6 @@
295295 }
296296
297297 /**
298 - * This hook registers parser functions and hooks to the given parser.
299 - * Note that parser hooks are something different than MW hooks
300 - * in general, which explains the two-level registration.
301 - */
302 -function smwfRegisterParserFunctions( &$parser) {
303 - $parser->setHook( 'ask', 'smwfProcessInlineQuery' );
304 - $parser->setFunctionHook( 'ask', 'smwfProcessInlineQueryParserFunction' );
305 - $parser->setFunctionHook( 'show', 'smwfProcessShowParserFunction' );
306 - $parser->setFunctionHook( 'info', 'smwfProcessInfoParserFunction' );
307 - $parser->setFunctionHook( 'concept', 'smwfProcessConceptParserFunction' );
308 - return true; // always return true, in order not to stop MW's hook processing!
309 -}
310 -
311 -/**
312 - * The <ask> parser hook processing part.
313 - */
314 -function smwfProcessInlineQuery($querytext, $params, &$parser) {
315 - global $smwgQEnabled, $smwgIQRunningNumber;
316 - if ($smwgQEnabled) {
317 - $smwgIQRunningNumber++;
318 - return SMWQueryProcessor::getResultFromHookParams($querytext,$params,SMW_OUTPUT_HTML);
319 - } else {
320 - wfLoadExtensionMessages('SemanticMediaWiki');
321 - return smwfEncodeMessages(array(wfMsgForContent('smw_iq_disabled')));
322 - }
323 -}
324 -
325 -/**
326 - * The {{#ask }} parser function processing part.
327 - */
328 -function smwfProcessInlineQueryParserFunction(&$parser) {
329 - global $smwgQEnabled, $smwgIQRunningNumber;
330 - if ($smwgQEnabled) {
331 - $smwgIQRunningNumber++;
332 - $params = func_get_args();
333 - array_shift( $params ); // we already know the $parser ...
334 - return SMWQueryProcessor::getResultFromFunctionParams($params,SMW_OUTPUT_WIKI);
335 - } else {
336 - wfLoadExtensionMessages('SemanticMediaWiki');
337 - return smwfEncodeMessages(array(wfMsgForContent('smw_iq_disabled')));
338 - }
339 -}
340 -
341 -/**
342 - * The {{#show }} parser function processing part.
343 - */
344 -function smwfProcessShowParserFunction(&$parser) {
345 - global $smwgQEnabled, $smwgIQRunningNumber;
346 - if ($smwgQEnabled) {
347 - $smwgIQRunningNumber++;
348 - $params = func_get_args();
349 - array_shift( $params ); // we already know the $parser ...
350 - return SMWQueryProcessor::getResultFromFunctionParams($params,SMW_OUTPUT_WIKI,SMWQueryProcessor::INLINE_QUERY,true);
351 - } else {
352 - wfLoadExtensionMessages('SemanticMediaWiki');
353 - return smwfEncodeMessages(array(wfMsgForContent('smw_iq_disabled')));
354 - }
355 -}
356 -
357 -/**
358 - * The {{#concept }} parser function processing part.
359 - */
360 -function smwfProcessConceptParserFunction(&$parser) {
361 - global $smwgQDefaultNamespaces, $smwgQMaxSize, $smwgQMaxDepth, $smwgPreviousConcept, $wgContLang;
362 - wfLoadExtensionMessages('SemanticMediaWiki');
363 - // The global $smwgConceptText is used to pass information to the MW hooks for storing it,
364 - // $smwgPreviousConcept is used to detect if we already have a concept defined for this page.
365 - $title = $parser->getTitle();
366 - if ($title->getNamespace() != SMW_NS_CONCEPT) {
367 - return smwfEncodeMessages(array(wfMsgForContent('smw_no_concept_namespace')));
368 - } elseif (isset($smwgPreviousConcept) && ($smwgPreviousConcept == $title->getText())) {
369 - return smwfEncodeMessages(array(wfMsgForContent('smw_multiple_concepts')));
370 - }
371 - $smwgPreviousConcept = $title->getText();
372 -
373 - // process input:
374 - $params = func_get_args();
375 - array_shift( $params ); // we already know the $parser ...
376 - $concept_input = str_replace(array('&gt;','&lt;'),array('>','<'),array_shift( $params )); // use first parameter as concept (query) string
377 - /// NOTE: the str_replace above is required in MediaWiki 1.11, but not in MediaWiki 1.14
378 - $query = SMWQueryProcessor::createQuery($concept_input, array('limit' => 20, 'format' => 'list'), SMWQueryProcessor::CONCEPT_DESC);
379 - $concept_text = $query->getDescription()->getQueryString();
380 - $concept_docu = array_shift( $params ); // second parameter, if any, might be a description
381 -
382 - $dv = SMWDataValueFactory::newSpecialValue(SMW_SP_CONCEPT_DESC);
383 - $dv->setValues($concept_text, $concept_docu, $query->getDescription()->getQueryFeatures(), $query->getDescription()->getSize(), $query->getDescription()->getDepth());
384 - if (SMWParseData::getSMWData($parser) !== NULL) {
385 - SMWParseData::getSMWData($parser)->addSpecialValue(SMW_SP_CONCEPT_DESC,$dv);
386 - }
387 -
388 - // display concept box:
389 - $rdflink = SMWInfolink::newInternalLink(wfMsgForContent('smw_viewasrdf'), $wgContLang->getNsText(NS_SPECIAL) . ':ExportRDF/' . $title->getPrefixedText(), 'rdflink');
390 - smwfRequireHeadItem(SMW_HEADER_STYLE);
391 -
392 - $result = '<div class="smwfact"><span class="smwfactboxhead">' . wfMsgForContent('smw_concept_description',$title->getText()) .
393 - (count($query->getErrors())>0?' ' . smwfEncodeMessages($query->getErrors()):'') .
394 - '</span>' . '<span class="smwrdflink">' . $rdflink->getWikiText() . '</span>' . '<br />' .
395 - ($concept_docu?"<p>$concept_docu</p>":'') .
396 - '<pre>' . str_replace('[', '&#x005B;', $concept_text) . "</pre>\n</div>";
397 - return $result;
398 -}
399 -
400 -/**
401 - * The {{#info }} parser function processing part.
402 - */
403 -function smwfProcessInfoParserFunction(&$parser) {
404 - $params = func_get_args();
405 - array_shift( $params ); // we already know the $parser ...
406 - $content = array_shift( $params ); // use only first parameter, ignore rest (may get meaning later)
407 - return smwfEncodeMessages(array($content), 'info');
408 -}
409 -
410 -/**
411298 * Add a link to the toobox to view the properties of the current page in Special:Browse.
412299 * The links has the CSS id "t-smwbrowselink" so that it can be skinned or hidden with all
413300 * standard mechanisms (also by individual users with custom CSS).
@@ -579,10 +466,12 @@
580467 * Set up (possibly localised) names for SMW's parser functions.
581468 */
582469 function smwfAddMagicWords(&$magicWords, $langCode) {
583 - $magicWords['ask'] = array( 0, 'ask' );
584 - $magicWords['show'] = array( 0, 'show' );
585 - $magicWords['info'] = array( 0, 'info' );
 470+ $magicWords['ask'] = array( 0, 'ask' );
 471+ $magicWords['show'] = array( 0, 'show' );
 472+ $magicWords['info'] = array( 0, 'info' );
586473 $magicWords['concept'] = array( 0, 'concept' );
 474+ $magicWords['set'] = array( 0, 'set' );
 475+ $magicWords['declare'] = array( 0, 'declare' );
587476 $magicWords['SMW_NOFACTBOX'] = array( 0, '__NOFACTBOX__' );
588477 $magicWords['SMW_SHOWFACTBOX'] = array( 0, '__SHOWFACTBOX__' );
589478 return true;
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_TemplateDeclare.php
@@ -35,54 +35,4 @@
3636 return true;
3737 }
3838
39 -function smwfTemplateDeclare_Render( Parser &$parser, PPFrame $frame, $args ) {
40 - if ($frame->isTemplate()) {
4139
42 - foreach ($args as $arg)
43 - if (trim($arg) != "") {
44 - $expanded = trim( $frame->expand( $arg ));
45 - $parts = explode("=", $expanded, 2);
46 - if (count($parts)==1) {
47 - $propertystring = $expanded;
48 - $argumentname = $expanded;
49 - } else {
50 - $propertystring = $parts[0];
51 - $argumentname = $parts[1];
52 - }
53 - $property = Title::newFromText( $propertystring, SMW_NS_PROPERTY );
54 - //if ($property == null) continue;
55 - $argument = $frame->getArgument($argumentname);
56 - $valuestring = $frame->expand($argument);
57 - if ($property != null) {
58 - $type = SMWDataValueFactory::getPropertyObjectTypeID($property);
59 - if ($type == "_wpg") {
60 - $matches = array();
61 - preg_match_all("/\[\[([^\[\]]*)\]\]/", $valuestring, $matches);
62 - $objects = $matches[1];
63 - if (count($objects) == 0) {
64 - if (trim($valuestring) != '') {
65 - SMWParseData::addProperty( $propertystring, $valuestring, false, $parser, true );
66 - }
67 - } else {
68 - foreach ($objects as $object) {
69 - SMWParseData::addProperty( $propertystring, $object, false, $parser, true );
70 - }
71 - }
72 - } else {
73 - if (trim($valuestring) != '') {
74 - SMWParseData::addProperty( $propertystring, $valuestring, false, $parser, true );
75 - }
76 - }
77 - $value = SMWDataValueFactory::newPropertyObjectValue($property, $valuestring);
78 - //if (!$value->isValid()) continue;
79 - }
80 - }
81 -
82 - } else {
83 -
84 - // @todo Save as metadata
85 -
86 - }
87 -
88 - return;
89 -}

Status & tagging log