r26298 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r26297‎ | r26298 | r26299 >
Date:02:01, 2 October 2007
Author:kim
Status:old
Tags:
Comment:
Lots of Copy.php work. (Needs technical/development testing)
Modified paths:
  • /trunk/extensions/Wikidata/OmegaWiki/Copy.php (modified) (history)
  • /trunk/extensions/Wikidata/OmegaWiki/WikiDataAPI.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Wikidata/OmegaWiki/WikiDataAPI.php
@@ -1129,6 +1129,9 @@
11301130 /** Write a concept mapping to db
11311131 * supply mapping as a valid
11321132 * 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
11331136 */
11341137
11351138 function createConceptMapping($concepts) {
@@ -1139,6 +1142,7 @@
11401143 writeDmToCollection($dc, $collid, $uuid_map[$dc], $dm_id);
11411144 }
11421145 }
 1146+ return $uuid_map;
11431147 }
11441148
11451149 function getMapping( $dc, $collid, $dm_id ){
@@ -1513,4 +1517,4 @@
15141518 }
15151519
15161520
1517 -}
\ No newline at end of file
 1521+}
Index: trunk/extensions/Wikidata/OmegaWiki/Copy.php
@@ -26,6 +26,45 @@
2727 # * some read/write/dup functions are still main namespace, should get their own
2828 # * classes (!!)
2929
 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+
3069 #require_once("../../../StartProfiler.php");
3170 #include_once("../../../includes/Defines.php");
3271 #include_once("../../../LocalSettings.php");
@@ -46,7 +85,26 @@
4786 protected $dc2;
4887 protected $object;
4988 protected $already_there=null;
 89+ protected $autovivify=false; # tradeoff: create object references if not found,
 90+ # but catch less errors
 91+ protected $tableName;
5092
 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+
