Beispiel #1
0
 /**
  * Get predb rows by limit and filter.
  */
 public function getPreRange($start = 0, $num, $dirname = '', $category = '')
 {
     // Only use Sphinx if this is a search request
     if ($dirname) {
         if ($this->pdo->getSetting('sphinxenabled') && $this->pdo->getSetting('sphinxindexpredb')) {
             // Search using Sphinx
             $sphinx = new Sphinx();
             $results = $sphinx->getPreRange($start, $num, $dirname, $category);
             if (is_array($results)) {
                 return $results;
             }
         }
     }
     $dirname = str_replace(' ', '%', $dirname);
     $dirname = empty($dirname) ? '' : sprintf('WHERE dirname LIKE %s', $this->pdo->escapeString('%' . $dirname . '%'));
     $category = empty($category) ? '' : sprintf((empty($dirname) ? 'WHERE' : ' AND') . " category = %s", $this->pdo->escapeString($category));
     $sql = sprintf('SELECT p.*, r.guid FROM predb p left outer join releases r on p.id = r.preid %s %s ORDER BY ctime DESC LIMIT %d,%d', $dirname, $category, $start, $num);
     return $this->pdo->query($sql, true);
 }
Beispiel #2
0
 public function __construct()
 {
     if (!class_exists('SphinxClient')) {
         Error::showError("SphinxClient Not Exists", 3001);
     }
     self::$sphinx = new SphinxClient();
     self::$sphinx->SetServer(Config::$sphinx['host'], Config::$sphinx['port']);
     if (!self::$sphinx->open()) {
         Error::showError('Connect to sphinx server failed.', 3002);
     }
     self::$sphinx->SetRankingMode(SPH_RANK_BM25);
     self::$sphinx->SetArrayResult(true);
 }
Beispiel #3
0
 /**
  * Search for releases by author/bookinfo. Used by API.
  */
 public function searchBook($author, $title, $offset = 0, $limit = 100, $maxage = -1)
 {
     $s = new Settings();
     if ($s->getSetting('sphinxenabled')) {
         $sphinx = new Sphinx();
         $results = $sphinx->searchBook($author, $title, $offset, $limit, $maxage, array(), true);
         if (is_array($results)) {
             return $results;
         }
     }
     $searchsql = "";
     if ($author != "") {
         $searchsql .= sprintf(" and bookinfo.author like %s ", $this->pdo->escapeString("%" . $author . "%"));
     }
     if ($title != "") {
         $searchsql .= sprintf(" and bookinfo.title like %s ", $this->pdo->escapeString("%" . $title . "%"));
     }
     if ($maxage > 0) {
         $maxage = sprintf(" and postdate > now() - interval %d day ", $maxage);
     } else {
         $maxage = "";
     }
     $sql = sprintf("SELECT releases.*, bookinfo.cover AS bi_cover, bookinfo.review AS bi_review, bookinfo.publisher AS bi_publisher, bookinfo.pages AS bi_pages, bookinfo.publishdate AS bi_publishdate, bookinfo.title AS bi_title, bookinfo.author AS bi_author, genres.title AS book_genrename, concat(cp.title, ' > ', c.title) AS category_name, concat(cp.id, ',', c.id) AS category_ids, groups.name AS group_name, rn.id AS nfoid FROM releases LEFT OUTER JOIN bookinfo ON bookinfo.id = releases.bookinfoid LEFT JOIN genres ON genres.id = bookinfo.genreID LEFT OUTER JOIN groups ON groups.id = releases.groupid LEFT OUTER JOIN category c ON c.id = releases.categoryid LEFT OUTER JOIN releasenfo rn ON rn.releaseid = releases.id AND rn.nfo IS NOT NULL LEFT OUTER JOIN category cp ON cp.id = c.parentid WHERE releases.passwordstatus <= (SELECT value FROM settings WHERE setting='showpasswordedrelease') %s %s ORDER BY postdate DESC LIMIT %d, %d ", $searchsql, $maxage, $offset, $limit);
     $orderpos = strpos($sql, "order by");
     $wherepos = strpos($sql, "where");
     $sqlcount = "SELECT count(releases.id) AS num FROM releases INNER JOIN bookinfo ON bookinfo.id = releases.bookinfoid " . substr($sql, $wherepos, $orderpos - $wherepos);
     $countres = $this->pdo->queryOneRow($sqlcount, true);
     $res = $this->pdo->query($sql, true);
     if (count($res) > 0) {
         $res[0]["_totalrows"] = $countres["num"];
     }
     return $res;
 }
