Index: trunk/extensions/Wikidata/util/copy.php |
— | — | @@ -8,6 +8,8 @@ |
9 | 9 | # probably will refactor this code into ulta-pretty helpers or |
10 | 10 | # other recordset improvements. |
11 | 11 | # |
| 12 | +# Addendum: this might not actually be so throwaway as was hoped. |
| 13 | +# Don't you love it when that happens? |
12 | 14 | |
13 | 15 | |
14 | 16 | # common abbreviations used in varnames and comments: |
— | — | @@ -15,8 +17,8 @@ |
16 | 18 | # dmid = defined meaning id: unique identifier for each dm. |
17 | 19 | # dc = dataset context. datasets are implemented by having |
18 | 20 | # tables with different prefixes |
19 | | -# dc1 = dataset (context) 1 (we are copying FROM dc1) |
20 | | -# dc2 = dataset (context) 2 (we are copying TO dc2) |
| 21 | +# dc1 = dataset (context) 1 (we are copying FROM dc1 (so we READ) ) |
| 22 | +# dc2 = dataset (context) 2 (we are copying TO dc2 (so we WRITE) ) |
21 | 23 | # |
22 | 24 | |
23 | 25 | header("Content-type: text/html; charset=UTF-8"); |
— | — | @@ -27,6 +29,7 @@ |
28 | 30 | include_once("../../../LocalSettings.php"); |
29 | 31 | require_once("Setup.php"); |
30 | 32 | require_once("../OmegaWiki/WikiDataAPI.php"); |
| 33 | +require_once("../OmegaWiki/Transaction.php"); |
31 | 34 | |
32 | 35 | |
33 | 36 | global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname; |
— | — | @@ -42,11 +45,27 @@ |
43 | 46 | mysql_query("SET NAMES 'utf8'"); |
44 | 47 | |
45 | 48 | |
| 49 | +/** Times our execution time, nifty! */ |
46 | 50 | function stopwatch(){ |
47 | 51 | list($usec, $sec) = explode(" ", microtime()); |
48 | 52 | return ((float)$usec + (float)$sec); |
49 | 53 | } |
50 | 54 | |
| 55 | +/** start a new copy transaction |
| 56 | + */ |
| 57 | +function newCopyTransaction($dc1, $dc2) { |
| 58 | + startNewTransaction(0, "127.0.0.1", "copying from $dc1 to $dc2", $dc2); |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +/** retrieve a single row from the database as an associative array |
| 64 | + * @param $dc the dataset prefix we need |
| 65 | + * @param $table the name of the table (minus dataset prefix) |
| 66 | + * @peram $where the actual WHERE clause we need to uniquely find our row |
| 67 | + * @returns an associative array, representing our row. \ |
| 68 | + * keys=column headers, values = row contents |
| 69 | + */ |
51 | 70 | function getrow($dc, $table, $where) { |
52 | 71 | $target_table=mysql_real_escape_string("${dc}_${table}"); |
53 | 72 | $query="SELECT * FROM $target_table ".$where; |
— | — | @@ -55,18 +74,41 @@ |
56 | 75 | } |
57 | 76 | |
58 | 77 | |
| 78 | +/** retrieve multiple rows from the database, as an array of associative arrays. |
| 79 | + * @param $dc the dataset prefix we need |
| 80 | + * @param $table the name of the table (minus dataset prefix) |
| 81 | + * @peram $where the actual WHERE clause we need to uniquely find our row |
| 82 | + * @returns an array of associative arrays, representing our rows. \ |
| 83 | + * each associative array is structured with: \ |
| 84 | + * keys=column headers, values = row contents |
| 85 | + */ |
59 | 86 | function getrows($dc, $table, $where) { |
60 | 87 | $target_table=mysql_real_escape_string("${dc}_${table}"); |
61 | 88 | $query="SELECT * FROM $target_table ".$where; |
62 | 89 | return do_multirow_query($query); |
63 | 90 | } |
64 | 91 | |
| 92 | + |
| 93 | +/** Performs an arbitrary SQL query and returns an associative array |
| 94 | + * Assumes that only 1 row can be returned! |
| 95 | + * @param $query a valid SQL query |
| 96 | + * @returns an associative array, representing our row. \ |
| 97 | + * keys=column headers, values = row contents |
| 98 | + * |
| 99 | + */ |
65 | 100 | function doquery($query) { |
66 | 101 | echo $query; |
67 | 102 | $result = mysql_query($query)or die ("error ".mysql_error()); |
68 | 103 | $data= mysql_fetch_assoc($result); |
69 | 104 | return $data; |
70 | 105 | } |
| 106 | +/** Perform an arbitrary SQL query |
| 107 | + * |
| 108 | + * @param $query a valid SQL query |
| 109 | + * @returns an array of associative arrays, representing our rows. \ |
| 110 | + * each associative array is structured with: \ |
| 111 | + * keys=column headers, values = row contents |
| 112 | + */ |
71 | 113 | |
72 | 114 | function do_multirow_query($query) { |
73 | 115 | $result = mysql_query($query)or die ("error ".mysql_error()); |
— | — | @@ -77,10 +119,20 @@ |
78 | 120 | return $items; |
79 | 121 | } |
80 | 122 | |
| 123 | +/** obtain an expression definition from the database |
| 124 | + * @param $expression_id the id of the expression |
| 125 | + * @param $dc1 dataset to READ expression FROM |
| 126 | + */ |
81 | 127 | function expression($expression_id, $dc1) { |
82 | 128 | return getrow($dc1, "expression_ns", "WHERE expression_id=$expression_id"); |
83 | 129 | } |
84 | 130 | |
| 131 | +/** copies items in the objects table. |
| 132 | + * As a "side-effect" |
| 133 | + * also conveniently reports to see if something was already_there |
| 134 | + * (we don't want to accidentally duplicate things umpteen times, so the |
| 135 | + * side-effect is almost as important) |
| 136 | + */ |
85 | 137 | class ObjectCopier { |
86 | 138 | |
87 | 139 | protected $id; |
— | — | @@ -114,6 +166,11 @@ |
115 | 167 | $this->object=getrow($dc1, "objects", "WHERE object_id=$id"); |
116 | 168 | } |
117 | 169 | |
| 170 | + /* tries to retrieve the identical UUID from the destination |
| 171 | + * (dc2) dataset, if it exists. |
| 172 | + * @returns the associative array representing this object, |
| 173 | + * if successful. Else returns an empty array. |
| 174 | + */ |
118 | 175 | protected function identical() { |
119 | 176 | var_dump($this->object); |
120 | 177 | $uuid=mysql_escape_string($this->object["UUID"]); |
— | — | @@ -145,7 +202,7 @@ |
146 | 203 | } |
147 | 204 | } |
148 | 205 | |
149 | | -/** identical to array_key_exists(), but eats dirtier input |
| 206 | +/** identical to the php function array_key_exists(), but eats dirtier input |
150 | 207 | * returns false (rather than an error) on somewhat invalid input |
151 | 208 | */ |
152 | 209 | function sane_key_exists($key, $array) { |
— | — | @@ -202,9 +259,13 @@ |
203 | 260 | |
204 | 261 | /**convenience wrapper around mysql_insert_assoc |
205 | 262 | * like mysql_insert_assoc, but allows you to specify dc prefix+table name separately |
| 263 | + * Also transparently handles the internal transaction (WHICH MUST ALREADY BE OPEN!) |
206 | 264 | */ |
207 | 265 | function dc_insert_assoc($dc, $table_name, $array) { |
208 | 266 | $target_table=mysql_real_escape_string("${dc}_${table_name}"); |
| 267 | + if (sane_key_exists("add_transaction_id", $array)) { |
| 268 | + $array["add_transaction_id"]=getUpdateTransactionId(); |
| 269 | + } |
209 | 270 | return mysql_insert_assoc($target_table, $array); |
210 | 271 | } |
211 | 272 | |