Index: trunk/extensions/SemanticMediaWiki/includes/dataitems/SMW_DI_Property.php |
— | — | @@ -33,11 +33,13 @@ |
34 | 34 | * @var array |
35 | 35 | */ |
36 | 36 | static protected $m_prop_types; |
| 37 | + |
37 | 38 | /** |
38 | 39 | * Array with entries "property id" => "property label" |
39 | 40 | * @var array |
40 | 41 | */ |
41 | 42 | static protected $m_prop_labels; |
| 43 | + |
42 | 44 | /** |
43 | 45 | * Array with entries "property alias" => "property id" |
44 | 46 | * @var array |
— | — | @@ -50,11 +52,13 @@ |
51 | 53 | * @var string |
52 | 54 | */ |
53 | 55 | protected $m_key; |
| 56 | + |
54 | 57 | /** |
55 | 58 | * Whether to take the inverse of this property or not. |
56 | 59 | * @var boolean |
57 | 60 | */ |
58 | 61 | protected $m_inverse; |
| 62 | + |
59 | 63 | /** |
60 | 64 | * Cache for property type ID. |
61 | 65 | * @var string |
— | — | @@ -75,14 +79,16 @@ |
76 | 80 | if ( ( $key === '' ) || ( $key{0} == '-' ) ) { |
77 | 81 | throw new SMWDataItemException( "Illegal property key \"$key\"." ); |
78 | 82 | } |
| 83 | + |
79 | 84 | if ( $key{0} == '_' ) { |
80 | 85 | SMWDIProperty::initPropertyRegistration(); |
81 | 86 | if ( !array_key_exists( $key, SMWDIProperty::$m_prop_types ) ) { |
82 | 87 | throw new SMWDataItemException( "There is no predefined property with \"$key\"." ); |
83 | 88 | } |
84 | 89 | } |
85 | | - $this->m_key = $key; |
86 | | - $this->m_inverse = ( $inverse == true ); |
| 90 | + |
| 91 | + $this->m_key = $key; |
| 92 | + $this->m_inverse = $inverse; |
87 | 93 | } |
88 | 94 | |
89 | 95 | public function getDIType() { |
— | — | @@ -115,6 +121,8 @@ |
116 | 122 | * Examples of properties that are not shown include Modificaiton date |
117 | 123 | * (not available in time), and Has improper value for (errors are |
118 | 124 | * shown directly on the page anyway). |
| 125 | + * |
| 126 | + * @return boolean |
119 | 127 | */ |
120 | 128 | public function isShown() { |
121 | 129 | return ( ( $this->isUserDefined() ) || |
— | — | @@ -125,6 +133,7 @@ |
126 | 134 | /** |
127 | 135 | * Return true if this is a usual wiki property that is defined by a |
128 | 136 | * wiki page, and not a property that is pre-defined in the wiki. |
| 137 | + * |
129 | 138 | * @return boolean |
130 | 139 | */ |
131 | 140 | public function isUserDefined() { |
— | — | @@ -134,6 +143,7 @@ |
135 | 144 | /** |
136 | 145 | * Find a user-readable label for this property, or return '' if it is |
137 | 146 | * a predefined property that has no label. |
| 147 | + * |
138 | 148 | * @return string |
139 | 149 | */ |
140 | 150 | public function getLabel() { |
— | — | @@ -158,12 +168,16 @@ |
159 | 169 | * @return SMWDIWikiPage or null |
160 | 170 | */ |
161 | 171 | public function getDiWikiPage() { |
162 | | - if ( $this->m_inverse ) return null; |
| 172 | + if ( $this->m_inverse ) { |
| 173 | + return null; |
| 174 | + } |
| 175 | + |
163 | 176 | if ( $this->isUserDefined() ) { |
164 | 177 | $dbkey = $this->m_key; |
165 | 178 | } else { |
166 | 179 | $dbkey = str_replace( ' ', '_', $this->getLabel() ); |
167 | 180 | } |
| 181 | + |
168 | 182 | try { |
169 | 183 | return new SMWDIWikiPage( $dbkey, SMW_NS_PROPERTY, '' ); |
170 | 184 | } catch ( SMWDataItemException $e ) { |
— | — | @@ -181,12 +195,15 @@ |
182 | 196 | */ |
183 | 197 | public function findPropertyTypeID() { |
184 | 198 | global $smwgPDefaultType; |
| 199 | + |
185 | 200 | if ( !isset( $this->m_proptypeid ) ) { |
186 | 201 | if ( $this->isUserDefined() ) { // normal property |
187 | 202 | $diWikiPage = new SMWDIWikiPage( $this->getKey(), SMW_NS_PROPERTY, '' ); |
188 | 203 | $typearray = smwfGetStore()->getPropertyValues( $diWikiPage, new SMWDIProperty( '_TYPE' ) ); |
| 204 | + |
189 | 205 | if ( count( $typearray ) >= 1 ) { // some types given, pick one (hopefully unique) |
190 | 206 | $typeDataItem = reset( $typearray ); |
| 207 | + |
191 | 208 | if ( $typeDataItem instanceof SMWDIUri ) { |
192 | 209 | $this->m_proptypeid = $typeDataItem->getFragment(); |
193 | 210 | } else { |
— | — | @@ -199,25 +216,31 @@ |
200 | 217 | $this->m_proptypeid = self::getPredefinedPropertyTypeId( $this->m_key ); |
201 | 218 | } |
202 | 219 | } |
| 220 | + |
203 | 221 | return $this->m_proptypeid; |
204 | 222 | } |
205 | 223 | |
206 | 224 | |
207 | 225 | public function getSerialization() { |
208 | | - return ( $this->m_inverse ? '-' : '' ) . $this->m_key ; |
| 226 | + return ( $this->m_inverse ? '-' : '' ) . $this->m_key; |
209 | 227 | } |
210 | 228 | |
211 | 229 | /** |
212 | 230 | * Create a data item from the provided serialization string and type |
213 | 231 | * ID. |
| 232 | + * |
| 233 | + * @param string $serialization |
| 234 | + * |
214 | 235 | * @return SMWDIProperty |
215 | 236 | */ |
216 | 237 | public static function doUnserialize( $serialization ) { |
217 | 238 | $inverse = false; |
| 239 | + |
218 | 240 | if ( $serialization{0} == '-' ) { |
219 | 241 | $serialization = substr( $serialization, 1 ); |
220 | 242 | $inverse = true; |
221 | 243 | } |
| 244 | + |
222 | 245 | return new SMWDIProperty( $serialization, $inverse ); |
223 | 246 | } |
224 | 247 | |
— | — | @@ -233,10 +256,12 @@ |
234 | 257 | * |
235 | 258 | * @param $label string label for the property |
236 | 259 | * @param $inverse boolean states if the inverse of the property is constructed |
| 260 | + * |
237 | 261 | * @return SMWDIProperty object |
238 | 262 | */ |
239 | 263 | public static function newFromUserLabel( $label, $inverse = false ) { |
240 | 264 | $id = SMWDIProperty::findPropertyID( $label ); |
| 265 | + |
241 | 266 | if ( $id === false ) { |
242 | 267 | return new SMWDIProperty( str_replace( ' ', '_', $label ), $inverse ); |
243 | 268 | } else { |
— | — | @@ -251,13 +276,16 @@ |
252 | 277 | * |
253 | 278 | * This function is protected. The public way of getting this data is |
254 | 279 | * to simply create a new property object and to get its ID (if any). |
| 280 | + * |
255 | 281 | * @param $label string normalized property label |
256 | 282 | * @param $useAlias boolean determining whether to check if the label is an alias |
| 283 | + * |
257 | 284 | * @return mixed string property ID or false |
258 | 285 | */ |
259 | 286 | protected static function findPropertyID( $label, $useAlias = true ) { |
260 | 287 | SMWDIProperty::initPropertyRegistration(); |
261 | 288 | $id = array_search( $label, SMWDIProperty::$m_prop_labels ); |
| 289 | + |
262 | 290 | if ( $id !== false ) { |
263 | 291 | return $id; |
264 | 292 | } elseif ( ( $useAlias ) && ( array_key_exists( $label, SMWDIProperty::$m_prop_aliases ) ) ) { |
— | — | @@ -274,6 +302,7 @@ |
275 | 303 | * properties where isUserDefined() returns false. |
276 | 304 | * |
277 | 305 | * @param $key string key of the property |
| 306 | + * |
278 | 307 | * @return string type ID |
279 | 308 | */ |
280 | 309 | public static function getPredefinedPropertyTypeId( $key ) { |
— | — | @@ -288,6 +317,10 @@ |
289 | 318 | * Get the translated user label for a given internal property ID. |
290 | 319 | * Returns false for properties without a translation (these are |
291 | 320 | * usually internal, generated by SMW but not shown to the user). |
| 321 | + * |
| 322 | + * @param string $id |
| 323 | + * |
| 324 | + * @return string|false |
292 | 325 | */ |
293 | 326 | static protected function findPropertyLabel( $id ) { |
294 | 327 | SMWDIProperty::initPropertyRegistration(); |
— | — | @@ -363,6 +396,7 @@ |
364 | 397 | */ |
365 | 398 | static public function registerProperty( $id, $typeid, $label = false, $show = false ) { |
366 | 399 | SMWDIProperty::$m_prop_types[$id] = array( $typeid, $show ); |
| 400 | + |
367 | 401 | if ( $label != false ) { |
368 | 402 | SMWDIProperty::$m_prop_labels[$id] = $label; |
369 | 403 | } |
— | — | @@ -376,6 +410,7 @@ |
377 | 411 | * |
378 | 412 | * @param $id string id of a property |
379 | 413 | * @param $label string alias label for the property |
| 414 | + * |
380 | 415 | * @note Always use registerProperty() for the first label. No property |
381 | 416 | * that has used "false" for a label on registration should have an |
382 | 417 | * alias. |