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 |
1 | 167 | + native |
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2.php |
— | — | @@ -163,7 +163,7 @@ |
164 | 164 | |
165 | 165 | // *** Prepare the cache ***// |
166 | 166 | 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 ); |
168 | 168 | $this->m_semdata[$sid]->addPropertyStubValue( '_SKEY', array( $sortkey ) ); |
169 | 169 | $this->m_sdstate[$sid] = array( '__key' ); |
170 | 170 | } |
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_SemanticData.php |
— | — | @@ -56,16 +56,6 @@ |
57 | 57 | protected $mProperties = array(); |
58 | 58 | |
59 | 59 | /** |
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 | | - /** |
70 | 60 | * States whether the container holds any normal properties. |
71 | 61 | * |
72 | 62 | * @var boolean |
— | — | @@ -84,9 +74,9 @@ |
85 | 75 | |
86 | 76 | /** |
87 | 77 | * 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. |
91 | 81 | * |
92 | 82 | * @note This setting is merely for optimization. The SMW data model |
93 | 83 | * never cares about the multiplicity of identical data assignments. |
— | — | @@ -107,8 +97,8 @@ |
108 | 98 | /** |
109 | 99 | * Constructor. |
110 | 100 | * |
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 |
113 | 103 | */ |
114 | 104 | public function __construct( SMWDIWikiPage $subject, $noDuplicates = true ) { |
115 | 105 | $this->clear(); |
— | — | @@ -117,13 +107,18 @@ |
118 | 108 | } |
119 | 109 | |
120 | 110 | /** |
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). |
126 | 120 | * |
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. |
128 | 123 | * |
129 | 124 | * @return array |
130 | 125 | */ |
— | — | @@ -146,9 +141,7 @@ |
147 | 142 | * @return array of SMWDIProperty objects |
148 | 143 | */ |
149 | 144 | public function getProperties() { |
150 | | - $this->unstubProperties(); |
151 | 145 | ksort( $this->mProperties, SORT_STRING ); |
152 | | - |
153 | 146 | return $this->mProperties; |
154 | 147 | } |
155 | 148 | |
— | — | @@ -163,25 +156,6 @@ |
164 | 157 | return array(); |
165 | 158 | } |
166 | 159 | |
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 | | - |
186 | 160 | if ( array_key_exists( $property->getKey(), $this->mPropVals ) ) { |
187 | 161 | return $this->mPropVals[$property->getKey()]; |
188 | 162 | } else { |
— | — | @@ -225,7 +199,6 @@ |
226 | 200 | * @return boolean |
227 | 201 | */ |
228 | 202 | public function hasVisibleProperties() { |
229 | | - $this->unstubProperties(); |
230 | 203 | return $this->mHasVisibleProps; |
231 | 204 | } |
232 | 205 | |
— | — | @@ -240,7 +213,6 @@ |
241 | 214 | * @return boolean |
242 | 215 | */ |
243 | 216 | public function hasVisibleSpecialProperties() { |
244 | | - $this->unstubProperties(); |
245 | 217 | return $this->mHasVisibleSpecs; |
246 | 218 | } |
247 | 219 | |
— | — | @@ -312,70 +284,14 @@ |
313 | 285 | } |
314 | 286 | |
315 | 287 | /** |
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 | | - /** |
332 | 288 | * Delete all data other than the subject. |
333 | 289 | */ |
334 | 290 | public function clear() { |
335 | 291 | $this->mPropVals = array(); |
336 | 292 | $this->mProperties = array(); |
337 | | - $this->mStubPropVals = array(); |
338 | 293 | $this->mHasVisibleProps = false; |
339 | 294 | $this->mHasVisibleSpecs = false; |
340 | 295 | $this->stubObject = false; |
341 | 296 | } |
342 | 297 | |
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 | | - |
382 | 298 | } |
\ No newline at end of file |
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php |
— | — | @@ -223,6 +223,7 @@ |
224 | 224 | $wgAutoloadClasses['SMWDisjunction'] = $stoDir . 'SMW_Description.php'; |
225 | 225 | $wgAutoloadClasses['SMWSomeProperty'] = $stoDir . 'SMW_Description.php'; |
226 | 226 | $wgAutoloadClasses['SMWSQLStore2'] = $stoDir . 'SMW_SQLStore2.php'; |
| 227 | + $wgAutoloadClasses['SMWSqlStubSemanticData'] = $stoDir . 'SMW_SqlStubSemanticData.php'; |
227 | 228 | $wgAutoloadClasses['SMWSQLStore2Table'] = $stoDir . 'SMW_SQLStore2Table.php'; |
228 | 229 | $wgAutoloadClasses['SMWSQLHelpers'] = $stoDir . 'SMW_SQLHelpers.php'; |
229 | 230 | $wgAutoloadClasses['SMWSparqlStore'] = $stoDir . 'SMW_SparqlStore.php'; |