Beispiel #4
0
    public function action_test($page = 1)
    {
        $page = $page <= 0 ? 1 : $page;
        $search = Sphinx::factory(Sprig::factory('actor'));
        $search->limit = 10;
        $search->offset = $search->limit * ($page - 1);
        $search->order_by('sort_fname');
        $result = $search->results;
        ?>
        <h2>Sorted by first name</h2>
        <h3><?php 
        echo $search->offset + 1, ' - ', $search->offset + 10;
        ?>
 results. Of <?php 
        echo $search->total;
        ?>
</h3>
        <a href="<?php 
        echo URL::site('sphinxexample/test/' . ($page - 1));
        ?>
">Prev</a> | <a href="<?php 
        echo URL::site('sphinxexample/test/' . ($page + 1));
        ?>
">Next</a>
        <br/>
        <table width="500"> 
        <tr>
            <th>docid</th>
            <th>Films</th>
            <th>First Name</th>
            <th>Last Name</th>
        </tr>
        <?php 
        foreach ($search as $i => $actor) {
            ?>
        <tr>
            <td><?php 
            echo $actor->actor_id;
            ?>
</td>
            <td>
            <?php 
            echo $search->attr($actor->actor_id, 'films');
            ?>
            </td>
            <td>
            <?php 
            echo $actor->first_name;
            ?>
            </td>
            <td>
            <?php 
            echo $actor->last_name;
            ?>
            </td>
        </tr>
        <?php 
        }
        ?>
        </table>
        <?php 
        if (!$result) {
            echo Kohana::debug($search->last_error);
        } else {
            echo Kohana::debug($result);
        }
    }
 /**
  * Search for releases by imdbid/movieinfo. Used by API/Couchpotato.
  */
 public function searchbyImdbId($imdbId, $offset = 0, $limit = 100, $name = "", $cat = array(-1), $genre = "", $maxage = -1)
 {
     $s = new Sites();
     $site = $s->get();
     if ($site->sphinxenabled) {
         $sphinx = new Sphinx();
         $results = $sphinx->searchbyImdbId($imdbId, $offset, $limit, $name, $cat, $genre, $maxage, array(), true);
         if (is_array($results)) {
             return $results;
         }
     }
     $db = new DB();
     if ($imdbId != "-1" && is_numeric($imdbId)) {
         //pad id with zeros just in case
         $imdbId = str_pad($imdbId, 7, "0", STR_PAD_LEFT);
         $imdbId = sprintf(" and releases.imdbID = %d ", $imdbId);
     } else {
         $imdbId = "";
     }
     //
     // if the query starts with a ^ it indicates the search is looking for items which start with the term
     // still do the fulltext match, but mandate that all items returned must start with the provided word
     //
     $words = explode(" ", $name);
     $searchsql = "";
     $intwordcount = 0;
     if (count($words) > 0) {
         foreach ($words as $word) {
             if ($word != "") {
                 //
                 // see if the first word had a caret, which indicates search must start with term
                 //
                 if ($intwordcount == 0 && strpos($word, "^") === 0) {
                     $searchsql .= sprintf(" and releases.searchname like %s", $db->escapeString(substr($word, 1) . "%"));
                 } elseif (substr($word, 0, 2) == '--') {
                     $searchsql .= sprintf(" and releases.searchname not like %s", $db->escapeString("%" . substr($word, 2) . "%"));
                 } else {
                     $searchsql .= sprintf(" and releases.searchname like %s", $db->escapeString("%" . $word . "%"));
                 }
                 $intwordcount++;
             }
         }
     }
     $catsrch = "";
     if (count($cat) > 0 && $cat[0] != -1) {
         $catsrch = " and (";
         foreach ($cat as $category) {
             if ($category != -1) {
                 $categ = new Category();
                 if ($categ->isParent($category)) {
                     $children = $categ->getChildren($category);
                     $chlist = "-99";
                     foreach ($children as $child) {
                         $chlist .= ", " . $child["ID"];
                     }
                     if ($chlist != "-99") {
                         $catsrch .= " releases.categoryID in (" . $chlist . ") or ";
                     }
                 } else {
                     $catsrch .= sprintf(" releases.categoryID = %d or ", $category);
                 }
             }
         }
         $catsrch .= "1=2 )";
     }
     if ($maxage > 0) {
         $maxage = sprintf(" and releases.postdate > now() - interval %d day ", $maxage);
     } else {
         $maxage = "";
     }
     if ($genre != "") {
         $genre = sprintf(" and movieinfo.genre like %s", $db->escapeString("%" . $genre . "%"));
     }
     $sql = sprintf("select releases.*, movieinfo.title as moi_title, movieinfo.tagline as moi_tagline, movieinfo.rating as moi_rating, movieinfo.plot as moi_plot, movieinfo.year as moi_year, movieinfo.genre as moi_genre, movieinfo.director as moi_director, movieinfo.actors as moi_actors, movieinfo.cover as moi_cover, movieinfo.backdrop as moi_backdrop, concat(cp.title, ' > ', c.title) as category_name, concat(cp.ID, ',', c.ID) as category_ids, groups.name as group_name, rn.ID as nfoID from releases left outer join groups on groups.ID = releases.groupID left outer join category c on c.ID = releases.categoryID left outer join releasenfo rn on rn.releaseID = releases.ID and rn.nfo is not null left outer join category cp on cp.ID = c.parentID left outer join movieinfo on releases.imdbID = movieinfo.imdbID where releases.passwordstatus <= (select value from site where setting='showpasswordedrelease') %s %s %s %s %s order by postdate desc limit %d, %d ", $searchsql, $imdbId, $catsrch, $maxage, $genre, $offset, $limit);
     $orderpos = strpos($sql, "order by");
     $wherepos = strpos($sql, "where");
     $sqlcount = "select count(releases.ID) as num from releases left outer join movieinfo on releases.imdbID = movieinfo.imdbID " . substr($sql, $wherepos, $orderpos - $wherepos);
     $countres = $db->queryOneRow($sqlcount, true);
     $res = $db->query($sql, true);
     if (count($res) > 0) {
         $res[0]["_totalrows"] = $countres["num"];
     }
     return $res;
 }
