r26422 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r26421‎ | r26422 | r26423 >
Date:00:00, 5 October 2007
Author:kim
Status:old
Tags:
Comment:
Copy.php now does some attributes copying.
Will debug this first, then finish rest of attributes.

basic "this doesn't break anything else at least" commit
Modified paths:
  • /trunk/extensions/Wikidata/OmegaWiki/Copy.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Wikidata/OmegaWiki/Copy.php
@@ -14,12 +14,16 @@
1515 # dc1 = dataset (context) 1 (we are copying FROM dc1 (so we READ) )
1616 # dc2 = dataset (context) 2 (we are copying TO dc2 (so we WRITE) )
1717 #
18 -# naming conventions:
 18+# naming conventions (may deviate slightly from current conventions document):
1919 # Normal: Java Style
2020 # * ClassName->methodName($variableName); /* comment */
2121 # * CopyTools::getRow(...); # comment
2222 # Wrappers around PHP functions or extensions to PHP function set: Same style as the wrapped function
2323 # * mysql_insert_assoc(...); # comment
 24+# Variables that coincide with database columns: Same style as column
 25+# * $object_id
 26+# * $defined_meaning_id
 27+# $ $attribute_mid
2428 #
2529 # TODO:
2630 # * Change to library
@@ -125,17 +129,14 @@
126130 }
127131
128132 protected function read() {
129 - echo "crumb e1<br>\n";
130133 $dc1=$this->dc1;
131134 $id=$this->id;
132135 if (is_null($dc1))
133136 throw new Exception("ObjectCopier: provided source dataset(dc1) is null");
134137 if (is_null($id))
135138 throw new Exception("ObjectCopier: provided identifier is null");
136 - echo "crumb e2 --$dc1--,--$id-- <br>\n";
 139+
137140 $this->object=CopyTools::getRow($dc1, "objects", "WHERE object_id=$id");
138 - var_dump($this->object);
139 - echo "crumb e3<br>\n";
140141 }
141142
142143 /* tries to retrieve the identical UUID from the destination
@@ -147,8 +148,6 @@
148149 $uuid=mysql_escape_string($this->object["UUID"]);
149150 if (is_null($uuid))
150151 throw new Exception("ObjectCopier: UUID is null");
151 - echo "crumb f1 identical() old object :";
152 - var_dump($this->object);
153152 $dc2=$this->dc2;
154153 return CopyTools::getRow($dc2, "objects", "WHERE `UUID`='$uuid'");
155154 }
@@ -201,7 +200,10 @@
202201 }
203202
204203 /**
205 - *
 204+ * create a valid object key in the objects table, and return it
 205+ * @param $dc the dataset (prefix) to create the object in
 206+ * @param $table which table is the object originally from? (minus dataset prefix)
 207+ * @param $uuid (optional) : override the auto-generated uuid with this one.
206208 */
207209 public static function makeObjectId($dc, $table, $uuid=null) {
208210 # Sorta Exploiting internals, because -hey- we're internal
@@ -215,10 +217,7 @@
216218 }
217219
218220 function dup() {
219 - echo "crumb d1<br>\n";
220 - echo "crumb d2\n";
221221 if (is_null($this->id)) {
222 - echo "autovivify follows<br>\n";
223222 var_dump($this->autovivify);
224223 if ($this->autovivify) {
225224 $this->create_key();
@@ -230,7 +229,6 @@
231230 $this->read();
232231 if (!CopyTools::sane_key_exists("object_id", $this->object)) {
233232 if ($this->autovivify) {
234 - echo "crumb d2A more autovivify\n";
235233 $this->create_key();
236234 } else {
237235 echo "crumb d2B\n";
@@ -240,10 +238,7 @@
241239 }
242240 }
243241
244 - echo "crumb d3\, comparison:\n";
245242 $object2=$this->identical();
246 - echo "other db object<br>";
247 - var_dump($object2);
248243 if (CopyTools::sane_key_exists("object_id",$object2)) {
249244 $this->already_there=true;
250245 $newid=$object2["object_id"];
@@ -251,7 +246,7 @@
252247 $this->already_there=false;
253248 $newid=$this->write();
254249 }
255 - echo "crumb d4\n";
 250+ AttributeCopier::copy($object["object_id"], $object2["object_id"]);
256251 return $newid;
257252 }
258253 }
@@ -395,8 +390,6 @@
396391 }
397392
398393 function write_single($relation) {
399 - $dc1=$this->dc1;
400 - $dc2=$this->dc2;
401394 $new_dmid=$this->new_dmid;
402395
403396 if ($this->doObject($relation, "relation_id"))
@@ -963,6 +956,9 @@
964957 }
965958
966959 /** copying stuff in the %_class_attributes table actually
 960+ * TODO: Actually I'm keying on class_mid atm, while I could be using the object_id-s
 961+ * instead, the same way as the other AttributesCopiers.
 962+ * I didn't realise this upfront. Changing this would be a nice improvement.
967963 */
968964 class ClassAttributesCopier extends Copier {
969965
@@ -1025,8 +1021,72 @@
10261022
10271023 }
10281024
 1025+/** copying stuff in the %_class_attributes table
 1026+ * This version keys on attribute_id (object_id)
 1027+ */
 1028+class ClassAttributesCopier2 extends Copier {
 1029+
 1030+ protected $object_id;
 1031+ protected $dc1;
 1032+ protected $dc2;
 1033+ protected $tableName="class_attributes";
10291034
 1035+ /** you saw that right, class_mid, not class_id, there's no such thing :-/
 1036+ */
 1037+ public function __construct($object_id, $dc1, $dc2) {
 1038+ $this->object_id=$object_id;
 1039+ $this->dc1=$dc1;
 1040+ $this->dc2=$dc2;
 1041+ }
10301042
 1043+ /** unchracteristically, returns the new class_mid, rather than object_id
 1044+ * because in this case, the class_mid is the key characteristic
 1045+ */
 1046+ public function dup() {
 1047+ if (is_null($this->object_id))
 1048+ throw new Exception ("ClassAttributesCopier2: Can't copy class by object_id: is null!");
 1049+ $attributes=$this->read();
 1050+ return $this->write($attributes);
 1051+ }
 1052+
 1053+ # refactor candidate?
 1054+ public function read() {
 1055+ $dc1=$this->dc1;
 1056+ $object_id=$this->object_id;
 1057+ return CopyTools::getRows($dc1, $this->tableName, "WHERE object_id=$object_id");
 1058+ }
 1059+
 1060+ #refactor_candidate
 1061+ public function write($attributes) {
 1062+ foreach ($attributes as $attribute) {
 1063+ $latest=$this->write_single($attribute);
 1064+ }
 1065+ return $latest;
 1066+ }
 1067+
 1068+
 1069+ public function write_single($attribute) {
 1070+ $dc1=$this->dc1;
 1071+ $dc2=$this->dc2;
 1072+
 1073+ # TODO: Check: Is *this* actually safe?
 1074+ if ($this->doObject($attribute,"object_id"))
 1075+ return $attribute["object_id"];
 1076+
 1077+ $this->doDM($attribute, "class_mid"); #safe to do here, though not in the first ver.
 1078+ $this->doDM($attribute, "level_mid");
 1079+ $this->doDM($attribute, "attribute_mid");
 1080+
 1081+ CopyTools::dc_insert_assoc($dc2, "class_attributes", $attribute);
 1082+
 1083+ return $attribute["object_id"];
 1084+ }
 1085+
 1086+}
 1087+
 1088+
 1089+
 1090+
