r25928 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r25927‎ | r25928 | r25929 >
Date:23:18, 18 September 2007
Author:kim
Status:old
Tags:
Comment:
Add transaction support for copy.php

Transaction restrictions not yet implemented
Modified paths:
  • /trunk/extensions/Wikidata/util/copy.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Wikidata/util/copy.php
@@ -8,6 +8,8 @@
99 # probably will refactor this code into ulta-pretty helpers or
1010 # other recordset improvements.
1111 #
 12+# Addendum: this might not actually be so throwaway as was hoped.
 13+# Don't you love it when that happens?
1214
1315
1416 # common abbreviations used in varnames and comments:
@@ -15,8 +17,8 @@
1618 # dmid = defined meaning id: unique identifier for each dm.
1719 # dc = dataset context. datasets are implemented by having
1820 # 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) )
2123 #
2224
2325 header("Content-type: text/html; charset=UTF-8");
@@ -27,6 +29,7 @@
2830 include_once("../../../LocalSettings.php");
2931 require_once("Setup.php");
3032 require_once("../OmegaWiki/WikiDataAPI.php");
 33+require_once("../OmegaWiki/Transaction.php");
3134
3235
3336 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname;
@@ -42,11 +45,27 @@
4346 mysql_query("SET NAMES 'utf8'");
4447
4548
 49+/** Times our execution time, nifty! */
4650 function stopwatch(){
4751 list($usec, $sec) = explode(" ", microtime());
4852 return ((float)$usec + (float)$sec);
4953 }
5054
 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+ */
5170 function getrow($dc, $table, $where) {
5271 $target_table=mysql_real_escape_string("${dc}_${table}");
5372 $query="SELECT * FROM $target_table ".$where;
@@ -55,18 +74,41 @@
5675 }
5776
5877
 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+ */
5986 function getrows($dc, $table, $where) {
6087 $target_table=mysql_real_escape_string("${dc}_${table}");
6188 $query="SELECT * FROM $target_table ".$where;
6289 return do_multirow_query($query);
6390 }
6491
 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+ */
65100 function doquery($query) {
66101 echo $query;
67102 $result = mysql_query($query)or die ("error ".mysql_error());
68103 $data= mysql_fetch_assoc($result);
69104 return $data;
70105 }
 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+ */
71113
72114 function do_multirow_query($query) {
73115 $result = mysql_query($query)or die ("error ".mysql_error());
@@ -77,10 +119,20 @@
78120 return $items;
79121 }
80122
 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+ */
81127 function expression($expression_id, $dc1) {
82128 return getrow($dc1, "expression_ns", "WHERE expression_id=$expression_id");
83129 }
84130
 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+ */
85137 class ObjectCopier {
86138
87139 protected $id;
@@ -114,6 +166,11 @@
115167 $this->object=getrow($dc1, "objects", "WHERE object_id=$id");
116168 }
117169
 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+ */
118175 protected function identical() {
119176 var_dump($this->object);
120177 $uuid=mysql_escape_string($this->object["UUID"]);
@@ -145,7 +202,7 @@
146203 }
147204 }
148205
149 -/** identical to array_key_exists(), but eats dirtier input
 206+/** identical to the php function array_key_exists(), but eats dirtier input
150207 * returns false (rather than an error) on somewhat invalid input
151208 */
152209 function sane_key_exists($key, $array) {
@@ -202,9 +259,13 @@
203260
204261 /**convenience wrapper around mysql_insert_assoc
205262 * 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!)
206264 */
207265 function dc_insert_assoc($dc, $table_name, $array) {
208266 $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+ }
209270 return mysql_insert_assoc($target_table, $array);
210271 }
211272

Status & tagging log