51109 function __construct($id, $dc1, $dc2) {
52110 $this->id=$id;
53111 $this->dc1=$dc1;
@@ -67,9 +125,17 @@
68126 }
69127
70128 protected function read() {
 129+ echo "crumb e1<br>\n";
71130 $dc1=$this->dc1;
72131 $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";
73137 $this->object=CopyTools::getRow($dc1, "objects", "WHERE object_id=$id");
 138+ var_dump($this->object);
 139+ echo "crumb e3<br>\n";
74140 }
75141
76142 /* tries to retrieve the identical UUID from the destination
@@ -79,6 +145,10 @@
80146 */
81147 protected function identical() {
82148 $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);
83153 $dc2=$this->dc2;
84154 return CopyTools::getRow($dc2, "objects", "WHERE `UUID`='$uuid'");
85155 }
@@ -89,23 +159,91 @@
90160 * destination table name.
91161 * Perhaps would be wiser to get the target table as an (override) parameter.
92162 */
93 - function write() {
94 - $dc2 = $this->dc2;
 163+ function write($dc=Null) {
 164+
 165+ if (is_null($dc)) {
 166+ $dc = $this->dc2;
 167+ }
 168+
95169 $object = $this->object;
96170 unset($object["object_id"]);
97171
98172 $tableName_exploded = explode("_", $object["table"]);
99 - $tableName_exploded[0] = $dc2;
 173+ $tableName_exploded[0] = $dc;
100174 $tableName = implode("_", $tableName_exploded);
101175 $object["table"]=$tableName;
102176
103 - CopyTools::dc_insert_assoc($dc2,"objects",$object);
 177+ CopyTools::dc_insert_assoc($dc,"objects",$object);
104178 return mysql_insert_id();
105179 }
106180
 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+
107218 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+
108231 $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";
109245 $object2=$this->identical();
 246+ echo "other db object<br>";
 247+ var_dump($object2);
110248 if (CopyTools::sane_key_exists("object_id",$object2)) {
111249 $this->already_there=true;
112250 $newid=$object2["object_id"];
@@ -113,6 +251,7 @@
114252 $this->already_there=false;
115253 $newid=$this->write();
116254 }
 255+ echo "crumb d4\n";
117256 return $newid;
118257 }
119258 }
@@ -234,12 +373,13 @@
235374 return $id;
236375 }
237376
238 -class RelationsCopier {
 377+class RelationsCopier extends Copier {
239378
240379 protected $old_dmid;
241380 protected $new_dmid;
242381 protected $dc1;
243382 protected $dc2;
 383+ protected $tableName="meaning_relations";
244384
245385 function __construct($dc1, $dc2, $old_dmid, $new_dmid) {
246386 $this->old_dmid=$old_dmid;
@@ -251,7 +391,7 @@
252392 function read() {
253393 $dc1=$this->dc1;
254394 $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");
256396 }
257397
258398 function write_single($relation) {
@@ -259,24 +399,15 @@
260400 $dc2=$this->dc2;
261401 $new_dmid=$this->new_dmid;
262402
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+
268406 $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");
280409
 410+ $this->doInsert($relation);
 411+ return $relation["relation_id"];
281412 }
282413
283414 function dup() {
@@ -287,18 +418,25 @@
288419 }
289420 }
290421
291 -class CollectionCopier {
 422+/** copies collections
 423+ * TODO:
 424+ * possibly *_definition should actually be in a different class.
 425+ */
 426+class CollectionCopier extends Copier {
292427 protected $dmid;
293428 protected $save_dmid;
294429 protected $dc1;
295430 protected $dc2;
296431 protected $already_there=false;
 432+ protected $autovivifyObjects=true;
 433+ protected $tableName="collection_contents";
297434
298435 public function already_there() {
299436 return $this->already_there;
300437 }
301438
302439 public function __construct ($dc1, $dc2, $dmid, $save_dmid) {
 440+ echo "crumb A1<br>\n";
303441 $this->dmid=$dmid;
304442 $this->save_dmid=$save_dmid;
305443 $this->dc1=$dc1;
@@ -306,6 +444,7 @@
307445 }
308446
309447 public function read($dc=Null){
 448+ echo "crumb A2<br>\n";
310449 if (is_null($dc)) {
311450 $dc=$this->dc1;
312451 }
@@ -315,6 +454,7 @@
316455
317456
318457 public function read_definition($collection_id) {
 458+ echo "crumb A3<br>\n";
319459 $dc1=$this->dc1;
320460 return CopyTools::getRow($dc1,"collection","WHERE collection_id=$collection_id");
321461 }
@@ -325,19 +465,28 @@
326466 * returns the id for dc2 either way.
327467 */
328468 public function write_definition($definition){
 469+ echo "crumb A4--<br>\n";
329470 $dc1=$this->dc1;
330471 $dc2=$this->dc2;
331 -
 472+
 473+ echo "crumb A4A-a<br>\n";
332474 $objcopier=new ObjectCopier($definition["collection_id"], $dc1, $dc2);
333475 $definition["collection_id"]=$objcopier->dup();
 476+ var_dump($objcopier->already_there()); #crumb !
 477+
 478+ echo "crumb A4A-b<br>\n";
334479 if (!$objcopier->already_there()) {
 480+ echo "crumb A4B<br>\n";
335481 $dmid= $definition["collection_mid"];
336482 $dmcopier=new DefinedMeaningCopier($dmid,$dc1,$dc2);
337483 $definition["collection_mid"]=$dmcopier->dup_stub();
338484
 485+ echo "crumb A4C<br>\n";
339486 CopyTools::dc_insert_assoc($dc2, "collection", $definition);
 487+ echo "crumb A4D<br>\n";
340488
341489 }
 490+ echo "crumb A4E<br>\n";
342491 return $definition["collection_id"];
343492
344493 }
@@ -346,14 +495,19 @@
347496 * and copy if doesn't already exist in dc2
348497 */
349498 public function dup_definition($collection_id) {
 499+ echo "crumb A5<br>\n";
350500 $definition=$this->read_definition($collection_id);
 501+
 502+ echo "crumb A5A<br>\n";
351503 return $this->write_definition($definition);
352504 }
353505
354506
355507 # we create a mapping and THEN do collections, now we need to prevent ourselves dupping
356508 # existing mappings
 509+ # @deprecated
357510 public function existing_mapping($member_id) {
 511+ echo "crumb A6 ALERT DEPRECATED <br>\n";
358512 $dc2=$this->dc2;
359513 $query="SELECT ${dc2}_collection_contents.* FROM ${dc2}_collection_contents, ${dc2}_collection
360514 WHERE ${dc2}_collection_contents.collection_id = ${dc2}_collection.collection_id
@@ -376,15 +530,19 @@
377531 $save_dmid=$this->save_dmid;
378532 $row["collection_id"]=$this->dup_definition($row["collection_id"]);
379533
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+ }
382538
383539 $row["member_mid"]=$save_dmid;
384540 CopyTools::dc_insert_assoc($dc2, "collection_contents", $row);
385541 }
386542
387543 public function write($rows){
 544+ echo "crumb A8 <br>\n";
388545 foreach ($rows as $row) {
 546+ echo "<a8>";
389547 $this->write_single($row);
390548 }
391549 }
@@ -393,18 +551,11 @@
394552 * are multiple ids
395553 */
396554 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";
407556 $rows=$this->read($this->dc1);
 557+ echo "crumb A10 <br>\n";
408558 $this->write($rows);
 559+ echo "crumb A11 <br>\n";
409560 }
410561 }
411562
@@ -455,6 +606,7 @@
456607 $dc2=$this->dc2;
457608
458609 $this->read();
 610+ echo "<br><h2>".$this->dmid."</h2></br>";
459611
460612 # bit of exp here too (defnitely need to tidy)
461613 $defining_expression=expression($this->defined_meaning["expression_id"], $dc1);
@@ -470,6 +622,7 @@
471623 }
472624 $this->save_meaning["meaning_text_tcid"]=dup_translated_content($dc1, $dc2, $this->defined_meaning["meaning_text_tcid"]);
473625 if (!($copier->already_there())) {
 626+ echo "crumb 1<br>\n";
474627 CopyTools::dc_insert_assoc($dc2, "defined_meaning", $this->save_meaning);
475628
476629 $title_name=$defining_expression["spelling"];
@@ -477,19 +630,32 @@
478631 $title=str_replace(" ","_",$title_name)."_(".$title_number.")";
479632 CopyTools::createPage($title);
480633
 634+ echo "crumb 2 concepts <br>\n";
481635 $concepts=array(
482636 $dc1 => $this->defined_meaning["defined_meaning_id"],
483637 $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]);
485641 }
486642
487 - return $this->save_meaning["defined_meaning_id"];
 643+ echo "crumb 3<br>\n";
 644+ return $this->save_meaning["defined_meaning_id"];
