r25529 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r25528‎ | r25529 | r25530 >
Date:08:21, 5 September 2007
Author:kim
Status:old
Tags:
Comment:
More copy work. Refactor only.
Now overwrite-check ready.
Then relations-ready (although there's still an issue
of uncontrolled recursion)

It runs, so let's ship it. :-P
Modified paths:
  • /trunk/extensions/Wikidata/util/copy.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Wikidata/util/copy.php
@@ -71,63 +71,123 @@
7272 return getrow($dc1, "expression_ns", "WHERE expression_id=$expression_id");
7373 }
7474
75 -function readobject($id, $dc1) {
76 - $objects=mysql_real_escape_string("${dc1}_objects");
77 - $query="SELECT * from $objects where object_id=$id";
78 - return getrow($dc1, "objects", "WHERE object_id=$id");
 75+class ObjectCopier {
 76+
 77+ protected $id;
 78+ protected $dc1;
 79+ protected $dc2;
 80+ protected $object;
 81+ protected $table;
 82+ protected $already_there=null;
 83+
 84+ function __construct($id, $table, $dc1, $dc2) {
 85+ $this->id=$id;
 86+ $this->dc1=$dc1;
 87+ $this->dc2=$dc2;
 88+ $this->table=mysql_escape_string("${table}_${dc2}");
 89+ }
 90+
 91+ function getObject() {
 92+ return $this->object;
 93+ }
 94+
 95+ function setObject($object) {
 96+ $this->object=$object;
 97+ }
 98+
 99+ /** return true if the object was already present in the other dataset*/
 100+ public function already_there(){
 101+ return $this->already_there;
 102+ }
 103+
 104+ protected function read() {
 105+ $dc1=$this->dc1;
 106+ $id=$this->id;
 107+ $this->object=getrow($dc1, "objects", "WHERE object_id=$id");
 108+ }
 109+
 110+ protected function identical() {
 111+ $uuid=mysql_escape_string($this->object["UUID"]);
 112+ $dc2=$this->dc2;
 113+ return getrow($dc2,"objects", "WHERE `uuid`='$uuid'");
 114+ }
 115+
 116+ function write() {
 117+ $dc2=$this->dc2;
 118+ $objects_table=mysql_real_escape_string("${dc2}_objects");
 119+ $object=$this->object;
 120+ unset($object["object_id"]);
 121+ $object["table"]=$objects_table;
 122+ mysql_insert_assoc($objects_table,$object);
 123+ return mysql_insert_id();
 124+ }
 125+
 126+ function dup() {
 127+ $this->read();
 128+ $object2=$this->identical();
 129+ if (sane_key_exists("object_id",$object2)) {
 130+ $this->already_there=true;
 131+ $newid=$object2["object_id"];
 132+ } else {
 133+ $this->already_there=false;
 134+ $newid=$this->write();
 135+ }
 136+ return $newid;
 137+ }
79138 }
80139
81 -function writeobject($object,$dc2,$table) {
82 - $objects=mysql_real_escape_string("${dc2}_objects");
83 - unset($object["object_id"]);
84 - $object["table"]=$table;
85 - mysql_insert_assoc($objects,$object);
86 - return mysql_insert_id();
 140+/** identical to array_key_exists(), but eats dirtier input
 141+ * returns false (rather than an error) on somewhat invalid input
 142+ */
 143+function sane_key_exists($key, $array) {
 144+ if (is_null($key) or $key==false){
 145+ return false;
 146+ }
 147+ if (is_null($array) or $array==false) {
 148+ return false;
 149+ }
 150+ var_dump($array);
 151+ return array_key_exists($key, $array);
87152 }
88153
89 -function dupobject($id, $table, $dc1, $dc2) {
90 - $object=readobject($id, $dc1);
91 - $newid=writeobject($object,$dc2, $table);
92 - return $newid;
93 -}
94154 /**
95155 * inverse of mysql_fetch_assoc
96156 /* see: http://www.php.net/mysql_fetch_assoc (Comment by R. Bradly, 14-Sep-2006)
97157 */
98 - function mysql_insert_assoc ($my_table, $my_array) {
 158+function mysql_insert_assoc ($my_table, $my_array) {
99159
100 - // Find all the keys (column names) from the array $my_array
 160+// Find all the keys (column names) from the array $my_array
101161
102 - // We compose the query
103 - $sql = "insert into `$my_table` set";
104 - // implode the column names, inserting "\", \"" between each (but not after the last one)
105 - // we add the enclosing quotes at the same time
106 - $sql_comma=$sql;
107 - foreach($my_array as $key=>$value) {
108 - $sql=$sql_comma;
109 - if (is_null($value)) {
110 - $value="DEFAULT";
111 - } else {
112 - $value="\"$value\"";
113 - }
114 - $sql.=" `$key`=$value";
115 - $sql_comma=$sql.",";
 162+// We compose the query
 163+$sql = "insert into `$my_table` set";
 164+// implode the column names, inserting "\", \"" between each (but not after the last one)
 165+// we add the enclosing quotes at the same time
 166+$sql_comma=$sql;
 167+foreach($my_array as $key=>$value) {
 168+ $sql=$sql_comma;
 169+ if (is_null($value)) {
 170+ $value="DEFAULT";
 171+ } else {
 172+ $value="\"$value\"";
116173 }
117 - // Same with the values
118 - echo $sql."; <br>\n";
119 - $result = mysql_query($sql);
 174+ $sql.=" `$key`=$value";
 175+ $sql_comma=$sql.",";
 176+}
 177+// Same with the values
 178+echo $sql."; <br>\n";
 179+$result = mysql_query($sql);
120180
121 - if ($result)
122 - {
123 - echo "The row was added sucessfully";
124 - return true;
125 - }
126 - else
127 - {
128 - echo ("The row was not added<br>The error was" . mysql_error());
129 - return false;
130 - }
131 - }
 181+if ($result)
 182+{
 183+ echo "The row was added sucessfully";
 184+ return true;
 185+}
 186+else
 187+{
 188+ echo ("The row was not added<br>The error was" . mysql_error());
 189+ return false;
 190+}
 191+}
132192
133193 function getOldSyntrans($dc1, $dmid, $expid) {
134194 return getrow($dc1, "syntrans", "where defined_meaning_id=$dmid and expression_id=$expid");
@@ -142,8 +202,8 @@
143203
144204 function dupSyntrans($dc1, $dc2, $olddmid, $oldexpid, $newdmid, $newexpid) {
145205 $syntrans=getOldSyntrans($dc1, $olddmid, $oldexpid);
146 - $table=mysql_real_escape_string("${dc2}_syntrans");
147 - $newid=dupObject($syntrans["syntrans_sid"], $table, $dc1, $dc2);
 206+ $copier=new ObjectCopier($syntrans["syntrans_sid"], "syntrans", $dc1, $dc2);
 207+ $newid=$copier->dup();
148208 $syntrans["syntrans_sid"]=$newid;
149209 writeSyntrans($syntrans, $newdmid, $newexpid, $dc2);
150210 }
@@ -156,11 +216,13 @@
157217 /* some coy&paste happening here, might want to tidy even before we
158218 * toss this throwaway code*/
159219 function write_expression($expression, $src_dmid, $dst_dmid, $dc1, $dc2) {
160 - $target_table=mysql_real_escape_string("${dc2}_expression_ns");
161 - $target_expid1=dupobject($expression["expression_id"], $target_table, $dc1, $dc2);
 220+
 221+ $copier=new ObjectCopier($expression["expression_id"], "expression_ns", $dc1, $dc2);
 222+ $target_expid1=$copier->dup();
162223 var_dump($target_expid1);
163224 $save_expression=$expression;
164225 $save_expression["expression_id"]=$target_expid1;
 226+ $target_table=mysql_real_escape_string("${dc2}_expression_ns");
165227 mysql_insert_assoc($target_table,$save_expression);
166228 dupsyntrans(
167229 $dc1,
@@ -205,8 +267,9 @@
206268
207269 function dup_translated_content($dc1, $dc2, $tcid) {
208270 $translated_content=read_translated_content($dc1, $tcid);
209 - $target_table=mysql_real_escape_string("${dc2}_translated_content");
210 - $new_tcid=dupobject($tcid, $target_table, $dc1, $dc2);
 271+ $target_table=mysql_real_escape_string("translated_content");
 272+ $copier=new ObjectCopier($tcid, $target_table, $dc1, $dc2);
 273+ $new_tcid=$copier->dup();
211274 foreach ($translated_content as $item) {
212275 write_translated_content($dc1, $dc2, $new_tcid, $item);
213276 }
@@ -236,7 +299,8 @@
237300 }
238301
239302 function write_meaning_relation($dc1, $dc2, $new_dmid, $relation) {
240 - $relation["relation_id"]=dupobject($relation["relation_id"]);
 303+ $copier=new ObjectCopier($relation["relation_id"], "meaning_relations", $dc1, $dc2);
 304+ $relation["relation_id"]=$copier->dup;
241305 $relation["meaning1_mid"]=$new_dmid;
242306 $relation["meaning2_mid"]=dup_defined_meaning($relation["meaning2_mid"],$dc1, $dc2);
243307 }
@@ -282,14 +346,16 @@
283347 # bit of exp here too (defnitely need to tidy)
284348 $defining_expression=expression($this->defined_meaning["expression_id"], $dc1);
285349 $dm_target_table=mysql_real_escape_string("${dc2}_defined_meaning");
286 - $target_dmid=dupobject($this->defined_meaning["defined_meaning_id"], $dm_target_table, $dc1, $dc2);
 350+ $copier=new ObjectCopier($this->defined_meaning["defined_meaning_id"], "defined_meaning", $dc1, $dc2);
 351+ $target_dmid=$copier->dup();
287352 var_dump($target_dmid);
288353 $this->save_meaning=$this->defined_meaning;
289354 $this->save_meaning["defined_meaning_id"]=$target_dmid;
290355
291356 # exp
292357 $target_table=mysql_real_escape_string("${dc2}_expression_ns");
293 - $target_expid1=dupobject($defining_expression["expression_id"], $target_table, $dc1, $dc2);
 358+ $exp_copier=new ObjectCopier($defining_expression["expression_id"], $target_table, $dc1, $dc2);
 359+ $target_expid1=$exp_copier->dup();
294360 var_dump($target_expid1);
295361 $save_expression=$defining_expression;
296362 $save_expression["expression_id"]=$target_expid1;

Status & tagging log