Index: trunk/extensions/Wikidata/OmegaWiki/WikiDataAPI.php |
— | — | @@ -1129,6 +1129,9 @@ |
1130 | 1130 | /** Write a concept mapping to db |
1131 | 1131 | * supply mapping as a valid |
1132 | 1132 | * array("dataset_prefix"=>defined_meaning_id,...) |
| 1133 | + * @returns: uuid used for mapping. (typically you an just |
| 1134 | + * discard this, but it's kinda important if you |
| 1135 | + * want to add objects table support |
1133 | 1136 | */ |
1134 | 1137 | |
1135 | 1138 | function createConceptMapping($concepts) { |
— | — | @@ -1139,6 +1142,7 @@ |
1140 | 1143 | writeDmToCollection($dc, $collid, $uuid_map[$dc], $dm_id); |
1141 | 1144 | } |
1142 | 1145 | } |
| 1146 | + return $uuid_map; |
1143 | 1147 | } |
1144 | 1148 | |
1145 | 1149 | function getMapping( $dc, $collid, $dm_id ){ |
— | — | @@ -1513,4 +1517,4 @@ |
1514 | 1518 | } |
1515 | 1519 | |
1516 | 1520 | |
1517 | | -} |
\ No newline at end of file |
| 1521 | +} |
Index: trunk/extensions/Wikidata/OmegaWiki/Copy.php |
— | — | @@ -26,6 +26,45 @@ |
27 | 27 | # * some read/write/dup functions are still main namespace, should get their own |
28 | 28 | # * classes (!!) |
29 | 29 | |
| 30 | +# How to use: |
| 31 | +# |
| 32 | +# Step 1: |
| 33 | +# Set up one of those fancy wikidata transactions on dc2 |
| 34 | +# Don't have one? CopyTools::newCopyTransaction($dc1, $dc2) is your friend |
| 35 | +# |
| 36 | +# Step 2: |
| 37 | +# copy |
| 38 | +# $copier=new DefinedMeaningCopier($dmid, $dc1, $dc2); |
| 39 | +# $copier->dup() |
| 40 | +# |
| 41 | +# et voila! |
| 42 | +# |
| 43 | +# Optional 1: |
| 44 | +# dup()ing something will return the new id. |
| 45 | +# so ie you can get the dmid (defined meaning id) in dc2 (the |
| 46 | +# destination dataset) with: |
| 47 | +# $newDmid=$copier->dup(); |
| 48 | +# |
| 49 | +# note that if something is already copied, no new |
| 50 | +# copy is made, but dup() will still return the appropriate |
| 51 | +# new id. |
| 52 | +# |
| 53 | +# This behaviour is only true for singular items (like defined meanings). |
| 54 | +# O classes that duplicate entire sets of items at once, the dup method |
| 55 | +# currently returns nothing. Logically it should really return an array, |
| 56 | +# but I haven't really found anything that needs that yet, so haven't |
| 57 | +# made an effort. |
| 58 | +# |
| 59 | +# Optional 2: |
| 60 | +# If you attempt to dup something that was already there, nothing will |
| 61 | +# happen, the item will not be dupped and the already_there flag will be set. |
| 62 | +# Querying $something->already_there() will return true in that case. |
| 63 | +# |
| 64 | +# TWarning on testing: |
| 65 | +# so far I've only tested this thoroughly where DefinedMeaningCopier is the entry point. |
| 66 | +# In all other cases: YMMV, HTH, HAND. |
| 67 | +# (we so need unit testing ^^;;) |
| 68 | + |
30 | 69 | #require_once("../../../StartProfiler.php"); |
31 | 70 | #include_once("../../../includes/Defines.php"); |
32 | 71 | #include_once("../../../LocalSettings.php"); |
— | — | @@ -46,7 +85,26 @@ |
47 | 86 | protected $dc2; |
48 | 87 | protected $object; |
49 | 88 | protected $already_there=null; |
| 89 | + protected $autovivify=false; # tradeoff: create object references if not found, |
| 90 | + # but catch less errors |
| 91 | + protected $tableName; |
50 | 92 | |
| 93 | + function setTableName($tableName) { |
| 94 | + $this->tableName=$tableName; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * if can't find object in src (dc1) dataset, |
| 99 | + * if set: create said object now. |
| 100 | + * if unset: throw exception. |
| 101 | + * default: unset, because typically not finding |
| 102 | + * the object we're looking for means something is |
| 103 | + * very wrong. Some tables (like uw_collection_conte |
| 104 | + */ |
| 105 | + function setAutovivify($bool) { |
| 106 | + $this->autovivify=$bool; |
| 107 | + } |
| 108 | + |
51 | 109 | function __construct($id, $dc1, $dc2) { |
52 | 110 | $this->id=$id; |
53 | 111 | $this->dc1=$dc1; |
— | — | @@ -67,9 +125,17 @@ |
68 | 126 | } |
69 | 127 | |
70 | 128 | protected function read() { |
| 129 | + echo "crumb e1<br>\n"; |
71 | 130 | $dc1=$this->dc1; |
72 | 131 | $id=$this->id; |
| 132 | + if (is_null($dc1)) |
| 133 | + throw new Exception("ObjectCopier: provided source dataset(dc1) is null"); |
| 134 | + if (is_null($id)) |
| 135 | + throw new Exception("ObjectCopier: provided identifier is null"); |
| 136 | + echo "crumb e2 --$dc1--,--$id-- <br>\n"; |
73 | 137 | $this->object=CopyTools::getRow($dc1, "objects", "WHERE object_id=$id"); |
| 138 | + var_dump($this->object); |
| 139 | + echo "crumb e3<br>\n"; |
74 | 140 | } |
75 | 141 | |
76 | 142 | /* tries to retrieve the identical UUID from the destination |
— | — | @@ -79,6 +145,10 @@ |
80 | 146 | */ |
81 | 147 | protected function identical() { |
82 | 148 | $uuid=mysql_escape_string($this->object["UUID"]); |
| 149 | + if (is_null($uuid)) |
| 150 | + throw new Exception("ObjectCopier: UUID is null"); |
| 151 | + echo "crumb f1 identical() old object :"; |
| 152 | + var_dump($this->object); |
83 | 153 | $dc2=$this->dc2; |
84 | 154 | return CopyTools::getRow($dc2, "objects", "WHERE `UUID`='$uuid'"); |
85 | 155 | } |
— | — | @@ -89,23 +159,91 @@ |
90 | 160 | * destination table name. |
91 | 161 | * Perhaps would be wiser to get the target table as an (override) parameter. |
92 | 162 | */ |
93 | | - function write() { |
94 | | - $dc2 = $this->dc2; |
| 163 | + function write($dc=Null) { |
| 164 | + |
| 165 | + if (is_null($dc)) { |
| 166 | + $dc = $this->dc2; |
| 167 | + } |
| 168 | + |
95 | 169 | $object = $this->object; |
96 | 170 | unset($object["object_id"]); |
97 | 171 | |
98 | 172 | $tableName_exploded = explode("_", $object["table"]); |
99 | | - $tableName_exploded[0] = $dc2; |
| 173 | + $tableName_exploded[0] = $dc; |
100 | 174 | $tableName = implode("_", $tableName_exploded); |
101 | 175 | $object["table"]=$tableName; |
102 | 176 | |
103 | | - CopyTools::dc_insert_assoc($dc2,"objects",$object); |
| 177 | + CopyTools::dc_insert_assoc($dc,"objects",$object); |
104 | 178 | return mysql_insert_id(); |
105 | 179 | } |
106 | 180 | |
| 181 | + /** create a new objects table entry . |
| 182 | + * See also database schema documentation (insofar available) or |
| 183 | + * do sql statement DESC <dc>_objects for table format (where <dc> is a valid |
| 184 | + * dataset prefix) */ |
| 185 | + function create_key($uuid=null) { |
| 186 | + echo "crumb B1<br>\n"; |
| 187 | + if (is_null($this->tableName)) { |
| 188 | + throw new Exception("ObjectCopier: Object autovivification requires a table name to assist in creating an object. No table name was provided."); |
| 189 | + } |
| 190 | + $this->object["object_id"]=null; # just to be on some kind of safe side. |
| 191 | + $this->object["table"]="unset_".$this->tableName; # slightly hackish, this |
| 192 | + $this->object["original_id"]=0; # no idea what this is for. |
| 193 | + |
| 194 | + if (is_null($uuid)) { |
| 195 | + $uuid_query=CopyTools::doQuery("SELECT UUID()"); |
| 196 | + $uuid=$uuid_query["UUID()"]; |
| 197 | + } |
| 198 | + $this->object["UUID"]=$uuid; |
| 199 | + $this->id=$this->write($this->dc1); |
| 200 | + return $this->id; |
| 201 | + |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * |
| 206 | + */ |
| 207 | + public static function makeObjectId($dc, $table, $uuid=null) { |
| 208 | + # Sorta Exploiting internals, because -hey- we're internal |
| 209 | + # don't try this at home kids. |
| 210 | + # probably this would be tidier if the non-static method called |
| 211 | + # the static one, or something. We only really need |
| 212 | + # create_key(), so only filling in the data that that method needs. |
| 213 | + $objectCopier=new ObjectCopier(null, $dc, null); |
| 214 | + $objectCopier->setTableName($table); |
| 215 | + return $objectCopier->create_key($uuid); |
| 216 | + } |
| 217 | + |
107 | 218 | function dup() { |
| 219 | + echo "crumb d1<br>\n"; |
| 220 | + echo "crumb d2\n"; |
| 221 | + if (is_null($this->id)) { |
| 222 | + echo "autovivify follows<br>\n"; |
| 223 | + var_dump($this->autovivify); |
| 224 | + if ($this->autovivify) { |
| 225 | + $this->create_key(); |
| 226 | + } else { |
| 227 | + throw new Exception("ObjectCopier: provided id is null"); |
| 228 | + } |
| 229 | + } |
| 230 | + |
108 | 231 | $this->read(); |
| 232 | + if (!CopyTools::sane_key_exists("object_id", $this->object)) { |
| 233 | + if ($this->autovivify) { |
| 234 | + echo "crumb d2A more autovivify\n"; |
| 235 | + $this->create_key(); |
| 236 | + } else { |
| 237 | + echo "crumb d2B\n"; |
| 238 | + $id=$this->id; |
| 239 | + $table=$this->object["table"]; |
| 240 | + throw new Exception("ObjectCopier: Could not find object information for object with id '$id' stored in `$table` in the objects table with prefix '$dc1'"); |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + echo "crumb d3\, comparison:\n"; |
109 | 245 | $object2=$this->identical(); |
| 246 | + echo "other db object<br>"; |
| 247 | + var_dump($object2); |
110 | 248 | if (CopyTools::sane_key_exists("object_id",$object2)) { |
111 | 249 | $this->already_there=true; |
112 | 250 | $newid=$object2["object_id"]; |
— | — | @@ -113,6 +251,7 @@ |
114 | 252 | $this->already_there=false; |
115 | 253 | $newid=$this->write(); |
116 | 254 | } |
| 255 | + echo "crumb d4\n"; |
117 | 256 | return $newid; |
118 | 257 | } |
119 | 258 | } |
— | — | @@ -234,12 +373,13 @@ |
235 | 374 | return $id; |
236 | 375 | } |
237 | 376 | |
238 | | -class RelationsCopier { |
| 377 | +class RelationsCopier extends Copier { |
239 | 378 | |
240 | 379 | protected $old_dmid; |
241 | 380 | protected $new_dmid; |
242 | 381 | protected $dc1; |
243 | 382 | protected $dc2; |
| 383 | + protected $tableName="meaning_relations"; |
244 | 384 | |
245 | 385 | function __construct($dc1, $dc2, $old_dmid, $new_dmid) { |
246 | 386 | $this->old_dmid=$old_dmid; |
— | — | @@ -251,7 +391,7 @@ |
252 | 392 | function read() { |
253 | 393 | $dc1=$this->dc1; |
254 | 394 | $dmid=$this->old_dmid; |
255 | | - return CopyTools::getRows($dc1,"meaning_relations","where meaning1_mid=$dmid"); |
| 395 | + return CopyTools::getRows($dc1,$this->tableName,"where meaning1_mid=$dmid"); |
256 | 396 | } |
257 | 397 | |
258 | 398 | function write_single($relation) { |
— | — | @@ -259,24 +399,15 @@ |
260 | 400 | $dc2=$this->dc2; |
261 | 401 | $new_dmid=$this->new_dmid; |
262 | 402 | |
263 | | - $copier=new ObjectCopier($relation["relation_id"], $dc1, $dc2); |
264 | | - $relation["relation_id"]=$copier->dup(); |
265 | | - if ($copier->already_there()) { |
266 | | - return; |
267 | | - } |
| 403 | + if ($this->doObject($relation, "relation_id")) |
| 404 | + return $relation["relation_id"]; |
| 405 | + |
268 | 406 | $relation["meaning1_mid"]=$new_dmid; |
269 | | - $dmcopier=new DefinedMeaningCopier($relation["meaning2_mid"],$dc1, $dc2); |
270 | | - $relation["meaning2_mid"]=$dmcopier->dup_stub(); |
271 | | - # Typically checks same values each time. Accelerated by query_cache: |
272 | | - $rtcopier=new DefinedMeaningCopier($relation["relationtype_mid"],$dc1, $dc2); |
273 | | - $relation["relationtype_mid"]=$rtcopier->dup_stub(); |
274 | | - $copier=new ObjectCopier($relation["relation_id"], $dc1, $dc2); |
275 | | - $relation["relation_id"]=$copier->dup(); |
276 | | - if ($copier->already_there()) { |
277 | | - return; |
278 | | - } |
279 | | - CopyTools::dc_insert_assoc($dc2,"meaning_relations",$relation); |
| 407 | + $this->doDM($relation,"meaning2_mid"); |
| 408 | + $this->doDM($relation,"relationtype_mid"); |
280 | 409 | |
| 410 | + $this->doInsert($relation); |
| 411 | + return $relation["relation_id"]; |
281 | 412 | } |
282 | 413 | |
283 | 414 | function dup() { |
— | — | @@ -287,18 +418,25 @@ |
288 | 419 | } |
289 | 420 | } |
290 | 421 | |
291 | | -class CollectionCopier { |
| 422 | +/** copies collections |
| 423 | + * TODO: |
| 424 | + * possibly *_definition should actually be in a different class. |
| 425 | + */ |
| 426 | +class CollectionCopier extends Copier { |
292 | 427 | protected $dmid; |
293 | 428 | protected $save_dmid; |
294 | 429 | protected $dc1; |
295 | 430 | protected $dc2; |
296 | 431 | protected $already_there=false; |
| 432 | + protected $autovivifyObjects=true; |
| 433 | + protected $tableName="collection_contents"; |
297 | 434 | |
298 | 435 | public function already_there() { |
299 | 436 | return $this->already_there; |
300 | 437 | } |
301 | 438 | |
302 | 439 | public function __construct ($dc1, $dc2, $dmid, $save_dmid) { |
| 440 | + echo "crumb A1<br>\n"; |
303 | 441 | $this->dmid=$dmid; |
304 | 442 | $this->save_dmid=$save_dmid; |
305 | 443 | $this->dc1=$dc1; |
— | — | @@ -306,6 +444,7 @@ |
307 | 445 | } |
308 | 446 | |
309 | 447 | public function read($dc=Null){ |
| 448 | + echo "crumb A2<br>\n"; |
310 | 449 | if (is_null($dc)) { |
311 | 450 | $dc=$this->dc1; |
312 | 451 | } |
— | — | @@ -315,6 +454,7 @@ |
316 | 455 | |
317 | 456 | |
318 | 457 | public function read_definition($collection_id) { |
| 458 | + echo "crumb A3<br>\n"; |
319 | 459 | $dc1=$this->dc1; |
320 | 460 | return CopyTools::getRow($dc1,"collection","WHERE collection_id=$collection_id"); |
321 | 461 | } |
— | — | @@ -325,19 +465,28 @@ |
326 | 466 | * returns the id for dc2 either way. |
327 | 467 | */ |
328 | 468 | public function write_definition($definition){ |
| 469 | + echo "crumb A4--<br>\n"; |
329 | 470 | $dc1=$this->dc1; |
330 | 471 | $dc2=$this->dc2; |
331 | | - |
| 472 | + |
| 473 | + echo "crumb A4A-a<br>\n"; |
332 | 474 | $objcopier=new ObjectCopier($definition["collection_id"], $dc1, $dc2); |
333 | 475 | $definition["collection_id"]=$objcopier->dup(); |
| 476 | + var_dump($objcopier->already_there()); #crumb ! |
| 477 | + |
| 478 | + echo "crumb A4A-b<br>\n"; |
334 | 479 | if (!$objcopier->already_there()) { |
| 480 | + echo "crumb A4B<br>\n"; |
335 | 481 | $dmid= $definition["collection_mid"]; |
336 | 482 | $dmcopier=new DefinedMeaningCopier($dmid,$dc1,$dc2); |
337 | 483 | $definition["collection_mid"]=$dmcopier->dup_stub(); |
338 | 484 | |
| 485 | + echo "crumb A4C<br>\n"; |
339 | 486 | CopyTools::dc_insert_assoc($dc2, "collection", $definition); |
| 487 | + echo "crumb A4D<br>\n"; |
340 | 488 | |
341 | 489 | } |
| 490 | + echo "crumb A4E<br>\n"; |
342 | 491 | return $definition["collection_id"]; |
343 | 492 | |
344 | 493 | } |
— | — | @@ -346,14 +495,19 @@ |
347 | 496 | * and copy if doesn't already exist in dc2 |
348 | 497 | */ |
349 | 498 | public function dup_definition($collection_id) { |
| 499 | + echo "crumb A5<br>\n"; |
350 | 500 | $definition=$this->read_definition($collection_id); |
| 501 | + |
| 502 | + echo "crumb A5A<br>\n"; |
351 | 503 | return $this->write_definition($definition); |
352 | 504 | } |
353 | 505 | |
354 | 506 | |
355 | 507 | # we create a mapping and THEN do collections, now we need to prevent ourselves dupping |
356 | 508 | # existing mappings |
| 509 | + # @deprecated |
357 | 510 | public function existing_mapping($member_id) { |
| 511 | + echo "crumb A6 ALERT DEPRECATED <br>\n"; |
358 | 512 | $dc2=$this->dc2; |
359 | 513 | $query="SELECT ${dc2}_collection_contents.* FROM ${dc2}_collection_contents, ${dc2}_collection |
360 | 514 | WHERE ${dc2}_collection_contents.collection_id = ${dc2}_collection.collection_id |
— | — | @@ -376,15 +530,19 @@ |
377 | 531 | $save_dmid=$this->save_dmid; |
378 | 532 | $row["collection_id"]=$this->dup_definition($row["collection_id"]); |
379 | 533 | |
380 | | - if ( $this->existing_mapping($row["internal_member_id"]) ) |
381 | | - return; |
| 534 | + if ($this->doObject($row, "object_id")) { |
| 535 | + $this->already_there=true; |
| 536 | + return $row["object_id"]; |
| 537 | + } |
382 | 538 | |
383 | 539 | $row["member_mid"]=$save_dmid; |
384 | 540 | CopyTools::dc_insert_assoc($dc2, "collection_contents", $row); |
385 | 541 | } |
386 | 542 | |
387 | 543 | public function write($rows){ |
| 544 | + echo "crumb A8 <br>\n"; |
388 | 545 | foreach ($rows as $row) { |
| 546 | + echo "<a8>"; |
389 | 547 | $this->write_single($row); |
390 | 548 | } |
391 | 549 | } |
— | — | @@ -393,18 +551,11 @@ |
394 | 552 | * are multiple ids |
395 | 553 | */ |
396 | 554 | public function dup() { |
397 | | - # Is there something already there? If so, do not dup. |
398 | | - $checkrows=$this->read($this->dc2); |
399 | | - foreach ($checkrows as $row) { |
400 | | - if ($row["member_mid"]==$this->save_dmid){ |
401 | | - $this->already_there=true; |
402 | | - return; |
403 | | - } |
404 | | - } |
405 | | - |
406 | | - #seems ok, let's dup. |
| 555 | + echo "crumb A9 <br>\n"; |
407 | 556 | $rows=$this->read($this->dc1); |
| 557 | + echo "crumb A10 <br>\n"; |
408 | 558 | $this->write($rows); |
| 559 | + echo "crumb A11 <br>\n"; |
409 | 560 | } |
410 | 561 | } |
411 | 562 | |
— | — | @@ -455,6 +606,7 @@ |
456 | 607 | $dc2=$this->dc2; |
457 | 608 | |
458 | 609 | $this->read(); |
| 610 | + echo "<br><h2>".$this->dmid."</h2></br>"; |
459 | 611 | |
460 | 612 | # bit of exp here too (defnitely need to tidy) |
461 | 613 | $defining_expression=expression($this->defined_meaning["expression_id"], $dc1); |
— | — | @@ -470,6 +622,7 @@ |
471 | 623 | } |
472 | 624 | $this->save_meaning["meaning_text_tcid"]=dup_translated_content($dc1, $dc2, $this->defined_meaning["meaning_text_tcid"]); |
473 | 625 | if (!($copier->already_there())) { |
| 626 | + echo "crumb 1<br>\n"; |
474 | 627 | CopyTools::dc_insert_assoc($dc2, "defined_meaning", $this->save_meaning); |
475 | 628 | |
476 | 629 | $title_name=$defining_expression["spelling"]; |
— | — | @@ -477,19 +630,32 @@ |
478 | 631 | $title=str_replace(" ","_",$title_name)."_(".$title_number.")"; |
479 | 632 | CopyTools::createPage($title); |
480 | 633 | |
| 634 | + echo "crumb 2 concepts <br>\n"; |
481 | 635 | $concepts=array( |
482 | 636 | $dc1 => $this->defined_meaning["defined_meaning_id"], |
483 | 637 | $dc2 => $this->save_meaning["defined_meaning_id"]); |
484 | | - createConceptMapping($concepts); |
| 638 | + $uuid_data=createConceptMapping($concepts); |
| 639 | + DefinedMeaningCopier::finishConceptMapping($dc1, $uuid_data[$dc1]); |
| 640 | + DefinedMeaningCopier::finishConceptMapping($dc2, $uuid_data[$dc2]); |
485 | 641 | } |
486 | 642 | |
487 | | - return $this->save_meaning["defined_meaning_id"]; |
| 643 | + echo "crumb 3<br>\n"; |
| 644 | + return $this->save_meaning["defined_meaning_id"]; |
488 | 645 | } |
489 | | - |
| 646 | + |
| 647 | + public static function finishConceptMapping($dc, $uuid) { |
| 648 | + $object_id=ObjectCopier::makeObjectID($dc, "collection_contents", $uuid); |
| 649 | + CopyTools::doQuery(" UPDATE ${dc}_collection_contents |
| 650 | + SET object_id=$object_id |
| 651 | + WHERE internal_member_id=\"$uuid\" |
| 652 | + "); |
| 653 | + } |
| 654 | + |
490 | 655 | function dup_rest() { |
491 | 656 | $dmid=$this->dmid; |
492 | 657 | $dc1=$this->dc1; |
493 | 658 | $dc2=$this->dc2; |
| 659 | + echo "crumb 4<br>\n"; |
494 | 660 | |
495 | 661 | dup_syntranses( |
496 | 662 | $this->defined_meaning["defined_meaning_id"], |
— | — | @@ -499,6 +665,7 @@ |
500 | 666 | ); |
501 | 667 | |
502 | 668 | |
| 669 | + echo "crumb 5 relation<br>\n"; |
503 | 670 | $relationsCopier=new RelationsCopier( |
504 | 671 | $dc1, |
505 | 672 | $dc2, |
— | — | @@ -506,17 +673,25 @@ |
507 | 674 | $this->save_meaning["defined_meaning_id"]); |
508 | 675 | $relationsCopier->dup(); |
509 | 676 | |
510 | | - # can't merge collections, since they're not entirely covered by |
511 | | - # the objects table. So we don't copy them more than once. |
512 | | - if (!$this->already_there()) { |
513 | | - $collectionCopier=new CollectionCopier( |
514 | | - $dc1, |
515 | | - $dc2, |
516 | | - $this->defined_meaning["defined_meaning_id"], |
517 | | - $this->save_meaning["defined_meaning_id"]); |
518 | | - $collectionCopier->dup(); |
519 | | - } |
| 677 | + echo "crumb 6 collection <br>\n"; |
| 678 | + $collectionCopier=new CollectionCopier( |
| 679 | + $dc1, |
| 680 | + $dc2, |
| 681 | + $this->defined_meaning["defined_meaning_id"], |
| 682 | + $this->save_meaning["defined_meaning_id"]); |
| 683 | + $collectionCopier->dup(); |
520 | 684 | |
| 685 | + echo "crumb 7 membership<br>\n"; |
| 686 | + $classMembershipCopier=new ClassMembershipCopier( |
| 687 | + $dc1, |
| 688 | + $dc2, |
| 689 | + $this->defined_meaning["defined_meaning_id"], |
| 690 | + $this->save_meaning["defined_meaning_id"]); |
| 691 | + $classMembershipCopier->dup(); |
| 692 | + |
| 693 | + |
| 694 | + |
| 695 | + |
521 | 696 | } |
522 | 697 | } |
523 | 698 | |
— | — | @@ -609,7 +784,18 @@ |
610 | 785 | * |
611 | 786 | */ |
612 | 787 | public static function doQuery($query) { |
613 | | - $result = mysql_query($query)or die ("error ".mysql_error()); |
| 788 | + $result = mysql_query($query); |
| 789 | + |
| 790 | + if (!$result) |
| 791 | + throw new Exception("Mysql query failed: $query"); |
| 792 | + |
| 793 | + if ($result===true) |
| 794 | + return null; |
| 795 | + |
| 796 | + if (mysql_num_rows($result)==0) |
| 797 | + return null; |
| 798 | + |
| 799 | + |
614 | 800 | $data= mysql_fetch_assoc($result); |
615 | 801 | return $data; |
616 | 802 | } |
— | — | @@ -622,7 +808,16 @@ |
623 | 809 | */ |
624 | 810 | |
625 | 811 | public static function doMultirowQuery($query) { |
626 | | - $result = mysql_query($query)or die ("error ".mysql_error()); |
| 812 | + $result = mysql_query($query); |
| 813 | + if (!$result) |
| 814 | + throw new Exception("Mysql query failed: $query"); |
| 815 | + |
| 816 | + if ($result===true) |
| 817 | + return array(); |
| 818 | + |
| 819 | + if (mysql_num_rows($result)==0) |
| 820 | + return array(); |
| 821 | + |
627 | 822 | $items=array(); |
628 | 823 | while ($nextexp=mysql_fetch_assoc($result)) { |
629 | 824 | $items[]=$nextexp; |
— | — | @@ -631,7 +826,8 @@ |
632 | 827 | } |
633 | 828 | |
634 | 829 | /** identical to the php function array_key_exists(), but eats dirtier input |
635 | | - * returns false (rather than an error) on somewhat invalid input |
| 830 | + * returns false (rather than an error) on somewhat invalid input. |
| 831 | + * (Namely, if either $key or $array is either null or false) |
636 | 832 | */ |
637 | 833 | public static function sane_key_exists($key, $array) { |
638 | 834 | if (is_null($key) or $key==false){ |
— | — | @@ -668,6 +864,14 @@ |
669 | 865 | $sql.=" `$key`=$value"; |
670 | 866 | $sql_comma=$sql.","; |
671 | 867 | } |
| 868 | + |
| 869 | + global |
| 870 | + $extra_debugging; |
| 871 | + if ($extra_debugging) { |
| 872 | + var_dump ($my_array); |
| 873 | + echo "<pre>$sql</pre>"; |
| 874 | + } |
| 875 | + |
672 | 876 | // Same with the values |
673 | 877 | $result = mysql_query($sql); |
674 | 878 | |
— | — | @@ -695,7 +899,203 @@ |
696 | 900 | } |
697 | 901 | return CopyTools::mysql_insert_assoc($target_table, $array); |
698 | 902 | } |
| 903 | +} |
699 | 904 | |
| 905 | +/**Copying uw_class_membership*/ |
| 906 | +class ClassMembershipCopier extends Copier{ |
700 | 907 | |
| 908 | + protected $old_class_member_mid; |
| 909 | + protected $new_class_member_mid; |
| 910 | + protected $dc1; |
| 911 | + protected $dc2; |
| 912 | + |
| 913 | + /** coming from the defined meaning(dm) we don't know the membership id, |
| 914 | + * but we do have the dmid (defined meaning id) for the class member, so let's use that |
| 915 | + */ |
| 916 | + public function __construct($dc1, $dc2, $old_class_member_mid, $new_class_member_mid) { |
| 917 | + $this->old_class_member_mid=$old_class_member_mid; |
| 918 | + $this->new_class_member_mid=$new_class_member_mid; |
| 919 | + $this->dc1=$dc1; |
| 920 | + $this->dc2=$dc2; |
| 921 | + } |
| 922 | + |
| 923 | + public function dup() { |
| 924 | + $memberships=$this->read(); |
| 925 | + $this->write($memberships); |
| 926 | + |
| 927 | + return; |
| 928 | + } |
| 929 | + |
| 930 | + /** read all class memberships associated with the dmid */ |
| 931 | + public function read() { |
| 932 | + $dc1=$this->dc1; |
| 933 | + $class_member_mid=$this->old_class_member_mid; |
| 934 | + return CopyTools::getRows($dc1, "class_membership", "WHERE class_member_mid=$class_member_mid"); |
| 935 | + } |
| 936 | + |
| 937 | + public function write($memberships) { |
| 938 | + foreach ($memberships as $membership) { |
| 939 | + $this->write_single($membership); |
| 940 | + } |
| 941 | + } |
| 942 | + |
| 943 | + public function write_single($membership) { |
| 944 | + $dc1=$this->dc1; |
| 945 | + $dc2=$this->dc2; |
| 946 | + $new_class_member_mid=$this->new_class_member_mid; |
| 947 | + |
| 948 | + $copier = new ObjectCopier($membership["class_membership_id"], $dc1, $dc2); |
| 949 | + $newid=$copier->dup(); |
| 950 | + if ($copier->already_there()) { |
| 951 | + return $newid; |
| 952 | + } |
| 953 | + $membership["class_membership_id"]=$newid; |
| 954 | + $membership["class_member_mid"]=$new_class_member_mid; |
| 955 | + # nope, that's not right. |
| 956 | + #$classCopier=new ClassCopier($membership["class_mid"], $dc1, $dc2); |
| 957 | + #$membership["class_mid"]=$classCopier->dup(); |
| 958 | + $this->doDM($membership, "class_mid", true); |
| 959 | + CopyTools::dc_insert_assoc($dc2, "class_membership", $membership); |
| 960 | + return $newid; |
| 961 | + } |
| 962 | + |
701 | 963 | } |
| 964 | + |
| 965 | +/** copying stuff in the %_class_attributes table actually |
| 966 | + */ |
| 967 | +class ClassAttributesCopier extends Copier { |
| 968 | + |
| 969 | + protected $src_class_mid; |
| 970 | + protected $dst_class_mid; |
| 971 | + protected $dc1; |
| 972 | + protected $dc2; |
| 973 | + protected $tableName="class_attributes"; |
| 974 | + |
| 975 | + /** you saw that right, class_mid, not class_id, there's no such thing :-/ |
| 976 | + */ |
| 977 | + public function __construct($class_mid, $dc1, $dc2) { |
| 978 | + $this->src_class_mid=$class_mid; |
| 979 | + $this->dc1=$dc1; |
| 980 | + $this->dc2=$dc2; |
| 981 | + } |
| 982 | + |
| 983 | + /** unchracteristically, returns the new class_mid, rather than object_id |
| 984 | + * because in this case, the class_mid is the key characteristic |
| 985 | + */ |
| 986 | + public function dup() { |
| 987 | + $attributes=$this->read(); |
| 988 | + $this->write($attributes); |
| 989 | + return $this->dst_class_mid; |
| 990 | + } |
| 991 | + |
| 992 | + public function read() { |
| 993 | + $dc1=$this->dc1; |
| 994 | + $class_mid=$this->src_class_mid; |
| 995 | + return CopyTools::getRows($dc1, "class_attributes", "WHERE class_mid=$class_mid"); |
| 996 | + } |
| 997 | + |
| 998 | + public function write($attributes) { |
| 999 | + foreach ($attributes as $attribute) { |
| 1000 | + $this->write_single($attribute); |
| 1001 | + } |
| 1002 | + } |
| 1003 | + |
| 1004 | + |
| 1005 | + public function write_single($attribute) { |
| 1006 | + $dc1=$this->dc1; |
| 1007 | + $dc2=$this->dc2; |
| 1008 | + $class_mid=$this->src_class_mid; |
| 1009 | + |
| 1010 | + if ($this->doObject($attribute,"object_id")) |
| 1011 | + return $attribute["object_id"]; |
| 1012 | + |
| 1013 | + $this->doDM($attribute,"class_mid"); |
| 1014 | + $this->doDM($attribute,"level_mid"); |
| 1015 | + $this->doDM($attribute,"attribute_mid"); |
| 1016 | + |
| 1017 | + CopyTools::dc_insert_assoc($dc2, "class_attributes", $attribute); |
| 1018 | + |
| 1019 | + $this->dst_class_mid=$attribute["class_mid"]; |
| 1020 | + return $attribute["object_id"]; |
| 1021 | + } |
| 1022 | + |
| 1023 | +} |
| 1024 | + |
| 1025 | + |
| 1026 | + |
| 1027 | +/** abstract superclass for copiers |
| 1028 | + * will gradually be implemented anywhere I create, refactor, or |
| 1029 | + */ |
| 1030 | +abstract class Copier { |
| 1031 | + |
| 1032 | + protected $dc1; |
| 1033 | + protected $dc2; |
| 1034 | + protected $tableName; |
| 1035 | + protected $autovivifyObjects=false; |
| 1036 | + |
| 1037 | + public abstract function dup(); |
| 1038 | + |
| 1039 | + /** |
| 1040 | + * Odd Combined function found through refactoring: |
| 1041 | + * So we have a row in the source (dc1) table, which has a column |
| 1042 | + * referring to a defined meaning |
| 1043 | + * Before the row can be stored in the destination dataset, |
| 1044 | + * we should |
| 1045 | + * - Ensure that at least a stub of this defined meaning exists |
| 1046 | + * - make sure that the row refers to the dmid in the *destination* dataset (dc2), |
| 1047 | + * instead of the source dataset (dc1). |
| 1048 | + * - returns True if the defined meaning was already_there. |
| 1049 | + * @param &$row : row to operate on, passed by reference |
| 1050 | + * @param $dmid_colum: a column in said row, containing the dmid to operate on |
| 1051 | + * @param $full=false (optional) : if true, does a dup instead of a dup_stub |
| 1052 | + * @return true if the updated dmid already existed in the destination (dc2) dataset before now |
| 1053 | + * false if it did not, and we just created it |
| 1054 | + */ |
| 1055 | + protected function doDM(&$row, $dmid_column, $full=false) { |
| 1056 | + $dmCopier=new DefinedMeaningCopier($row[$dmid_column], $this->dc1, $this->dc2); |
| 1057 | + if ($full) { |
| 1058 | + $row[$dmid_column]=$dmCopier->dup(); |
| 1059 | + } else { |
| 1060 | + $row[$dmid_column]=$dmCopier->dup_stub(); |
| 1061 | + } |
| 1062 | + return $dmCopier->already_there(); |
| 1063 | + } |
| 1064 | + |
| 1065 | + /** |
| 1066 | + * Another odd combined function found through refactoring |
| 1067 | + * performs all operations to do with the column associated with |
| 1068 | + * the objects table in one go. |
| 1069 | + * |
| 1070 | + * Assuming the row originally contains an object_id in the source dataset (dc1) |
| 1071 | + * updates the row(passed by reference) with the relevant object_id in the destination |
| 1072 | + * dataset (dc2) |
| 1073 | + * |
| 1074 | + * @param row : row to operate on, passed by reference |
| 1075 | + * @param object_column: a column in said row, containing the object reference to operate on |
| 1076 | + * @returns true if examination of the objects table reveals that this particular row should already |
| 1077 | + * exist in the destination dataset |
| 1078 | + * false if this particular row does not yet exist. The objects table, and object reference |
| 1079 | + * have already been set correctly. Continue by filling in the rest of the rest |
| 1080 | + * 0f the row data. (Do so before COMMIT). |
| 1081 | + */ |
| 1082 | + protected function doObject(&$row, $object_column) { |
| 1083 | + var_dump($object_column); |
| 1084 | + var_dump($row); |
| 1085 | + $copier=new ObjectCopier($row[$object_column], $this->dc1, $this->dc2); |
| 1086 | + $copier->setTableName($this->tableName); |
| 1087 | + var_dump($this->autovivifyObjects); |
| 1088 | + $copier->setAutovivify($this->autovivifyObjects); |
| 1089 | + $row[$object_column]=$copier->dup(); |
| 1090 | + var_dump($copier->already_there()); |
| 1091 | + return $copier->already_there(); |
| 1092 | + } |
| 1093 | + |
| 1094 | + protected function doInsert($row) { |
| 1095 | + CopyTools::dc_insert_assoc($this->dc2, $this->tableName, $row); |
| 1096 | + } |
| 1097 | + |
| 1098 | + |
| 1099 | +} |
| 1100 | + |
| 1101 | + |
702 | 1102 | ?> |