39 $selectSql =
"SELECT count(*) AS cnt FROM ";
40 $where =
" WHERE (content IS NOT NULL AND content != '')";
41 $statement = __METHOD__ .
".getCountsFor";
44 $sql = $selectSql . $table . $where;
45 $row = $dbManager->getSingleRow($sql, array(), $statement . $table);
46 $count += intval($row[
'cnt']);
64 $values = array_map(
function($id,$content)
66 return "($id,'" . pg_escape_string($content) .
"')";
67 }, array_keys($rows), $rows);
69 $sql =
"UPDATE $table AS cp " .
70 "SET content = up.content, hash = md5(up.content) " .
71 "FROM (VALUES" . join(
",", $values) .
72 ") AS up(id,content) " .
73 "WHERE up.id = cp." . $table .
"_pk";
75 $statement = __METHOD__ .
".updateContentFor.$table";
78 $dbManager->queryOnce($sql, $statement);
92 $cmd =
"$MODDIR/copyright/agent/fo_unicode_clean";
93 $descriptorspec = array(
94 0 => array(
"pipe",
"r"),
95 1 => array(
"pipe",
"w")
99 foreach ($rows as $key => $content) {
100 if (empty($content)) {
103 $input .=
"$key,|>$content<|\n";
107 $escaper = proc_open($cmd, $descriptorspec, $pipes);
109 if (is_resource($escaper)) {
110 fwrite($pipes[0], $input);
114 $output =
trim(stream_get_contents($pipes[1]));
117 $returnVal = proc_close($escaper);
118 assert($returnVal == 0,
"Encoder tool failed. Returned $returnVal.");
120 throw new \UnexpectedValueException(
"Unable to fork encoder tool ($cmd)");
123 $output = explode(
"<|\n", $output);
126 $lastIndex = count($output) - 1;
127 $output[$lastIndex] = str_replace(
"<|",
"", $output[$lastIndex]);
129 foreach ($output as $row) {
133 $line = explode(
",|>", $row);
135 $recodedContent = $line[1];
136 if ($rows[$id] == $recodedContent) {
139 $rows[$id] = $recodedContent;
150 if ($dbManager == NULL) {
151 echo
"No connection object passed!\n";
155 $sql =
"SHOW client_encoding;";
156 $statement = __METHOD__ .
".getOldClientEncoding";
157 $oldEnc = $dbManager->getSingleRow($sql, array(), $statement);
158 $oldEnc = $oldEnc[
'client_encoding'];
160 $sql =
"SET client_encoding = 'SQL_ASCII';";
161 $dbManager->queryOnce($sql);
162 $where =
" WHERE (content IS NOT NULL AND content != '')";
165 $countSql =
"SELECT count(*) AS cnt FROM $table $where;";
166 $countStatement = __METHOD__ .
".getCountFor.$table";
167 $contentSql =
"SELECT " . $table .
"_pk AS id, content " .
168 "FROM $table $where " .
169 "ORDER BY " . $table .
"_pk " .
170 "LIMIT $1 OFFSET $2;";
171 $contentStatement = __METHOD__ .
".getContentFor.$table";
173 $length = $dbManager->getSingleRow($countSql, array(), $countStatement);
174 $length = $length[
'cnt'];
175 echo
"*** Recoding $length records from $table table ***\n";
178 while ($i < $length) {
179 $rows = $dbManager->getRows($contentSql, array(
MAX_ROW_SIZE, $i),
183 foreach ($rows as $row) {
184 if (empty($row[
'content'])) {
187 $data[$row[
'id']] = $row[
'content'];
191 if ($i % 10000 == 0) {
192 echo
" Processed $i rows for $table table\n";
195 echo
"*** Recoded $updatedCount for $table table ***\n";
198 $sql =
"SET client_encoding = '$oldEnc';";
199 $dbManager->queryOnce($sql);
209 $dbName = $GLOBALS[
"SysConf"][
"DBCONF"][
"dbname"];
210 $sql =
"SELECT pg_encoding_to_char(encoding) AS encoding " .
211 "FROM pg_database WHERE datname = '$dbName';";
212 $row = $dbManager->getSingleRow($sql);
213 return $row[
"encoding"];
222 if (strcasecmp(
getDbEncoding($dbManager),
'sql_ascii') == 0) {
223 $dbName = $GLOBALS[
"SysConf"][
"DBCONF"][
"dbname"];
224 $sql =
"UPDATE pg_database SET encoding = pg_char_to_encoding('UTF8') " .
225 "WHERE datname = '$dbName';";
226 $cmd =
'su postgres --command "psql --command=\"BEGIN;' . $sql .
239 if ($dbManager == NULL){
240 echo
"No connection object passed!\n";
247 $migRequired =
false;
252 $migRequired =
false;
253 if (strcasecmp(
getDbEncoding($dbManager),
'sql_ascii') == 0) {
278 if ($totalRecords == 0) {
282 $envYes = getenv(
'FOSSENCODING');
284 $force = !empty($envYes);
287 echo
"*** Recoding $totalRecords records to UTF-8. ***\n";
289 if (!$force && $totalRecords > 500000) {
290 $REDCOLOR =
"\033[0;31m";
291 $NOCOLOR =
"\033[0m";
292 echo
"\n*********************************************************" .
293 "***********************\n";
294 echo
"*** " . $REDCOLOR .
"Error, script will take too much time. Not " .
295 "recoding entries. " . $NOCOLOR .
" ***\n";
296 echo
"*** Either rerun the fo-postinstall with \"--force-encode\" flag " .
298 "*** \"FOSSENCODING=1\" in environment or run script at " .
300 echo
"*** \"" . dirname(__FILE__) .
301 "/dbmigrate_change_db_encoding.php\" to continue as a separate process ***\n";
302 echo
"*********************************************************" .
303 "***********************\n";
308 echo
"*** Recoding entries in copyright and sister tables ***\n";
311 }
catch (Exception $e) {
312 echo
"*** Something went wrong. Try again! ***\n";
313 $dbManager->rollback();
332 }
catch (Exception $e) {
333 echo
"Something went wrong. Try running postinstall again!\n";
334 $dbManager->rollback();
DB_TableExists($tableName)
Check if table exists.
getDbEncoding($dbManager)
updateDbEncoding($dbManager)
recodeContents(&$rows, $MODDIR)
updateRecodedValues($dbManager, $table, $rows)
startRecodingTables($dbManager, $MODDIR)
Migrate_37_38($dbManager, $MODDIR)
checkMigrate3738Required($dbManager)
calculateNumberOfRecordsToRecode($dbManager)
recodeTables($dbManager, $MODDIR, $force=false)
char * trim(char *ptext)
Trimming whitespace.