function delete_failures() { $deletes = array(); foreach (array_keys($_GET) as $k) { if (preg_match("/^del_(\\d+)\$/i", $k, $m)) { $i = $m[1]; if ($_GET[$k] == 'on') { array_push($deletes, "failure_id = '{$i}'"); } } } # Compose the SQL DELETE query $query = "\n\t DELETE FROM failure " . "\n\t WHERE " . "\n\t " . join(" OR \n\t\t", $deletes) . "\n\t ;"; do_pg_query($query); }
function make_redir($params) { unset($params["make_redir"]); unset($params["do_redir"]); $qstring = arr2qstring($params); $domain = $_SERVER['SERVER_NAME']; $script = $_SERVER['SCRIPT_NAME']; $url = "http://{$domain}{$script}?{$qstring}"; # Create tiny URLs for the permalinks $query = "SELECT permalink_id FROM permalinks WHERE permalink = '{$url}'"; $id = select_scalar($query); if (is_null($id)) { $query = "SELECT nextval('permalinks_permalink_id_seq')"; $id = select_scalar($query); $insert = "INSERT INTO " . "permalinks (permalink_id, permalink) " . "VALUES ('{$id}', '{$url}');"; do_pg_query($insert); } $tinyurl = "http://{$domain}{$script}?do_redir={$id}"; # Print tiny link in a tiny window $t = 50; print "<html>\n" . display_header("Tiny link") . "<body>\n<div align=center>\n<p>The original permalink was " . strlen($url) . " chars long.\nHere's a tiny link that is only {$t} chars long:</p>\n\n<p><form name=url_form><code>\n<input type=text name=url value='{$tinyurl}' size={$t}\n onFocus=\"this.value='{$tinyurl}';\" readonly>\n</code>\n</form></p>\n\n<script language='javascript' type='text/javascript'>\ndocument.url_form.url.focus();\ndocument.url_form.url.select();\n</script>\n\n<p><form>\n<input type=button value='Close this window' onClick='javascript:window.close();'>\n</form></p>\n\n</div>\n</body>\n</html>"; exit; }
function select_insert($table, $table_id, $stmt_fields, $stmt_values, $always_new) { $num_good_fields = 0; $nl = "\n"; $nlt = "\n\t"; $rtn_insert = null; $select_stmt = "SELECT {$table_id} {$nl}" . "FROM {$table} {$nl}"; $insert_stmt = "INSERT INTO {$table} {$nlt}" . "({$table_id}"; for ($i = 0; $i < count($stmt_fields); ++$i) { $insert_stmt .= ", " . $stmt_fields[$i]; # Select Skips 'DEFAULT' values if (0 == strncmp($stmt_values[$i], "DEFAULT", strlen("DEFAULT"))) { continue; } if ($i != 0) { $select_stmt .= " AND {$nlt}"; } else { $select_stmt .= "WHERE {$nlt}"; } $select_stmt .= $stmt_fields[$i] . " = "; $select_stmt .= quote_(pg_escape_string($stmt_values[$i])); $num_good_fields++; } $select_stmt .= $nl . "ORDER BY {$table_id} ASC LIMIT 1 "; $insert_stmt .= ") VALUES " . $nlt . "("; ############### # Try out the select to see if we have to insert if (!$always_new && 0 < $num_good_fields) { debug("\n--- SELECT STMT ---\n"); debug("{$select_stmt}\n"); $idx_value = select_scalar($select_stmt); if (!is_null_($idx_value)) { return $idx_value; } } ############### # Since it does not exist, insert a new tuple $seq_name = $table . "_" . $table_id . "_seq"; $idx_value = fetch_single_nextval($seq_name); $insert_stmt .= quote_(pg_escape_string($idx_value)); for ($i = 0; $i < count($stmt_fields); ++$i) { $insert_stmt .= ", "; if (0 == strncmp($stmt_values[$i], "DEFAULT", strlen("DEFAULT"))) { $insert_stmt .= "DEFAULT"; } else { $insert_stmt .= quote_(pg_escape_string($stmt_values[$i])); } } $insert_stmt .= ")"; debug("\n--- INSERT STMT ---\n"); debug("{$insert_stmt}\n"); $rtn_insert = do_pg_query($insert_stmt, false); ############# # If the insert operation failed, then this usually means that another # thread beat us to the insert, so just select the last id. # if this select fails, then badness happened somewhere :( if (!$rtn_insert) { $idx_value = select_scalar($select_stmt); } return $idx_value; }