Sphinx enable STP_MATCH_EXTENDED for PDO connection - pdo

If my PDO connection is as below:
$sphinx = new PDO('mysql:dbname=db;host=127.0.0.1;port=9306;charset=utf8', 'user', 'pass');
$sphinx->exec('SET CHARACTER SET utf8');
$sphinx->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$sphinx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
And my query is:
$array = $sphinx->prepare("select * from `indexname` where MATCH #(title,body) hello world");
What is the correct way to enable SPH_MATCH_EXTENDED?
If I add:
require ("sphinxapi.php");
$sphinx = new SphinxClient();
$sphinx->SetMatchMode(SPH_MATCH_EXTENDED);
$sphinx->SetArrayResult(true);
Under the PDO connection, is this the correct way to enable stp_match_extended for PDO?

What is the correct way to enable SPH_MATCH_EXTENDED?
SphinxQL uses Extended Match Mode by default.
Don't need to do anything to use it.
You can change to use a different match mode if really want, but its NOT recommended. Internally Sphinx only (now!) implements Extended match mode, the others are all emulated.

Related

Force verbatim mode in Google Custom Search

Is it possible to force Google Custom Search to use verbatim mode be default?
For the purpose I am using it, verbatim mode will produce the most relevant results but users may not know to put their search in quotes.
I have an idea I should be able to add the quotes with Javascript before the string is submitted to Google, but I can't work out how.
Any help would be much appreciated!
Your best bet is to use a PHP Proxy or something similar to edit the query in order to add %22 which transforms the query into "query" before firing off the request.
header('Content-type: application/json');
# Setup Base URL and array for Parameters
$host = 'https://www.googleapis.com/customsearch/v1?';
$queries = array();
$queries['cx'] = "CSEKey";
$queries['key'] = "YourAPIKey";
# Setup possible incoming params
if (isset($_GET['search_term'])) $queries['q'] = "%22"+ $_GET['search_term']+ "%22";
if (isset($_GET['result_count'])) $queries['result_count'] = $_GET['result_count'];
if (isset($_GET['callback'])) $queries['callback'] = $_GET['callback'];
# Build query and Final URL
$queriesURL = http_build_query($queries);
$finalURL = $host.$queriesURL;
/*
DEBUG generated URL
echo $finalURL;
*/
$response = file_get_contents($finalURL);
echo $response;
?>
I believe that this technique forced Verbatim mode because the new JSON response lacked the usual weird suggested spelling of name:
"spelling": {
"correctedQuery": "Deo Vandski",
"htmlCorrectedQuery": "\u003cb\u003e\u003ci\u003eDeo Vandski\u003c/i\u003e\u003c/b\u003e"
},
I also saw something about appending &tbs=li:1, but I did not see any difference when I tried on my searches...

PDO update column based on its current value not working

I have this PDO statement:
parent::$db->custom('UPDATE users_credits SET availabe = availabe - :reward_credits, used = used + :reward_credits WHERE user_id = :user_id', array(
'reward_credits' => $reward_credits,
'user_id' => $user_id
));
For some reasons it simply does not work. I tried the very same query on the DB manually and it works.
What's wrong with PDO and how do I achieve the very same result I would achieve normally?
Thanks for any suggestion
First of all. There is nothing wrong with PDO and never has been.
It is some your own custom code to blame.
Simple checklist to solve any PDO related problem
Make sure you can see all the PHP errors.
Configure PDO to throw exceptions in SQL errors, by calling this after connect
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Debug your code.

PDO - connection - how to protect

