1

I've been trying to set up a very basic search engine using the Whoosh modules in python called on from PHP. I had it working until I upgraded the modules for some additional features I needed. At which point an odd bug seemed to appear. Any print statement after the line "searcher = ixtemp.searcher()" is not being received by the PHP script.

The python search script is called from PHP and the first result is displayed with the following commands

exec("python print.py",$output,$ret_code);

echo $output[0];

The python script -

from whoosh.index import open_dir

ixtemp = open_dir("index")

searcher = ixtemp.searcher()

results = searcher.find("content", u""+"test")

for k in results:
    print k['filename']

Running the PHP now gives the following error -

Notice: Undefined offset: 0 in /opt/lampp/htdocs/new/search.php on line 17

The python script is working when I run it by itself. After a little investigation it seems that any print statements before the line "searcher = ixtemp.searcher()" can be read by the PHP, but all after are not received by the PHP script. I've also tried the popen() and proc_open functions too, but they have the same problem.

Any ideas on what the problem is or how I can work around it?

Thanks

usertest
  • 27,132
  • 30
  • 72
  • 94

3 Answers3

0

Two things. Are you sure the script is completing? It could be hanging on the searcher() call. Second, I suppose searcher() could be redirecting stdout.

My guess is that your script isn't completing, or it's timing out or something.

EDIT Looks like this code depends on the current working directory. You mention below that it runs fine in the interpreter - in the same wd as the webserver sets up?

Robert
  • 6,412
  • 3
  • 24
  • 26
  • Hello, the script takes less then a second to execute and works perfectly inside the interpreter. Also and print statements before "searcher = ixtemp.searcher()" work. – usertest Dec 14 '10 at 03:40
  • How would I find out if stdout was being redirected? And how could I stop that happedning? Thanks – usertest Dec 14 '10 at 03:42
  • Hi, yes both scripts are in the same place and data does move between them as long as its not after "searcher = ixtemp.searcher()" so it can't be a problem with the working directory. – usertest Dec 14 '10 at 03:49
0

Are you sure the script runs at all?

Check the $return_code.

Its possible it cannot locate your print.py script or perhaps even the python.exe.

James Anderson
  • 27,109
  • 7
  • 50
  • 78
  • Hi, yes it runs and as I said the print statement sends data, but only if its before "searcher = ixtemp.searcher()" – usertest Dec 14 '10 at 03:51
  • Ah! In that case its probably having trouble locating the "index" directory! – James Anderson Dec 14 '10 at 03:58
  • I don't think it could be that since the PHP file wouldn't need to know where the index directory is only the python script needs to know that and output the processed results. And the python works fine the interpreter. Besides, it wouldn't explain why no output can be sent after the above mentioned line. – usertest Dec 14 '10 at 04:04
  • What happens when you "import os print os.getcwd()" – James Anderson Dec 14 '10 at 05:22
0

I ran out ideas and decided to chmod 777 the directory...and...it worked. I'm not sure why it did since data could always be sent above the searcher() function anyway.

Perhaps while initiating the search the method was trying to modify protected index files?

But that couldn't in itself be the problem since it always worked from the interpreter.

So it must have been the PHP file. It seems to have been protection against indirectly modifying the index files.

usertest
  • 27,132
  • 30
  • 72
  • 94