488645 }
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+
490655 function dup_rest() {
491656 $dmid=$this->dmid;
492657 $dc1=$this->dc1;
493658 $dc2=$this->dc2;
 659+ echo "crumb 4<br>\n";
494660
495661 dup_syntranses(
496662 $this->defined_meaning["defined_meaning_id"],
@@ -499,6 +665,7 @@
500666 );
501667
502668
 669+ echo "crumb 5 relation<br>\n";
503670 $relationsCopier=new RelationsCopier(
504671 $dc1,
505672 $dc2,
@@ -506,17 +673,25 @@
507674 $this->save_meaning["defined_meaning_id"]);
508675 $relationsCopier->dup();
509676
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();
520684
 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+
521696 }
522697 }
523698
@@ -609,7 +784,18 @@
610785 *
611786 */
612787 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+
614800 $data= mysql_fetch_assoc($result);
615801 return $data;
616802 }
@@ -622,7 +808,16 @@
623809 */
624810
625811 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+
627822 $items=array();
628823 while ($nextexp=mysql_fetch_assoc($result)) {
629824 $items[]=$nextexp;
@@ -631,7 +826,8 @@
632827 }
633828
634829 /** 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)
636832 */
637833 public static function sane_key_exists($key, $array) {
638834 if (is_null($key) or $key==false){
@@ -668,6 +864,14 @@
669865 $sql.=" `$key`=$value";
670866 $sql_comma=$sql.",";
671867 }
 868+
 869+ global
 870+ $extra_debugging;
 871+ if ($extra_debugging) {
 872+ var_dump ($my_array);
 873+ echo "<pre>$sql</pre>";
 874+ }
 875+
672876 // Same with the values
673877 $result = mysql_query($sql);
674878
@@ -695,7 +899,203 @@
696900 }
697901 return CopyTools::mysql_insert_assoc($target_table, $array);
698902 }
 903+}
699904
 905+/**Copying uw_class_membership*/
 906+class ClassMembershipCopier extends Copier{
700907
 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+
701963 }
 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+
7021102 ?>

Status & tagging log