/** * Return a new instance of Image * @since Version 3.9.1 * @param int|string $id * @param string $provider * @param int $options * @return \Railpage\Images\Image */ public static function CreateImage($id = null, $provider = null, $options = null) { $Redis = AppCore::GetRedis(); $Registry = Registry::GetInstance(); $cachekey = sprintf("rp:v2;cache.image=%s;o=%s", $id, crc32(json_encode($options))); if ($id != null && $provider == null) { return new Image($id, $options); try { $Image = $Registry->get($cachekey); } catch (Exception $e) { if (!($Image = $Redis->fetch($cachekey))) { $Image = new Image($id, $options); $Redis->save($cachekey, $Image, strtotime("+10 minutes")); } $Registry->set($cachekey, $Image); } return $Image; } $cachekey .= sprintf(";p=%s", $provider); #$Registry = Registry::getInstance(); #echo $cachekey;die; return (new Images())->findImage($provider, $id, $options); try { $Image = $Registry->get($cachekey); } catch (Exception $e) { if ($Image = $Redis->fetch($cachekey) && $Image instanceof Image) { $Registry->set($cachekey, $Image); return $Image; } try { $Images = new Images(); $Image = $Images->findImage($provider, $id, $options); $Redis->save($cachekey, $Image, strtotime("+10 minutes")); $Registry->set($cachekey, $Image); } catch (Exception $e) { $Image = false; } } return $Image; }
/** * Load the locomotive object * @since Version 3.2 * @version 3.2 * @return boolean */ public function fetch() { if (!$this->id) { throw new Exception("Cannot load loco object - loco ID not provided"); return false; } $this->mckey = "railpage:locos.loco_id=" . $this->id; #deleteMemcacheObject($this->mckey); if ($row = $this->getCache($this->mckey)) { // Do nothing } elseif ($this->db instanceof \sql_db) { $query = "SELECT l.*, s.name AS loco_status, ow.operator_name AS owner_name, op.operator_name AS operator_name\r\n\t\t\t\t\t\t\tFROM loco_unit AS l\r\n\t\t\t\t\t\t\tLEFT JOIN loco_status AS s ON l.loco_status_id = s.id\r\n\t\t\t\t\t\t\tLEFT JOIN operators AS ow ON ow.operator_id = l.owner_id\r\n\t\t\t\t\t\t\tLEFT JOIN operators AS op ON op.operator_id = l.operator_id\r\n\t\t\t\t\t\t\tWHERE l.loco_id = " . $this->id; if ($rs = $this->db->query($query)) { $row = $rs->fetch_assoc(); $this->setCache($this->mckey, $row, strtotime("+24 hours")); } } else { if (RP_DEBUG) { global $site_debug; $debug_timer_start = microtime(true); } $query = "SELECT l.*, s.name AS loco_status, ow.operator_name AS owner_name, op.operator_name AS operator_name\r\n\t\t\t\t\t\t\tFROM loco_unit AS l\r\n\t\t\t\t\t\t\tLEFT JOIN loco_status AS s ON l.loco_status_id = s.id\r\n\t\t\t\t\t\t\tLEFT JOIN operators AS ow ON ow.operator_id = l.owner_id\r\n\t\t\t\t\t\t\tLEFT JOIN operators AS op ON op.operator_id = l.operator_id\r\n\t\t\t\t\t\t\tWHERE l.loco_id = ?"; $row = $this->db->fetchRow($query, $this->id); if (RP_DEBUG) { if ($row === false) { $site_debug[] = "Zend_DB: FAILED select loco ID " . $this->id . " in " . round(microtime(true) - $debug_timer_start, 5) . "s"; } else { $site_debug[] = "Zend_DB: SUCCESS select loco ID " . $this->id . " in " . round(microtime(true) - $debug_timer_start, 5) . "s"; } } $this->setCache($this->mckey, $row, strtotime("+24 hours")); } if (isset($row) && is_array($row)) { $this->number = stripslashes($row['loco_num']); $this->name = stripslashes($row['loco_name']); $this->gauge = stripslashes($row['loco_gauge']); $this->gauge_id = $row['loco_gauge_id']; $this->status_id = $row['loco_status_id']; $this->status = $row['loco_status']; $this->class_id = $row['class_id']; $this->owner_id = $row['owner_id']; $this->owner = $row['owner_name']; $this->operator_id = $row['operator_id']; $this->operator = $row['operator_name']; $this->entered_service = $row['entered_service']; $this->withdrawal_date = $row['withdrawn']; $this->date_added = $row['date_added']; $this->date_modified = $row['date_modified']; $this->builders_num = $row['builders_number']; $this->photo_id = intval($row['photo_id']); $this->manufacturer_id = $row['manufacturer_id']; $this->Class = new LocoClass($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->url = new Url(strtolower($this->makeLocoURL($this->Class->slug, $this->number))); $this->url->edit = sprintf("%s?mode=loco.edit&id=%d", $this->Module->url, $this->id); $this->url->sightings = sprintf("%s/sightings", $this->url->url); /** * Set the meta data */ if (isset($row['meta'])) { $this->meta = json_decode($row['meta'], true); } else { $this->meta = array(); } // Fetch the gauge data if ($this->gauge = getMemcacheObject(sprintf("railpage:locos.gauge_id=%d", $row['loco_gauge_id']))) { // Do nothing } elseif ($this->db instanceof \sql_db) { $query = "SELECT * FROM loco_gauge WHERE gauge_id = '" . $this->db->real_escape_string($row['loco_gauge_id']) . "'"; if ($rs = $this->db->query($query)) { $this->gauge = $rs->fetch_assoc(); $this->setCache("rp-locos-gauge-" . $row['loco_gauge_id'], $this->gauge); } } else { $query = "SELECT * FROM loco_gauge WHERE gauge_id = ?"; $this->gauge = $this->db->fetchRow($query, $row['loco_gauge_id']); $this->setCache("railpage:locos.gauge_id=" . $row['loco_gauge_id'], $this->gauge, strtotime("+2 months")); } /** * 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 \Railpage\Assets\Asset($row['asset_id']); } catch (Exception $e) { global $Error; $Error->save($e); } } /** * Try to load the Image object */ if (filter_var($row['photo_id'], FILTER_VALIDATE_INT)) { $Images = new \Railpage\Images\Images(); $this->Image = $Images->findImage("flickr", $row['photo_id']); $this->Image->addLink($this->namespace, $this->id); } /** * Get all owners of this locomotive */ try { $this->owners = $this->getOrganisations(1); if (!empty($this->owner_id) && empty($this->owners)) { $this->addOrganisation($this->owner_id, 1); // Re-fetch the owners $this->owners = $this->getOrganisations(1); } reset($this->owners); if (isset($this->owners[0]['organisation_id']) && isset($this->owners[0]['organisation_name'])) { $this->owner_id = $this->owners[0]['organisation_id']; $this->owner = $this->owners[0]['organisation_name']; } else { $this->owner_id = 0; $this->owner = "Unknown"; } } catch (Exception $e) { global $Error; $Error->save($e); } /** * Get all operators of this locomotive */ try { $this->operators = $this->getOrganisations(2); if (!empty($this->operator_id) && empty($this->operators)) { $this->addOrganisation($this->operator_id, 2); // Re-fetch the operators $this->operators = $this->getOrganisations(2); } reset($this->operators); if (isset($this->operators[0]['organisation_id']) && isset($this->operators[0]['organisation_name'])) { $this->operator_id = $this->operators[0]['organisation_id']; $this->operator = $this->operators[0]['organisation_name']; } else { $this->operator_id = 0; $this->operator = "Unknown"; } } catch (Exception $e) { global $Error; $Error->save($e); } /** * Get the average rating of the data of this loco */ try { $this->rating = $this->getRating(); } catch (Exception $e) { // Do nothing for now } /** * Get the manufacturer */ if (empty($this->manufacturer_id)) { $this->manufacturer_id = $this->Class->manufacturer_id; $this->manufacturer = $this->Class->manufacturer; } else { try { $builders = $this->listManufacturers(); if (count($builders['manufacturers'])) { $this->manufacturer = $builders['manufacturers'][$this->manufacturer_id]['manufacturer_name']; } } catch (Exception $e) { // I hate globals, but I don't want to throw an exception here... global $Error; $Error->save($e); } } /** * Update the latest owner/operator stored in this row */ $owners = $this->getOrganisations(1, 1); $operators = $this->getOrganisations(2, 1); if (count($owners) && intval(trim($this->owner_id)) != intval(trim($owners[0]['operator_id']))) { if (RP_DEBUG) { global $site_debug; $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : committing changes to owner for loco ID " . $this->id; $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : Current owner_id: " . $this->owner_id . ", Proposed owner_id: " . $owners[0]['operator_id']; } $this->owner = $owners[0]['organisation_name']; $this->owner_id = $owners[0]['operator_id']; $this->commit(); } if (count($operators) && intval(trim($this->operator_id)) != intval(trim($operators[0]['operator_id']))) { if (RP_DEBUG) { global $site_debug; $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : committing changes to operator for loco ID " . $this->id; $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : Current operator_id: " . $this->operator_id . ", Proposed operator_id: " . $owners[0]['operator_id']; } $this->operator = $operators[0]['organisation_name']; $this->operator_id = $operators[0]['operator_id']; $this->commit(); } /** * Build the shortcut URL for this object */ try { $this->fwlink = new \Railpage\fwlink($this->url); if (empty($this->fwlink->url)) { $this->fwlink->url = $this->url; $this->fwlink->title = $this->number . " - " . $this->Class->name; $this->fwlink->commit(); } } catch (Exception $e) { global $Error; $Error->save($e); } /** * Populate the list of liveries */ foreach ($this->db->fetchAll("SELECT lu.livery_id FROM loco_unit_livery AS lu LEFT JOIN loco_livery AS li ON lu.livery_id = li.livery_id WHERE lu.loco_id = ? ORDER BY li.livery", $this->id) as $row) { $Livery = new Livery($row['livery_id']); $livery = array("id" => $Livery->id, "name" => $Livery->name, "tag" => $Livery->tag, "country" => array("code" => $Livery->country), "region" => array("code" => $Livery->region)); if ($Livery->Image instanceof \Railpage\Images\Image) { $livery['image'] = array("id" => $Livery->Image->id, "title" => $Livery->Image->title, "description" => $Livery->Image->description, "provider" => $Livery->Image->provider, "photo_id" => $Livery->Image->photo_id, "sizes" => $Livery->Image->sizes); } $this->liveries[] = $livery; } /** * 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); } else { throw new Exception("No data found for Loco ID " . $this->id); return false; } }
/** * Load this livery object * @since Version 3.2 * @return boolean */ public function fetch() { if (!filter_var($this->id, FILTER_VALIDATE_INT)) { throw new Exception("Cannot fetch Livery object - no ID given"); return false; } $query = "SELECT * FROM loco_livery WHERE livery_id = ?"; $row = $this->db->fetchRow($query, $this->id); if (isset($row) && is_array($row)) { $this->name = $row['livery']; $this->introduced = $row['introduced']; $this->withdrawn = $row['withdrawn']; $this->superseded_by_id = $row['superseded_by'] > 0 ? $row['superseded_by'] : NULL; $this->supersedes_id = $row['supersedes'] > 0 ? $row['supersedes'] : NULL; $this->photo_id = $row['photo_id']; $this->region = $row['region']; $this->country = $row['country']; $this->tag = sprintf("railpage:livery=%d", $this->id); $this->url = "/flickr?tag=" . $this->tag; if (filter_var($this->photo_id, FILTER_VALIDATE_INT)) { $Images = new Images(); $this->Image = $Images->findImage("flickr", $this->photo_id); } if ($this->recurse && $this->superseded_by_id > 0) { $Superseded_by = new Livery($this->superseded_by_id, false); $this->superseded_by = $Superseded_by->name; } if ($this->recurse && $this->supersedes_id > 0) { $Supersedes = new Livery($this->supersedes_id, false); $this->supersedes = $Supersedes->name; } return true; } else { throw new Exception("Cannot fetch Livery object - no livery for ID " . $this->id . " was found"); return false; } }
/** * Get the cover photo for this locomotive * @since Version 3.8.7 * @return array * @todo Set the AssetProvider (requires creating AssetProvider) */ public function getCoverImage() { /** * Image stored in meta data */ if (isset($this->meta['coverimage'])) { $Image = new Image($this->meta['coverimage']['id']); return array("type" => "image", "provider" => $Image->provider, "title" => $Image->title, "author" => array("id" => $Image->author->id, "username" => $Image->author->username, "realname" => isset($Image->author->realname) ? $Image->author->realname : $Image->author->username, "url" => $Image->author->url), "image" => array("id" => $Image->id), "sizes" => $Image->sizes, "url" => $Image->url->getURLs()); } /** * Asset */ if ($this->Asset instanceof Asset) { return array("type" => "asset", "provider" => "", "title" => $Asset->meta['title'], "author" => array("id" => "", "username" => "", "realname" => "", "url" => ""), "sizes" => array("large" => array("source" => $Asset->meta['image']), "original" => array("source" => $Asset->meta['original'])), "url" => array("url" => $Asset['meta']['image'])); } /** * Ordinary Flickr image */ if (filter_var($this->photo_id, FILTER_VALIDATE_INT) && $this->photo_id > 0) { $Images = new Images(); $Image = $Images->findImage("flickr", $this->photo_id); return array("type" => "image", "provider" => $Image->provider, "title" => $Image->title, "author" => array("id" => $Image->author->id, "username" => $Image->author->username, "realname" => isset($Image->author->realname) ? $Image->author->realname : $Image->author->username, "url" => $Image->author->url), "image" => array("id" => $Image->id), "sizes" => $Image->sizes, "url" => $Image->url->getURLs()); } /** * No cover image! */ return false; }
/** * Load / fetch a class * @since Version 3.2 * @param boolean $recurse */ public function fetch($recurse) { if (!$this->id) { throw new Exception("Cannot fetch loco class - no class ID given"); return false; } if (!filter_var($this->id, FILTER_VALIDATE_INT)) { $this->id = $this->db->fetchOne("SELECT id FROM loco_class WHERE slug = ?", $this->id); } $this->mckey = "railpage:locos.class_id=" . $this->id; $key = "id"; if ($row = $this->getCache($this->mckey)) { // Do nothing } elseif ($this->db instanceof \sql_db) { $query = "SELECT c.id, 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\t\t\t\t\t\t\tFROM loco_class AS c\r\n\t\t\t\t\t\t\tLEFT JOIN loco_type AS t ON c.loco_type_id = t.id\r\n\t\t\t\t\t\t\tLEFT JOIN wheel_arrangements AS w ON c.wheel_arrangement_id = w.id\r\n\t\t\t\t\t\t\tLEFT JOIN loco_manufacturer AS m ON m.manufacturer_id = c.manufacturer_id\r\n\t\t\t\t\t\t\tWHERE c." . $key . " = " . $this->db->real_escape_string($this->id); if ($rs = $this->db->query($query)) { $row = $rs->fetch_assoc(); $this->setCache($this->mckey, $row, strtotime("+24 hours")); } } else { if (RP_DEBUG) { global $site_debug; $debug_timer_start = microtime(true); } $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\t\t\t\t\t\t\tFROM loco_class AS c\r\n\t\t\t\t\t\t\tLEFT JOIN loco_type AS t ON c.loco_type_id = t.id\r\n\t\t\t\t\t\t\tLEFT JOIN wheel_arrangements AS w ON c.wheel_arrangement_id = w.id\r\n\t\t\t\t\t\t\tLEFT JOIN loco_manufacturer AS m ON m.manufacturer_id = c.manufacturer_id\r\n\t\t\t\t\t\t\tWHERE c." . $key . " = ?"; $row = $this->db->fetchRow($query, $this->id); if (RP_DEBUG) { if ($row === false) { $site_debug[] = "Zend_DB: FAILED select loco class ID/slug " . $this->id . " in " . round(microtime(true) - $debug_timer_start, 5) . "s"; } else { $site_debug[] = "Zend_DB: SUCCESS select loco class ID/slug " . $this->id . " in " . round(microtime(true) - $debug_timer_start, 5) . "s"; } } foreach ($row as $key => $val) { $row[$key] = convert_to_utf8($val); } $this->setCache($this->mckey, $row, strtotime("+24 hours")); } if (isset($row) && is_array($row)) { if (!isset($row['id'])) { deleteMemcacheObject($this->mckey); } else { $this->id = $row['id']; } // 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(); if (RP_DEBUG) { global $site_debug; $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : Creating url slug for loco class ID " . $this->id; } $this->commit(); } $this->url = new Url($this->makeClassURL($this->slug)); $this->url->view = $this->url->url; $this->url->edit = sprintf("%s?mode=class.edit&id=%d", $this->Module->url, $this->id); $this->url->addLoco = sprintf("%s?mode=loco.edit&class_id=%d", $this->Module->url, $this->id); $this->url->sightings = sprintf("%s/sightings", $this->url->url); $this->url->bulkadd = sprintf("%s?mode=loco.bulkadd&class_id=%d", $this->Module->url, $this->id); $this->url->bulkedit = sprintf("%s?mode=class.bulkedit&id=%d", $this->Module->url, $this->id); $this->url->bulkedit_operators = sprintf("%s?mode=class.bulkedit.operators&id=%d", $this->Module->url, $this->id); $this->url->bulkedit_buildersnumbers = sprintf("%s?mode=class.bulkedit.buildersnumbers&id=%d", $this->Module->url, $this->id); $this->url->bulkedit_status = sprintf("%s?mode=class.bulkedit.status&id=%d", $this->Module->url, $this->id); $this->url->bulkedit_gauge = sprintf("%s?mode=class.bulkedit.gauge&id=%d", $this->Module->url, $this->id); /** * Set the meta data */ if (isset($row['meta'])) { $this->meta = json_decode($row['meta'], true); } else { $this->meta = array(); } /** * 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 \Railpage\Assets\Asset($row['asset_id']); } catch (Exception $e) { global $Error; $Error->save($e); } } /** * Try to load the Image object */ if (filter_var($row['flickr_image_id'], FILTER_VALIDATE_INT)) { $Images = new \Railpage\Images\Images(); $this->Image = $Images->findImage("flickr", $row['flickr_image_id']); $this->Image->addLink($this->namespace, $this->id); } /** * Create the fwlink object */ try { #var_dump($this->url);die; $this->fwlink = new \Railpage\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); /* // Child classes if ($this->db instanceof \sql_db) { $query = "SELECT c.id AS child_class_id, c.name AS child_class_name FROM loco_class AS c WHERE c.parent = ".$this->db->real_escape_string($this->id); if ($rs = $this->db->query($query)) { while ($row = $rs->fetch_assoc()) { $this->children[$row['child_class_id']] = $row['child_class_name']; } } else { throw new Exception($this->db->error); } } else { $query = "SELECT c.id AS child_class_id, c.name AS child_class_name FROM loco_class AS c WHERE c.parent = ?"; foreach ($this->db->fetchAll($query, $this->id) as $row) { $this->children[$row['child_class_id']] = $row['child_class_name']; } } */ /* if (RP_PLATFORM != "API" && !filter_var($this->download_id, FILTER_VALIDATE_INT)) { // Create a new download for this class' datasheet if (RP_DEBUG) { global $site_debug; $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : No download ID for class ID " . $this->id; } try { $Download = new \Railpage\Downloads\Download(); if (!empty($this->name) && strlen($this->name) > 1) { $Download->name = $this->name." data sheet"; $Download->url = "http://www.railpage.com.au/modules.php?name=Locos&mode=exportclass&id=".$this->id."&format=xlsx"; $Download->desc = "Data sheet for the ".$this->name." class, formatted as a Microsoft Excel spreadsheet"; $Download->date = time(); $Download->mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; $Download->active = 1; $Download->approved = 1; $Download->Category = new \Railpage\Downloads\Category("23"); $Download->cat_id = "23"; $Download->extra_data = array("Class name" => $this->name, "Class ID" => $this->id); $Download->filename = 'Railpage-Locodata-'.$this->name.'.xlsx'; $Download->user_id = 45; $Download->filepath = "/"; $Download->commit(); $this->download_id = $Download->id; if (RP_DEBUG) { global $site_debug; $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : committing changes to download ID for loco ID " . $this->id; } $this->commit(); } } catch (Exception $e) { // Discard the error } } */ } }