Skip to content

Instantly share code, notes, and snippets.

@naoa
Created December 8, 2013 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naoa/7855617 to your computer and use it in GitHub Desktop.
Save naoa/7855617 to your computer and use it in GitHub Desktop.
This script has the feature that select full text phrase search from line text in text_file. %php wiki_fts.php <database> <text_file> <output_file>
<?php
$db = "127.0.0.1";
$db_name = $argv[1];
$table = "text";
$index = "title,text";
$username = "mysql";
$password = "";
$category_file = $argv[2];
$output_file = $argv[3];
$con = mysql_connect($db, $username, $password);
if (!$con) {
exit('Database connection error');
}
$result = mysql_select_db($db_name, $con);
if (!$result) {
exit('Database select error');
}
$fp = fopen($category_file, 'r');
$fw = fopen($output_file, 'a+');
while (!feof($fp)){
$line = fgets($fp);
$line = str_replace(array("\r\n","\r","\n"),'',$line);
$line = mysql_escape_string($line);
$query = "SELECT COUNT(*) as cnt FROM $table WHERE MATCH($index) AGAINST('\"$line\"' IN BOOLEAN MODE);";
$startTime = microtime(true);
$result = mysql_query($query, $con);
if(!$result){
echo "SELECT ERROR aborted" . "\n";
$con = mysql_close($con);
if (!$con) {
echo 'Database close error';
}
fclose($fp);
fclose($fw);
exit;
}
while ($item = mysql_fetch_array($result)){
$cnt = $item['cnt'];
}
$endTime = microtime(true);
$elapsedTime = $endTime - $startTime;
//echo $line . "," . $cnt . "," . $elapsedTime . "\n";
fwrite($fw,$line . "," . $cnt . "," . $elapsedTime . "\n");
}
$con = mysql_close($con);
if (!$con) {
exit('Database close error');
}
fclose($fp);
fclose($fw);
echo "Done.\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment