/** * Return a locomotive class * * @since Version 3.9.1 * @return \Railpage\Locos\LocoClass * * @param int|string|bool $id Integer or string are valid values - defaults to bool false * * @throws \Exception if loco class id could not be found * @throws \Exception if an invalid class ID was supplied */ public static function CreateLocoClass($id = false) { $Redis = AppCore::getRedis(); $Registry = Registry::getInstance(); if (!filter_var($id, FILTER_VALIDATE_INT)) { $id = Utility\LocomotiveUtility::getClassId($id); } if ($id = filter_var($id, FILTER_VALIDATE_INT)) { $regkey = sprintf(LocoClass::REGISTRY_KEY, $id); try { $LocoClass = $Registry->get($regkey); } catch (Exception $e) { $cachekey = sprintf(LocoClass::CACHE_KEY, $id); if (!self::USE_REDIS || !($LocoClass = $Redis->fetch($cachekey))) { $LocoClass = new LocoClass($id); if (self::USE_REDIS) { $Redis->save($cachekey, $LocoClass); } } $Registry->set($regkey, $LocoClass); } if (filter_var($LocoClass->id, FILTER_VALIDATE_INT)) { return $LocoClass; } throw new Exception(sprintf("Locomotive class id %s could not be found", $id)); } throw new Exception("An invalid locomotive class ID was supplied"); }
/** * Load / fetch a class * @since Version 3.2 * @param boolean $recurse */ private function fetch($recurse) { if (!filter_var($this->id, FILTER_VALIDATE_INT)) { $this->id = Utility\LocomotiveUtility::getClassId($this->id); } $this->mckey = sprintf("railpage:locos.class_id=%d", $this->id); $key = "id"; if (!($row = $this->Memcached->fetch($this->mckey))) { $timer = Debug::getTimer(); $query = "SELECT c.id, c.meta, c.asset_id, c.slug, c.download_id, c.date_added, c.date_modified, c.model, c.axle_load, c.tractive_effort, c.weight, c.length, c.parent AS parent_class_id, c.source_id AS source, c.id AS class_id, c.flickr_tag, c.flickr_image_id, c.introduced AS class_introduced, c.name AS class_name, c.loco_type_id AS loco_type_id, c.desc AS class_desc, c.manufacturer_id AS class_manufacturer_id, m.manufacturer_name AS class_manufacturer, w.arrangement AS wheel_arrangement, w.id AS wheel_arrangement_id, t.title AS loco_type\r\n FROM loco_class AS c\r\n LEFT JOIN loco_type AS t ON c.loco_type_id = t.id\r\n LEFT JOIN wheel_arrangements AS w ON c.wheel_arrangement_id = w.id\r\n LEFT JOIN loco_manufacturer AS m ON m.manufacturer_id = c.manufacturer_id\r\n WHERE c." . $key . " = ?"; $row = $this->db->fetchRow($query, $this->id); Debug::logEvent(__METHOD__, $timer); /** * Normalise some items */ if (function_exists("convert_to_utf8")) { foreach ($row as $key => $val) { $row[$key] = convert_to_utf8($val); } } $this->Memcached->save($this->mckey, $row, strtotime("+1 year")); } // Get out early if we don't have a valid data source if (!isset($row) || !is_array($row)) { return; } $timer = Debug::getTimer(); if (isset($row['id'])) { $this->id = $row['id']; } if (!isset($row['id'])) { deleteMemcacheObject($this->mckey); } // Populate the class objects $this->slug = $row['slug']; $this->name = $row['class_name']; $this->desc = $row['class_desc']; $this->type = $row['loco_type']; $this->type_id = $row['loco_type_id']; $this->introduced = $row['class_introduced']; $this->manufacturer = $row['class_manufacturer']; $this->manufacturer_id = $row['class_manufacturer_id']; $this->wheel_arrangement = $row['wheel_arrangement']; $this->wheel_arrangement_id = $row['wheel_arrangement_id']; $this->flickr_tag = $row['flickr_tag']; $this->flickr_image_id = $row['flickr_image_id']; $this->axle_load = $row['axle_load']; $this->tractive_effort = $row['tractive_effort']; $this->weight = $row['weight']; $this->length = $row['length']; $this->model = $row['model']; $this->date_added = $row['date_added']; $this->date_modified = $row['date_modified']; $this->download_id = $row['download_id']; if (empty($this->slug) || $this->slug === "1") { $this->createSlug(); $this->commit(); } $this->url = Utility\LocoClassUtility::buildUrls($this); /** * Set the meta data */ $this->meta = array(); if (isset($row['meta'])) { $this->meta = json_decode($row['meta'], true); } /** * If an asset ID exists and is greater than 0, create the asset object */ if (isset($row['asset_id']) && $row['asset_id'] > 0) { try { $this->Asset = new Asset($row['asset_id']); } catch (Exception $e) { global $Error; $Error->save($e); } } /** * Create the fwlink object */ try { $this->fwlink = new fwlink($this->url); if (empty($this->fwlink->url) && !empty(trim($this->name))) { $this->fwlink->url = $this->url; $this->fwlink->title = $this->name; $this->fwlink->commit(); } } catch (Exception $e) { // Do nothing } /* // Parent object if ($row['parent_class_id'] > 0) { try { $this->parent = new LocoClass($row['parent_class_id'], false); } catch (Exception $e) { // Re-throw the error throw new Exception($e->getMessage()); } } // Data source object if ($row['source'] > 0 && class_exists("Source")) { try { $this->source = new \Source($row['source']); } catch (Exception $e) { // Re-throw the error throw new Exception($e->getMessage()); } } */ /** * Set the StatsD namespaces */ $this->StatsD->target->view = sprintf("%s.%d.view", $this->namespace, $this->id); $this->StatsD->target->edit = sprintf("%s.%d.view", $this->namespace, $this->id); Debug::logEvent(__METHOD__, $timer); }