Index: trunk/extensions/Wikidata/OmegaWiki/Copy.php |
— | — | @@ -14,12 +14,16 @@ |
15 | 15 | # dc1 = dataset (context) 1 (we are copying FROM dc1 (so we READ) ) |
16 | 16 | # dc2 = dataset (context) 2 (we are copying TO dc2 (so we WRITE) ) |
17 | 17 | # |
18 | | -# naming conventions: |
| 18 | +# naming conventions (may deviate slightly from current conventions document): |
19 | 19 | # Normal: Java Style |
20 | 20 | # * ClassName->methodName($variableName); /* comment */ |
21 | 21 | # * CopyTools::getRow(...); # comment |
22 | 22 | # Wrappers around PHP functions or extensions to PHP function set: Same style as the wrapped function |
23 | 23 | # * 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 |
24 | 28 | # |
25 | 29 | # TODO: |
26 | 30 | # * Change to library |
— | — | @@ -125,17 +129,14 @@ |
126 | 130 | } |
127 | 131 | |
128 | 132 | protected function read() { |
129 | | - echo "crumb e1<br>\n"; |
130 | 133 | $dc1=$this->dc1; |
131 | 134 | $id=$this->id; |
132 | 135 | if (is_null($dc1)) |
133 | 136 | throw new Exception("ObjectCopier: provided source dataset(dc1) is null"); |
134 | 137 | if (is_null($id)) |
135 | 138 | throw new Exception("ObjectCopier: provided identifier is null"); |
136 | | - echo "crumb e2 --$dc1--,--$id-- <br>\n"; |
| 139 | + |
137 | 140 | $this->object=CopyTools::getRow($dc1, "objects", "WHERE object_id=$id"); |
138 | | - var_dump($this->object); |
139 | | - echo "crumb e3<br>\n"; |
140 | 141 | } |
141 | 142 | |
142 | 143 | /* tries to retrieve the identical UUID from the destination |
— | — | @@ -147,8 +148,6 @@ |
148 | 149 | $uuid=mysql_escape_string($this->object["UUID"]); |
149 | 150 | if (is_null($uuid)) |
150 | 151 | throw new Exception("ObjectCopier: UUID is null"); |
151 | | - echo "crumb f1 identical() old object :"; |
152 | | - var_dump($this->object); |
153 | 152 | $dc2=$this->dc2; |
154 | 153 | return CopyTools::getRow($dc2, "objects", "WHERE `UUID`='$uuid'"); |
155 | 154 | } |
— | — | @@ -201,7 +200,10 @@ |
202 | 201 | } |
203 | 202 | |
204 | 203 | /** |
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. |
206 | 208 | */ |
207 | 209 | public static function makeObjectId($dc, $table, $uuid=null) { |
208 | 210 | # Sorta Exploiting internals, because -hey- we're internal |
— | — | @@ -215,10 +217,7 @@ |
216 | 218 | } |
217 | 219 | |
218 | 220 | function dup() { |
219 | | - echo "crumb d1<br>\n"; |
220 | | - echo "crumb d2\n"; |
221 | 221 | if (is_null($this->id)) { |
222 | | - echo "autovivify follows<br>\n"; |
223 | 222 | var_dump($this->autovivify); |
224 | 223 | if ($this->autovivify) { |
225 | 224 | $this->create_key(); |
— | — | @@ -230,7 +229,6 @@ |
231 | 230 | $this->read(); |
232 | 231 | if (!CopyTools::sane_key_exists("object_id", $this->object)) { |
233 | 232 | if ($this->autovivify) { |
234 | | - echo "crumb d2A more autovivify\n"; |
235 | 233 | $this->create_key(); |
236 | 234 | } else { |
237 | 235 | echo "crumb d2B\n"; |
— | — | @@ -240,10 +238,7 @@ |
241 | 239 | } |
242 | 240 | } |
243 | 241 | |
244 | | - echo "crumb d3\, comparison:\n"; |
245 | 242 | $object2=$this->identical(); |
246 | | - echo "other db object<br>"; |
247 | | - var_dump($object2); |
248 | 243 | if (CopyTools::sane_key_exists("object_id",$object2)) { |
249 | 244 | $this->already_there=true; |
250 | 245 | $newid=$object2["object_id"]; |
— | — | @@ -251,7 +246,7 @@ |
252 | 247 | $this->already_there=false; |
253 | 248 | $newid=$this->write(); |
254 | 249 | } |
255 | | - echo "crumb d4\n"; |
| 250 | + AttributeCopier::copy($object["object_id"], $object2["object_id"]); |
256 | 251 | return $newid; |
257 | 252 | } |
258 | 253 | } |
— | — | @@ -395,8 +390,6 @@ |
396 | 391 | } |
397 | 392 | |
398 | 393 | function write_single($relation) { |
399 | | - $dc1=$this->dc1; |
400 | | - $dc2=$this->dc2; |
401 | 394 | $new_dmid=$this->new_dmid; |
402 | 395 | |
403 | 396 | if ($this->doObject($relation, "relation_id")) |
— | — | @@ -963,6 +956,9 @@ |
964 | 957 | } |
965 | 958 | |
966 | 959 | /** 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. |
967 | 963 | */ |
968 | 964 | class ClassAttributesCopier extends Copier { |
969 | 965 | |
— | — | @@ -1025,8 +1021,72 @@ |
1026 | 1022 | |
1027 | 1023 | } |
1028 | 1024 | |
| 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"; |
1029 | 1034 | |
| 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 | + } |
1030 | 1042 | |
| 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 | + |
1031 | 1091 | /** abstract superclass for copiers |
1032 | 1092 | * will gradually be implemented anywhere I create, refactor, or |
1033 | 1093 | */ |
— | — | @@ -1057,7 +1117,7 @@ |
1058 | 1118 | |
1059 | 1119 | /** reads row or rows from table in source dataset (dc1) |
1060 | 1120 | * @return row or array of rows for table in mysql_read_assoc() format */ |
1061 | | - public abstract function read(); |
| 1121 | + protected abstract function read(); |
1062 | 1122 | |
1063 | 1123 | /** writes row or array of rows in mysql_read_assoc() format |
1064 | 1124 | * @return the unique id for the item we just copied in the destination (dc2) dataset, |
— | — | @@ -1133,8 +1193,166 @@ |
1134 | 1194 | CopyTools::dc_insert_assoc($this->dc2, $this->tableName, $row); |
1135 | 1195 | } |
1136 | 1196 | |
| 1197 | +} |
1137 | 1198 | |
| 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 | + |
1138 | 1265 | } |
1139 | 1266 | |
1140 | 1267 | |
| 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 | + |
1141 | 1359 | ?> |