/** * * @param string[] $parameters */ public static function GetContentList($parameters) { $logger = \Swiftriver\Core\Setup::GetLogger(); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContentList [Method invoked]", \PEAR_LOG_DEBUG); $baseSql = "from content left join " . "content_source on content.id = content_source.content_id left join " . "source on content_source.source_id = source.id"; $filters = array(); $state = key_exists("state", $parameters) ? $parameters["state"] : null; if ($state != null) { $filters[] = "content.state = '{$state}'"; } $minVeracity = key_exists("minVeracity", $parameters) ? $parameters["minVeracity"] : null; if ($minVeracity != null || $minVeracity === 0) { $filters[] = $minVeracity === 0 ? "(source.score >= {$minVeracity} OR source.score IS NULL)" : "source.score >= {$minVeracity}"; } $maxVeracity = key_exists("maxVeracity", $parameters) ? $parameters["maxVeracity"] : null; if ($maxVeracity != null) { $filters[] = $minVeracity === 0 ? "(source.score <= {$maxVeracity} OR source.score IS NULL)" : "source.score <= {$maxVeracity}"; } $type = key_exists("type", $parameters) ? $parameters["type"] : null; if ($type != null) { $filters[] = "source.type = '{$type}'"; } $subType = key_exists("subType", $parameters) ? $parameters["subType"] : null; if ($subType != null) { $filters[] = "source.subType = '{$subType}'"; } $source = key_exists("source", $parameters) ? $parameters["source"] : null; if ($source != null) { $filters[] = "source.textId = '{$source}'"; } $pageSize = key_exists("pageSize", $parameters) ? $parameters["pageSize"] : null; $pageStart = key_exists("pageStart", $parameters) ? $parameters["pageStart"] : null; $pagination = $pageSize != null ? "limit " . ($pageStart == null ? "0" : $pageStart) . ", {$pageSize}" : ""; $orderBy = key_exists("orderBy", $parameters) ? $parameters["orderBy"] : null; if ($orderBy == null) { $orderBy = "date desc"; } $sql = $baseSql; for ($i = 0; $i < count($filters); $i++) { $addition = $i == 0 ? "WHERE" : "AND"; $sql .= " " . $addition . " " . $filters[$i]; } $selectSql = "select content.textId " . $sql; $allIds = RedBeanController::DataBaseAdapter()->getCol($selectSql, array()); $totalCount = count($allIds); $selectSql .= " order by content." . $orderBy . " " . $pagination; $navigation = array(); if ($subType == null) { $typeSql = "select source.type as name, source.type as id, count(source.type) as count " . $sql . " group by source.type order by count desc"; $types = array("type" => "list", "key" => "type", "selected" => $type != null, "facets" => RedBeanController::DataBaseAdapter()->get($typeSql, array())); $navigation["Channels"] = $types; } if ($type != null && $source == null) { $subTypeSql = "select source.subType as name, source.subType as id, count(source.subType) as count " . $sql . " group by source.subType order by count desc"; $subTypes = array("type" => "list", "key" => "subType", "selected" => $subType != null, "facets" => RedBeanController::DataBaseAdapter()->get($subTypeSql, array())); $navigation["Sub Channels"] = $subTypes; } if ($subType != null && $type != null) { $sourceSql = "select source.name as name, source.textId as id, count(source.name) as count " . $sql . " group by source.name order by count desc"; $sources = array("type" => "list", "key" => "source", "selected" => $source != null, "facets" => RedBeanController::DataBaseAdapter()->get($sourceSql, array())); $navigation["Sources"] = $sources; } $subjectIds = RedBeanController::DataBaseAdapter()->getCol($selectSql, array()); $content = DataContext::GetContent($subjectIds, $orderBy); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContentList [Method finished]", \PEAR_LOG_DEBUG); return array("totalCount" => $totalCount, "contentItems" => $content, "navigation" => $navigation); }
/** * Given the correct parameters, this method will reatun a page of content * in the correct state for whome the source of that content has a veracity * score in between the $minVeracity and $maxVeracity supplied. * * @param int $state * @param int $pagesize * @param int $pagestart * @param int $minVeracity 0 - 100 * @param int $maxVeracity 0 - 100 * @param string $orderby * @return array("totalCount" => int, "contentItems" => Content[]) */ public static function GetPagedContentByStateAndSourceVeracity($state, $pagesize, $pagestart, $minVeracity, $maxVeracity, $orderby = null) { $logger = \Swiftriver\Core\Setup::GetLogger(); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetPagedContentByStateAndSourceVeracity [Method invoked]", \PEAR_LOG_DEBUG); //if no $orderby is sent if (!$orderby || $orderby == null) { $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetPagedContentByStateAndSourceVeracity [No Order By clause set, setting to 'date desc']", \PEAR_LOG_DEBUG); //Set it to the default - date DESC $orderby = "date desc"; } //initilise the red bean controller $rb = RedBeanController::RedBean(); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetPagedContentByStateAndSourceVeracity [START: Get total record count for state: {$state}]", \PEAR_LOG_DEBUG); try { //get the total count to return $sql = "select count(content.id) from content left join content_source " . "on content.id = content_source.content_id left join source " . "on content_source.source_id = source.id where state = :state " . "and ((source.score > :min and source.score < :max) or source.score " . ($minVeracity == 0 ? "is" : "is not") . " null)"; $totalCount = RedBeanController::DataBaseAdapter()->getCell($sql, array(":state" => $state, ":min" => $minVeracity, ":max" => $maxVeracity)); } catch (\Exception $e) { //no content defined yet $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetPagedContentByStateAndSourceVeracity [No content to return]", \PEAR_LOG_DEBUG); return array(); } //set the return as an int $totalCount = (int) $totalCount; $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetPagedContentByStateAndSourceVeracity [Total record count = {$totalCount}]", \PEAR_LOG_DEBUG); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetPagedContentByStateAndSourceVeracity [END: Get total record count for state: {$state}]", \PEAR_LOG_DEBUG); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetPagedContentByStateAndSourceVeracity [START: Get the id's of the content that should be returned]", \PEAR_LOG_DEBUG); //set the SQL $isNullCondition = $minVeracity == 0 ? "is" : "is not"; $sql = "select content.textId from content left join content_source " . "on content.id = content_source.content_id left join source " . "on content_source.source_id = source.id where state = '{$state}' " . "and ((source.score >= {$minVeracity} and source.score <= {$maxVeracity}) " . "or source.score {$isNullCondition} null)"; $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetPagedContentByStateAndSourceVeracity [Getting ID's with query: {$sql}]", \PEAR_LOG_DEBUG); //Get the page of IDs $ids = RedBeanController::DataBaseAdapter()->getCol($sql); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetPagedContentByStateAndSourceVeracity [END: Get the id's of the content that should be returned]", \PEAR_LOG_DEBUG); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetPagedContentByStateAndSourceVeracity [START: Getting the content for the ids]", \PEAR_LOG_DEBUG); //Get the content items $content = self::GetContent($ids, $orderby); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetPagedContentByStateAndSourceVeracity [END: Getting the content for the ids]", \PEAR_LOG_DEBUG); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetPagedContentByStateAndSourceVeracity [Method finished]", \PEAR_LOG_DEBUG); return array("totalCount" => $totalCount, "contentItems" => $content); }