r88534 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88533‎ | r88534 | r88535 >
Date:19:31, 21 May 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
moving the change related stuff to SWL
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/SMW_Settings.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_ChangeSet.php (deleted) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_PropertyChange.php (deleted) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_PropertyChanges.php (deleted) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_Store.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_PropertyChanges.php
@@ -1,178 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * A collection of semantic properties and changes changes made to them.
6 - * This class is based on SMWSemanticData and can be seen as a simplified
7 - * version with SMWPropertyChange objects, each holding 2 SMWDataItem objects,
8 - * instead of SMWDataItem objects.
9 - *
10 - * @since 1.6
11 - *
12 - * @file SMW_PropertyChange.php
13 - * @ingroup SMW
14 - *
15 - * @licence GNU GPL v3 or later
16 - * @author Jeroen De Dauw < jeroendedauw@gmail.com >
17 - */
18 -class SMWPropertyChanges implements Iterator {
19 -
20 - protected $pos = 0;
21 - protected $currentRow = null;
22 -
23 - /**
24 - * Cache for the localized version of the namespace prefix "Property:".
25 - *
26 - * @var string
27 - */
28 - static protected $propertyPrefix = '';
29 -
30 - /**
31 - * Array mapping property keys (string) to arrays of SMWPropertyChange.
32 - *
33 - * @var array of SMWPropertyChange
34 - */
35 - protected $changes = array();
36 -
37 - /**
38 - * Array mapping property keys (string) to SMWDIProperty objects.
39 - *
40 - * @var array of SMWDIProperty
41 - */
42 - protected $properties = array();
43 -
44 - /**
45 - * Indicates if there are changes in the list.
46 - *
47 - * @var boolean
48 - */
49 - protected $hasChanges = false;
50 -
51 - /**
52 - * Get the array of all properties that have changes.
53 - *
54 - * @return array of SMWDIProperty
55 - */
56 - public function getProperties() {
57 - return $this->properties;
58 - }
59 -
60 - /**
61 - * Returns if the list contains any changes.
62 - * This info is cached, so the call is cheaper then doing a count.
63 - *
64 - * @return boolean
65 - */
66 - public function hasChanges() {
67 - return $this->hasChanges;
68 - }
69 -
70 - /**
71 - * Get the array of all stored values for some property.
72 - *
73 - * @param $property SMWDIProperty
74 - *
75 - * @return array of SMWPropertyChange
76 - */
77 - public function getPropertyChanges( SMWDIProperty $property ) {
78 - if ( array_key_exists( $property->getKey(), $this->changes ) ) {
79 - return $this->changes[$property->getKey()];
80 - } else {
81 - return array();
82 - }
83 - }
84 -
85 - /**
86 - * Store a value for a property identified by its SMWDataItem object.
87 - *
88 - * @note There is no check whether the type of the given data item
89 - * agrees with the type of the property. Since property types can
90 - * change, all parts of SMW are prepared to handle mismatched data item
91 - * types anyway.
92 - *
93 - * @param SMWDIProperty $property
94 - * @param SMWPropertyChange $change
95 - */
96 - public function addPropertyObjectChange( SMWDIProperty $property, SMWPropertyChange $change ) {
97 - if ( $property->isInverse() ) { // inverse properties cannot be used for annotation
98 - return;
99 - }
100 -
101 - if ( !array_key_exists( $property->getKey(), $this->changes ) ) {
102 - $this->changes[$property->getKey()] = array();
103 - $this->properties[$property->getKey()] = $property;
104 - }
105 -
106 - $this->changes[$property->getKey()][] = $change;
107 -
108 - $this->hasChanges = true;
109 - }
110 -
111 - /**
112 - * Store a value for a given property identified by its text label
113 - * (without namespace prefix).
114 - *
115 - * @param string $propertyName
116 - * @param SMWPropertyChange $change
117 - */
118 - public function addPropertyChange( $propertyName, SMWPropertyChange $change ) {
119 - $propertyKey = smwfNormalTitleDBKey( $propertyName );
120 -
121 - if ( array_key_exists( $propertyKey, $this->properties ) ) {
122 - $property = $this->properties[$propertyKey];
123 - } else {
124 - if ( self::$propertyPrefix == '' ) {
125 - global $wgContLang;
126 - self::$propertyPrefix = $wgContLang->getNsText( SMW_NS_PROPERTY ) . ':';
127 - } // explicitly use prefix to cope with things like [[Property:User:Stupid::somevalue]]
128 -
129 - $propertyDV = SMWPropertyValue::makeUserProperty( self::$propertyPrefix . $propertyName );
130 -
131 - if ( !$propertyDV->isValid() ) { // error, maybe illegal title text
132 - return;
133 - }
134 -
135 - $property = $propertyDV->getDataItem();
136 - }
137 -
138 - $this->addPropertyObjectChange( $property, $change );
139 - }
140 -
141 - /**
142 - * Removes all changes for a certian property.
143 - *
144 - * @param SMWDIProperty $property
145 - */
146 - public function removeChangesForProperty( SMWDIProperty $property ) {
147 - if ( array_key_exists( $property->getKey(), $this->changes ) ) {
148 - unset( $this->changes[$property->getKey()] );
149 - unset( $this->properties[$property->getKey()] );
150 - }
151 - }
152 -
153 - function rewind() {
154 - $this->pos = 0;
155 - $this->currentRow = null;
156 - }
157 -
158 - function current() {
159 - if ( is_null( $this->currentRow ) ) {
160 - $this->next();
161 - }
162 - return $this->currentRow;
163 - }
164 -
165 - function key() {
166 - return $this->pos;
167 - }
168 -
169 - function next() {
170 - $this->pos++;
171 - $this->currentRow = array_key_exists( $this->pos, $this->changes ) ? $this->changes[$this->pos] : false;
172 - return $this->currentRow;
173 - }
174 -
175 - function valid() {
176 - return $this->current() !== false;
177 - }
178 -
179 -}
\ No newline at end of file
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_PropertyChange.php
@@ -1,100 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * Represents a change to a semantic property.
6 - *
7 - * @since 1.6
8 - *
9 - * @file SMW_PropertyChange.php
10 - * @ingroup SMW
11 - *
12 - * @licence GNU GPL v3 or later
13 - * @author Jeroen De Dauw < jeroendedauw@gmail.com >
14 - */
15 -class SMWPropertyChange {
16 -
17 - const TYPE_INSERT = 0;
18 - const TYPE_UPDATE = 1;
19 - const TYPE_DELETE = 2;
20 -
21 - /**
22 - * The old value.
23 - *
24 - * @var SMWDataItem or null
25 - */
26 - protected $oldValue;
27 -
28 - /**
29 - * The new value.
30 - *
31 - * @var SMWDataItem or null
32 - */
33 - protected $newValue;
34 -
35 - /**
36 - * Creates and returns a new SMWPropertyChange instance from a serialization.
37 - *
38 - * @param string|null $oldValue
39 - * @param string|null $newValue
40 - *
41 - * @return SMWPropertyChange
42 - */
43 - public static function newFromSerialization( SMWDIProperty $property, $oldValue, $newValue ) {
44 - $diType = SMWDataValueFactory::getDataItemId( $property->findPropertyTypeID() );
45 - //var_dump($property);
46 - //if($diType!=7) {throw new Exception();exit;}
47 - return new self(
48 - is_null( $oldValue ) ? null : SMWDataItem::newFromSerialization( $diType, $oldValue ),
49 - is_null( $newValue ) ? null : SMWDataItem::newFromSerialization( $diType, $newValue )
50 - );
51 - }
52 -
53 - /**
54 - * Create a new SMWPropertyChange.
55 - *
56 - * @param SMWDataItem $oldValue
57 - * @param SMWDataItem $newValue
58 - */
59 - public function __construct( /* SMWDataItem */ $oldValue, /* SMWDataItem */ $newValue ) {
60 - $this->oldValue = $oldValue;
61 - $this->newValue = $newValue;
62 - }
63 -
64 - /**
65 - * Retruns the old value, or null if there is none.
66 - *
67 - * @return SMWDataItem or null
68 - */
69 - public function getOldValue() {
70 - return $this->oldValue;
71 - }
72 -
73 -
74 - /**
75 - * returns the new value, or null if there is none.
76 - *
77 - * @return SMWDataItem or null
78 - */
79 - public function getNewValue() {
80 - return $this->newValue;
81 - }
82 -
83 - /**
84 - * Returns the type of the change.
85 - *
86 - * @return element of the SMWPropertyChange::TYPE_ enum
87 - */
88 - public function getType() {
89 - if ( is_null( $this->oldValue ) ) {
90 - return self::TYPE_INSERT;
91 - }
92 - else if ( is_null( $this->newValue ) ) {
93 - return self::TYPE_DELETE;
94 - }
95 - else {
96 - return self::TYPE_UPDATE;
97 - }
98 - }
99 -
100 -}
101 -
\ No newline at end of file
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_ChangeSet.php
@@ -1,289 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * This class represents a semantic property diff between 2 versions
6 - * of a single page.
7 - *
8 - * @since 1.6
9 - *
10 - * @file SMW_ChangeSet.php
11 - * @ingroup SMW
12 - *
13 - * @licence GNU GPL v3 or later
14 - * @author Jeroen De Dauw < jeroendedauw@gmail.com >
15 - */
16 -class SMWChangeSet {
17 -
18 - /**
19 - * The subject the changes apply to.
20 - *
21 - * @var SMWDIWikiPage
22 - */
23 - protected $subject;
24 -
25 - /**
26 - * Object holding semantic data that got inserted.
27 - *
28 - * @var SMWSemanticData
29 - */
30 - protected $insertions;
31 -
32 - /**
33 - * Object holding semantic data that got deleted.
34 - *
35 - * @var SMWSemanticData
36 - */
37 - protected $deletions;
38 -
39 - /**
40 - * List of all changes(, not including insertions and deletions).
41 - *
42 - * @var SMWPropertyChanges
43 - */
44 - protected $changes;
45 -
46 - /**
47 - * Creates and returns a new SMWChangeSet from 2 SMWSemanticData objects.
48 - *
49 - * @param SMWSemanticData $old
50 - * @param SMWSemanticData $new
51 - *
52 - * @return SMWChangeSet
53 - */
54 - public static function newFromSemanticData( SMWSemanticData $old, SMWSemanticData $new ) {
55 - $subject = $old->getSubject();
56 -
57 - if ( $subject != $new->getSubject() ) {
58 - return new self( $subject );
59 - }
60 -
61 - $changes = new SMWPropertyChanges();
62 - $insertions = new SMWSemanticData( $subject );
63 - $deletions = new SMWSemanticData( $subject );
64 -
65 - $oldProperties = $old->getProperties();
66 - $newProperties = $new->getProperties();
67 -
68 - // Find the deletions.
69 - self::findSingleDirectionChanges( $deletions, $oldProperties, $old, $newProperties );
70 -
71 - // Find the insertions.
72 - self::findSingleDirectionChanges( $insertions, $newProperties, $new, $oldProperties );
73 -
74 - foreach ( $oldProperties as $propertyKey => /* SMWDIProperty */ $diProperty ) {
75 - $oldDataItems = array();
76 - $newDataItems = array();
77 -
78 - // Populate the data item arrays using keys that are their hash, so matches can be found.
79 - // Note: this code assumes there are no duplicates.
80 - foreach ( $old->getPropertyValues( $diProperty ) as /* SMWDataItem */ $dataItem ) {
81 - $oldDataItems[$dataItem->getHash()] = $dataItem;
82 - }
83 - foreach ( $new->getPropertyValues( $diProperty ) as /* SMWDataItem */ $dataItem ) {
84 - $newDataItems[$dataItem->getHash()] = $dataItem;
85 - }
86 -
87 - $foundMatches = array();
88 -
89 - // Find values that are both in the old and new version.
90 - foreach ( array_keys( $oldDataItems ) as $hash ) {
91 - if ( array_key_exists( $hash, $newDataItems ) ) {
92 - $foundMatches[] = $hash;
93 - }
94 - }
95 -
96 - // Remove the values occuring in both sets, so only changes remain.
97 - foreach ( $foundMatches as $foundMatch ) {
98 - unset( $oldDataItems[$foundMatch] );
99 - unset( $newDataItems[$foundMatch] );
100 - }
101 -
102 - // Find which group is biggest, so it's easy to loop over all values of the smallest.
103 - $oldIsBigger = count( $oldDataItems ) > count ( $newDataItems );
104 - $bigGroup = $oldIsBigger ? $oldDataItems : $newDataItems;
105 - $smallGroup = $oldIsBigger ? $newDataItems : $oldDataItems;
106 -
107 - // Add all one-to-one changes.
108 - while ( $dataItem = array_shift( $smallGroup ) ) {
109 - $changes->addPropertyObjectChange( $diProperty, new SMWPropertyChange( $dataItem, array_shift( $bigGroup ) ) );
110 - }
111 -
112 - // If the bigger group is not-equal to the smaller one, items will be left,
113 - // that are either insertions or deletions, depending on the group.
114 - if ( count( $bigGroup > 0 ) ) {
115 - $semanticData = $oldIsBigger ? $deletions : $insertions;
116 -
117 - foreach ( $bigGroup as /* SMWDataItem */ $dataItem ) {
118 - $semanticData->addPropertyObjectValue( $diProperty, $dataItem );
119 - }
120 - }
121 - }
122 -
123 - return new self( $subject, $changes, $insertions, $deletions );
124 - }
125 -
126 - /**
127 - * Finds the inserts or deletions and adds them to the passed SMWSemanticData object.
128 - * These values will also be removed from the first list of properties and their values,
129 - * so it can be used for one-to-one change finding later on.
130 - *
131 - * @param SMWSemanticData $changeSet
132 - * @param array $oldProperties
133 - * @param SMWSemanticData $oldData
134 - * @param array $newProperties
135 - */
136 - protected static function findSingleDirectionChanges( SMWSemanticData &$changeSet,
137 - array &$oldProperties, SMWSemanticData $oldData, array $newProperties ) {
138 -
139 - $deletionKeys = array();
140 -
141 - foreach ( $oldProperties as $propertyKey => /* SMWDIProperty */ $diProperty ) {
142 - if ( !array_key_exists( $propertyKey, $newProperties ) ) {
143 - foreach ( $oldData->getPropertyValues( $diProperty ) as /* SMWDataItem */ $dataItem ) {
144 - $changeSet->addPropertyObjectValue( $diProperty, $dataItem );
145 - }
146 - $deletionKeys[] = $propertyKey;
147 - }
148 - }
149 -
150 - foreach ( $deletionKeys as $key ) {
151 - unset( $oldProperties[$propertyKey] );
152 - }
153 - }
154 -
155 - /**
156 - * Create a new instance of a change set.
157 - *
158 - * @param SMWDIWikiPage $subject
159 - * @param SMWPropertyChanges $changes Can be null
160 - * @param SMWSemanticData $insertions Can be null
161 - * @param SMWSemanticData $deletions Can be null
162 - */
163 - public function __construct( SMWDIWikiPage $subject, /* SMWPropertyChanges */ $changes = null,
164 - /* SMWSemanticData */ $insertions = null, /* SMWSemanticData */ $deletions = null ) {
165 -
166 - $this->subject = $subject;
167 - $this->changes = is_null( $changes ) ? new SMWPropertyChanges() : $changes;
168 - $this->insertions = is_null( $insertions ) ? new SMWSemanticData( $subject ): $insertions;
169 - $this->deletions = is_null( $deletions ) ? new SMWSemanticData( $subject ): $deletions;
170 - }
171 -
172 - /**
173 - * Returns whether the set contains any changes.
174 - *
175 - * @param boolean $refresh
176 - *
177 - * @return boolean
178 - */
179 - public function hasChanges( $refresh = false ) {
180 - return $this->changes->hasChanges()
181 - || $this->insertions->hasVisibleProperties( $refresh )
182 - || $this->deletions->hasVisibleProperties( $refresh );
183 - }
184 -
185 - /**
186 - * Returns a SMWSemanticData object holding all inserted SMWDataItem objects.
187 - *
188 - * @return SMWSemanticData
189 - */
190 - public function getInsertions() {
191 - return $this->insertions;
192 - }
193 -
194 - /**
195 - * Returns a SMWSemanticData object holding all deleted SMWDataItem objects.
196 - *
197 - * @return SMWSemanticData
198 - */
199 - public function getDeletions() {
200 - return $this->deletions;
201 - }
202 -
203 - /**
204 - * Returns a SMWPropertyChanges object holding all SMWPropertyChange objects.
205 - *
206 - * @return SMWPropertyChanges
207 - */
208 - public function getChanges() {
209 - return $this->changes;
210 - }
211 -
212 - /**
213 - * Returns the subject these changes apply to.
214 - *
215 - * @return SMWDIWikiPage
216 - */
217 - public function getSubject() {
218 - return $this->subject;
219 - }
220 -
221 - /**
222 - * Adds a SMWPropertyChange to the set for the specified SMWDIProperty.
223 - *
224 - * @param SMWDIProperty $property
225 - * @param SMWPropertyChange $change
226 - */
227 - public function addChange( SMWDIProperty $property, SMWPropertyChange $change ) {
228 - switch ( $change->getType() ) {
229 - case SMWPropertyChange::TYPE_UPDATE:
230 - $this->changes->addPropertyObjectChange( $property, $change );
231 - break;
232 - case SMWPropertyChange::TYPE_INSERT:
233 - $this->insertions->addPropertyObjectValue( $property, $change->getNewValue() );
234 - break;
235 - case SMWPropertyChange::TYPE_DELETE:
236 - $this->deletions->addPropertyObjectValue( $property, $change->getOldValue() );
237 - break;
238 - }
239 - }
240 -
241 - /**
242 - * Returns a list of all properties.
243 - *
244 - * @return array of SMWDIProperty
245 - */
246 - public function getAllProperties() {
247 - return array_merge(
248 - $this->getChanges()->getProperties(),
249 - $this->getInsertions()->getProperties(),
250 - $this->getDeletions()->getProperties()
251 - );
252 - }
253 -
254 - /**
255 - * Removes all changes for a certian property.
256 - *
257 - * @param SMWDIProperty $property
258 - */
259 - public function removeChangesForProperty( SMWDIProperty $property ) {
260 - $this->getChanges()->removeChangesForProperty( $property );
261 - $this->getInsertions()->removeDataForProperty( $property );
262 - $this->getDeletions()->removeDataForProperty( $property );
263 - }
264 -
265 - /**
266 - * Returns a list of ALL changes, including isertions and deletions.
267 - *
268 - * @param SMWDIProperty $proprety
269 - *
270 - * @return array of SMWPropertyChange
271 - */
272 - public function getAllPropertyChanges( SMWDIProperty $property ) {
273 - $changes = array();
274 -
275 - foreach ( $this->changes->getPropertyChanges( $property ) as /* SMWPropertyChange */ $change ) {
276 - $changes[] = $change;
277 - }
278 -
279 - foreach ( $this->insertions->getPropertyValues( $property ) as /* SMWDataItem */ $dataItem ) {
280 - $changes[] = new SMWPropertyChange( null, $dataItem );
281 - }
282 -
283 - foreach ( $this->deletions->getPropertyValues( $property ) as /* SMWDataItem */ $dataItem ) {
284 - $changes[] = new SMWPropertyChange( $dataItem, null );
285 - }
286 -
287 - return $changes;
288 - }
289 -
290 -}
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_Store.php
@@ -283,16 +283,6 @@
284284 public function updateData( SMWSemanticData $data ) {
285285 wfRunHooks( 'SMWStore::updateDataBefore', array( $this, $data ) );
286286
287 - global $smwgCheckChangesBeforeUpdate;
288 - if ( $smwgCheckChangesBeforeUpdate && $data->hasVisibleProperties() ) {
289 - $oldData = $this->getSemanticData( $data->getSubject() );
290 - $changeSet = SMWChangeSet::newFromSemanticData( $oldData, $data );
291 -
292 - if ( $changeSet->hasChanges() ) {
293 - wfRunHooks( 'SMWStore::dataChanged', array( $this, $changeSet ) );
294 - }
295 - }
296 -
297287 // Invalidate the page, so data stored on it gets displayed immediately in queries.
298288 global $smwgAutoRefreshSubject;
299289 if ( $smwgAutoRefreshSubject && !wfReadOnly() ) {
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
@@ -100,7 +100,6 @@
101101
102102 // Set up autoloading; essentially all classes should be autoloaded!
103103 $incDir = $smwgIP . 'includes/';
104 - $wgAutoloadClasses['SMWChangeSet'] = $incDir . 'SMW_ChangeSet.php';
105104 $wgAutoloadClasses['SMWCompatibilityHelpers'] = $incDir . 'SMW_CompatibilityHelpers.php';
106105 $wgAutoloadClasses['SMWDataValueFactory'] = $incDir . 'SMW_DataValueFactory.php';
107106 $wgAutoloadClasses['SMWFactbox'] = $incDir . 'SMW_Factbox.php';
@@ -108,8 +107,6 @@
109108 $wgAutoloadClasses['SMWOutputs'] = $incDir . 'SMW_Outputs.php';
110109 $wgAutoloadClasses['SMWParseData'] = $incDir . 'SMW_ParseData.php';
111110 $wgAutoloadClasses['SMWParserExtensions'] = $incDir . 'SMW_ParserExtensions.php';
112 - $wgAutoloadClasses['SMWPropertyChange'] = $incDir . 'SMW_PropertyChange.php';
113 - $wgAutoloadClasses['SMWPropertyChanges'] = $incDir . 'SMW_PropertyChanges.php';
114111 $wgAutoloadClasses['SMWQueryLanguage'] = $incDir . 'SMW_QueryLanguage.php';
115112 $wgAutoloadClasses['SMWSemanticData'] = $incDir . 'SMW_SemanticData.php';
116113 $wgAutoloadClasses['SMWPageLister'] = $incDir . 'SMW_PageLister.php';
Index: trunk/extensions/SemanticMediaWiki/SMW_Settings.php
@@ -485,11 +485,3 @@
486486 ##
487487 $smwgAutoRefreshSubject = true;
488488 ##
489 -
490 -###
491 -# Sets whether or not SMW should check if properties where changed and update only those
492 -# instead of not checking what changed and simply updating everything.
493 -# Introduced in SMW 1.6
494 -##
495 -$smwgCheckChangesBeforeUpdate = false;
496 -##
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r88542follow up to r88534jeroendedauw19:55, 21 May 2011

Status & tagging log