Index: trunk/extensions/Distribution/maintenance/getSvnMetadata.php |
— | — | @@ -152,22 +152,21 @@ |
153 | 153 | * |
154 | 154 | * @since 0.1 |
155 | 155 | * |
156 | | - * @param array $metaData |
| 156 | + * @param $metaData Array |
157 | 157 | */ |
158 | 158 | protected function saveExtensionMetadata( array $metaData ) { |
159 | 159 | // Get the database connections. |
160 | 160 | $dbr = wfGetDB( DB_SLAVE ); |
161 | | - $dbw = wfGetDB( DB_MASTER ); |
162 | 161 | |
163 | 162 | // Query for existing units with the same name. |
164 | | - $extension = $dbr->selectRow( |
| 163 | + $unit = $dbr->selectRow( |
165 | 164 | 'distribution_units', |
166 | 165 | array( 'unit_id' ), |
167 | 166 | array( 'unit_name' => $metaData['name'] ) |
168 | 167 | ); |
169 | 168 | |
170 | | - // Map the values to the db schema. |
171 | | - $values = array( |
| 169 | + // Map the unit values to the db schema. |
| 170 | + $unitValues = array( |
172 | 171 | 'unit_name' => $metaData['name'], |
173 | 172 | 'current_version_nr' => $metaData['version'], |
174 | 173 | 'current_desc' => $metaData['description'], |
— | — | @@ -175,23 +174,100 @@ |
176 | 175 | 'current_url' => $metaData['url'], |
177 | 176 | ); |
178 | 177 | |
179 | | - // Insert or update depending on if it already exists. |
180 | | - if ( $extension == false ) { |
| 178 | + // Map the version values to the db schema. |
| 179 | + $versionValues = array( |
| 180 | + 'version_status' => 0, // TODO |
| 181 | + 'version_desc' => $metaData['description'], |
| 182 | + 'version_authors' => $metaData['authors'], |
| 183 | + 'version_url' => $metaData['url'], |
| 184 | + ); |
| 185 | + |
| 186 | + // Insert or update the unit. |
| 187 | + if ( $unit == false ) { |
| 188 | + $this->insertUnit( $unitValues, $versionValues ); |
| 189 | + } |
| 190 | + else { |
| 191 | + $this->updateUnit( $unit, $unitValues, $versionValues, $dbr ); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Inserts a new unit and creates a new version for this unit. |
| 197 | + * |
| 198 | + * @since 0.1 |
| 199 | + * |
| 200 | + * @param $unitValues Array |
| 201 | + * @param $versionValues Array |
| 202 | + */ |
| 203 | + protected function insertUnit( array $unitValues, array $versionValues ) { |
| 204 | + $dbw = wfGetDB( DB_MASTER ); |
| 205 | + |
| 206 | + $dbw->insert( |
| 207 | + 'distribution_units', |
| 208 | + $unitValues |
| 209 | + ); |
| 210 | + |
| 211 | + $versionValues['version_nr'] = $unitValues['current_version_nr']; |
| 212 | + $versionValues['unit_id'] = $dbw->insertId(); |
| 213 | + |
| 214 | + $dbw->insert( |
| 215 | + 'distribution_unit_versions', |
| 216 | + $versionValues |
| 217 | + ); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Updates an existing unit. If the unit already had a version for the current number, |
| 222 | + * it will be updated, otherwise a new one will be created. |
| 223 | + * |
| 224 | + * @since 0.1 |
| 225 | + * |
| 226 | + * @param $unit Array |
| 227 | + * @param $unitValues Array |
| 228 | + * @param $versionValues Array |
| 229 | + * @param $dbr DatabaseBase |
| 230 | + */ |
| 231 | + protected function updateUnit( array $unit, array $unitValues, array $versionValues, DatabaseBase $dbr ) { |
| 232 | + $dbw = wfGetDB( DB_MASTER ); |
| 233 | + |
| 234 | + $versionValues['unit_id'] = $unit['unit_id']; |
| 235 | + |
| 236 | + // Query for existing versions of this unit with the same version number. |
| 237 | + $version = $dbr->selectRow( |
| 238 | + 'distribution_unit_versions', |
| 239 | + array( 'version_id' ), |
| 240 | + array( |
| 241 | + 'unit_id' => $unit['unit_id'], |
| 242 | + 'version_nr' => $unitValues['current_version_nr'] |
| 243 | + ) |
| 244 | + ); |
| 245 | + |
| 246 | + if ( $version == false ) { |
| 247 | + $versionValues['version_nr'] = $unitValues['current_version_nr']; |
| 248 | + |
181 | 249 | $dbw->insert( |
182 | | - 'distribution_units', |
183 | | - $values |
184 | | - ); |
| 250 | + 'distribution_unit_versions', |
| 251 | + $versionValues |
| 252 | + ); |
| 253 | + |
| 254 | + $unitValues['current_version_nr'] = $dbw->insertId(); |
185 | 255 | } |
186 | 256 | else { |
187 | 257 | $dbw->update( |
188 | | - 'distribution_units', |
189 | | - $values, |
190 | | - array( 'unit_name' => $metaData['name'] ) |
191 | | - ); |
| 258 | + 'distribution_unit_versions', |
| 259 | + $versionValues, |
| 260 | + array( 'version_id' => $version['version_id'] ) |
| 261 | + ); |
| 262 | + |
| 263 | + $unitValues['current_version_nr'] = $version['version_id']; |
192 | 264 | } |
193 | 265 | |
194 | | - // TODO: distribution_unit_versions |
195 | | - } |
| 266 | + $dbw->update( |
| 267 | + 'distribution_units', |
| 268 | + $unitValues, |
| 269 | + array( 'unit_id' => $unit['unit_id'] ) |
| 270 | + ); |
| 271 | + } |
196 | 272 | |
197 | 273 | } |
198 | 274 | |