Skip to content

Commit 8b6d8c7

Browse files
committed
add tool to backpopulate data
1 parent 354fc4d commit 8b6d8c7

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/**
3+
* This script is intended for a one-time use only to restore the value of the
4+
* `UserID` column of instrument tables and the `UserID` key of the instrument
5+
* JSON `Data` in the flag table.
6+
*
7+
* This tool queries data from the `history` table and meaningfully imports
8+
* data from its `userID` column back into the instrument and `flag` tables.
9+
*
10+
* Example use:
11+
* $ php SaveUserIDToInstrumentData.php [confirm]
12+
13+
* PHP Version 7
14+
*
15+
* @category Tools
16+
* @package Loris
17+
* @author Zaliqa Rosli <zaliqa.rosli@mcin.ca>
18+
* @licence LORIS License
19+
* @link https://www.github.com/aces/Loris
20+
*/
21+
require_once __DIR__ . '/../generic_includes.php';
22+
23+
$confirm = false;
24+
if (isset($argv[1]) && $argv[1] === 'confirm') {
25+
$confirm = true;
26+
}
27+
28+
echo "\n\nQuerying data...\n";
29+
$db = \NDB_Factory::singleton()->database();
30+
// Query CommentIDs with DECS 'Complete', and
31+
// userID for latest changes made to the instrument data
32+
$history = $db->pselect(
33+
"SELECT h1.tbl, h1.primaryVals AS CommentID, h1.userID
34+
FROM history h1
35+
INNER JOIN
36+
(
37+
SELECT primaryVals, MAX(changeDate) AS max_date
38+
FROM history
39+
WHERE primaryVals IN (
40+
SELECT primaryVals
41+
FROM history
42+
WHERE col = 'Data_entry_completion_status'
43+
AND primaryCols = 'CommentID'
44+
AND new = 'Complete'
45+
)
46+
GROUP BY primaryVals
47+
) h2
48+
ON h1.primaryVals = h2.primaryVals
49+
AND h1.changeDate = h2.max_date
50+
WHERE userID <> 'unknown'
51+
AND tbl <> 'flag'
52+
AND col <> 'CommentID'
53+
GROUP BY h1.primaryVals, h1.changeDate, h1.userID",
54+
array()
55+
);
56+
if (empty($history)) {
57+
echo "\n\nThere is no data to import.\n";
58+
die();
59+
}
60+
echo "\n\nThe following data can be imported into the instrument tables:\n";
61+
foreach ($history as $index => $row) {
62+
$result = $index + 1;
63+
echo "\n\nResult $result:\n";
64+
print_r($row);
65+
66+
$table = $row['tbl'];
67+
$commentID = $row['CommentID'];
68+
$userID = $row['userID'];
69+
70+
// Extract old data from flag so we can update it with merge so that we
71+
// don't overwrite it
72+
$oldData = $db->pselectOne(
73+
"SELECT Data FROM flag WHERE CommentID=:cid",
74+
array('cid' => $commentID)
75+
);
76+
echo "\n\nOld Data: \n";
77+
print_r($oldData);
78+
79+
// (The PDO driver seems to return null as "null" for JSON column types)
80+
if (!empty($oldData) && $oldData !== "null") {
81+
$oldData = json_decode($oldData, true);
82+
} else {
83+
$oldData = array();
84+
}
85+
$newData = array_merge(
86+
$oldData ?? array(),
87+
array('UserID' => $userID)
88+
);
89+
echo "\n\nNew Data: \n";
90+
print_r(json_encode($newData));
91+
92+
if ($confirm) {
93+
echo "\n\nImporting data into respective instrument tables and commentID flag entries...\n";
94+
// Update instrument table
95+
$db->update(
96+
$table,
97+
array('UserID' => $userID),
98+
array('CommentID' => $commentID)
99+
);
100+
101+
// Save the JSON to the flag.Data column.
102+
//
103+
// json_encode ensures that this is safe. If we use the safe wrapper,
104+
// HTML encoding the quotation marks will make it invalid JSON.
105+
$db->unsafeUpdate(
106+
"flag",
107+
array("Data" => json_encode($newData)),
108+
array('CommentID' => $commentID)
109+
);
110+
echo "\n\nDone. $userID saved as UserID in $table and flag tables for CommentID $commentID.\n";
111+
}
112+
}
113+
114+
if (!$confirm) {
115+
echo "\n\nRun this tool again with the argument 'confirm' to ".
116+
"perform the changes.\n";
117+
}

0 commit comments

Comments
 (0)