$_pdo = new Data('mysql:host='.$db_host.';dbname='.$db_name.';port='.$db_port, $db_user, $db_pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES '.$charset));
Variables come from form.
User can make a sql injection if I don't strip this variables?
Connect credentials should not come from user under any circumstances.
You have to store them in your server in some config file. And then use with connect string.
Please note that charset should be set in DSN, not options.
include 'settings.php';
$dsn = "mysql:host=$db_host;dbname=$db_name;port=$db_port;charset=$charset";
$pdo = new Data($dsn, $db_user, $db_pass);
If you are accepting input from a form to create the connection I would probably use some sanitization functions to clean it up before using it. If this is being stored in a text file or a database it would be a good idea to sanitize before it is saved as well before it is used.
http://php.net/manual/en/function.filter-var.php
$db_host = filter_var($db_host,FILTER_SANITIZE_FULL_SPECIAL_CHARS);
Equivalent to calling htmlspecialchars() with ENT_QUOTES set. Encoding
quotes can be disabled by setting FILTER_FLAG_NO_ENCODE_QUOTES. Like
htmlspecialchars(), this filter is aware of the default_charset and if
a sequence of bytes is detected that makes up an invalid character in
the current character set then the entire string is rejected resulting
in a 0-length string.
http://www.php.net/manual/en/filter.filters.sanitize.php

Why are my package (our'd) variables getting cleared out between PerlChildInitHandler and PerlResponseHandler in mod_perl2?

I have mod_perl2 running on a virtual host and I'm trying to make my mysql connection persistent between requests to handle server load. I have read all of the documentation and a book on the topic and I still have no idea why this bare-bones implementation of a mod_perl2 web application replies with "It's broken!".
package Test;
use strict;
use warnings;
use Apache2::Const;
use Carp qw{croak};
use DBI;
our $mysql_handle;
sub handler {
print "Content-Type: text/plain\n\n";
print (defined $mysql_handle ? "It's defined!" : "It's broken!");
return Apache2::Const::OK;
}
sub child_init {
my ($db, $host, $port, $user, $pass)
= qw{app_db localhost 3306 app_user app_pass};
$mysql_handle
= DBI->connect("dbi:mysql:database=$db;host=$host;port=$port", $user, $pass)
or croak("Failed to establish a connection with mysqld: $DBI::errstr");
return Apache2::Const::OK;
}
1;
This is very strange and makes no sense at all to me. It's as if $mysql_handle is lexically-scoped -- when it's not! Please, can some one explain this to me?
You should look at Apache::DBI for mysql connection persistance in mod_perl. It overloads DBI's connect and disconnect which allows you to use DBI->connect(...) normally, with the added benefit of the code working in or out of a mod perl environment.
As far as the scoping issue, I'd need a little more feedback on your mp setup. I would try use vars '$mysql_handle' or even $Test::mysql_handle = DBI->connect(...) and see if you don't get the results you are looking for.

How do I set a dynamic datasource for ORM?

ORM settings in Coldfusion application.cfc run before anything else runs (onapplicationstart, etc). So how do you set a dynamic datasource (code before the ORM init) in application.cfc? we can set it after and it re-points the ORM to a dynamic datasource, but that requires that the hardcoded datasource must be valid as well. This is tenuous at best.
Here is an example:
<cfscript>
this.name = "someapp_#hash(cgi.http_host)#";
this.ormenabled = "true";
this.ormsettings = { cfclocation = "config/definitions", eventhandling = "true",datasource="STATICDATASOURCE" };
</cfscript>
If it's not specified in application.cfc scope then you get errors like "ORM is not configured for the current application."
We need to be able to get the datasource from a text file on the server.
this.datasource="YourDatasourceName";
Well, if you wanted to store a file, for this example we'll call it "datasource.xml" consisting of:
<dataSourceName>Name goes here</dataSourceName>
You can read it in with:
dataFile = fileRead("pathToFile/datasource.xml");
data = xmlParse(dataFile);
dataSourceName = data.dataSourceName.xmlText;
this.datasource=dataSourceName;
ORM datasource just uses the default datasource if not defined.
Having said that, if you want to add / remove datasource dynamically, see Administrator API at: http://help.adobe.com/en_US/ColdFusion/9.0/Admin/WSc3ff6d0ea77859461172e0811cbf364104-7fcf.html (available since CF8)
I'm not sure if you can re-set the this.ormsettings.datasource to something else at runtime (i.e. onApplicationStart()? or onServerStart()?), but many of the settings can be set again. You may want to try it out.