r87951 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87950‎ | r87951 | r87952 >
Date:08:12, 13 May 2011
Author:mkroetzsch
Status:resolved (Comments)
Tags:
Comment:
moved stubbing features of SMWSemanticData container to a new subclass, since the code is specific to SQL backends, and not needed in other places that use such data
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_SemanticData.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SqlStubSemanticData.php (added) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SqlStubSemanticData.php
@@ -0,0 +1,165 @@
 2+<?php
 3+/**
 4+ * The class in this file provides a subclass of SMWSemanticData that can store
 5+ * prefetched values from SMW's SQL stores, and unstub this data on demand when
 6+ * it is accessed.
 7+ *
 8+ * @file
 9+ * @ingroup SMWStore
 10+ *
 11+ * @author Markus Krötzsch
 12+ */
 13+
 14+/**
 15+ * This class provides a subclass of SMWSemanticData that can store
 16+ * prefetched values from SMW's SQL stores, and unstub this data on demand when
 17+ * it is accessed.
 18+ *
 19+ * @ingroup SMWStore
 20+ */
 21+class SMWSqlStubSemanticData extends SMWSemanticData {
 22+
 23+ /**
 24+ * Stub property data that is not part of $mPropVals and $mProperties
 25+ * yet. Entries use property keys as keys. The value is an array of
 26+ * DBkey-arrays that define individual datavalues. The stubs will be
 27+ * set up when first accessed.
 28+ *
 29+ * @var array
 30+ */
 31+ protected $mStubPropVals = array();
 32+
 33+ /**
 34+ * SMWDIWikiPage object that is the subject of this container.
 35+ * Subjects that are null are used to represent "internal objects"
 36+ * only.
 37+ *
 38+ * @var SMWDIWikiPage
 39+ */
 40+ protected $mSubject;
 41+
 42+ /**
 43+ * Get the array of all properties that have stored values.
 44+ *
 45+ * @return array of SMWDIProperty objects
 46+ */
 47+ public function getProperties() {
 48+ $this->unstubProperties();
 49+ return parent::getProperties();
 50+ }
 51+
 52+ /**
 53+ * Get the array of all stored values for some property.
 54+ *
 55+ * @param $property SMWDIProperty
 56+ * @return array of SMWDataItem
 57+ */
 58+ public function getPropertyValues( SMWDIProperty $property ) {
 59+ if ( $property->isInverse() ) { // we never have any data for inverses
 60+ return array();
 61+ }
 62+
 63+ if ( array_key_exists( $property->getKey(), $this->mStubPropVals ) ) {
 64+ $this->unstubProperty( $property->getKey(), $property );
 65+
 66+ foreach ( $this->mStubPropVals[$property->getKey()] as $dbkeys ) {
 67+ try {
 68+ $di = SMWCompatibilityHelpers::dataItemFromDBKeys( $property->findPropertyTypeID(), $dbkeys );
 69+ if ( $this->mNoDuplicates ) {
 70+ $this->mPropVals[$property->getKey()][$di->getHash()] = $di;
 71+ } else {
 72+ $this->mPropVals[$property->getKey()][] = $di;
 73+ }
 74+ } catch ( SMWDataItemException $e ) {
 75+ // ignore data
 76+ }
 77+ }
 78+
 79+ unset( $this->mStubPropVals[$property->getKey()] );
 80+ }
 81+
 82+ return parent::getPropertyValues( $property );
 83+ }
 84+
 85+ /**
 86+ * Return true if there are any visible properties.
 87+ *
 88+ * @return boolean
 89+ */
 90+ public function hasVisibleProperties() {
 91+ $this->unstubProperties();
 92+ return parent::hasVisibleProperties();
 93+ }
 94+
 95+ /**
 96+ * Return true if there are any special properties that can
 97+ * be displayed.
 98+ *
 99+ * @return boolean
 100+ */
 101+ public function hasVisibleSpecialProperties() {
 102+ $this->unstubProperties();
 103+ return parent::hasVisibleSpecialProperties();
 104+ }
 105+
 106+ /**
 107+ * Add data in abbreviated form so that it is only expanded if needed. The property key
 108+ * is the DB key (string) of a property value, whereas valuekeys is an array of DBkeys for
 109+ * the added value that will be used to initialize the value if needed at some point.
 110+ *
 111+ * @param $propertyKey string
 112+ * @param $valueKeys array
 113+ */
 114+ public function addPropertyStubValue( $propertyKey, array $valueKeys ) {
 115+ $this->mStubPropVals[$propertyKey][] = $valueKeys;
 116+ }
 117+
 118+ /**
 119+ * Delete all data other than the subject.
 120+ */
 121+ public function clear() {
 122+ $this->mStubPropVals = array();
 123+ parent::clear();
 124+ }
 125+
 126+ /**
 127+ * Process all mProperties that have been added as stubs.
 128+ * Associated data may remain in stub form.
 129+ */
 130+ protected function unstubProperties() {
 131+ foreach ( $this->mStubPropVals as $pkey => $values ) { // unstub property values only, the value lists are still kept as stubs
 132+ $this->unstubProperty( $pkey );
 133+ }
 134+ }
 135+
 136+ /**
 137+ * Unstub a single property from the stub data array. If available, an
 138+ * existing object for that property might be provided, so we do not
 139+ * need to make a new one. It is not checked if the object matches the
 140+ * property name.
 141+ *
 142+ * @param $propertyKey string
 143+ * @param $diProperty SMWDIProperty
 144+ */
 145+ protected function unstubProperty( $propertyKey, $diProperty = null ) {
 146+ if ( !array_key_exists( $propertyKey, $this->mProperties ) ) {
 147+ if ( $diProperty === null ) {
 148+ //$propertyDV = SMWPropertyValue::makeProperty( $propertyKey );
 149+ //$diProperty = $propertyDV->getDataItem();
 150+ $diProperty = new SMWDIProperty( $propertyKey, false );
 151+ }
 152+
 153+ $this->mProperties[$propertyKey] = $diProperty;
 154+
 155+ if ( !$diProperty->isUserDefined() ) {
 156+ if ( $diProperty->isShown() ) {
 157+ $this->mHasVisibleSpecs = true;
 158+ $this->mHasVisibleProps = true;
 159+ }
 160+ } else {
 161+ $this->mHasVisibleProps = true;
 162+ }
 163+ }
 164+ }
 165+
 166+}
