/** * Populate this object with data returned from Memcached/Redis/DB * @since Version 3.9.1 * @return void */ private function populate() { $timer = Debug::getTimer(); $row = Utility\LocomotiveUtility::fetchLocomotive($this); if (!is_array($row) || count($row) === 0) { throw new Exception("Data for this locomotive could not be retrieved"); } $lookup = Utility\DataUtility::getLocoColumnMapping(); foreach ($row as $key => $val) { if (isset($lookup[$key])) { $var = $lookup[$key]; $this->{$var} = $val; } } $ints = ["gauge_id", "status_id", "class_id", "owner_id", "operator_id", "photo_id", "manufacturer_id"]; foreach ($ints as $int) { $this->{$int} = filter_var($this->{$int}, FILTER_VALIDATE_INT); } $this->Class = Factory::CreateLocoClass($this->class_id); $this->class =& $this->Class; $this->flickr_tag = trim(str_replace(" ", "", $this->Class->flickr_tag . "-" . $this->number)); $this->gauge_formatted = format_gauge($this->gauge); $this->makeLinks(); Debug::logEvent(__METHOD__, $timer); return $row; }
/** * Find Railpage objects (loco, class, livery) in this image * * @since Version 3.8.7 * * @param string $namespace * @param boolean $force * * @return \Railpage\Images\Image; * @throws \Exception if $namespace is null or empty */ public function findObjects($namespace = null, $force = false) { if (is_null($namespace)) { throw new Exception("Parameter 1 (namespace) cannot be empty"); } $key = sprintf("railpage:images.image=%d;objects.namespace=%s;lastupdate", $this->id, $namespace); $lastupdate = $this->Memcached->fetch($key); if (!$force && $lastupdate && $lastupdate > strtotime("1 day ago")) { return $this; } /** * Start the debug timer */ $timer = Debug::GetTimer(); switch ($namespace) { case "railpage.locos.loco": if (isset($this->meta['tags'])) { foreach ($this->meta['tags'] as $tag) { if (preg_match("@railpage:class=([0-9]+)@", $tag, $matches)) { Debug::LogEvent(__METHOD__ . " :: #1 Instantating new LocoClass object with ID " . $matches[1] . " "); $LocoClass = LocosFactory::CreateLocoClass($matches[1]); } } foreach ($this->meta['tags'] as $tag) { if (isset($LocoClass) && $LocoClass instanceof LocoClass && preg_match("@railpage:loco=([a-zA-Z0-9]+)@", $tag, $matches)) { Debug::LogEvent(__METHOD__ . " :: #2 Instantating new LocoClass object with class ID " . $LocoClass->id . " and loco number " . $matches[1] . " "); $Loco = LocosFactory::CreateLocomotive(false, $LocoClass->id, $matches[1]); if (filter_var($Loco->id, FILTER_VALIDATE_INT)) { $this->addLink($Loco->namespace, $Loco->id); } } } foreach ($this->db->fetchAll("SELECT id AS class_id, flickr_tag AS class_tag FROM loco_class") as $row) { foreach ($this->meta['tags'] as $tag) { if (stristr($tag, $row['class_tag']) && strlen(str_replace($row['class_tag'] . "-", "", $tag) > 0)) { $loco_num = str_replace($row['class_tag'] . "-", "", $tag); Debug::LogEvent(__METHOD__ . " :: #3 Instantating new LocoClass object with class ID " . $row['class_id'] . " and loco number " . $loco_num . " "); $Loco = LocosFactory::CreateLocomotive(false, $row['class_id'], $loco_num); if (filter_var($Loco->id, FILTER_VALIDATE_INT)) { $this->addLink($Loco->namespace, $Loco->id); if (!$Loco->hasCoverImage()) { $Loco->setCoverImage($this); } if (!$Loco->Class->hasCoverImage()) { $Loco->Class->setCoverImage($this); } } } } } } break; case "railpage.locos.class": if (isset($this->meta['tags'])) { foreach ($this->db->fetchAll("SELECT id AS class_id, flickr_tag AS class_tag FROM loco_class") as $row) { foreach ($this->meta['tags'] as $tag) { if ($tag == $row['class_tag']) { $LocoClass = LocosFactory::CreateLocoClass($row['class_id']); if (filter_var($LocoClass->id, FILTER_VALIDATE_INT)) { $this->addLink($LocoClass->namespace, $LocoClass->id); } } } } foreach ($this->meta['tags'] as $tag) { if (preg_match("@railpage:class=([0-9]+)@", $tag, $matches)) { $LocoClass = LocosFactory::CreateLocoClass($matches[1]); if (filter_var($LocoClass->id, FILTER_VALIDATE_INT)) { $this->addLink($LocoClass->namespace, $LocoClass->id); if (!$LocoClass->hasCoverImage()) { $LocoClass->setCoverImage($this); } } } } } break; case "railpage.locos.liveries.livery": if (isset($this->meta['tags'])) { foreach ($this->meta['tags'] as $tag) { if (preg_match("@railpage:livery=([0-9]+)@", $tag, $matches)) { $Livery = new Livery($matches[1]); if (filter_var($Livery->id, FILTER_VALIDATE_INT)) { $this->addLink($Livery->namespace, $Livery->id); } } } } break; } Debug::LogEvent(__METHOD__ . "(\"" . $namespace . "\")", $timer); $this->Memcached->save($key, time()); return $this; }
/** * Process this row as a locomotive class * @since Version 3.9.1 * @return void */ private static function processLocomotiveClass() { $LocoClass = LocosFactory::CreateLocoClass(self::$row['value']); if (!$LocoClass instanceof LocoClass) { return; } self::$row['meta']['object']['title'] = $LocoClass->name; self::$row['meta']['namespace'] = $LocoClass->namespace; self::$row['meta']['id'] = $LocoClass->id; }
/** * Get locomotive classes built by this wheel arrangement * @return array */ public function getClasses() { $query = "SELECT id, name, loco_type_id, introduced AS year_introduced, manufacturer_id, wheel_arrangement_id FROM loco_class WHERE wheel_arrangement_id = ? ORDER BY name"; $return = array(); foreach ($this->db->fetchAll($query, $this->id) as $row) { $LocoClass = Factory::CreateLocoClass($row['id']); $return[] = $LocoClass->getArray(); } return $return; }
/** * @depends testAddLocoClass * @depends testAddGauge * @depends testAddStatus * @depends testAddManufacturer */ public function test_bulkAddLocos_break_manufacturer($class_id, $gauge_id, $status_id, $manufacturer_id) { $this->setExpectedException("Exception", "Cannot add locomotives to class - no manufacturer ID was provided"); $Class = LocosFactory::CreateLocoClass($class_id); $Class->bulkAddLocos(10, 20, $gauge_id, $status_id, false, "Z"); }