10311091 /** abstract superclass for copiers
10321092 * will gradually be implemented anywhere I create, refactor, or
10331093 */
@@ -1057,7 +1117,7 @@
10581118
10591119 /** reads row or rows from table in source dataset (dc1)
10601120 * @return row or array of rows for table in mysql_read_assoc() format */
1061 - public abstract function read();
 1121+ protected abstract function read();
10621122
10631123 /** writes row or array of rows in mysql_read_assoc() format
10641124 * @return the unique id for the item we just copied in the destination (dc2) dataset,
@@ -1133,8 +1193,166 @@
11341194 CopyTools::dc_insert_assoc($this->dc2, $this->tableName, $row);
11351195 }
11361196
 1197+}
11371198
 1199+
 1200+abstract class AttributeCopier extends Copier {
 1201+
 1202+ protected $src_object_id=null;
 1203+ protected $dst_object_id=null;
 1204+
 1205+ public function __construct($dc1, $dc2, $src_object_id, $dst_object_id){
 1206+ $this->dc1=$dc1;
 1207+ $this->dc2=$dc2;
 1208+ $this->src_object_id=$src_object_id;
 1209+ $this->dst_object_id=$dst_object_id;
 1210+ }
 1211+
 1212+
 1213+ public static function copy($src_object_id, $dst_object_id) {
 1214+ echo "<h3> crumb: would copy attribs </h3>";
 1215+ if (is_null($src_object_id))
 1216+ throw new Exception("AttributeCopier: cannot copy: source object_id=null");
 1217+
 1218+ if (is_null($dst_object_id))
 1219+ throw new Exception("AttributeCopier: cannot copy: destination object_id=null");
 1220+ #$optionAttribueCopier=new OptionAttributeCopier($src_object_id, $dst_object_id);
 1221+ #$optionAttributeCopier.dup();
 1222+
 1223+ #$textAttributeCopier=new textAttributeCopier($src_object_id, $dst_object_id);
 1224+ #$textAttributeCopier.dup();
 1225+
 1226+ #$translatedContentCopier=new translatedContentCopier($src_object_id, $dst_object_id);
 1227+ #$translatedContentCopier.dup();
 1228+
 1229+ #$urlAttributeCopier=new URLAttributeCpier($src_object_id, $dst_object_id);
 1230+ #$urlAttributeCopier.dup();
 1231+ }
 1232+
 1233+ protected function write($values) {
 1234+ foreach ($values as $value) {
 1235+ $latest=write_single($value);
 1236+ }
 1237+ return $latest;
 1238+ }
 1239+
 1240+ protected abstract function write_single($attribute);
 1241+
 1242+ protected function read() {
 1243+ $src_object_id=$this->src_object_id;
 1244+ if (is_null($src_object_id))
 1245+ throw new Exception("*AttributeCopier: cannot read: source object_id is null");
 1246+
 1247+ $tableName=$this->tableName;
 1248+ if (is_null($tableName))
 1249+ throw new Exception("*AttributeCopier: cannot read: table name is null");
 1250+
 1251+ return CopyTools::getRows($dc1, $tableName, "WHERE object_id=$src_object_id");
 1252+ }
 1253+
 1254+ /** slightly different dup interface yet again.
 1255+ * (I'm still experimenting. TODO: Settle on one for all.)
 1256+ * always returns destination object_id of last/arbitrary
 1257+ * item dupped. (which means we can use this particular dup functuon
 1258+ * for single *or* multi copy)
 1259+ */
 1260+ public function dup() {
 1261+ $attributes=$this->read();
 1262+ return $this->write($attributes);
 1263+ }
 1264+
