r37104 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r37103‎ | r37104 | r37105 >
Date:14:08, 5 July 2008
Author:mkroetzsch
Status:old
Tags:
Comment:
Change in data storage during parsing: static data container initisliased earlier (so it can be used in parser
functions too), mechanism to prevent mutiple inits, mechanism to prevent mutliple factboxes to be printed (does
not fix the issue with <gallery> being treated like a standalone pages).
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Factbox.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_SemanticData.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Hooks.php
@@ -14,8 +14,6 @@
1515 */
1616 function smwfParserHook(&$parser, &$text) {
1717 global $smwgStoreAnnotations, $smwgTempStoreAnnotations, $smwgStoreActive;
18 - // Init global storage for semantic data of this article.
19 - SMWFactbox::initStorage($parser->getTitle());
2018
2119 // store the results if enabled (we have to parse them in any case, in order to
2220 // clean the wiki source for further processing)
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Factbox.php
@@ -19,29 +19,40 @@
2020 * The actual container for the semantic annotations. Public, since
2121 * it is ref-passed to others for further processing.
2222 */
23 - static $semdata;
 23+ static $semdata = NULL;
2424 /**
2525 * True if the respective article is newly created. This affects some
2626 * storage operations.
2727 */
2828 static protected $m_new = false;
 29+ /**
 30+ * True if Factbox was printed, our best attempt at reliably preventing multiple
 31+ * Factboxes to appear on one page.
 32+ */
 33+ static protected $m_printed = false;
2934
3035 /**
3136 * Initialisation method. Must be called before anything else happens.
3237 */
3338 static function initStorage($title) {
34 - SMWFactbox::$semdata = new SMWSemanticData($title); // reset data
35 - ///TODO: is this (global) reset safe when cloned subparses happen? May kill unsafed data.
 39+ // reset only if title is new
 40+ if ( (SMWFactbox::$semdata === NULL) ||
 41+ (SMWFactbox::$semdata->getSubject()->getText() != $title->getText()) ||
 42+ (SMWFactbox::$semdata->getSubject()->getNamespace() != $title->getNamespace()) ) {
 43+ SMWFactbox::$semdata = new SMWSemanticData($title); // reset data
 44+ SMWFactbox::$m_printed = false;
 45+ }
3646 //SMWFactbox::$m_new = false; // do not reset, keep (order of hooks can be strange ...)
3747 }
3848
3949 /**
40 - * Clear all stored data
 50+ * Clear all stored data.
4151 */
4252 static function clearStorage() {
4353 global $smwgStoreActive;
4454 if ($smwgStoreActive) {
4555 SMWFactbox::$semdata->clear();
 56+ SMWFactbox::$m_printed = false;
4657 }
4758 }
4859
@@ -200,7 +211,9 @@
201212 static function printFactbox(&$text) {
202213 global $wgContLang, $wgServer, $smwgShowFactbox, $smwgShowFactboxEdit, $smwgStoreActive, $smwgIP, $wgRequest;
203214 if (!$smwgStoreActive) return;
 215+ if (SMWFactbox::$m_printed) return;
204216 wfProfileIn("SMWFactbox::printFactbox (SMW)");
 217+ SMWFactbox::$m_printed = true;
205218
206219 // Global settings:
207220 if ( $wgRequest->getCheck('wpPreview') ) {
@@ -223,13 +236,13 @@
224237 wfProfileOut("SMWFactbox::printFactbox (SMW)");
225238 return;
226239 case SMW_FACTBOX_SPECIAL: // only when there are special properties
227 - if ( !SMWFactbox::$semdata->hasSpecialProperties() ) {
 240+ if ( !SMWFactbox::$semdata->hasVisibleSpecialProperties() ) {
228241 wfProfileOut("SMWFactbox::printFactbox (SMW)");
229242 return;
230243 }
231244 break;
232245 case SMW_FACTBOX_NONEMPTY: // only when non-empty
233 - if ( (!SMWFactbox::$semdata->hasProperties()) && (!SMWFactbox::$semdata->hasSpecialProperties()) ) {
 246+ if ( (!SMWFactbox::$semdata->hasProperties()) && (!SMWFactbox::$semdata->hasVisibleSpecialProperties()) ) {
234247 wfProfileOut("SMWFactbox::printFactbox (SMW)");
235248 return;
236249 }
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_SemanticData.php
@@ -18,6 +18,7 @@
1919 protected $attribtitles = array(); // text keys and title objects
2020 protected $hasprops = false; // any normal properties yet?
2121 protected $hasspecs = false; // any special properties yet?
 22+ protected $hasvisiblespecs = false; // any displayable special properties yet? (some are internal only withot a display name)
2223 protected $m_noduplicates; // avoid repeated values?
2324 /// NOTE: not needing (e.g. when loading from store) can safe much time,
2425 /// since objects can remain stubs until someone really acesses their value
@@ -74,6 +75,14 @@
7576 }
7677
7778 /**
 79+ * Return true if there are any special properties that can
 80+ * be displayed.
 81+ */
 82+ public function hasVisibleSpecialProperties() {
 83+ return $this->hasvisiblespecs;
 84+ }
 85+
 86+ /**
7887 * Store a value for an property identified by its title object. Duplicate
7988 * value entries are ignored.
8089 */
@@ -119,6 +128,8 @@
120129 $property = $smwgContLang->findSpecialPropertyLabel($special);
121130 if ($property === false) {
122131 $property = '_' . $special;
 132+ } else {
 133+ $this->hasvisiblespecs = true;
123134 }
124135 if (!array_key_exists($property, $this->attribvals)) {
125136 $this->attribvals[$property] = array();
@@ -147,6 +158,7 @@
148159 $this->attribtitles = array();
149160 $this->hasprops = false;
150161 $this->hasspecs = false;
 162+ $this->hasvisiblespecs = false;
151163 }
152164
153165 }
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_GlobalFunctions.php
@@ -246,11 +246,16 @@
247247 * in general, which explains the two-level registration.
248248 */
249249 function smwfRegisterInlineQueries( &$parser, &$text, &$stripstate ) {
250 - $parser->setHook( 'ask', 'smwfProcessInlineQuery' );
251 - $parser->setFunctionHook( 'ask', 'smwfProcessInlineQueryParserFunction' );
252 - $parser->setFunctionHook( 'show', 'smwfProcessShowParserFunction' );
253 - $parser->setFunctionHook( 'info', 'smwfProcessInfoParserFunction' );
254 - $parser->setFunctionHook( 'concept', 'smwfProcessConceptParserFunction' );
 250+ SMWFactbox::initStorage($parser->getTitle());
 251+
 252+ $oldhook = $parser->setFunctionHook( 'ask', 'smwfProcessInlineQueryParserFunction' );
 253+ if ($oldhook != 'smwfProcessInlineQueryParserFunction') {
 254+ $parser->setHook( 'ask', 'smwfProcessInlineQuery' );
 255+ $parser->setFunctionHook( 'ask', 'smwfProcessInlineQueryParserFunction' );
 256+ $parser->setFunctionHook( 'show', 'smwfProcessShowParserFunction' );
 257+ $parser->setFunctionHook( 'info', 'smwfProcessInfoParserFunction' );
 258+ $parser->setFunctionHook( 'concept', 'smwfProcessConceptParserFunction' );
 259+ }
255260 return true; // always return true, in order not to stop MW's hook processing!
256261 }
257262
@@ -301,7 +306,7 @@
302307 * The {{#concept }} parser function processing part.
303308 */
304309 function smwfProcessConceptParserFunction(&$parser) {
305 - global $smwgQDefaultNamespaces, $smwgQMaxSize, $smwgQMaxDepth, $smwgPreviousConcept, $smwgConceptText;
 310+ global $smwgQDefaultNamespaces, $smwgQMaxSize, $smwgQMaxDepth, $smwgPreviousConcept;
306311 // The global $smwgConceptText is used to pass information to the MW hooks for storing it,
307312 // $smwgPreviousConcept is used to detect if we already have a concept defined for this page.
308313 $title = $parser->getTitle();
@@ -311,21 +316,25 @@
312317 return smwfEncodeMessages(array(wfMsgForContent('smw_multiple_concepts')));
313318 }
314319 $smwgPreviousConcept = $title->getText();
 320+
 321+ // process input:
315322 $params = func_get_args();
316323 array_shift( $params ); // we already know the $parser ...
317324 $concept_input = array_shift( $params ); // use only first parameter, ignore rest (may get meaning later)
318325 $query = SMWQueryProcessor::createQuery($concept_input, array('limit' => -1), SMWQueryProcessor::CONCEPT_DESC);
319 - $smwgConceptText = $query->getDescription()->getQueryString();
 326+ $conceptText = $query->getDescription()->getQueryString();
 327+ $dv = SMWDataValueFactory::newSpecialValue(SMW_SP_CONCEPT_DESC, $conceptText);
 328+ SMWFactbox::$semdata->addSpecialValue(SMW_SP_CONCEPT_DESC,$dv);
320329
 330+ // display concept box:
321331 $qresult = smwfGetStore()->getQueryResult($query);
322332 $printer = new SMWListResultPrinter('list',true);
323333 $resultlink = $printer->getResult($qresult, array('searchlabel' => wfMsgForContent('smw_concept_preview')), SMW_OUTPUT_WIKI);
324 -
325334 smwfRequireHeadItem(SMW_HEADER_STYLE);
326335 $result = '<div class="smwfact"><span class="smwfactboxhead">' . wfMsgForContent('smw_concept_description',$title->getText()) .
327336 //(count($query->getErrors())>0?' ' . smwfEncodeMessages($query->getErrors()):'') . // errors are shown by $resultlink anyway
328337 '</span> &nbsp;&nbsp;&nbsp;' . $resultlink . '<br/>' .
329 - '<pre>' . str_replace('[', '&#x005B;', $smwgConceptText) . '</pre></div>';
 338+ '<pre>' . str_replace('[', '&#x005B;', $conceptText) . '</pre></div>';
330339 return $result;
331340 }
332341
@@ -387,10 +396,9 @@
388397 * output. This is our preferred method of working off the required scripts, since it
389398 * exploits parser caching.
390399 * (2) Fetch category information from parser output.
391 - * (3) Store concept descriptions for concept pages.
392400 */
393401 function smwfParserAfterTidy(&$parser, &$text) {
394 - global $smwgHeadItems, $smwgStoreActive, $smwgConceptText;
 402+ global $smwgHeadItems, $smwgStoreActive;
395403 // make HTML header
396404 if (!$smwgStoreActive) return true; // avoid doing this in SMW-generated sub-parsers
397405 foreach ($smwgHeadItems as $key => $item) {
@@ -407,12 +415,6 @@
408416 SMWFactbox::$semdata->addSpecialValue(SMW_SP_SUBCLASS_OF,$dv);
409417 }
410418 }
411 - // store concept descriptions
412 - if (isset($smwgConceptText) && ($smwgConceptText != '') ) { // no check for Concept namespace here, was done earlier
413 - $dv = SMWDataValueFactory::newSpecialValue(SMW_SP_CONCEPT_DESC, $smwgConceptText);
414 - SMWFactbox::$semdata->addSpecialValue(SMW_SP_CONCEPT_DESC,$dv);
415 - $smwgConceptText = '';
416 - }
417419 return true;
418420 }
419421

Status & tagging log