Index: trunk/extensions/Wikidata/util/copy.php |
— | — | @@ -57,8 +57,6 @@ |
58 | 58 | startNewTransaction(0, "127.0.0.1", "copying from $dc1 to $dc2", $dc2); |
59 | 59 | } |
60 | 60 | |
61 | | - |
62 | | - |
63 | 61 | /** retrieve a single row from the database as an associative array |
64 | 62 | * @param $dc the dataset prefix we need |
65 | 63 | * @param $table the name of the table (minus dataset prefix) |
— | — | @@ -70,7 +68,7 @@ |
71 | 69 | $target_table=mysql_real_escape_string("${dc}_${table}"); |
72 | 70 | $query="SELECT * FROM $target_table ".$where; |
73 | 71 | print $query."<br>\n"; |
74 | | - return doquery($query); |
| 72 | + return doQuery($query); |
75 | 73 | } |
76 | 74 | |
77 | 75 | |
— | — | @@ -96,7 +94,7 @@ |
97 | 95 | * keys=column headers, values = row contents |
98 | 96 | * |
99 | 97 | */ |
100 | | -function doquery($query) { |
| 98 | +function doQuery($query) { |
101 | 99 | echo $query; |
102 | 100 | $result = mysql_query($query)or die ("error ".mysql_error()); |
103 | 101 | $data= mysql_fetch_assoc($result); |
— | — | @@ -178,13 +176,23 @@ |
179 | 177 | return getrow($dc2, "objects", "WHERE `UUID`='$uuid'"); |
180 | 178 | } |
181 | 179 | |
| 180 | + /** Write copy of object into the objects table,taking into account |
| 181 | + * necessary changes. |
| 182 | + * possible TODO: Currently induces the target table from the original |
| 183 | + * destination table name. |
| 184 | + * Perhaps would be wiser to get the target table as an (override) parameter. |
| 185 | + */ |
182 | 186 | function write() { |
183 | | - $dc2=$this->dc2; |
184 | | - $objects_table=mysql_real_escape_string("${dc2}_objects"); |
185 | | - $object=$this->object; |
| 187 | + $dc2 = $this->dc2; |
| 188 | + $object = $this->object; |
186 | 189 | unset($object["object_id"]); |
187 | | - $object["table"]=$objects_table; |
188 | | - mysql_insert_assoc($objects_table,$object); |
| 190 | + |
| 191 | + $tableName_exploded = explode("_", $object["table"]); |
| 192 | + $tableName_exploded[0] = $dc2; |
| 193 | + $tableName = implode("_", $tableName_exploded); |
| 194 | + $object["table"]=$tableName; |
| 195 | + |
| 196 | + dc_insert_assoc($dc2,"objects",$object); |
189 | 197 | return mysql_insert_id(); |
190 | 198 | } |
191 | 199 | |
— | — | @@ -276,8 +284,7 @@ |
277 | 285 | function writeSyntrans($syntrans, $newdmid, $newexpid, $dc2) { |
278 | 286 | $syntrans["defined_meaning_id"]=$newdmid; |
279 | 287 | $syntrans["expression_id"]=$newexpid; |
280 | | - $syntrans_table=mysql_real_escape_string("${dc2}_syntrans"); |
281 | | - mysql_insert_assoc($syntrans_table,$syntrans); |
| 288 | + dc_insert_assoc($dc2,"syntrans",$syntrans); |
282 | 289 | } |
283 | 290 | |
284 | 291 | function dupSyntrans($dc1, $dc2, $olddmid, $oldexpid, $newdmid, $newexpid) { |
— | — | @@ -304,9 +311,8 @@ |
305 | 312 | $target_expid1=$copier->dup(); |
306 | 313 | $save_expression=$expression; |
307 | 314 | $save_expression["expression_id"]=$target_expid1; |
308 | | - $target_table=mysql_real_escape_string("${dc2}_expression_ns"); |
309 | 315 | if (!($copier->already_there())) { |
310 | | - mysql_insert_assoc($target_table,$save_expression); |
| 316 | + dc_insert_assoc($dc,"expression_ns",$save_expression); |
311 | 317 | } |
312 | 318 | dupsyntrans( |
313 | 319 | $dc1, |
— | — | @@ -340,12 +346,10 @@ |
341 | 347 | } |
342 | 348 | |
343 | 349 | function write_translated_content($dc1, $dc2, $tcid, $content) { |
344 | | - $target_table=mysql_real_escape_string("${dc2}_translated_content"); |
345 | | - var_dump($content); |
346 | 350 | $content["translated_content_id"]=$tcid; |
347 | 351 | $content["text_id"]=dup_text($dc1, $dc2, $content["text_id"]); |
348 | 352 | var_dump($content); |
349 | | - mysql_insert_assoc($target_table, $content); |
| 353 | + dc_insert_assoc($dc2, "translated_content", $content); |
350 | 354 | } |
351 | 355 | |
352 | 356 | |
— | — | @@ -373,7 +377,7 @@ |
374 | 378 | unset($text["text_id"]); |
375 | 379 | # inconsistent, insert_assoc should accept dc, table |
376 | 380 | $target_table=mysql_real_escape_string("${dc2}_text"); |
377 | | - mysql_insert_assoc($target_table,$text); |
| 381 | + dc_insert_assoc($dc2, "text", $text); |
378 | 382 | return mysql_insert_id(); |
379 | 383 | } |
380 | 384 | |
— | — | @@ -407,7 +411,6 @@ |
408 | 412 | } |
409 | 413 | |
410 | 414 | function write_single($relation) { |
411 | | - echo "RELATION"; |
412 | 415 | var_dump($relation); |
413 | 416 | $dc1=$this->dc1; |
414 | 417 | $dc2=$this->dc2; |
— | — | @@ -424,17 +427,13 @@ |
425 | 428 | # Typically checks same values each time. Accelerated by query_cache: |
426 | 429 | $rtcopier=new defined_meaning_copier($relation["relationtype_mid"],$dc1, $dc2); |
427 | 430 | $relation["relationtype_mid"]=$rtcopier->dup(); |
428 | | - echo ">>PRE!<br>\n"; |
429 | 431 | var_dump($relation); |
430 | 432 | $copier=new ObjectCopier($relation["relation_id"], $dc1, $dc2); |
431 | 433 | $relation["relation_id"]=$copier->dup(); |
432 | 434 | if ($copier->already_there()) { |
433 | 435 | return; |
434 | 436 | } |
435 | | - echo ">>POST!<br>\n"; |
436 | | - var_dump($relation); |
437 | | - $target_table=mysql_real_escape_string("${dc2}_meaning_relations"); |
438 | | - mysql_insert_assoc($target_table,$relation); |
| 437 | + dc_insert_assoc($dc2,"meaning_relations",$relation); |
439 | 438 | |
440 | 439 | } |
441 | 440 | |
— | — | @@ -585,7 +584,14 @@ |
586 | 585 | return $this->already_there; |
587 | 586 | } |
588 | 587 | |
589 | | - function dup (){ |
| 588 | + public function dup() { |
| 589 | + $this->dup_stub(); |
| 590 | + $this->dup_rest(); |
| 591 | + return $this->save_meaning["defined_meaning_id"]; |
| 592 | + } |
| 593 | + |
| 594 | + |
| 595 | + function dup_stub (){ |
590 | 596 | $dmid=$this->dmid; |
591 | 597 | $dc1=$this->dc1; |
592 | 598 | $dc2=$this->dc2; |
— | — | @@ -611,21 +617,27 @@ |
612 | 618 | var_dump($target_expid1); |
613 | 619 | $save_expression=$defining_expression; |
614 | 620 | $save_expression["expression_id"]=$target_expid1; |
615 | | - mysql_insert_assoc($target_table,$save_expression); |
| 621 | + dc_insert_assoc($dc2, "expression_ns", $save_expression); |
616 | 622 | # and insert that info into the dm |
617 | 623 | $this->save_meaning["expression_id"]=$target_expid1; |
618 | 624 | } |
619 | 625 | $this->save_meaning["meaning_text_tcid"]=dup_translated_content($dc1, $dc2, $this->defined_meaning["meaning_text_tcid"]); |
620 | 626 | |
621 | 627 | if (!($copier->already_there())) { |
622 | | - mysql_insert_assoc($dm_target_table, $this->save_meaning); |
| 628 | + dc_insert_assoc($dc2, "defined_meaning", $this->save_meaning); |
623 | 629 | |
624 | 630 | $title_name=$defining_expression["spelling"]; |
625 | 631 | $title_number=$target_dmid; |
626 | 632 | $title=str_replace(" ","_",$title_name)."_(".$title_number.")"; |
627 | | - $pagedata=array("page_namespace"=>24, "page_title"=>$title); |
628 | | - mysql_insert_assoc("page",$pagedata); |
| 633 | + CopyTools::createPage($title); |
629 | 634 | } |
| 635 | + return $this->save_meaning["defined_meaning_id"]; |
| 636 | + } |
| 637 | + |
| 638 | + function dup_rest() { |
| 639 | + $dmid=$this->dmid; |
| 640 | + $dc1=$this->dc1; |
| 641 | + $dc2=$this->dc2; |
630 | 642 | |
631 | 643 | $concepts=array( |
632 | 644 | $dc1 => $this->defined_meaning["defined_meaning_id"], |
— | — | @@ -658,9 +670,21 @@ |
659 | 671 | $collectionCopier->dup(); |
660 | 672 | } |
661 | 673 | |
662 | | - return $this->save_meaning["defined_meaning_id"]; |
663 | 674 | } |
664 | 675 | } |
| 676 | + |
| 677 | +class CopyTools { |
| 678 | + /** create a relevant entry in the `page` table. */ |
| 679 | + public static function createPage($title) { |
| 680 | + # page is not a Wikidata table, so it needs to be treated differently (yet again :-/) |
| 681 | + $escTitle=mysql_real_escape_string($title); |
| 682 | + $existing_page_data=doQuery("SELECT * FROM page WHERE page_namespace=24 AND page_title=\"$escTitle\""); |
| 683 | + if (count($existing_page_data)==0) { |
| 684 | + $pagedata=array("page_namespace"=>24, "page_title"=>$title); |
| 685 | + mysql_insert_assoc("page",$pagedata); |
| 686 | + } |
| 687 | + } |
| 688 | +} |
665 | 689 | |
666 | 690 | |
667 | 691 | $start=stopwatch(); |