r67055 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r67054‎ | r67055 | r67056 >
Date:21:00, 29 May 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Code/doc/style improvements
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Factbox.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_SemanticData.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Factbox.php
@@ -107,8 +107,8 @@
108108 if ( $showfactbox == SMW_FACTBOX_HIDDEN ) { // use shortcut
109109 return '';
110110 }
111 - // deal with complete dataset only if needed:
112 - if ( !isset( $parseroutput->mSMWData ) || $parseroutput->mSMWData->stubobject ) {
 111+ // Deal with complete dataset only if needed:
 112+ if ( !isset( $parseroutput->mSMWData ) || $parseroutput->mSMWData->stubObject ) {
113113 $semdata = smwfGetStore()->getSemanticData( $title );
114114 } else {
115115 $semdata = $parseroutput->mSMWData;
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_SemanticData.php
@@ -1,6 +1,6 @@
22 <?php
33 /**
4 - * The class in this file manages (special) properties that are
 4+ * The class in this file manages (special) mProperties that are
55 * associated with a certain subject (article). It is used as a
66 * container for chunks of subject-centred data.
77 *
@@ -8,6 +8,7 @@
99 * @ingroup SMW
1010 *
1111 * @author Markus Krötzsch
 12+ * @author Jeroen De Dauw
1213 */
1314
1415 /**
@@ -18,37 +19,85 @@
1920 * @ingroup SMW
2021 */
2122 class SMWSemanticData {
22 - /// Text keys and arrays of datavalue objects.
23 - protected $propvals = array();
24 - /// Text keys and title objects.
25 - protected $properties = array();
26 - /// Stub property data that is not part of $propvals and $properties yet. Entries use
27 - /// property DB keys as keys. The value is an array of DBkey-arrays that define individual
28 - /// datavalues. The stubs will be set up when first accessed.
29 - protected $stubpropvals = array();
30 - /// Boolean, stating whether the container holds any normal properties.
31 - protected $hasvisibleprops = false;
32 - /// Boolean, stating whether the container holds any displayable special properties (some are internal only without a display name).
33 - protected $hasvisiblespecs = false;
34 - /// Boolean, stating whether this is a stub object. Stubbing might happen on serialisation to safe DB space
35 - public $stubobject = true;
 23+
3624 /**
37 - * Boolean, stating whether repeated values should be avoided. Not needing duplicte elimination
 25+ * States whether this is a stub object. Stubbing might happen on serialisation to safe DB space.
 26+ *
 27+ * @var boolean
 28+ */
 29+ public $stubObject = true;
 30+
 31+ /**
 32+ * Cache for the local version of "Property:"
 33+ *
 34+ * @var mixed
 35+ */
 36+ static protected $mPropertyPrefix = false;
 37+
 38+ /**
 39+ * Text keys and arrays of datavalue objects.
 40+ *
 41+ * @var array
 42+ */
 43+ protected $mPropVals = array();
 44+
 45+ /**
 46+ * Text keys and title objects.
 47+ *
 48+ * @var array
 49+ */
 50+ protected $mProperties = array();
 51+
 52+ /**
 53+ * Stub property data that is not part of $propvals and $mProperties yet. Entries use
 54+ * property DB keys as keys. The value is an array of DBkey-arrays that define individual
 55+ * datavalues. The stubs will be set up when first accessed.
 56+ *
 57+ * @var array
 58+ */
 59+ protected $mStubPropVals = array();
 60+
 61+ /**
 62+ * States whether the container holds any normal properties.
 63+ *
 64+ * @var boolean
 65+ */
 66+ protected $mHasVisibleProps = false;
 67+
 68+ /**
 69+ * States whether the container holds any displayable special mProperties (some are internal only without a display name).
 70+ *
 71+ * @var boolean
 72+ */
 73+ protected $mHasVisibleSpecs = false;
 74+
 75+ /**
 76+ * States whether repeated values should be avoided. Not needing duplicte elimination
3877 * (e.g. when loading from store) can safe much time, since objects can remain stubs until someone
3978 * really acesses their value.
 79+ *
 80+ * @var boolean
4081 */
41 - protected $m_noduplicates;
42 - /// Cache for the local version of "Property:"
43 - static protected $m_propertyprefix = false;
 82+ protected $mNoDuplicates;
4483
45 - /// SMWWikiPageValue object that is the subject of this container.
46 - /// Subjects that are NULL are used to represent "internal objects" only.
47 - protected $subject;
 84+ /**
 85+ * SMWWikiPageValue object that is the subject of this container.
 86+ * Subjects that are NULL are used to represent "internal objects" only.
 87+ *
 88+ * @var SMWWikiPageValue
 89+ */
 90+ protected $mSubject;
4891
49 - public function __construct( $subject, $noduplicates = true ) {
50 - $this->subject = $subject;
51 - $this->m_noduplicates = $noduplicates;
52 - $this->stubobject = false;
 92+ /**
 93+ * Constructor.
 94+ *
 95+ * @param SMWWikiPageValue $subject
 96+ * @param boolean $noDuplicates
 97+ */
 98+ public function __construct( $subject, $noDuplicates = true ) {
 99+ $this->mSubject = $subject;
 100+ $this->mNoDuplicates = $noDuplicates;
 101+ $this->stubObject = false;
53102 }
54103
55104 /**
@@ -57,7 +106,10 @@
58107 * subject is serialised, yielding a minimal stub data container after unserialisation. This is a little safer than serialising
59108 * nothing: if, for any reason, SMW should ever access an unserialised parser output, then the Semdata container will at least
60109 * look as if properly initialised (though empty).
 110+ *
61111 * @note It might be even better to have other members with stub object data that is used for serializing, thus using much less data.
 112+ *
 113+ * @return array
62114 */
63115 public function __sleep() {
64116 return array( 'subject' );
@@ -65,10 +117,11 @@
66118
67119 /**
68120 * Return subject to which the stored semantic annotation refer to.
 121+ *
69122 * @return SMWWikiPageValue subject
70123 */
71124 public function getSubject() {
72 - return $this->subject;
 125+ return $this->mSubject;
73126 }
74127
75128 /**
@@ -76,36 +129,39 @@
77130 */
78131 public function getProperties() {
79132 $this->unstubProperties();
80 - ksort( $this->properties, SORT_STRING );
81 - return $this->properties;
 133+ ksort( $this->mProperties, SORT_STRING );
 134+
 135+ return $this->mProperties;
82136 }
83137
84138 /**
85139 * Get the array of all stored values for some property.
86140 *
87141 * @param SMWPropertyValue $property
 142+ *
 143+ * @return array
88144 */
89145 public function getPropertyValues( SMWPropertyValue $property ) {
90 - if ( array_key_exists( $property->getDBkey(), $this->stubpropvals ) ) {
 146+ if ( array_key_exists( $property->getDBkey(), $this->mStubPropVals ) ) {
91147 // Unstub those entries completely.
92148 $this->unstubProperty( $property->getDBkey(), $property );
93149
94 - foreach ( $this->stubpropvals[$property->getDBkey()] as $dbkeys ) {
 150+ foreach ( $this->mStubPropVals[$property->getDBkey()] as $dbkeys ) {
95151 $dv = SMWDataValueFactory::newPropertyObjectValue( $property );
96152 $dv->setDBkeys( $dbkeys );
97153
98 - if ( $this->m_noduplicates ) {
99 - $this->propvals[$property->getDBkey()][$dv->getHash()] = $dv;
 154+ if ( $this->mNoDuplicates ) {
 155+ $this->mPropVals[$property->getDBkey()][$dv->getHash()] = $dv;
100156 } else {
101 - $this->propvals[$property->getDBkey()][] = $dv;
 157+ $this->mPropVals[$property->getDBkey()][] = $dv;
102158 }
103159 }
104160
105 - unset( $this->stubpropvals[$property->getDBkey()] );
 161+ unset( $this->mStubPropVals[$property->getDBkey()] );
106162 }
107163
108 - if ( array_key_exists( $property->getDBkey(), $this->propvals ) ) {
109 - return $this->propvals[$property->getDBkey()];
 164+ if ( array_key_exists( $property->getDBkey(), $this->mPropVals ) ) {
 165+ return $this->mPropVals[$property->getDBkey()];
110166 } else {
111167 return array();
112168 }
@@ -115,12 +171,14 @@
116172 * Generate a hash value to simplify the comparison of this data container with other
117173 * containers. The hash uses PHP's md5 implementation, which is among the fastest hash
118174 * algorithms that PHP offers.
 175+ *
 176+ * @return string
119177 */
120178 public function getHash() {
121179 $ctx = hash_init( 'md5' );
122180
123 - if ( $this->subject !== null ) { // here and below, use "_#_" to separate values; really not much care needed here
124 - hash_update( $ctx, '_#_' . $this->subject->getHash() );
 181+ if ( $this->mSubject !== null ) { // here and below, use "_#_" to separate values; really not much care needed here
 182+ hash_update( $ctx, '_#_' . $this->mSubject->getHash() );
125183 }
126184
127185 foreach ( $this->getProperties() as $property ) {
@@ -136,72 +194,85 @@
137195
138196 /**
139197 * Return true if there are any visible properties.
 198+ *
140199 * @note While called "visible" this check actually refers to the function
141200 * SMWPropertyValue::isShown(). The name is kept for compatibility.
 201+ *
 202+ * @return boolean
142203 */
143204 public function hasVisibleProperties() {
144205 $this->unstubProperties();
145 - return $this->hasvisibleprops;
 206+ return $this->mHasVisibleProps;
146207 }
147208
148209 /**
149210 * Return true if there are any special properties that can
150211 * be displayed.
 212+ *
151213 * @note While called "visible" this check actually refers to the function
152214 * SMWPropertyValue::isShown(). The name is kept for compatibility.
 215+ *
 216+ * @return boolean
153217 */
154218 public function hasVisibleSpecialProperties() {
155219 $this->unstubProperties();
156 - return $this->hasvisiblespecs;
 220+ return $this->mHasVisibleSpecs;
157221 }
158222
159223 /**
160224 * Store a value for a property identified by its title object. Duplicate
161225 * value entries are usually ignored.
 226+ *
162227 * @note Attention: there is no check whether the type of the given datavalue agrees
163228 * with what SMWDataValueFactory is producing (based on predefined property records and
164229 * the current DB content). Always use SMWDataValueFactory to produce fitting values!
 230+ *
 231+ * @param SMWPropertyValue $property
 232+ * @param SMWDataValue $value
165233 */
166234 public function addPropertyObjectValue( SMWPropertyValue $property, SMWDataValue $value ) {
167235 if ( !$property->isValid() ) return; // nothing we can do
168236
169 - if ( !array_key_exists( $property->getDBkey(), $this->propvals ) ) {
170 - $this->propvals[$property->getDBkey()] = array();
171 - $this->properties[$property->getDBkey()] = $property;
 237+ if ( !array_key_exists( $property->getDBkey(), $this->mPropVals ) ) {
 238+ $this->mPropVals[$property->getDBkey()] = array();
 239+ $this->mProperties[$property->getDBkey()] = $property;
172240 }
173241
174 - if ( $this->m_noduplicates ) {
175 - $this->propvals[$property->getDBkey()][$value->getHash()] = $value;
 242+ if ( $this->mNoDuplicates ) {
 243+ $this->mPropVals[$property->getDBkey()][$value->getHash()] = $value;
176244 } else {
177 - $this->propvals[$property->getDBkey()][] = $value;
 245+ $this->mPropVals[$property->getDBkey()][] = $value;
178246 }
179247
180248 if ( !$property->isUserDefined() ) {
181249 if ( $property->isShown() ) {
182 - $this->hasvisiblespecs = true;
183 - $this->hasvisibleprops = true;
 250+ $this->mHasVisibleSpecs = true;
 251+ $this->mHasVisibleProps = true;
184252 }
185253 } else {
186 - $this->hasvisibleprops = true;
 254+ $this->mHasVisibleProps = true;
187255 }
188256 }
189257
190258 /**
191259 * Store a value for a given property identified by its text label (without
192260 * namespace prefix). Duplicate value entries are usually ignored.
 261+ *
 262+ * @param string $propertyName
 263+ * @param SMWDataValue $value
193264 */
194 - public function addPropertyValue( $propertyname, SMWDataValue $value ) {
195 - $propertykey = smwfNormalTitleDBKey( $propertyname );
 265+ public function addPropertyValue( $propertyName, SMWDataValue $value ) {
 266+ $propertykey = smwfNormalTitleDBKey( $propertyName );
196267
197 - if ( array_key_exists( $propertykey, $this->properties ) ) {
198 - $property = $this->properties[$propertykey];
 268+ if ( array_key_exists( $propertykey, $this->mProperties ) ) {
 269+ $property = $this->mProperties[$propertykey];
199270 } else {
200 - if ( SMWSemanticData::$m_propertyprefix == false ) {
 271+ if ( self::$mPropertyPrefix == false ) {
201272 global $wgContLang;
202 - SMWSemanticData::$m_propertyprefix = $wgContLang->getNsText( SMW_NS_PROPERTY ) . ':';
 273+ self::$mPropertyPrefix = $wgContLang->getNsText( SMW_NS_PROPERTY ) . ':';
203274 } // explicitly use prefix to cope with things like [[Property:User:Stupid::somevalue]]
204275
205 - $property = SMWPropertyValue::makeUserProperty( SMWSemanticData::$m_propertyprefix . $propertyname );
 276+ $property = SMWPropertyValue::makeUserProperty( self::$mPropertyPrefix . $propertyName );
206277
207278 if ( !$property->isValid() ) { // error, maybe illegal title text
208279 return;
@@ -216,33 +287,33 @@
217288 * is the DB key (string) of a property value, whereas valuekeys is an array of DBkeys for
218289 * the added value that will be used to initialize the value if needed at some point.
219290 */
220 - public function addPropertyStubValue( $propertykey, $valuekeys ) {
221 - // Catch built-in properties, since their internal key is not what is used as a key elsewhere in SMWSemanticData.
222 - if ( $propertykey { 0 } == '_' ) {
223 - $property = SMWPropertyValue::makeProperty( $propertykey );
224 - $propertykey = $property->getDBkey();
225 - $this->unstubProperty( $propertykey, $property );
 291+ public function addPropertyStubValue( $propertyKey, $valueKeys ) {
 292+ // Catch built-in mProperties, since their internal key is not what is used as a key elsewhere in SMWSemanticData.
 293+ if ( $propertyKey { 0 } == '_' ) {
 294+ $property = SMWPropertyValue::makeProperty( $propertyKey );
 295+ $propertyKey = $property->getDBkey();
 296+ $this->unstubProperty( $propertyKey, $property );
226297 }
227298
228 - $this->stubpropvals[$propertykey][] = $valuekeys;
 299+ $this->mStubPropVals[$propertyKey][] = $valueKeys;
229300 }
230301
231302 /**
232303 * Delete all data other than the subject.
233304 */
234305 public function clear() {
235 - $this->propvals = array();
236 - $this->properties = array();
237 - $this->stubpropvals = array();
238 - $this->hasvisibleprops = false;
239 - $this->hasvisiblespecs = false;
 306+ $this->mPropVals = array();
 307+ $this->mProperties = array();
 308+ $this->mStubPropVals = array();
 309+ $this->mHasVisibleProps = false;
 310+ $this->mHasVisibleSpecs = false;
240311 }
241312
242313 /**
243 - * Process all properties that have been added as stubs. Associated data may remain in stub form.
 314+ * Process all mProperties that have been added as stubs. Associated data may remain in stub form.
244315 */
245316 protected function unstubProperties() {
246 - foreach ( $this->stubpropvals as $pname => $values ) { // unstub property values only, the value lists are still kept as stubs
 317+ foreach ( $this->mStubPropVals as $pname => $values ) { // unstub property values only, the value lists are still kept as stubs
247318 $this->unstubProperty( $pname );
248319 }
249320 }
@@ -251,22 +322,25 @@
252323 * Unstub a single property from the stub data array. If available, an existing object
253324 * for that property might be provided, so we do not need to make a new one. It is not
254325 * checked if the object matches the property name.
 326+ *
 327+ * @param string $propertyName
 328+ * @param $propertyObject
255329 */
256 - protected function unstubProperty( $pname, $propertyobj = null ) {
257 - if ( !array_key_exists( $pname, $this->properties ) ) {
258 - if ( $propertyobj === null ) {
259 - $propertyobj = SMWPropertyValue::makeProperty( $pname );
 330+ protected function unstubProperty( $propertyName, $propertyObject = null ) {
 331+ if ( !array_key_exists( $propertyName, $this->mProperties ) ) {
 332+ if ( $propertyObject === null ) {
 333+ $propertyObject = SMWPropertyValue::makeProperty( $propertyName );
260334 }
261335
262 - $this->properties[$pname] = $propertyobj;
 336+ $this->mProperties[$propertyName] = $propertyObject;
263337
264 - if ( !$propertyobj->isUserDefined() ) {
265 - if ( $propertyobj->isShown() ) {
266 - $this->hasvisiblespecs = true;
267 - $this->hasvisibleprops = true;
 338+ if ( !$propertyObject->isUserDefined() ) {
 339+ if ( $propertyObject->isShown() ) {
 340+ $this->mHasVisibleSpecs = true;
 341+ $this->mHasVisibleProps = true;
268342 }
269343 } else {
270 - $this->hasvisibleprops = true;
 344+ $this->mHasVisibleProps = true;
271345 }
272346 }
273347 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r67074Follow up to r67055 - fixed returned variable namejeroendedauw14:56, 30 May 2010

Status & tagging log