11381265 }
11391266
11401267
 1268+class OptionAttributeCopier extends AttributeCopier{
 1269+ protected $tableName="option_attribute_values"; //Name of the table this class operates on.
 1270+
 1271+ public function __construct($dc1, $dc2, $src_object_id, $dst_object_id){
 1272+ parent::__construct($dc1, $dc2, $src_object_id, $dst_object_id);
 1273+ }
 1274+
 1275+ /**
 1276+ * *all attribute_value tables:
 1277+ * **value_id: unique id in objects table
 1278+ * **object_id: object we are referring to
 1279+ * * Unique to option_attribute_values
 1280+ * ** option_id: reference to the option_attribute_options table
 1281+ */
 1282+ public function write_single($attribute) {
 1283+
 1284+ if ($this->doObject($attribute, "value_id"))
 1285+ return $attribute["value_id"];
 1286+
 1287+ $attribute["object_id"]=$this->new_object_id;
 1288+
 1289+ $oaocopier=new OptionAttributeOptionsCopier($attribute["option_id"], $dc1, $dc2);
 1290+ $attribute["option_id"]=$oaocopier->dup();
 1291+
 1292+ $this->doInsert($attribute);
 1293+ return $attribute["value_id"];
 1294+ }
 1295+}
 1296+
 1297+/** Yes, there is actually a table called option_attribute_options.
 1298+ * These are the actual *options* that go with a particular option attribute
 1299+ * extends copier, not AttributeCopier, because oa_options are not themselves attributes.
 1300+ *
 1301+ * naming: $oao(s) is/are ObjectAtributeOption(s).
 1302+ */
 1303+class OptionAttributeOptionsCopier extends Copier {
 1304+ protected $option_id;
 1305+ protected $tableName="option_attribute_options"; //Name of the table this class operates on.
 1306+
 1307+ public function __construct($option_id, $dc1, $dc2) {
 1308+ if (is_null($option_id))
 1309+ throw new Exception("OptionAttributeOptionsCopier: trying to construct with null option_id. No can do compadre.");
 1310+
 1311+ $this->option_id=$option_id;
 1312+ $this->dc1=$dc1;
 1313+ $this->dc2=$dc2;
 1314+ }
 1315+
 1316+ public function dup() {
 1317+ $oaos=read();
 1318+ return write($oaos);
 1319+ }
 1320+
 1321+ public function read(){
 1322+ return CopyTools::getRows($dc1, "class_membership", "WHERE class_member_mid=$class_member_mid");
 1323+ }
 1324+
 1325+ /**
 1326+ * TODO This is a refactor-candidate.
 1327+ */
 1328+ public function write($oaos) {
 1329+ foreach ($oaos as $oao) {
 1330+ $latest=$this->write_single($oao);
 1331+ }
 1332+ return $latest;
 1333+ }
 1334+
 1335+ /**
 1336+ * option_id: unique/ objects reference
 1337+ * attribute_id: reference to class_attributes (we think!)
 1338+ * option_mid: dm for this object.
 1339+ * language_id: reference to mediawiki languages table
 1340+ */
 1341+ public function write_single($oao) {
 1342+
 1343+ if ($this->doObject($oao, "option_id"))
 1344+ return $oao["option_id"];
 1345+
 1346+ $cacopier=new ClassAttributesCopier($oao["attribute_id"], $dc1, $dc2);
 1347+ $oao["attribute_id"]=$cacopier->dup();
 1348+
 1349+ $this->doDM($oao, "option_mid");
 1350+ #language_id is mediawiki, not wikidata, so that's ok.
 1351+
 1352+ return $oao["option_id"];
 1353+ }
 1354+
 1355+
 1356+}
 1357+
 1358+
11411359 ?>

Status & tagging log