<? define('__LIBRARY__', __ROOT__ . '/library'); define('__MODEL__',__ROOT__.'/model'); define('__EXTERNAL_LIBRARIES__', __ROOT__ . '/external'); require (__LIBRARY__ . '/autoloader/PCAutoloader.php'); PCCache::setDefaultCacheProvider(PCCache::getTestCacheProvider()); $rootDomain = PCConfigManager::sharedManager()->getValue('DOMAIN_NAME'); ini_set('session.cookie_domain', ".$rootDomain"); $apiSubdomain = "api.".$rootDomain; $ajaxSubdomain = "ajax.".$rootDomain; $router = PCRouter::sharedRouter(); $router->setControllersBaseDirectory(__ROOT__."/controllers"); $router->addSubdomain("/^$apiSubdomain/", PCRequest::TYPE_API); $router->addSubdomain("/^$ajaxSubdomain/", PCRequest::TYPE_AJAX); $router->addSubdomain("/^$rootDomain/", PCRequest::TYPE_WEB); $pages = new PCRouterRoute("/web/PCPageController.php", "page", "PCPageController"); $pages->addSubroute(new PCRouterSubroute("me","meAction")); $pages->setType(PCRequest::TYPE_AJAX); $router->addRoute($pages, PCRequest::TYPE_AJAX);
/** * Returns an array of instanced subclass of PCModelObject according to $name * @param PCMapper $mapper a subclass of PCMapper * @param array $keys a key value array of elements for filtering search in database * @param array $optionalAttributes an array of strings containing the names of the optional fields to fetch * @param boolean $useCache if use the cache or not * @param string $limit the sql query limit * @param string $order the order specification * @return array an array of instanced subclass of PCModelObject according to $name */ public static function fetchModelObjectInstances($mapper, $keys, $optionalAttributes = null, $useCache = false, $limit = null, $order = NULL) { /* if(empty($keys)){ throw new Exception("Illegal argument, \"keys\" is empty"); } */ if ($mapper == null) { throw new Exception("Mapper must not be null"); } $required_field = $mapper->getRequiredAttributes(); if ($useCache && isset($keys['identifier'])) { $identifier = $keys['identifier']; $cache = PCCache::cacheProvider(); //error_log("GETTING : " . $mapper->getCacheKey($keys)); $item = $cache->getItem($mapper->getCacheKey($keys)); if (isset($item) && $item !== FALSE) { //error_log("GETTED : " . $mapper->getCacheKey($keys)); return $item; } } $fields = NULL; //unisco parametri opzionali (se presenti) a parametri richiesti if ($optionalAttributes == null) { $fields = $required_field; } else { $fields = array_merge($optionalAttributes, $required_field); } //creo stringa da parametri separati da virgola $fields_string = implode(",", $fields); $table_name = $mapper->getTableName(); $select_stm = "SELECT " . $fields_string . " FROM " . $table_name . " WHERE "; $prepared_keys_array = array(); $count = 0; $tot = count($keys); foreach ($keys as $key => $value) { $count++; $placeHolder = ":{$key}"; $select_stm .= $count == $tot ? " {$key} = {$placeHolder} " : " {$key} = {$placeHolder} AND "; $prepared_keys_array[$placeHolder] = $value; } if ($tot == NULL) { $select_stm .= " TRUE"; } if (isset($order)) { $select_stm .= ' ORDER BY ' . $order; } if (isset($limit)) { $select_stm .= ' LIMIT ' . $limit; } $pdo = PCDatabase::getSharedDatabaseConnection(); $prepared = $pdo->prepare($select_stm); if ($prepared->execute($prepared_keys_array) == FALSE) { $message = "Errore database: (" . $prepared->errorCode() . ") " . $prepared->errorInfo()[1] . " " . $prepared->errorInfo()[2]; throw new Exception($message); } $result = null; $toReturn = array(); while (($result = $prepared->fetch(PDO::FETCH_ASSOC)) != NULL) { $toReturn[] = $mapper->getMappedInstance($result); } if ($useCache && count($toReturn) == 1) { $identifier = $keys['identifier']; //error_log("STORING: " . $mapper->getCacheKey($keys)); PCCache::cacheProvider()->setItem($toReturn, $mapper->getCacheKey($keys), 300); } $prepared = NULL; return $toReturn; }
/** * @XXX remove direct database interaction * @param PCModelWebsite $site */ public static function recacheSiteReview($site) { //error_log('RECACHING SITE INFO: '.$site->getIdentifier()); $select = "SELECT avg(usability) as usability, avg(reliability) as reliability,"; $select .= " avg(contents) as contents, count(identifier) as count "; $mapper = PCModelReview::getMapper(); $select .= " FROM " . $mapper->getTableName() . " WHERE site_identifier = :id"; $pdo = PCDatabase::getSharedDatabaseConnection(); $prepared = $pdo->prepare($select); $result = $prepared->execute(array(":id" => $site->getIdentifier())); if ($result === FALSE) { return NULL; } $item = $prepared->fetch(PDO::FETCH_ASSOC); if (!isset($item)) { return NULL; } $usability = (double) $item['usability']; $reliability = (double) $item['reliability']; $contents = (double) $item['contents']; $count = (double) $item['count']; $cache_time = new DateTime('now', new DateTimeZone('UTC')); $keys = array('usability' => $usability, 'reliability' => $reliability, 'contents' => $contents, 'number_of_votes' => $count, 'cached' => $cache_time->format('Y-m-d H:i:s')); $condition = "identifier = :id"; $bindings = array(':id' => $site->getIdentifier()); $websiteMapper = PCModelWebsite::getMapper(); if (PCModelManager::updateObject($websiteMapper, $keys, $condition, $bindings)) { $site->cached_date = $cache_time; $site->contents = $contents; $site->number_of_votes = $count; $site->reliability = $reliability; $site->usability = $usability; PCCache::cacheProvider()->setItem($site, $websiteMapper->getTableName() . $site->getIdentifier()); return $site; } return NULL; }