49 $sql =
"SELECT count(*) AS cnt FROM ";
50 $statement = __METHOD__ .
".getCountsFor";
53 $row = $dbManager->getSingleRow($sql . $table, array(), $statement . $table);
54 $count += intval($row[
'cnt']);
72 $values = array_map(
function($id,$content)
74 return "($id,'" . pg_escape_string($content) .
"')";
75 }, array_keys($rows), $rows);
77 $sql =
"UPDATE $table AS cp " .
78 "SET content = up.content, hash = md5(up.content) " .
79 "FROM (VALUES" . join(
",", $values) .
80 ") AS up(id,content) " .
81 "WHERE up.id = cp." . $table .
"_pk";
83 $statement = __METHOD__ .
".updateContentFor.$table";
86 foreach ($rows as $id => $content) {
87 $dbManager->queryOnce($sql, $statement);
102 $cmd =
"$MODDIR/copyright/agent/fo_unicode_clean";
103 $descriptorspec = array(
104 0 => array(
"pipe",
"r"),
105 1 => array(
"pipe",
"w")
109 foreach ($rows as $key => $content) {
110 if (empty($content)) {
113 $input .=
"$key,|>$content<|\n";
117 $escaper = proc_open($cmd, $descriptorspec, $pipes);
119 if (is_resource($escaper)) {
120 fwrite($pipes[0], $input);
124 $output =
trim(stream_get_contents($pipes[1]));
127 $returnVal = proc_close($escaper);
128 assert($returnVal == 0,
"Encoder tool failed. Returned $returnVal.");
130 throw new \UnexpectedValueException(
"Unable to fork encoder tool ($cmd)");
133 $output = explode(
"<|\n", $output);
136 $lastIndex = count($output) - 1;
137 $output[$lastIndex] = str_replace(
"<|",
"", $output[$lastIndex]);
139 foreach ($output as $row) {
143 $line = explode(
",|>", $row);
145 $recodedContent = $line[1];
146 if ($rows[$id] == $recodedContent) {
149 $rows[$id] = $recodedContent;
160 if ($dbManager == NULL) {
161 echo
"No connection object passed!\n";
165 $sql =
"SHOW client_encoding;";
166 $statement = __METHOD__ .
".getOldClientEncoding";
167 $oldEnc = $dbManager->getSingleRow($sql, array(), $statement);
168 $oldEnc = $oldEnc[
'client_encoding'];
170 $sql =
"SET client_encoding = 'SQL_ASCII';";
171 $dbManager->queryOnce($sql);
174 $countSql =
"SELECT count(*) AS cnt FROM $table;";
175 $countStatement = __METHOD__ .
".getCountFor.$table";
176 $contentSql =
"SELECT " . $table .
"_pk AS id, content " .
178 "ORDER BY " . $table .
"_pk " .
179 "LIMIT $1 OFFSET $2;";
180 $contentStatement = __METHOD__ .
".getContentFor.$table";
182 $length = $dbManager->getSingleRow($countSql, array(), $countStatement);
183 $length = $length[
'cnt'];
184 echo
"*** Recoding $length records from $table table ***\n";
187 while ($i < $length) {
188 $rows = $dbManager->getRows($contentSql, array(
MAX_ROW_SIZE, $i),
192 foreach ($rows as $row) {
193 $data[$row[
'id']] = $row[
'content'];
197 if ($i % 10000 == 0) {
198 echo
" Processed $i rows for $table table\n";
201 echo
"*** Recoded $updatedCount for $table table ***\n";
204 $sql =
"SET client_encoding = '$oldEnc';";
205 $dbManager->queryOnce($sql);
215 $dbName = $GLOBALS[
"SysConf"][
"DBCONF"][
"dbname"];
216 $sql =
"SELECT pg_encoding_to_char(encoding) AS encoding " .
217 "FROM pg_database WHERE datname = '$dbName';";
218 $row = $dbManager->getSingleRow($sql);
219 return $row[
"encoding"];
228 if (strcasecmp(
getDbEncoding($dbManager),
'sql_ascii') == 0) {
229 $dbName = $GLOBALS[
"SysConf"][
"DBCONF"][
"dbname"];
230 $sql =
"UPDATE pg_database SET encoding = pg_char_to_encoding('UTF8') " .
231 "WHERE datname = '$dbName';";
232 $cmd =
'su postgres --command "psql --command=\"BEGIN;' . $sql .
245 if ($dbManager == NULL){
246 echo
"No connection object passed!\n";
253 $migRequired =
false;
258 $migRequired =
false;
259 if (strcasecmp(
getDbEncoding($dbManager),
'sql_ascii') == 0) {
284 if ($totalRecords == 0) {
288 $envYes = getenv(
'FOSSENCODING');
290 $force = !empty($envYes);
293 echo
"*** Recoding $totalRecords records to UTF-8. ***\n";
295 if (!$force && $totalRecords > 500000) {
296 $REDCOLOR =
"\033[0;31m";
297 $NOCOLOR =
"\033[0m";
298 echo
"\n*********************************************************" .
299 "***********************\n";
300 echo
"*** " . $REDCOLOR .
"Error, script will take too much time. Not " .
301 "recoding entries. " . $NOCOLOR .
" ***\n";
302 echo
"*** Either rerun the fo-postinstall with \"--force-encode\" flag " .
304 "*** \"FOSSENCODING=1\" in environment or run script at " .
306 echo
"*** \"" . dirname(__FILE__) .
307 "/dbmigrate_change_db_encoding.php\" to continue as a separate process ***\n";
308 echo
"*********************************************************" .
309 "***********************\n";
314 echo
"*** Recoding entries in copyright and sister tables ***\n";
317 }
catch (Exception $e) {
318 echo
"*** Something went wrong. Try again! ***\n";
319 $dbManager->rollback();
338 }
catch (Exception $e) {
339 echo
"Something went wrong. Try running postinstall again!\n";
340 $dbManager->rollback();
recodeTables($dbManager, $MODDIR, $force=false)
Migrate_37_38($dbManager, $MODDIR)
updateRecodedValues($dbManager, $table, $rows)
updateDbEncoding($dbManager)
checkMigrate3738Required($dbManager)
DB_TableExists($tableName)
Check if table exists.
recodeContents(&$rows, $MODDIR)
calculateNumberOfRecordsToRecode($dbManager)
startRecodingTables($dbManager, $MODDIR)
char * trim(char *ptext)
Trimming whitespace.
getDbEncoding($dbManager)