2008/3/8 土曜日

Services_HyperEstraierを使ってPHPでノードから検索

このエントリをはてなブックマークに追加Services_HyperEstraier篏帥cPHPс若罎膣△里呂討淵屮奪マーク被リンク数
Filed under: linux — akky @ 20:50:24

HyperestraylerのPHPバインディングは標準では配布されてないので、移植されたものを http://page2.xrea.jp/ からダウンロード。今回使うのは、PHP5 専用のServices_HyperEstraier の方。

ファイルの構成は下の通り。ServiesはServices_HyperEstraierから取得したものです。

common.php
index.php
Services/HyperEstraier.php
Services/HyperEstraier/Error.php
Services/HyperEstraier/Condition.php
Services/HyperEstraier/ResultDocument.php
Services/HyperEstraier/Node.php
Services/HyperEstraier/Document.php
Services/HyperEstraier/Utility.php
Services/HyperEstraier/HttpResponse.php
Services/HyperEstraier/.ResultDocument.php.swp
Services/HyperEstraier/NodeResult.php
Services/HyperEstraier.php

common.php

<?php
define('SERVICES_HYPERESTRAIER_DEBUG', 1);
error_reporting(E_ALL & ~E_STRICT);

require_once 'Services/HyperEstraier/Node.php';

$uri = 'http://localhost:1978/node/main';
$user = 'admin';
$pass = 'admin';

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Cache-Control" content="no-cache, must-revalidate, no-transform" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
</head>
<body>
<?php
$phrase = isset($_GET["phrase"]) ? $_GET["phrase"] : null;
?>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="get">
検索:
<input type="text" name="phrase" value="<?php echo $phrase ?>" size="32" id="phrase" class="text" tabindex="1" accesskey="0" />
<!--
並び替え
<select name="sort">
  <option value="score">候補</option>
  <option value="updateDate">更新日付</option>
  <option value="url">URL(ディレクトリ)</option>
  <option value="size">サイズ</option>
</select>
-->
<input type="submit" value="検索"/>
</form>

<?php
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'common.php';
// create and configure the node connecton object

if  ($phrase != null ) {

  $node = new Services_HyperEstraier_Node;
  $node->setUrl($uri);

  // create a search condition object
  $cond = new Services_HyperEstraier_Condition;
  $cond->setPhrase($phrase);
  $cond->setMax(10);
  $cond->setSkip(0);

  // get the result of search
  $nres = $node->search($cond, 1);
  if ($nres) {
      if ($nres->docNum() == 0) {
          printf( "%s: not found.n", $cond->getPhrase());
      } else {
          foreach ($nres as $rdoc) {
              printf("<h2><a href='%s'>%s<a/></h2>n"
                ,$rdoc->getAttribute('@uri')
                ,$rdoc->getAttribute('@title')) ;
              printf("%s<br/>n",$rdoc->getAttribute('@uri'));
              print("<BLOCKQUOTE>");
              foreach( split("n",$rdoc->getSnippet()) as $line ){
                if ( mbereg('   ',$line) ){
                  printf("<strong>%s</strong>",mbereg_replace(' .*$','',$line));
                }else{
                  printf("%s",$line);
                }
              }
              print("</BLOCKQUOTE>n");
          }
      }
  } else {
      printf("error: %dn", $node->status);
      if (Services_HyperEstraier_Error::hasErrors()) {
          printf( print_r(Services_HyperEstraier_Error::getErrors(), true));
      }
  }
}
?>

</body>
</html>
次のページ »