/** * Вставляет записи из массива в таблицу * @param string $tableName - имя таблицы * @param array $data - массив строк * @param string $delimeter - разделитель значений в строках * @return bool * @throws Exception */ function insertFromCSV($tableName, $data, $delimeter = '|') { $result = pg_copy_from($this->connection, $tableName, $data, $delimeter); if ($result === false) { throw new Exception(pg_last_error()); } return $result; }
protected function copyActualRows() { $ret = pg_copy_from($this->getConnection(self::CONN_NEWEVE), $this->copying_to, $this->copying_rows); $this->copying_count += count($this->copying_rows); $mem_usage = number_format(memory_get_usage() / 1024 / 1024, 4); echo "[" . $mem_usage . " MB]: Copied " . $this->copying_count . " row(s) to " . $this->copying_to . PHP_EOL; if (!$ret) { throw new Exception("pg_copy_from failed"); } $this->copying_rows = array(); }
function csv2pg($options = array()) { /* * get command line args here, replacing default values * much like pg2gviz * in other words, defaults get used unless over-ridden by command line args * the difference is they all get stuffed into an options array, makes for much cleaner code */ /* * get the debugging arg */ if (array_key_exists(DEBUGGING, $options)) { $debugging = $options[DEBUGGING]; } else { $debugging = false; } if ($debugging) { echo "debugging default: {$debugging} \n"; } $debugging_arg = getargs("debugging", $debugging); if ($debugging) { echo "debugging_arg: {$debugging_arg} \n"; } if (strlen(trim($debugging_arg))) { $debugging = strtobool($debugging_arg); $options[DEBUGGING] = $debugging; } if ($debugging) { echo "debugging final: {$debugging} \n"; } /* * get the logging arg */ if (array_key_exists(LOGGING, $options)) { $logging = $options[LOGGING]; } else { $logging = false; } if ($debugging) { echo "logging default: {$logging} \n"; } $logging_arg = getargs("logging", $logging); if ($debugging) { echo "logging_arg: {$logging_arg} \n"; } if (strlen(trim($logging_arg))) { $logging = strtobool($logging_arg); $options[LOGGING] = $logging; } if ($debugging) { echo "logging final: {$logging} \n"; } /* * get the filename arg * this is required, so bail if it is not set from either the default above or the cli arg */ if (array_key_exists(FILENAME, $options)) { $file_name = $options[FILENAME]; } else { // we can NOT set a default for this so the arg better have something! $file_name = ""; } if ($debugging) { echo "file_name default: {$file_name} \n"; } $file_name_arg = getargs("filename", $file_name); if ($debugging) { echo "file_name_arg: {$file_name_arg} \n"; } if (strlen(trim($file_name_arg))) { $file_name = trim($file_name_arg); $options[FILENAME] = $file_name; if ($debugging) { echo "file_name final: {$file_name} \n"; } } else { if (strlen(trim($file_name))) { if ($debugging) { echo "file_name final: {$file_name} \n"; } } else { // we can NOT proceed without a file name!! if ($logging) { echo "Error: csv2pg: Missing file name. \n"; } if ($debugging) { print_r($options); } return false; } } /* * get the delimiter arg */ if (array_key_exists(DELIMITER, $options)) { $delimiter = $options[DELIMITER]; } else { $delimiter = ","; } if ($debugging) { echo "delimiter default: {$delimiter} \n"; } $delimiter_arg = getargs("delimiter", $delimiter); if ($debugging) { echo "delimiter_arg: {$delimiter_arg} \n"; } $fixed_width = 0; $fixed_width_array = array(); if (strlen(trim($delimiter_arg))) { switch ($delimiter_arg) { case "tab": $delimiter = "\t"; break; case "space": $delimiter = " "; break; case "fixed_" . substr($delimiter_arg, 6): // emulate fixed_* $fixed_width_array = explode("_", substr($delimiter_arg, 6)); $fixed_width = intval($fixed_width_array[0]); if ($logging) { echo "switched to fixed width fields of size: {$fixed_width} \n"; } break; case "comma": default: $delimiter = ","; break; } $options[DELIMITER] = $delimiter; } if ($debugging) { echo "delimiter final: {$delimiter} \n"; } /* * get the method arg */ if (array_key_exists(METHOD, $options)) { $method = $options[METHOD]; } else { // we can set a default for this $method = 1; } if ($debugging) { echo "method default: {$method} \n"; } $method_arg = getargs("method", $method); if ($debugging) { echo "method_arg: {$method_arg} \n"; } if (strlen(trim($method_arg))) { $method = intval($method_arg); $options[METHOD] = $method; } if ($debugging) { echo "logging final: {$logging} \n"; } /* * get the skiplines arg */ if (array_key_exists(SKIPLINES, $options)) { $skiplines = $options[SKIPLINES]; } else { // we can set a default for this $skiplines = 0; } if ($debugging) { echo "skiplines default: {$skiplines} \n"; } $skiplines_arg = getargs("skiplines", $skiplines); if ($debugging) { echo "skiplines_arg: {$skiplines_arg} \n"; } if (strlen(trim($skiplines_arg))) { $skiplines = intval($skiplines_arg); $options[SKIPLINES] = $skiplines; } if ($debugging) { echo "skiplines final: {$skiplines} \n"; } /* * get the fieldcount arg */ if (array_key_exists(FIELDCOUNT, $options)) { $fieldcount = $options[FIELDCOUNT]; } else { // we can set a default for this, 0 means unknown $fieldcount = 0; } if ($debugging) { echo "fieldcount default: {$fieldcount} \n"; } $fieldcount_arg = getargs("fieldcount", $fieldcount); if ($debugging) { echo "fieldcount_arg: {$fieldcount_arg} \n"; } if (strlen(trim($fieldcount_arg))) { $fieldcount = intval($fieldcount_arg); $options[FIELDCOUNT] = $fieldcount; } if ($debugging) { echo "fieldcount final: {$fieldcount} \n"; } /* * get the linenumbers arg */ if (array_key_exists(LINENUMBERS, $options)) { $linenumbers = $options[LINENUMBERS]; } else { $linenumbers = false; } if ($debugging) { echo "linenumbers default: {$linenumbers} \n"; } $linenumbers_arg = getargs("linenumbers", $linenumbers); if ($debugging) { echo "linenumbers_arg: {$linenumbers_arg} \n"; } if (strlen(trim($linenumbers_arg))) { $linenumbers = strtobool($linenumbers_arg); $options[LINENUMBERS] = $linenumbers; } if ($debugging) { echo "linenumbers final: {$linenumbers} \n"; } /* * get the modflow well file arg - 0 is false, xx > is true, read the data for year xx from the file, <0 is true, read the WHOLE file */ if (array_key_exists(MODFLOWWELLFILE, $options)) { $modflowwellfile = $options[MODFLOWWELLFILE]; } else { $modflowwellfile = 0; } if ($debugging) { echo "modflowwellfile default: {$modflowwellfile} \n"; } $modflowwellfile_arg = getargs("modflowwellfile", $modflowwellfile); if ($debugging) { echo "modflowwellfile_arg: {$modflowwellfile_arg} \n"; } if (strlen(trim($modflowwellfile_arg))) { $modflowwellfile = intval($modflowwellfile_arg); $options[MODFLOWWELLFILE] = $modflowwellfile; } if ($debugging) { echo "modflowwellfile final: {$modflowwellfile} \n"; } /* * get the pguser arg */ if (array_key_exists(PGUSER, $options)) { $pguser = $options[PGUSER]; } else { $pguser = ""; } if ($debugging) { echo "pguser default: {$pguser} \n"; } $pguser_arg = getargs("pguser", $pguser); if ($debugging) { echo "pguser_arg: {$pguser_arg} \n"; } if (strlen(trim($pguser_arg))) { $pguser = trim($pguser_arg); $options[PGUSER] = $pguser; if ($debugging) { echo "pguser final: {$pguser} \n"; } } else { if ($debugging) { echo "pguser final: {$pguser} \n"; } } /* * get the pgpassword arg */ if (array_key_exists(PGPASSWORD, $options)) { $pgpassword = $options[PGPASSWORD]; } else { $pgpassword = ""; } if ($debugging) { echo "pgpassword default: {$pgpassword} \n"; } $pgpassword_arg = getargs("pgpassword", $pgpassword); if ($debugging) { echo "pgpassword_arg: {$pgpassword_arg} \n"; } if (strlen(trim($pgpassword_arg))) { $pgpassword = trim($pgpassword_arg); $options[PGPASSWORD] = $pgpassword; if ($debugging) { echo "pgpassword final: {$pgpassword} \n"; } } else { if ($debugging) { echo "pgpassword final: {$pgpassword} \n"; } } /* * get the pgtable arg * this is required, so bail if it is not set from either the default above or the cli arg */ if (array_key_exists(PGTABLE, $options)) { $pgtable = $options[PGTABLE]; } else { // we can NOT set a default for this so the arg better have something! $pgtable = ""; } if ($debugging) { echo "pgtable default: {$pgtable} \n"; } $pgtable_arg = getargs("pgtable", $pgtable); if ($debugging) { echo "pgtable_arg: {$pgtable_arg} \n"; } if (strlen(trim($pgtable_arg))) { $pgtable = trim($pgtable_arg); $options[PGTABLE] = $pgtable; if ($debugging) { echo "pgtable final: {$pgtable} \n"; } } else { if (strlen(trim($pgtable))) { if ($debugging) { echo "pgtable final: {$pgtable} \n"; } } else { // we can NOT proceed without a pgtable!! if ($logging) { echo "Error: csv2pg: Missing pgtable. \n"; } if ($debugging) { print_r($options); } return false; } } /* * get the pgdb arg * this is required, so bail if it is not set from either the default above or the cli arg */ if (array_key_exists(PGDB, $options)) { $pgdb = $options[PGDB]; } else { // we can NOT set a default for this so the arg better have something! $pgdb = ""; } if ($debugging) { echo "pgdb default: {$pgdb} \n"; } $pgdb_arg = getargs("pgdb", $pgdb); if ($debugging) { echo "pgdb_arg: {$pgdb_arg} \n"; } if (strlen(trim($pgdb_arg))) { $pgdb = trim($pgdb_arg); $options[PGDB] = $pgdb; if ($debugging) { echo "pgdb final: {$pgdb} \n"; } } else { if (strlen(trim($pgdb))) { if ($debugging) { echo "pgdb final: {$pgdb} \n"; } } else { // we can NOT proceed without a pgdb!! if ($logging) { echo "Error: csv2pg: Missing pgdb. \n"; } if ($debugging) { print_r($options); } return false; } } /* * get the pghost arg * this is required, so bail if it is not set from either the default above or the cli arg */ if (array_key_exists(PGHOST, $options)) { $pghost = $options[PGHOST]; } else { // we can set a default for this $pghost = "localhost"; } if ($debugging) { echo "pghost default: {$pghost} \n"; } $pghost_arg = getargs("pghost", $pghost); if ($debugging) { echo "pghost_arg: {$pghost_arg} \n"; } if (strlen(trim($pghost_arg))) { $pghost = trim($pghost_arg); $options[PGHOST] = $pghost; if ($debugging) { echo "pghost final: {$pghost} \n"; } } else { if ($debugging) { echo "pghost final: {$pghost} \n"; } } /* * get the pgport arg * this is required, so bail if it is not set from either the default above or the cli arg */ if (array_key_exists(PGPORT, $options)) { $pgport = $options[PGPORT]; } else { // we can set a default for this $pgport = 5432; } if ($debugging) { echo "pgport default: {$pgport} \n"; } $pgport_arg = getargs("pgport", $pgport); if ($debugging) { echo "pgport_arg: {$pgport_arg} \n"; } if (strlen(trim($pgport_arg))) { $pgport = intval($pgport_arg); $options[PGPORT] = $pgport; if ($debugging) { echo "pgport final: {$pgport} \n"; } } else { if ($debugging) { echo "pgport final: {$pgport} \n"; } } /* * now start the file processing * convert file into array of file records */ if ($file_records = file($file_name, FILE_IGNORE_NEW_LINES)) { if ($debugging) { print_r($file_records); } /* * convert array of file records into an array of file field arrays */ if ($fixed_width) { // read the records with fixed width fields of size $fixed_width $file_fields = fixedwidth2array($file_records, $fixed_width_array, $options); } else { //else assume it is a delimited record if ($modflowwellfile) { //hopefully the user set things up for the right file format - such as free format and space delimited which is my case! $file_fields = mfwf2array($file_records, $modflowwellfile, $options); } else { $file_fields = csv2array($file_records, $options); } } if ($file_fields) { if ($debugging) { print_r($file_fields); } /* * append field values to pg table */ $pgconnectionstring = "dbname={$pgdb} host={$pghost} port={$pgport}"; if (strlen($pguser)) { $pgconnectionstring .= " user={$pguser}"; } if (strlen($pgpassword)) { $pgconnectionstring .= " password={$pgpassword}"; } $pgconnection = pg_connect($pgconnectionstring); if (!$pgconnection) { if ($logging) { echo "Error: could not make database connection: " . pg_last_error($pgconnection); } return false; } $results = pg_query($pgconnection, "SELECT * FROM {$pgtable} LIMIT 1"); $pgtable_fieldcount = pg_num_fields($results); if (!$pgtable_fieldcount) { if ($logging) { echo "Error: could not get target table {$pgtable} field count \n"; } return false; } $file_recordcount = count($file_records); $parsed_recordcount = count($file_fields); if ($logging) { echo "\$pgtable_fieldcount: {$pgtable_fieldcount}\n"; } if ($logging) { echo "\$fieldcount (expected): {$fieldcount}\n"; } if ($fieldcount < 0) { if ($logging) { print "Warning: field count is <0 ({$fieldcount}), therefore saving each field to a relational record in table {$pgtable} \n"; } } if ($logging) { echo "\$file_recordcount: {$file_recordcount}\n"; } if ($logging) { echo "\$parsed_recordcount: {$parsed_recordcount}\n"; } if ($linenumbers) { if ($logging) { echo "Warning: using first field in table {$pgtable} as a line number field \n"; } } $arraytocopy = array(); //loop over the file records //for($recordnumber=0;$recordnumber<$file_recordcount;$recordnumber++) { //loop over the parsed file records for ($recordnumber = 0; $recordnumber < $parsed_recordcount; $recordnumber++) { if ($recordnumber >= $skiplines) { $file_fieldcount = count($file_fields[$recordnumber]); if ($debugging) { echo "\$recordnumber: " . ($recordnumber + 1) . " \$file_fieldcount: {$file_fieldcount}\n"; } /* * if $fieldcount >=0 then copy the record fields to database table fields, 1 to 1 * but if $fieldcount <0 then it means to create relational records in the target table for every field. like: row#, column#, value */ if ($fieldcount >= 0) { if ($fieldcount && $file_fieldcount != $fieldcount) { if ($logging) { echo "Warning: record " . ($recordnumber + 1) . " in file {$file_name} has {$file_fieldcount} fields, expected {$fieldcount} \n"; } } if ($file_fieldcount > $pgtable_fieldcount) { if ($logging) { echo "Warning: record " . ($recordnumber + 1) . " in file {$file_name} has {$file_fieldcount} fields, pg table {$pgtable} only has {$pgtable_fieldcount} \n"; } } /* * create the tab delimited string to use for the "row" */ $row = ""; $fieldcounttocopy = $pgtable_fieldcount; $fieldcountcopied = 0; /* * use the first field in the output table for file line numbers */ if ($linenumbers) { $row .= $recordnumber + 1; $fieldcounttocopy = $pgtable_fieldcount - 1; $fieldcountcopied = 1; } /* * now fill out the tab delimited string to paste in each row */ for ($fieldnumber = 0; $fieldnumber < $fieldcounttocopy; $fieldnumber++) { if ($fieldcountcopied) { $row .= "\t"; } if ($fieldnumber < $file_fieldcount) { $row .= $file_fields[$recordnumber][$fieldnumber]; } else { $row .= "\\NULL"; } $fieldcountcopied++; } $row .= "\n"; $arraytocopy[] = $row; } else { /* * make sure the target table is the right size, should be three fields */ if ($pgtable_fieldcount != 3) { if ($logging) { echo "Error, relational table must have three fields. The pg table {$pgtable} has {$pgtable_fieldcount} \n"; } return false; } /* * create the tab delimited string to use for the "row" */ $fieldcounttocopy = $file_fieldcount; $fieldcountcopied = 0; /* * loop over all the fields, creating a record out of each */ for ($fieldnumber = 0; $fieldnumber < $fieldcounttocopy; $fieldnumber++) { /* * now fill out the tab delimited string to paste in each row */ $zero_base = false; if ($zero_base) { $r = $recordnumber - $skiplines; $c = $fieldnumber; } else { $r = $recordnumber - $skiplines + 1; $c = $fieldnumber + 1; } $val = $file_fields[$recordnumber][$fieldnumber]; $row = ""; $row .= $r; $row .= "\t"; $row .= $c; $row .= "\t"; $row .= $val; $row .= "\n"; $arraytocopy[] = $row; $fieldcountcopied++; } } } else { if ($logging) { print "Warning: skipping line " . ($recordnumber + 1) . " of file {$file_name} \n"; } } } return pg_copy_from($pgconnection, $pgtable, $arraytocopy, "\t", "\\NULL"); } else { if ($logging) { echo "Error: csv2pg: could not convert file record array into file field arrays\n"; } return false; } } else { if ($logging) { echo "Error: csv2pg: could not read from file: {$file_name} \n"; } return false; } }
public static function execute($schema) { foreach (self::$instructions as $instr) { $iType = array_shift($instr); if ($iType == 'CADM') { self::setConnection(self::$aConnection); } elseif ($iType == 'CUSR') { self::setConnection(self::$aConnection); } elseif ($iType == 'QUERY') { if (!pg_query(self::$cConnection, $instr[0])) { $argh = "Could not execute query {$instr[0]}"; pg_query(self::$aConnection, "DROP SCHEMA \"{$schema}\" CASCADE"); include 'cg_argh.inc'; exit(1); } } elseif ($iType == 'COPY') { if (!pg_copy_from(self::$cConnection, $instr[0], $instr[1])) { $argh = "Copy failed for table {$instr[0]}"; pg_query(self::$aConnection, "DROP SCHEMA \"{$schema}\" CASCADE"); include 'cg_argh.inc'; exit(1); } } } }
/** * PGSQLでcopyを実行 * @return ary */ function execCopy($table, $data) { $conDB = pg_connect(DB_PASS); pg_query($conDB, 'SET standard_conforming_strings = FALSE'); pg_copy_from($conDB, $table, $data); echo "copyExec"; }
/** * Internal method that actually adds words for this document to the * database. * @param string $title Title of document * @param string $content XHTML content of document * @throws dml_exception If failure */ private function internal_add_words($title, $content) { global $DB; // Build up set of words with counts. $wordset = array(); self::internal_add_to_wordset($wordset, $title, true); self::internal_add_to_wordset($wordset, self::strip_xhtml($content)); if (count($wordset) == 0) { return true; } // Cut down all words to max db length. foreach ($wordset as $word => $count) { // Check byte length just to save time. if (strlen($word) > self::MAX_WORD_LENGTH) { // Cut length of word. $short = core_text::substr($word, 0, self::MAX_WORD_LENGTH); // Combine with existing word if there are two with same prefix. if (array_key_exists($short, $wordset)) { $count += $wordset[$short]; } // Save as short word and remove long one. $wordset[$short] = $count; unset($wordset[$word]); } } // Get word IDs from database. $list = ''; $listarray = array(); foreach ($wordset as $word => $count) { $list .= ",?"; $listarray[] = $word; } $list = substr($list, 1); // Get rid of first comma. $dbwords = $DB->get_records_select('local_ousearch_words', 'word IN (' . $list . ')', $listarray, '', 'word,id'); // Add any missing words to database. This is a performance-critical // operation, so we provide an efficient way to do it coded specifically // for Postgres. Unfortunately this cannot be done with standard Moodle // Postgres driver because it does not expose the Postgres connection // ID, so you can only use this if you use an OU overridden version of // the Postgres driver. (This override is very simple. It is available // in OU public repository in lib/dml folder.) if ($fastpg = is_a($DB, 'ou_pgsql_native_moodle_database')) { // Do this in 512-word blocks (there is a limit at 1664). $sequences = array(); $pos = 0; $missingwords = array(); foreach ($wordset as $word => $count) { if (!isset($dbwords[$word])) { $missingwords[$pos] = $word; $sequenceindex = (int) ($pos / 512); if (!array_key_exists($sequenceindex, $sequences)) { $sequences[$sequenceindex] = ''; } // Note: Cannot use {} syntax here because the sequence name // is inside a string so I don't think Moodle will replace // it. $sequences[$sequenceindex] .= ',nextval(\'' . $DB->get_prefix() . 'local_ousearch_words_id_seq\') AS s' . $pos; $pos++; } } if (count($missingwords) > 0) { foreach ($sequences as $sequenceindex => $sequenceselect) { $rs = $DB->get_recordset_sql($sql = 'SELECT ' . substr($sequences[$sequenceindex], 1), array()); $fields = (array) $rs->current(); $rs->close(); $data = array(); for ($i = $sequenceindex * 512; $i < $pos && $i < ($sequenceindex + 1) * 512; $i++) { $id = $fields['s' . $i]; $data[] = $id . "\t" . $missingwords[$i]; $dbwords[$missingwords[$i]] = new stdClass(); $dbwords[$missingwords[$i]]->id = $id; } if (!pg_copy_from($DB->get_pgsql_connection_id(), $DB->get_prefix() . 'local_ousearch_words', $data)) { throw new dml_exception('fastinserterror', 'local_ousearch'); } } } } else { // This is the slow Moodle-standard way to insert words, for all // other database drivers. foreach ($wordset as $word => $count) { if (!isset($dbwords[$word])) { $newword = (object) array('word' => $word); $newword->id = $DB->insert_record('local_ousearch_words', $newword); $dbwords[$word] = $newword; } } } // Now add the records attaching the words, with scoring, to this document. if ($fastpg && count($wordset) > 0) { // Fast insert data. $data = array(); foreach ($wordset as $word => $count) { $titlecount = empty($count[true]) ? 0 : $count[true]; $bodycount = empty($count[false]) ? 0 : $count[false]; $score = ($bodycount < 15 ? $bodycount : 15) + ($titlecount < 15 ? $titlecount * 16 : 15 * 16); $data[] = $dbwords[$word]->id . "\t" . $this->id . "\t" . $score; } if (!pg_copy_from($DB->get_pgsql_connection_id(), $DB->get_prefix() . $this->get_occurrences_table(), $data)) { throw new dml_exception('fastinserterror', 'local_ousearch'); } } else { // Slow insert data for all databases. foreach ($wordset as $word => $count) { $titlecount = empty($count[true]) ? 0 : $count[true]; $bodycount = empty($count[false]) ? 0 : $count[false]; $score = ($bodycount < 15 ? $bodycount : 15) + ($titlecount < 15 ? $titlecount * 16 : 15 * 16); $DB->execute('INSERT INTO {' . $this->get_occurrences_table() . '}' . '(wordid, documentid, score) VALUES(?,?,?)', array($dbwords[$word]->id, $this->id, $score)); } } }
<?php include 'config.inc'; $db = pg_connect($conn_str); $rows = pg_copy_to($db, $table_name); pg_query($db, "DELETE FROM {$table_name}"); pg_copy_from($db, $table_name, $rows); echo "OK";
/** * Simula el COPY command del Postgresql * @author MJB * @param string $sTable Table a la que se le va a hacer el COPY * @param array $aData Array de valores. * @return boolean true or false */ function copyData($sTable, $aData) { return pg_copy_from($this->conn, $sTable, $aData); }
/** * Insert de datos desde un arreglo hacia una tabla. Requiere la extension original pgsql. * @param string $tabla Nombre de la tabla en la que se insertarán los datos * @param array $datos Los datos a insertar: cada elemento del arreglo será un registro en la tabla. * @param string $delimitador Separador de datos de cada fila. * @param string $valor_nulo Cadena que se utlilizará como valor nulo. * @return boolean Retorn TRUE en caso de éxito o FALSE en caso de error. */ function insert_masivo($tabla, $datos, $delimitador = "\t", $valor_nulo = "\\N") { $dbconn = $this->get_pg_connect_nativo(); $salida = pg_copy_from($dbconn, $tabla, $datos, $delimitador, $valor_nulo); if (!$salida) { $mensaje = pg_last_error($dbconn); pg_close($dbconn); toba::logger()->error($mensaje); throw new toba_error($mensaje); } pg_close($dbconn); return $salida; }
public function copyFrom($table, $rows) { pg_copy_from($this->conn, $table, $rows); }
/** * Internal method that actually adds words for this document to the * database. * @param string $title Title of document * @param string $content XHTML content of document * @return True if add was successful, false otherwise */ function internal_add_words($title, $content) { global $CFG; // Build up set of words with counts $wordset = array(); self::internal_add_to_wordset($wordset, $title, true); self::internal_add_to_wordset($wordset, self::strip_xhtml($content)); if (count($wordset) == 0) { return true; } // Cut down all words to max db length $tl = textlib_get_instance(); foreach ($wordset as $word => $count) { // Check byte length just to save time if (strlen($word) > OUSEARCH_DB_MAX_WORD_LENGTH) { // Cut length of word $short = $tl->substr($word, 0, OUSEARCH_DB_MAX_WORD_LENGTH); // Combine with existing word if there are two with same prefix if (array_key_exists($short, $wordset)) { $count += $wordset[$short]; } // Save as short word and remove long one $wordset[$short] = $count; unset($wordset[$word]); } } // Get word IDs from database $list = ''; foreach ($wordset as $word => $count) { $list .= ",'" . addslashes($word) . "'"; } $list = substr($list, 1); // Get rid of first comma $dbwords = get_records_select('block_ousearch_words', 'word IN (' . $list . ')', '', 'word,id'); if (!$dbwords) { $dbwords = array(); } global $db; // Add any missing words to database if ($CFG->dbtype == 'postgres7') { // Do this in 512-word blocks (there is a limit at 1664) $sequences = array(); $pos = 0; $missingwords = array(); foreach ($wordset as $word => $count) { if (!isset($dbwords[$word])) { $missingwords[$pos] = $word; $sequenceindex = (int) ($pos / 512); if (!array_key_exists($sequenceindex, $sequences)) { $sequences[$sequenceindex] = ''; } $sequences[$sequenceindex] .= ',nextval(\'' . $CFG->prefix . 'block_ousearch_words_id_seq\') AS s' . $pos; $pos++; } } if (count($missingwords) > 0) { foreach ($sequences as $sequenceindex => $sequenceselect) { $rs = get_recordset_sql($sql = 'SELECT ' . substr($sequences[$sequenceindex], 1)); if (!$rs) { print_object($sql); print_object($db->ErrorMsg()); error('Error getting word sequences'); } $data = array(); for ($i = $sequenceindex * 512; $i < $pos && $i < ($sequenceindex + 1) * 512; $i++) { $id = $rs->fields['s' . $i]; $data[] = $id . "\t" . $missingwords[$i]; $dbwords[$missingwords[$i]] = new StdClass(); $dbwords[$missingwords[$i]]->id = $id; } if (!pg_copy_from($db->_connectionID, $CFG->prefix . 'block_ousearch_words', $data)) { debugging('Failed to insert words'); } } } } else { foreach ($wordset as $word => $count) { if (!isset($dbwords[$word])) { $newword = new StdClass(); $newword->word = addslashes($word); if (!($newword->id = insert_record('block_ousearch_words', $newword))) { debugging('Failed to insert word into ousearch table'); return false; } $dbwords[$word] = $newword; } } } // Now add the records attaching the words, with scoring, to this document if ($CFG->dbtype == 'postgres7' && count($wordset) > 0) { $data = array(); foreach ($wordset as $word => $count) { $titlecount = empty($count[true]) ? 0 : $count[true]; $bodycount = empty($count[false]) ? 0 : $count[false]; $score = ($bodycount < 15 ? $bodycount : 15) + ($titlecount < 15 ? $titlecount * 16 : 15 * 16); $data[] = $dbwords[$word]->id . "\t" . $this->id . "\t" . $score; } if (!pg_copy_from($db->_connectionID, $CFG->prefix . 'block_ousearch_occurrences', $data)) { debugging('Failed to insert occurrence records'); } } else { foreach ($wordset as $word => $count) { $titlecount = empty($count[true]) ? 0 : $count[true]; $bodycount = empty($count[false]) ? 0 : $count[false]; $score = ($bodycount < 15 ? $bodycount : 15) + ($titlecount < 15 ? $titlecount * 16 : 15 * 16); if (!execute_sql($sql = 'INSERT INTO ' . $CFG->prefix . 'block_ousearch_occurrences(wordid,documentid,score) ' . 'VALUES(' . $dbwords[$word]->id . ',' . $this->id . ',' . $score . ')', false)) { debugging('Failed to insert occurrence record'); return false; } } } return true; }