\ No newline at end of file
Property changes on: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SqlStubSemanticData.php
___________________________________________________________________
Added: svn:eol-style
1167 + native
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2.php
@@ -163,7 +163,7 @@
164164
165165 // *** Prepare the cache ***//
166166 if ( !array_key_exists( $sid, $this->m_semdata ) ) { // new cache entry
167 - $this->m_semdata[$sid] = new SMWSemanticData( $subject, false );
 167+ $this->m_semdata[$sid] = new SMWSqlStubSemanticData( $subject, false );
168168 $this->m_semdata[$sid]->addPropertyStubValue( '_SKEY', array( $sortkey ) );
169169 $this->m_sdstate[$sid] = array( '__key' );
170170 }
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_SemanticData.php
@@ -56,16 +56,6 @@
5757 protected $mProperties = array();
5858
5959 /**
60 - * Stub property data that is not part of $mPropVals and $mProperties
61 - * yet. Entries use property keys as keys. The value is an array of
62 - * DBkey-arrays that define individual datavalues. The stubs will be
63 - * set up when first accessed.
64 - *
65 - * @var array
66 - */
67 - protected $mStubPropVals = array();
68 -
69 - /**
7060 * States whether the container holds any normal properties.
7161 *
7262 * @var boolean
@@ -84,9 +74,9 @@
8575
8676 /**
8777 * States whether repeated values should be avoided. Not needing
88 - * duplicate elimination (e.g. when loading from store) can save much
89 - * time, since objects can remain stubs until someone really acesses
90 - * their value.
 78+ * duplicate elimination (e.g. when loading from store) can save some
 79+ * time, especially in subclasses like SMWSqlStubSemanticData, where
 80+ * the first access to a data item is more costy.
9181 *
9282 * @note This setting is merely for optimization. The SMW data model
9383 * never cares about the multiplicity of identical data assignments.
@@ -107,8 +97,8 @@
10898 /**
10999 * Constructor.
110100 *
111 - * @param $subject SMWDIWikiPage to which this data refers
112 - * @param $noDuplicates boolean stating if duplicate data should be avoided
 101+ * @param SMWDIWikiPage $subject to which this data refers
 102+ * @param boolean $noDuplicates stating if duplicate data should be avoided
113103 */
114104 public function __construct( SMWDIWikiPage $subject, $noDuplicates = true ) {
115105 $this->clear();
@@ -117,13 +107,18 @@
118108 }
119109
120110 /**
121 - * This object is added to the parser output of MediaWiki, but it is not useful to have all its data as part of the parser cache
122 - * since the data is already stored in more accessible format in SMW. Hence this implementation of __sleep() makes sure only the
123 - * subject is serialised, yielding a minimal stub data container after unserialisation. This is a little safer than serialising
124 - * nothing: if, for any reason, SMW should ever access an unserialised parser output, then the Semdata container will at least
125 - * look as if properly initialised (though empty).
 111+ * This object is added to the parser output of MediaWiki, but it is
 112+ * not useful to have all its data as part of the parser cache since
 113+ * the data is already stored in more accessible format in SMW. Hence
 114+ * this implementation of __sleep() makes sure only the subject is
 115+ * serialised, yielding a minimal stub data container after
 116+ * unserialisation. This is a little safer than serialising nothing:
 117+ * if, for any reason, SMW should ever access an unserialised parser
 118+ * output, then the Semdata container will at least look as if properly
 119+ * initialised (though empty).
126120 *
127 - * @note It might be even better to have other members with stub object data that is used for serializing, thus using much less data.
 121+ * @note It might be even better to have other members with stub object
 122+ * data that is used for serializing, thus using much less data.
128123 *
129124 * @return array
130125 */
@@ -146,9 +141,7 @@
147142 * @return array of SMWDIProperty objects
148143 */
149144 public function getProperties() {
150 - $this->unstubProperties();
151145 ksort( $this->mProperties, SORT_STRING );
152 -
153146 return $this->mProperties;
154147 }
155148
@@ -163,25 +156,6 @@
164157 return array();
165158 }
166159
167 - if ( array_key_exists( $property->getKey(), $this->mStubPropVals ) ) {
168 - $this->unstubProperty( $property->getKey(), $property );
169 -
170 - foreach ( $this->mStubPropVals[$property->getKey()] as $dbkeys ) {
171 - try {
172 - $di = SMWCompatibilityHelpers::dataItemFromDBKeys( $property->findPropertyTypeID(), $dbkeys );
173 - if ( $this->mNoDuplicates ) {
174 - $this->mPropVals[$property->getKey()][$di->getHash()] = $di;
175 - } else {
176 - $this->mPropVals[$property->getKey()][] = $di;
177 - }
178 - } catch ( SMWDataItemException $e ) {
179 - // ignore data
180 - }
181 - }
182 -
183 - unset( $this->mStubPropVals[$property->getKey()] );
184 - }
185 -
186160 if ( array_key_exists( $property->getKey(), $this->mPropVals ) ) {
187161 return $this->mPropVals[$property->getKey()];
188162 } else {
@@ -225,7 +199,6 @@
226200 * @return boolean
227201 */
228202 public function hasVisibleProperties() {
229 - $this->unstubProperties();
230203 return $this->mHasVisibleProps;
231204 }
232205
@@ -240,7 +213,6 @@
241214 * @return boolean
242215 */
243216 public function hasVisibleSpecialProperties() {
244 - $this->unstubProperties();
245217 return $this->mHasVisibleSpecs;
246218 }
247219
@@ -312,70 +284,14 @@
313285 }
314286
315287 /**
316 - * Add data in abbreviated form so that it is only expanded if needed. The property key
317 - * is the DB key (string) of a property value, whereas valuekeys is an array of DBkeys for
318 - * the added value that will be used to initialize the value if needed at some point.
319 - */
320 - public function addPropertyStubValue( $propertyKey, $valueKeys ) {
321 - // Catch built-in properties, since their internal key is not what is used as a key elsewhere in SMWSemanticData.
322 -// if ( $propertyKey { 0 } == '_' ) {
323 -// $property = new SMWDIProperty( $propertyKey );
324 -// $propertyKey = $property->getKey();
325 -// $this->unstubProperty( $propertyKey, $property );
326 -// }
327 -
328 - $this->mStubPropVals[$propertyKey][] = $valueKeys;
329 - }
330 -
331 - /**
332288 * Delete all data other than the subject.
333289 */
334290 public function clear() {
335291 $this->mPropVals = array();
336292 $this->mProperties = array();
337 - $this->mStubPropVals = array();
338293 $this->mHasVisibleProps = false;
339294 $this->mHasVisibleSpecs = false;
340295 $this->stubObject = false;
341296 }
342297
343 - /**
344 - * Process all mProperties that have been added as stubs.
345 - * Associated data may remain in stub form.
346 - */
347 - protected function unstubProperties() {
348 - foreach ( $this->mStubPropVals as $pkey => $values ) { // unstub property values only, the value lists are still kept as stubs
349 - $this->unstubProperty( $pkey );
350 - }
351 - }
352 -
353 - /**
354 - * Unstub a single property from the stub data array. If available, an existing object
355 - * for that property might be provided, so we do not need to make a new one. It is not
356 - * checked if the object matches the property name.
357 - *
358 - * @param $propertyKey string
359 - * @param SMWDIProperty $diProperty
360 - */
361 - protected function unstubProperty( $propertyKey, $diProperty = null ) {
362 - if ( !array_key_exists( $propertyKey, $this->mProperties ) ) {
363 - if ( $diProperty === null ) {
364 - //$propertyDV = SMWPropertyValue::makeProperty( $propertyKey );
365 - //$diProperty = $propertyDV->getDataItem();
366 - $diProperty = new SMWDIProperty( $propertyKey, false );
367 - }
368 -
369 - $this->mProperties[$propertyKey] = $diProperty;
370 -
371 - if ( !$diProperty->isUserDefined() ) {
372 - if ( $diProperty->isShown() ) {
373 - $this->mHasVisibleSpecs = true;
374 - $this->mHasVisibleProps = true;
375 - }
376 - } else {
377 - $this->mHasVisibleProps = true;
378 - }
379 - }
380 - }
381 -
382298 }
\ No newline at end of file
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
@@ -223,6 +223,7 @@
224224 $wgAutoloadClasses['SMWDisjunction'] = $stoDir . 'SMW_Description.php';
225225 $wgAutoloadClasses['SMWSomeProperty'] = $stoDir . 'SMW_Description.php';
226226 $wgAutoloadClasses['SMWSQLStore2'] = $stoDir . 'SMW_SQLStore2.php';
 227+ $wgAutoloadClasses['SMWSqlStubSemanticData'] = $stoDir . 'SMW_SqlStubSemanticData.php';
