Index: trunk/extensions/SemanticMediaWiki/includes/dataitems/SMW_DI_Container.php |
— | — | @@ -0,0 +1,142 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * @ingroup SMWDataItems |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Subclass of SMWSemanticData that can be make read only to enforce the |
| 10 | + * immutability of all SMW data items. This ensures that the container dataitem |
| 11 | + * can safely give out an object reference without concern that this is |
| 12 | + * exploited to indirectly change its content. |
| 13 | + * |
| 14 | + * Objects of this class are usually only made immutable when passed to a data |
| 15 | + * item, so they can be built as usual. When cloning the object, the clone |
| 16 | + * becomes mutable again. This is safe since all data is stored in arrays that |
| 17 | + * contain only immutable objects and values of basic types. Arrays are copied |
| 18 | + * (lazily) when cloning in PHP, so later changes in the cloce will not affect |
| 19 | + * the original. |
| 20 | + */ |
| 21 | +class SMWContainerSemanticData extends SMWSemanticData { |
| 22 | + |
| 23 | + /** |
| 24 | + * If true, the object will not allow further changes. |
| 25 | + * @var boolean |
| 26 | + */ |
| 27 | + protected $m_immutable = false; |
| 28 | + |
| 29 | + /** |
| 30 | + * Constructor. |
| 31 | + * |
| 32 | + * @param boolean $noDuplicates stating if duplicate data should be avoided |
| 33 | + */ |
| 34 | + public function __construct( $noDuplicates = true ) { |
| 35 | + $subject = new SMWDIWikiPage( 'SMWInternalObject', NS_SPECIAL, '' ); // dummy subject |
| 36 | + parent::__construct( $noDuplicates ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Clone handler. Make any clone mutable again. |
| 41 | + */ |
| 42 | + public function __clone() { |
| 43 | + $this->m_immutable = false; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Freeze the object: no more change operations allowed after calling |
| 48 | + * this. Normally this is only called when passing the object to an |
| 49 | + * SMWDIContainer. Other code should not need this. |
| 50 | + */ |
| 51 | + public function makeImmutable() { |
| 52 | + $this->m_immutable = true; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Store a value for a property identified by its SMWDataItem object, |
| 57 | + * if the object was not set to immutable. |
| 58 | + * |
| 59 | + * @param $property SMWDIProperty |
| 60 | + * @param $dataItem SMWDataItem |
| 61 | + */ |
| 62 | + public function addPropertyObjectValue( SMWDIProperty $property, SMWDataItem $dataItem ) { |
| 63 | + $this->throwImmutableException(); |
| 64 | + parent::addPropertyObjectValue( $property, $dataItem ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Delete all data other than the subject, if the object was not set to |
| 69 | + * immutable. |
| 70 | + */ |
| 71 | + public function clear() { |
| 72 | + $this->throwImmutableException(); |
| 73 | + parent::clear(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Throw an exception if the object is immutable. |
| 78 | + */ |
| 79 | + protected function throwImmutableException() { |
| 80 | + if ( $this->m_immutable ) { |
| 81 | + throw SMWDataItemException( 'Changing the SMWSemanticData object that belongs to a data item of type SMWDIContainer is not allowed. Data items are immutable.' ); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * This class implements container data items that can store SMWSemanticData |
| 88 | + * objects. In this sense, data items of this type are a kind of "internal |
| 89 | + * object" that can contain the data that is otherwise associated with a wiki |
| 90 | + * page. |
| 91 | + * |
| 92 | + * @since 1.6 |
| 93 | + * |
| 94 | + * @author Markus Krötzsch |
| 95 | + * @ingroup SMWDataItems |
| 96 | + */ |
| 97 | +class SMWDIContainer extends SMWDataItem { |
| 98 | + |
| 99 | + /** |
| 100 | + * Internal value. |
| 101 | + * @var SMWSemanticData |
| 102 | + */ |
| 103 | + protected $m_semanticData; |
| 104 | + |
| 105 | + /** |
| 106 | + * Constructor. The given SMWContainerSemanticData object will be owned |
| 107 | + * by the constructed object afterwards, and in particular will not |
| 108 | + * allow further changes. |
| 109 | + * |
| 110 | + * @param $semanticData SMWContainerSemanticData |
| 111 | + */ |
| 112 | + public function __construct( SMWContainerSemanticData $semanticData, $typeid = '_rec' ) { |
| 113 | + parent::__construct( $typeid ); |
| 114 | + $this->m_semanticData = $semanticData; |
| 115 | + $this->m_semanticData->makeImmutable(); |
| 116 | + } |
| 117 | + |
| 118 | + public function getDIType() { |
| 119 | + return SMWDataItem::TYPE_CONTAINER; |
| 120 | + } |
| 121 | + |
| 122 | + public function getSemanticData() { |
| 123 | + return $this->m_semanticData; |
| 124 | + } |
| 125 | + |
| 126 | + public function getSortKey() { |
| 127 | + return ''; |
| 128 | + } |
| 129 | + |
| 130 | + public function getSerialization() { |
| 131 | + return $this->m_string; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Create a data item from the provided serialization string and type |
| 136 | + * ID. |
| 137 | + * @return SMWDIContainer |
| 138 | + */ |
| 139 | + public static function doUnserialize( $serialization, $typeid = '_rec' ) { |
| 140 | + return new SMWDIBlob( $serialization, $typeid ); |
| 141 | + } |
| 142 | + |
| 143 | +} |
Property changes on: trunk/extensions/SemanticMediaWiki/includes/dataitems/SMW_DI_Container.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 144 | + native |
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php |
— | — | @@ -137,20 +137,22 @@ |
138 | 138 | |
139 | 139 | // Data items |
140 | 140 | $diDir = $smwgIP . 'includes/dataitems/'; |
141 | | - $wgAutoloadClasses['SMWDataItem'] = $diDir . 'SMW_DataItem.php'; |
142 | | - $wgAutoloadClasses['SMWDataItemException'] = $diDir . 'SMW_DataItem.php'; |
143 | | - $wgAutoloadClasses['SMWDIProperty'] = $diDir . 'SMW_DI_Property.php'; |
144 | | - $wgAutoloadClasses['SMWDIBoolean'] = $diDir . 'SMW_DI_Bool.php'; |
145 | | - $wgAutoloadClasses['SMWDINumber'] = $diDir . 'SMW_DI_Number.php'; |
146 | | - $wgAutoloadClasses['SMWDIBlob'] = $diDir . 'SMW_DI_Blob.php'; |
147 | | - $wgAutoloadClasses['SMWDIString'] = $diDir . 'SMW_DI_String.php'; |
| 141 | + $wgAutoloadClasses['SMWDataItem'] = $diDir . 'SMW_DataItem.php'; |
| 142 | + $wgAutoloadClasses['SMWDataItemException'] = $diDir . 'SMW_DataItem.php'; |
| 143 | + $wgAutoloadClasses['SMWDIProperty'] = $diDir . 'SMW_DI_Property.php'; |
| 144 | + $wgAutoloadClasses['SMWDIBoolean'] = $diDir . 'SMW_DI_Bool.php'; |
| 145 | + $wgAutoloadClasses['SMWDINumber'] = $diDir . 'SMW_DI_Number.php'; |
| 146 | + $wgAutoloadClasses['SMWDIBlob'] = $diDir . 'SMW_DI_Blob.php'; |
| 147 | + $wgAutoloadClasses['SMWDIString'] = $diDir . 'SMW_DI_String.php'; |
148 | 148 | $wgAutoloadClasses['SMWStringLengthException'] = $diDir . 'SMW_DI_String.php'; |
149 | | - $wgAutoloadClasses['SMWDIUri'] = $diDir . 'SMW_DI_URI.php'; |
150 | | - $wgAutoloadClasses['SMWDIWikiPage'] = $diDir . 'SMW_DI_WikiPage.php'; |
151 | | - $wgAutoloadClasses['SMWDITime'] = $diDir . 'SMW_DI_Time.php'; |
152 | | - $wgAutoloadClasses['SMWDIConcept'] = $diDir . 'SMW_DI_Concept.php'; |
153 | | - $wgAutoloadClasses['SMWDIError'] = $diDir . 'SMW_DI_Error.php'; |
154 | | - $wgAutoloadClasses['SMWDIGeoCoord'] = $diDir . 'SMW_DI_GeoCoord.php'; |
| 149 | + $wgAutoloadClasses['SMWDIUri'] = $diDir . 'SMW_DI_URI.php'; |
| 150 | + $wgAutoloadClasses['SMWDIWikiPage'] = $diDir . 'SMW_DI_WikiPage.php'; |
| 151 | + $wgAutoloadClasses['SMWDITime'] = $diDir . 'SMW_DI_Time.php'; |
| 152 | + $wgAutoloadClasses['SMWDIConcept'] = $diDir . 'SMW_DI_Concept.php'; |
| 153 | + $wgAutoloadClasses['SMWDIError'] = $diDir . 'SMW_DI_Error.php'; |
| 154 | + $wgAutoloadClasses['SMWDIGeoCoord'] = $diDir . 'SMW_DI_GeoCoord.php'; |
| 155 | + $wgAutoloadClasses['SMWContainerSemanticData'] = $diDir . 'SMW_DI_Container.php'; |
| 156 | + $wgAutoloadClasses['SMWDIContainer'] = $diDir . 'SMW_DI_Container.php'; |
155 | 157 | |
156 | 158 | // Datavalues |
157 | 159 | $dvDir = $smwgIP . 'includes/datavalues/'; |