Index: trunk/extensions/SemanticInternalObjects/SemanticInternalObjects_body.php |
— | — | @@ -10,26 +10,28 @@ |
11 | 11 | * its properties. |
12 | 12 | */ |
13 | 13 | class SIOInternalObject { |
14 | | - var $main_title; |
15 | | - var $index; |
16 | | - var $property_value_pairs; |
| 14 | + public $mPropertyValuePairs; |
| 15 | + |
| 16 | + protected $mMainTitle; |
| 17 | + protected $mIndex; |
17 | 18 | |
18 | | - public function SIOInternalObject( $main_title, $index ) { |
19 | | - $this->main_title = $main_title; |
20 | | - $this->index = $index; |
21 | | - $this->property_value_pairs = array(); |
| 19 | + public function SIOInternalObject( $mainTitle, $index ) { |
| 20 | + $this->mMainTitle = $mainTitle; |
| 21 | + $this->mIndex = $index; |
| 22 | + $this->mPropertyValuePairs = array(); |
22 | 23 | } |
23 | 24 | |
24 | | - public function addPropertyAndValue( $prop_name, $value ) { |
25 | | - $property = SMWPropertyValue::makeUserProperty( $prop_name ); |
26 | | - $data_value = SMWDataValueFactory::newPropertyObjectValue( $property, $value ); |
27 | | - if ( $data_value->isValid() ) { |
28 | | - $this->property_value_pairs[] = array( $property, $data_value ); |
| 25 | + public function addPropertyAndValue( $propName, $value ) { |
| 26 | + $property = SMWPropertyValue::makeUserProperty( $propName ); |
| 27 | + $dataValue = SMWDataValueFactory::newPropertyObjectValue( $property, $value ); |
| 28 | + |
| 29 | + if ( $dataValue->isValid() ) { |
| 30 | + $this->mPropertyValuePairs[] = array( $property, $dataValue ); |
29 | 31 | } // else - show an error message? |
30 | 32 | } |
31 | 33 | |
32 | 34 | public function getName() { |
33 | | - return $this->main_title->getDBkey() . '#' . $this->index; |
| 35 | + return $this->mMainTitle->getDBkey() . '#' . $this->mIndex; |
34 | 36 | } |
35 | 37 | } |
36 | 38 | |
— | — | @@ -46,21 +48,26 @@ |
47 | 49 | $iw = ''; |
48 | 50 | $db = wfGetDB( DB_SLAVE ); |
49 | 51 | $res = $db->select( 'smw_ids', array( 'smw_id' ), 'smw_title LIKE ' . $db->addQuotes( $page_name . '#%' ) . ' AND ' . 'smw_namespace=' . $db->addQuotes( $namespace ) . ' AND smw_iw=' . $db->addQuotes( $iw ), 'SIO::getSMWPageObjectIDs', array() ); |
| 52 | + |
50 | 53 | while ( $row = $db->fetchObject( $res ) ) { |
51 | 54 | $ids[] = $row->smw_id; |
52 | 55 | } |
| 56 | + |
53 | 57 | return $ids; |
54 | 58 | } |
55 | 59 | |
56 | 60 | function getStorageSQL( $main_page_name, $namespace, $internal_object ) { |
57 | 61 | $main_page_id = $this->makeSMWPageID( $main_page_name, $namespace, '' ); |
58 | 62 | $io_id = $this->makeSMWPageID( $internal_object->getName(), $namespace, '' ); |
| 63 | + |
59 | 64 | $up_rels2 = array(); |
60 | 65 | $up_atts2 = array(); |
61 | 66 | $up_text2 = array(); |
| 67 | + |
62 | 68 | // set all the properties pointing from this internal object |
63 | | - foreach ( $internal_object->property_value_pairs as $property_value_pair ) { |
| 69 | + foreach ( $internal_object->mPropertyValuePairs as $property_value_pair ) { |
64 | 70 | list( $property, $value ) = $property_value_pair; |
| 71 | + |
65 | 72 | // handling changed in SMW 1.5 |
66 | 73 | if ( method_exists( 'SMWSQLStore2', 'findPropertyTableID' ) ) { |
67 | 74 | $tableid = SMWSQLStore2::findPropertyTableID( $property ); |
— | — | @@ -73,6 +80,7 @@ |
74 | 81 | $is_attribute = ( $mode == SMW_SQL2_ATTS2 ); |
75 | 82 | $is_text = ( $mode == SMW_SQL2_TEXT2 ); |
76 | 83 | } |
| 84 | + |
77 | 85 | if ( $is_relation ) { |
78 | 86 | $up_rels2[] = array( |
79 | 87 | 's_id' => $io_id, |
— | — | @@ -81,27 +89,31 @@ |
82 | 90 | ); |
83 | 91 | } elseif ( $is_attribute ) { |
84 | 92 | $keys = $value->getDBkeys(); |
| 93 | + |
85 | 94 | if ( method_exists( $value, 'getValueKey' ) ) { |
86 | 95 | $value_num = $value->getValueKey(); |
87 | 96 | } else { |
88 | 97 | $value_num = $value->getNumericValue(); |
89 | 98 | } |
| 99 | + |
90 | 100 | $up_atts2[] = array( |
91 | 101 | 's_id' => $io_id, |
92 | 102 | 'p_id' => $this->makeSMWPropertyID( $property ), |
93 | 103 | 'value_unit' => $value->getUnit(), |
94 | | - 'value_xsd' => $keys[0], |
| 104 | + 'value_xsd' => $keys[0], // FIXME: this fails for all datatypes with more then one value field |
95 | 105 | 'value_num' => $value_num |
96 | 106 | ); |
97 | 107 | } elseif ( $is_text ) { |
98 | 108 | $keys = $value->getDBkeys(); |
| 109 | + |
99 | 110 | $up_text2[] = array( |
100 | 111 | 's_id' => $io_id, |
101 | 112 | 'p_id' => $this->makeSMWPropertyID( $property ), |
102 | | - 'value_blob' => $keys[0] |
| 113 | + 'value_blob' => $keys[0] // FIXME: this fails for all datatypes with more then one value field |
103 | 114 | ); |
104 | 115 | } |
105 | 116 | } |
| 117 | + |
106 | 118 | return array( $up_rels2, $up_atts2, $up_text2 ); |
107 | 119 | } |
108 | 120 | } |
— | — | @@ -120,12 +132,14 @@ |
121 | 133 | self::$cur_page_name = ''; |
122 | 134 | self::$cur_page_namespace = 0; |
123 | 135 | self::$internal_object_index = 1; |
| 136 | + |
124 | 137 | return true; |
125 | 138 | } |
126 | 139 | |
127 | 140 | public static function doSetInternal( &$parser ) { |
128 | 141 | $main_page_name = $parser->getTitle()->getDBKey(); |
129 | 142 | $main_page_namespace = $parser->getTitle()->getNamespace(); |
| 143 | + |
130 | 144 | if ( $main_page_name == self::$cur_page_name && |
131 | 145 | $main_page_namespace == self::$cur_page_namespace ) { |
132 | 146 | self::$internal_object_index++; |
— | — | @@ -134,26 +148,35 @@ |
135 | 149 | self::$cur_page_namespace = $main_page_namespace; |
136 | 150 | self::$internal_object_index = 1; |
137 | 151 | } |
| 152 | + |
138 | 153 | $cur_object_num = self::$internal_object_index; |
| 154 | + |
139 | 155 | $params = func_get_args(); |
140 | 156 | array_shift( $params ); // we already know the $parser... |
| 157 | + |
141 | 158 | $internal_object = new SIOInternalObject( $parser->getTitle(), $cur_object_num ); |
142 | 159 | $obj_to_page_prop_name = array_shift( $params ); |
143 | 160 | $main_page_name = $parser->getTitle()->getText(); |
| 161 | + |
144 | 162 | if ( ( $ns_text = $parser->getTitle()->getNsText() ) != '' ) { |
145 | 163 | $main_page_name = $ns_text . ':' . $main_page_name; |
146 | 164 | } |
| 165 | + |
147 | 166 | $internal_object->addPropertyAndValue( $obj_to_page_prop_name, $main_page_name ); |
| 167 | + |
148 | 168 | foreach ( $params as $param ) { |
149 | | - $parts = explode( "=", trim( $param ), 2 ); |
| 169 | + $parts = explode( '=', trim( $param ), 2 ); |
| 170 | + |
150 | 171 | if ( count( $parts ) == 2 ) { |
151 | 172 | $key = $parts[0]; |
152 | 173 | $value = $parts[1]; |
| 174 | + |
153 | 175 | // if the property name ends with '#list', it's |
154 | 176 | // a comma-delimited group of values |
155 | 177 | if ( substr( $key, - 5 ) == '#list' ) { |
156 | 178 | $key = substr( $key, 0, strlen( $key ) - 5 ); |
157 | 179 | $list_values = explode( ',', $value ); |
| 180 | + |
158 | 181 | foreach ( $list_values as $list_value ) { |
159 | 182 | $internal_object->addPropertyAndValue( $key, trim( $list_value ) ); |
160 | 183 | } |
— | — | @@ -162,11 +185,13 @@ |
163 | 186 | } |
164 | 187 | } |
165 | 188 | } |
| 189 | + |
166 | 190 | self::$internal_objects[] = $internal_object; |
167 | 191 | } |
168 | 192 | |
169 | 193 | public static function updateData( $subject ) { |
170 | 194 | $sio_sql_store = new SIOSQLStore(); |
| 195 | + |
171 | 196 | // Find all "pages" in the SMW IDs table that are internal |
172 | 197 | // objects for this page, and delete their properties from |
173 | 198 | // the SMW tables. |
— | — | @@ -179,8 +204,10 @@ |
180 | 205 | $all_rels2_inserts = array(); |
181 | 206 | $all_atts2_inserts = array(); |
182 | 207 | $all_text2_inserts = array(); |
| 208 | + |
183 | 209 | foreach ( self::$internal_objects as $internal_object ) { |
184 | 210 | list( $up_rels2, $up_atts2, $up_text2 ) = $sio_sql_store->getStorageSQL( $page_name, $namespace, $internal_object ); |
| 211 | + |
185 | 212 | $all_rels2_inserts = array_merge( $all_rels2_inserts, $up_rels2 ); |
186 | 213 | $all_atts2_inserts = array_merge( $all_atts2_inserts, $up_atts2 ); |
187 | 214 | $all_text2_inserts = array_merge( $all_text2_inserts, $up_text2 ); |
— | — | @@ -189,6 +216,7 @@ |
190 | 217 | // now save everything to the database, in a single transaction |
191 | 218 | $db = wfGetDB( DB_MASTER ); |
192 | 219 | $db->begin( 'SIO::updatePageData' ); |
| 220 | + |
193 | 221 | if ( count( $ids_for_deletion ) > 0 ) { |
194 | 222 | $ids_string = '(' . implode ( ', ', $ids_for_deletion ) . ')'; |
195 | 223 | $db->delete( 'smw_rels2', array( "(s_id IN $ids_string) OR (o_id IN $ids_string)" ), 'SIO::deleteRels2Data' ); |
— | — | @@ -199,15 +227,20 @@ |
200 | 228 | if ( count( $all_rels2_inserts ) > 0 ) { |
201 | 229 | $db->insert( 'smw_rels2', $all_rels2_inserts, 'SIO::updateRels2Data' ); |
202 | 230 | } |
| 231 | + |
203 | 232 | if ( count( $all_atts2_inserts ) > 0 ) { |
204 | 233 | $db->insert( 'smw_atts2', $all_atts2_inserts, 'SIO::updateAtts2Data' ); |
205 | 234 | } |
| 235 | + |
206 | 236 | if ( count( $all_text2_inserts ) > 0 ) { |
207 | 237 | $db->insert( 'smw_text2', $all_text2_inserts, 'SIO::updateText2Data' ); |
208 | 238 | } |
| 239 | + |
209 | 240 | // end transaction |
210 | 241 | $db->commit( 'SIO::updatePageData' ); |
| 242 | + |
211 | 243 | self::$internal_objects = array(); |
| 244 | + |
212 | 245 | return true; |
213 | 246 | } |
214 | 247 | |
— | — | @@ -219,15 +252,19 @@ |
220 | 253 | */ |
221 | 254 | static function handleUpdatingOfInternalObjects( &$jobs ) { |
222 | 255 | $unique_titles = array(); |
| 256 | + |
223 | 257 | foreach ( $jobs as $i => $job ) { |
224 | 258 | $title = Title::makeTitleSafe( $job->title->getNamespace(), $job->title->getText() ); |
225 | 259 | $id = $title->getArticleID(); |
226 | 260 | $unique_titles[$id] = $title; |
227 | 261 | } |
| 262 | + |
228 | 263 | $jobs = array(); |
| 264 | + |
229 | 265 | foreach ( $unique_titles as $id => $title ) { |
230 | 266 | $jobs[] = new SMWUpdateJob( $title ); |
231 | 267 | } |
| 268 | + |
232 | 269 | return true; |
233 | 270 | } |
234 | 271 | |
— | — | @@ -238,13 +275,17 @@ |
239 | 276 | * will be in a single one of these batches so we remove everything updating |
240 | 277 | * a SIO object instead of filtering them down to unique titles. |
241 | 278 | */ |
242 | | - static function handleRefreshingOfInternalObjects(&$jobs) { |
| 279 | + static function handleRefreshingOfInternalObjects( &$jobs ) { |
243 | 280 | $all_jobs = $jobs; |
244 | 281 | $jobs = array(); |
| 282 | + |
245 | 283 | foreach ( $all_jobs as $job ) { |
246 | | - if ( strpos( $job->title->getText(), '#' ) === false ) |
| 284 | + if ( strpos( $job->title->getText(), '#' ) === false ) { |
247 | 285 | $jobs[] = $job; |
| 286 | + } |
248 | 287 | } |
| 288 | + |
249 | 289 | return true; |
250 | 290 | } |
251 | | -} |
| 291 | + |
| 292 | +} |
\ No newline at end of file |