227228 $wgAutoloadClasses['SMWSQLStore2Table'] = $stoDir . 'SMW_SQLStore2Table.php';
228229 $wgAutoloadClasses['SMWSQLHelpers'] = $stoDir . 'SMW_SQLHelpers.php';
229230 $wgAutoloadClasses['SMWSparqlStore'] = $stoDir . 'SMW_SparqlStore.php';

Comments

#Comment by Jeroen De Dauw (talk | contribs)   11:32, 13 May 2011

I'm getting this error when refreshing the semantic data and then running runJobs.php.

Fatal error: Call to undefined method SMWSemanticData::addPropertyStubValue() in /var/www/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2.php on line 194

Call Stack:
    0.0178     686312   1. {main}() /var/www/phase3/maintenance/runJobs.php:0
    0.1104    1241968   2. require_once('/var/www/phase3/maintenance/doMaintenance.php') /var/www/phase3/maintenance/runJobs.php:108
    0.4175   22347808   3. RunJobs->execute() /var/www/phase3/maintenance/doMaintenance.php:115
   27.1408   42121760   4. SMWUpdateJob->run() /var/www/phase3/maintenance/runJobs.php:78
   27.1447   42136640   5. Parser->parse() /var/www/extensions/SemanticMediaWiki/includes/jobs/SMW_UpdateJob.php:60
   27.1455   42129408   6. Parser->internalParse() /var/www/phase3/includes/parser/Parser.php:315
   27.1455   42129800   7. Parser->replaceVariables() /var/www/phase3/includes/parser/Parser.php:1106
   27.1462   42133384   8. PPFrame_DOM->expand() /var/www/phase3/includes/parser/Parser.php:2948
   27.1464   42142568   9. Parser->braceSubstitution() /var/www/phase3/includes/parser/Preprocessor_DOM.php:986
   27.1532   42158128  10. PPFrame_DOM->expand() /var/www/phase3/includes/parser/Parser.php:3281
   27.1535   42169248  11. Parser->braceSubstitution() /var/www/phase3/includes/parser/Preprocessor_DOM.php:986
   27.1689   42190088  12. call_user_func_array() /var/www/phase3/includes/parser/Parser.php:3153
   27.1689   42191088  13. SMWAsk::render() /var/www/phase3/includes/parser/Parser.php:0
   27.1690   42192568  14. SMWQueryProcessor::getResultFromFunctionParams() /var/www/extensions/SemanticMediaWiki/includes/parserhooks/SMW_Ask.php:34
   27.1707   42218072  15. SMWQueryProcessor::getResultFromQueryString() /var/www/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php:254
   27.1910   42436312  16. SMWQueryProcessor::getResultFromQuery() /var/www/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php:270
   27.2159   42475712  17. SMMapper->getResult() /var/www/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php:302
   27.2159   42476464  18. SMMapper->__call() /var/www/extensions/SemanticMaps/includes/queryprinters/SM_Mapper.php:0
   27.2159   42477096  19. call_user_func_array() /var/www/extensions/SemanticMaps/includes/queryprinters/SM_Mapper.php:84
   27.2159   42477840  20. SMMapPrinter->getResult() /var/www/extensions/SemanticMaps/includes/queryprinters/SM_Mapper.php:0
   27.2437   42492400  21. SMMapPrinter->getResultText() /var/www/extensions/SemanticMaps/includes/queryprinters/SM_MapPrinter.php:335
   27.2451   42644552  22. SMQueryHandler->getLocations() /var/www/extensions/SemanticMaps/includes/queryprinters/SM_MapPrinter.php:186
   27.2452   42644552  23. SMQueryHandler->findLocations() /var/www/extensions/SemanticMaps/includes/queryprinters/SM_QueryHandler.php:224
   27.2452   42648640  24. SMQueryHandler->handleResultRow() /var/www/extensions/SemanticMaps/includes/queryprinters/SM_QueryHandler.php:241
   27.2632   44512912  25. SMWResultArray->getNextDataValue() /var/www/extensions/SemanticMaps/includes/queryprinters/SM_QueryHandler.php:268
   27.2632   44512912  26. SMWResultArray->getNextDataItem() /var/www/extensions/SemanticMediaWiki/includes/storage/SMW_QueryResult.php:347
   27.2632   44512912  27. SMWResultArray->loadContent() /var/www/extensions/SemanticMediaWiki/includes/storage/SMW_QueryResult.php:331
   27.2633   44512992  28. SMWSQLStore2->getPropertyValues() /var/www/extensions/SemanticMediaWiki/includes/storage/SMW_QueryResult.php:405
   27.2640   44513640  29. SMWSQLStore2->getSemanticData() /var/www/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2.php:223
#Comment by Jeroen De Dauw (talk | contribs)   16:57, 15 May 2011

Fixed in r88142

Status & tagging log