Beispiel #6
0
 public static function index($index = null, $push = FALSE)
 {
     $bin = Kohana::config('sphinx.default.bin');
     $config = Kohana::config('sphinx.default.conf');
     $index = is_null($index) ? '--all' : $index;
     return Sphinx::run($bin . '/indexer ' . $index . ' --config ' . $config . ' --rotate', $push);
 }
Beispiel #7
0
function search($qry, $ds, $num_of_records = 10)
{
    //$qry = $_POST['queryString'];
    //$ds = $_POST['datasources'];
    $nf = new Sphinx();
    $sphinxPort = $nf->getPort();
    $nf->initiateFiles();
    foreach ($ds as $datasource) {
        switch ($datasource) {
            case "mospace":
                $nf->putMOSpace();
                break;
            case "ieee":
                $nf->putIEEE();
                break;
            case "pubmed":
                $nf->putPubmed();
                break;
            case "googlescholar":
                $nf->putGoogleScholar();
                break;
            case "news":
                $nf->putNews();
                break;
            case "events":
                $nf->putEvents();
                break;
            default:
                break;
        }
    }
    $nf->putIndexer();
    $nf->putSearchd();
    $nf->startSearchd();
    $post_result_data = array();
    foreach ($ds as $datasource) {
        switch ($datasource) {
            case "mospace":
                $post_result_data = array_merge($post_result_data, array("mospace" => MOSpaceHandler($qry, $sphinxPort)));
                break;
            case "pubmed":
                $post_result_data = array_merge($post_result_data, array("pubmed" => pubmedHandler($qry, $sphinxPort, $num_of_records)));
                break;
            case "ieee":
                $post_result_data = array_merge($post_result_data, array("ieee" => IEEEHandler($qry, $sphinxPort, $num_of_records)));
                break;
            case "events":
                $post_result_data = array_merge($post_result_data, array("events" => EventsHandler($qry, $sphinxPort)));
                break;
            case "news":
                $post_result_data = array_merge($post_result_data, array("news" => NewsHandler($qry, $sphinxPort)));
                break;
            default:
                break;
        }
    }
    $post_result_data = array_merge($post_result_data, array("contacts" => ContactHandler($qry, $sphinxPort)));
    //echo "<pre>";
    //print_r(json_encode($post_result_data));
    //echo "</pre>";
    $nf->stopSearchd();
    $nf->deleteSphinxFiles();
    $nf->releasePort();
    unset($nf);
    return $post_result_data;
}
function merge()
{
    global $argv, $title;
    printf($title);
    $sphinx = new Sphinx();
    if ($argv[2] == "all") {
        $indexes = $sphinx->getAllEnabledIndexes();
    } else {
        $indexes = array_slice($argv, 2);
    }
    printf("Going to merge %d %s\n\n", count($indexes), count($indexes) == 1 ? "index" : "indexes");
    foreach ($indexes as $index) {
        printf("Merging index '%s'\n", $index);
        $sphinx->mergeIndex($index);
        printf("Completed merging index '%s'\n\n", $index);
    }
    return true;
}
<?php

require "config.php";
require_once WWW_DIR . "/lib/releases.php";
require_once WWW_DIR . "/lib/sphinx.php";
$releases = new Releases();
$sphinx = new Sphinx();
$releases->processReleases();
$sphinx->update();