/** * Search for releases by rage id. Used by API/Sickbeard. */ public function searchbyTVId($rageId, $tvdbId, $mazeId, $series = "", $episode = "", $offset = 0, $limit = 100, $name = "", $cat = array(-1), $maxage = -1) { $s = new Sites(); $site = $s->get(); // Sphinx appears much slower than searching mysql directly when you have a rage ID already if ($site->sphinxenabled && ($rageId == "-1" && $tvdbId == "-1" && $mazeId == "-1")) { $sphinx = new Sphinx(); $results = $sphinx->searchbyTVId($rageId, $tvdbId, $mazeId, $series, $episode, $offset, $limit, $name, $cat, $maxage, array(), true); if (is_array($results)) { return $results; } } $db = new DB(); $tvinfojoin = " left outer join "; $idsql = ""; if ($rageId != "-1" || $tvdbId != "-1" || $mazeId != "-1") { $idsql = " and ( "; if ($rageId != "-1") { $idsql .= sprintf(" rageID = %d or ", $rageId); } if ($tvdbId != "-1") { $idsql .= sprintf(" tvdbID = %d or ", $tvdbId); } if ($mazeId != "-1") { $idsql .= sprintf(" mazeId = %d or ", $mazeId); } $idsql .= " 1=2 ) "; $tvinfojoin = " inner join "; } if ($series != "") { // // Exclude four digit series, which will be the year 2010 etc // if (is_numeric($series) && strlen($series) != 4) { $series = sprintf('S%02d', $series); } $series = sprintf(" and releases.season = %s", $db->escapeString($series)); } if ($episode != "") { if (is_numeric($episode)) { $episode = sprintf('E%02d', $episode); } $episode = sprintf(" and releases.episode like %s", $db->escapeString('%' . $episode . '%')); } // // 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 postdate > now() - interval %d day ", $maxage); } else { $maxage = ""; } $sql = sprintf("select releases.*, tvinfo.rageID, tvinfo.tvdbID, tvinfo.mazeID, tvinfo.imdbID as tvimdbID, 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 from releases %s tvinfo on tvinfo.ID = releases.tvinfoID left outer join category c on c.ID = releases.categoryID left outer join groups on groups.ID = releases.groupID left outer join releasevideo re on re.releaseID = releases.ID 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 site where setting='showpasswordedrelease') %s %s %s %s %s %s order by postdate desc limit %d, %d ", $tvinfojoin, $idsql, $series, $episode, $searchsql, $catsrch, $maxage, $offset, $limit); $orderpos = strpos($sql, "order by"); $wherepos = strpos($sql, "where"); $sqlcount = "select count(releases.ID) as num from releases " . $tvinfojoin . " tvinfo on tvinfo.ID = releases.tvinfoID " . 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; }