コード例 #1
0
 /**
  * Search for releases.
  */
 public function search($search, $cat = array(-1), $offset = 0, $limit = 1000, $orderby = '', $maxage = -1, $excludedcats = array(), $grp = array(), $minsize = -1, $maxsize = -1)
 {
     $s = new Sites();
     $site = $s->get();
     if ($site->sphinxenabled) {
         $sphinx = new Sphinx();
         $order = $this->getBrowseOrder($orderby);
         $results = $sphinx->search($search, $cat, $offset, $limit, $order, $maxage, $excludedcats, $grp, array(), true, $minsize, $maxsize);
         if (is_array($results)) {
             return $results;
         }
     }
     //
     // Search using MySQL
     //
     $db = new DB();
     $catsrch = "";
     $usecatindex = "";
     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 )";
         $usecatindex = " use index (ix_releases_categoryID) ";
     }
     $grpsql = "";
     if (count($grp) > 0) {
         $grpsql = " and (";
         foreach ($grp as $grpname) {
             $grpsql .= sprintf(" groups.name = %s or ", $db->escapeString(str_replace("a.b.", "alt.binaries.", $grpname)));
         }
         $grpsql .= "1=2 )";
     }
     //
     // 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(" ", $search);
     $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++;
             }
         }
     }
     if ($maxage > 0) {
         $maxage = sprintf(" and postdate > now() - interval %d day ", $maxage);
     } else {
         $maxage = "";
     }
     if ($minsize != -1) {
         $minsize = sprintf(" and size > %d ", $minsize);
     } else {
         $minsize = "";
     }
     if ($maxsize != -1) {
         $maxsize = sprintf(" and size < %d ", $maxsize);
     } else {
         $maxsize = "";
     }
     $exccatlist = "";
     if (count($excludedcats) > 0) {
         $exccatlist = " and releases.categoryID not in (" . implode(",", $excludedcats) . ")";
     }
     if ($orderby == "") {
         $order[0] = " postdate ";
         $order[1] = " desc ";
     } else {
         $order = $this->getBrowseOrder($orderby);
     }
     $sql = sprintf("select releases.*, 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, re.releaseID as reID, cp.ID as categoryParentID, pre.ctime, pre.nuketype, coalesce(movieinfo.ID,0) as movieinfoID from releases %s left outer join movieinfo on movieinfo.imdbID = releases.imdbID left outer join releasevideo re on re.releaseID = releases.ID left outer join releasenfo rn on rn.releaseID = releases.ID left outer join groups on groups.ID = releases.groupID left outer join category c on c.ID = releases.categoryID left outer join category cp on cp.ID = c.parentID left outer join predb pre on pre.ID = releases.preID where releases.passwordstatus <= (select value from site where setting='showpasswordedrelease') %s %s %s %s %s %s %s order by %s %s limit %d, %d ", $usecatindex, $searchsql, $catsrch, $maxage, $exccatlist, $grpsql, $minsize, $maxsize, $order[0], $order[1], $offset, $limit);
     $orderpos = strpos($sql, "order by");
     $wherepos = strpos($sql, "where");
     $sqlcount = "select count(releases.ID) as num from releases " . 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;
 }