From 46fbbd88c93e8cd00824d2295ad26448fabe922b Mon Sep 17 00:00:00 2001 From: habbes Date: Thu, 6 Jul 2017 13:46:28 +0200 Subject: [PATCH 1/6] Create script to update object field names --- bin/update_fields.php | 173 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 bin/update_fields.php diff --git a/bin/update_fields.php b/bin/update_fields.php new file mode 100644 index 00000000..8e02e799 --- /dev/null +++ b/bin/update_fields.php @@ -0,0 +1,173 @@ +#!/usr/bin/php +updateObjects(); + println('Done'); +} + + +class FieldUpdater { + + private $templateId; + private $fields; + + function __construct ($fs, $ts=null) { + $this->templateIds = $ts; + $this->fields = $fs; + } + + private function fetchObjectIds () { + $q = $this->buildQuery(); + return DB\dbQuery($q); + } + + private function buildQuery () { + $q = "SELECT o.id + FROM objects o"; + if (!empty($this->templateIds)) { + $ids = implode(',', $this->templateIds); + $q .= " JOIN tree t on o.id=t.id AND t.template_id in (".$ids.")"; + } + return $q; + } + + public function updateObjects () { + $res = $this->fetchObjectIds(); + while ($row = $res->fetch_assoc()) { + $this->updateObject($row['id']); + } + } + + public function updateObject ($id) { + $o = Objects::getCustomClassById($id); + $o->load(); + $data = $o->getData(); + foreach ($this->fields as $old=>$new) { + if (isset($data['data'][$old])) { + $data['data'][$new] = $data[$old]; + unset($data['data'][$old]); + } + } + $o->update($data); + } +} + +/** + * parses the templates string arg and returns + * an array of template ids + * @param string $t comma separated list of template + * ids: id1,id2,id3 + * @return array + */ +function parseTemplates ($t) { + return explode(',', $t); +} + +/** + * parses the fields string arg and returns + * an array mapping old fields to new fields + * @param string $f string-encoded list old to new fields + * mapping in the form oldName1:newName1,oldName2:newName2 + * @return array associative array mapping old field names + * to new names + */ +function parseFields ($f) { + $pairs = explode(',', $f); + return array_reduce($pairs, function($res, $item) { + $oldNew = explode(':', $item); + $res[$oldNew[0]] = $oldNew[1]; + return $res; + }, []); +} + +function printUsage() { + println('php update_fields.php -c -f -t '); + println('Options:'); + println('-c : the Casebox core name without the cb_ prefix'); + println('-f : list of fields to updated in the form oldName1:newName1,oldName2:newName2'); + println('-t (optional): comma-separated list of template ids'); + println('Note: If option -t is provided, only objects from those templates will be updated,' + .' otherwise ALL objects will be updated.'); + println(); + println("Example:"); + println("php update_fields.php -c demo -f age:victim_age,sex:gender -t 3849,1234"); +} + +function println ($s='') { + echo "$s\n"; +} From 8fcb8c4e7f61dc8c897e05c228d601992725bb11 Mon Sep 17 00:00:00 2001 From: habbes Date: Thu, 6 Jul 2017 13:54:14 +0200 Subject: [PATCH 2/6] Add comments to update_fields script --- bin/update_fields.php | 64 ++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/bin/update_fields.php b/bin/update_fields.php index 8e02e799..97b539d0 100644 --- a/bin/update_fields.php +++ b/bin/update_fields.php @@ -22,6 +22,8 @@ php update_fields.php -c mycore -t 1123,230 -f age:victim_age,sex:gender php update_fields.php -c mycore -f name:first_name + + After running this script, you should update the solr prepared date and reindex the solr core. */ @@ -78,34 +80,29 @@ function run () { // $updater = new FieldUpdater($fs, $ts); // $updater->updateObjects(); println('Done'); + println("Now update solr prepared data and reindex the solr core"); } class FieldUpdater { - private $templateId; + private $templateIds; private $fields; + /** + * @param array $fs mapping of old field names to new names + * @param array $ts list of template ids + */ function __construct ($fs, $ts=null) { $this->templateIds = $ts; $this->fields = $fs; } - private function fetchObjectIds () { - $q = $this->buildQuery(); - return DB\dbQuery($q); - } - - private function buildQuery () { - $q = "SELECT o.id - FROM objects o"; - if (!empty($this->templateIds)) { - $ids = implode(',', $this->templateIds); - $q .= " JOIN tree t on o.id=t.id AND t.template_id in (".$ids.")"; - } - return $q; - } - + /** + * Fetches and updates objects + * based on the set template ids and field + * names to udpate + */ public function updateObjects () { $res = $this->fetchObjectIds(); while ($row = $res->fetch_assoc()) { @@ -113,6 +110,10 @@ public function updateObjects () { } } + /** + * update the object with the specified id + * @param mixed $id + */ public function updateObject ($id) { $o = Objects::getCustomClassById($id); $o->load(); @@ -125,6 +126,30 @@ public function updateObject ($id) { } $o->update($data); } + + /** + * fetch object ids from the db + * @return resource the db cursor + */ + private function fetchObjectIds () { + $q = $this->buildQuery(); + return DB\dbQuery($q); + } + + /** + * builds sql query to use for fetching + * objects based on the specified templates + * @return string + */ + private function buildQuery () { + $q = "SELECT o.id + FROM objects o"; + if (!empty($this->templateIds)) { + $ids = implode(',', $this->templateIds); + $q .= " JOIN tree t on o.id=t.id AND t.template_id in (".$ids.")"; + } + return $q; + } } /** @@ -155,6 +180,9 @@ function parseFields ($f) { }, []); } +/** + * prints helpful usage info + */ function printUsage() { println('php update_fields.php -c -f -t '); println('Options:'); @@ -168,6 +196,10 @@ function printUsage() { println("php update_fields.php -c demo -f age:victim_age,sex:gender -t 3849,1234"); } +/** + * ouputs the specified string followed by a new line + * @param string $s + */ function println ($s='') { echo "$s\n"; } From 75c30f4477e1847f3204b2006a2bfac1e8028dce Mon Sep 17 00:00:00 2001 From: habbes Date: Thu, 6 Jul 2017 13:55:55 +0200 Subject: [PATCH 3/6] Enable object fields update code --- bin/update_fields.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/bin/update_fields.php b/bin/update_fields.php index 97b539d0..55d556b1 100644 --- a/bin/update_fields.php +++ b/bin/update_fields.php @@ -1,6 +1,6 @@ #!/usr/bin/php updateObjects(); + $updater = new FieldUpdater($fs, $ts); + $updater->updateObjects(); println('Done'); println("Now update solr prepared data and reindex the solr core"); } From 697df163dfc147f02fcce4ee6c36edf0dad84116 Mon Sep 17 00:00:00 2001 From: habbes Date: Fri, 7 Jul 2017 11:00:37 +0200 Subject: [PATCH 4/6] Fix bugs in update_fields script --- bin/update_fields.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bin/update_fields.php b/bin/update_fields.php index 55d556b1..426a0259 100644 --- a/bin/update_fields.php +++ b/bin/update_fields.php @@ -50,9 +50,9 @@ run(); function run () { - $opts = getopt('c:f:t:h', ['help']); + $opts = getopt('c:f:t:h'); var_dump($opts); - if (isset($opts['h']) || isset($opts['help'])) { + if (isset($opts['h'])) { printUsage(); return; } @@ -113,12 +113,12 @@ public function updateObjects () { * @param mixed $id */ public function updateObject ($id) { - $o = Objects::getCustomClassById($id); + $o = Objects::getCustomClassByObjectId($id); $o->load(); $data = $o->getData(); foreach ($this->fields as $old=>$new) { - if (isset($data['data'][$old])) { - $data['data'][$new] = $data[$old]; + if (array_key_exists($old, $data['data'])) { + $data['data'][$new] = $data['data'][$old]; unset($data['data'][$old]); } } From 980d3c0339843bacc768e664c38394772976ae01 Mon Sep 17 00:00:00 2001 From: habbes Date: Mon, 10 Jul 2017 09:30:33 +0200 Subject: [PATCH 5/6] Fix errors in update_fields script --- bin/update_fields.php | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/bin/update_fields.php b/bin/update_fields.php index 426a0259..0ee653d4 100644 --- a/bin/update_fields.php +++ b/bin/update_fields.php @@ -104,7 +104,7 @@ function __construct ($fs, $ts=null) { public function updateObjects () { $res = $this->fetchObjectIds(); while ($row = $res->fetch_assoc()) { - $this->updateObject($row['id']); + $this->updateObject($row); } } @@ -112,17 +112,15 @@ public function updateObjects () { * update the object with the specified id * @param mixed $id */ - public function updateObject ($id) { - $o = Objects::getCustomClassByObjectId($id); - $o->load(); - $data = $o->getData(); + public function updateObject ($row) { + $data = json_decode($row['data'], true); foreach ($this->fields as $old=>$new) { - if (array_key_exists($old, $data['data'])) { - $data['data'][$new] = $data['data'][$old]; - unset($data['data'][$old]); + if (array_key_exists($old, $data)) { + $data[$new] = $data[$old]; + unset($data[$old]); } } - $o->update($data); + $this->saveToDb($row['id'], $data); } /** @@ -140,7 +138,7 @@ private function fetchObjectIds () { * @return string */ private function buildQuery () { - $q = "SELECT o.id + $q = "SELECT o.id, o.data FROM objects o"; if (!empty($this->templateIds)) { $ids = implode(',', $this->templateIds); @@ -148,6 +146,18 @@ private function buildQuery () { } return $q; } + + /** + * persists object data in db + * @param mixed $id id of the object to update + * @param array $data + */ + private function saveToDb ($id, $data) { + $data = json_encode($data); + $q = "UPDATE objects SET data=$2 + WHERE id=$1"; + DB\dbQuery($q, [$id, $data]); + } } /** From 099bac62243bdaab1735c607bf436b54432ea24e Mon Sep 17 00:00:00 2001 From: habbes Date: Mon, 10 Jul 2017 12:13:32 +0200 Subject: [PATCH 6/6] Update debug statements for update_fields.php script --- bin/update_fields.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bin/update_fields.php b/bin/update_fields.php index 0ee653d4..82f4e007 100644 --- a/bin/update_fields.php +++ b/bin/update_fields.php @@ -51,7 +51,6 @@ function run () { $opts = getopt('c:f:t:h'); - var_dump($opts); if (isset($opts['h'])) { printUsage(); return; @@ -110,10 +109,14 @@ public function updateObjects () { /** * update the object with the specified id - * @param mixed $id + * @param array $row db row with id and data fields */ public function updateObject ($row) { $data = json_decode($row['data'], true); + if (empty($data)) { + println("Empty data for object ".$row['id']); + return; + } foreach ($this->fields as $old=>$new) { if (array_key_exists($old, $data)) { $data[$new] = $data[$old];