/** * Get locations within region * @since Version 3.0 * @param string $region * @param string $country * @return array */ public function getLocations($region = false, $country = false) { if (!$region || !$country) { return false; } $mckey = "railpage:locations"; if ($country) { $mckey .= ".country=" . $country; } if ($region) { $mckey .= ".region=" . $region; } deleteMemcacheObject($mckey); // Check memcache if ($this->memcache && ($cache = $this->memcache->get($mckey))) { return $cache; } else { $return = $this->db->fetchAll("SELECT * FROM location WHERE country = ? AND region = ? AND active = 1 ORDER BY locality, neighbourhood", array($country, $region)); setMemcacheObject($mckey, $return, strtotime("+1 hour")); return $return; } }
/** * Commit changes to this entry * @since Version 3.8.7 * @return $this */ public function commit() { $this->validate(); $data = array("type" => $this->Type->id, "short" => $this->name, "full" => $this->text, "example" => $this->example, "date" => $this->Date->format("Y-m-d H:i:s"), "author" => $this->Author->id); if (filter_var($this->id, FILTER_VALIDATE_INT)) { $where = array("id = ?" => $this->id); deleteMemcacheObject($this->mckey); $this->db->update("glossary", $data, $where); } else { $this->db->insert("glossary", $data); $this->id = $this->db->lastInsertId(); $this->url = sprintf("%s?mode=entry.view&id=%d", $this->Module->url, $this->id); } return $this; }
/** * Commit changes to this timetabling point * @since Version 3.9 * @return $this */ public function commit() { $this->validate(); if (isset($this->mckey)) { deleteMemcacheObject($this->mckey); } $data = array("name" => $this->name, "lat" => $this->lat, "lon" => $this->lon, "slug" => $this->slug); if (filter_var($this->id, FILTER_VALIDATE_INT)) { $where = array("id = ?" => $this->id); $this->db->update("timetable_points", $data, $where); } else { $this->db->insert("timetable_points", $data); $this->id = $this->db->lastInsertId(); } return $this; }
/** * Commit changes to this event * @since Version 3.8.7 * @return boolean ZendFramework\Db will throw an exception if there's a fault */ public function commit() { $this->validate(); if (isset($this->mckey) && !empty($this->mckey)) { deleteMemcacheObject($this->mckey); } $data = array("title" => $this->title, "description" => $this->desc, "meta" => json_encode($this->meta), "category_id" => $this->Category->id, "slug" => $this->slug); if ($this->Organisation instanceof Organisation) { $data['organisation_id'] = $this->Organisation->id; } else { $data['organisation_id'] = 0; } if ($this->Place instanceof Place) { $data['lat'] = $this->Place->lat; $data['lon'] = $this->Place->lon; } else { $data['lat'] = NULL; $data['lon'] = NULL; } if (filter_var($this->id)) { $where = array("id = ?" => $this->id); $this->db->update("event", $data, $where); } else { $this->db->insert("event", $data); $this->id = $this->db->lastInsertId(); $this->createUrls(); } return true; }
/** * Commit changes to this image * @since Version 3.8.7 * @return $this */ public function commit() { $data = array("title" => $this->title, "meta" => json_encode($this->meta), "hidden" => $this->hidden); $where = array("id = ?" => $this->id); $this->db->update("gallery_mig_image", $data, $where); deleteMemcacheObject($this->mckey); #deleteMemcacheObject("railpage:gallery.album=717"); $albums = $this->db->fetchAll("SELECT id FROM gallery_mig_album WHERE featured_photo = ?", $this->id); foreach ($albums as $album) { deleteMemcacheObject(sprintf("railpage:gallery.album=%d", $album['id'])); } $data = array("featured_photo" => 0); $where = array("featured_photo = ?" => $this->id); $this->db->update("gallery_mig_album", $data, $where); return $this; }
/** * Add a custom rank * @since Version 3.9 * @param string $rank */ public function addCustomRank($rank = false, $image = false) { if (!is_string($rank) || empty($rank)) { throw new Exception("No rank text given"); } $data = array("rank_special" => 1, "rank_title" => $rank, "rank_min" => "-1", "rank_image" => is_string($image) && !empty($image) ? $image : ""); $this->db->insert("nuke_bbranks", $data); deleteMemcacheObject("railpage:ranks"); return $this->db->lastInsertId(); }
/** * 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); }
/** * Get photo info and sizes * @since Version 3.5 * @param int $photo_id * @return array */ public function getPhoto($photo_id = false) { if (!$photo_id) { throw new Exception("Cannot fetch photo info and sizes - no photo ID given"); return false; } $mckey = "railpage:railcam.photo.id=" . $photo_id; deleteMemcacheObject($mckey); if ($return = getMemcacheObject($mckey)) { $return['photo']['time_relative'] = relative_date($return['photo']['dateuploaded']); return $return; } else { $f = new \flickr_railpage(RP_FLICKR_API_KEY); $f->oauth_token = $this->flickr_oauth_token; $f->oauth_secret = $this->flickr_oauth_secret; $f->cache = false; $return = array(); if ($return = $f->photos_getInfo($photo_id)) { $return['photo']['sizes'] = $f->photos_getSizes($photo_id); $return['photo']['time_relative'] = relative_date($return['photo']['dateuploaded']); setMemcacheObject($mckey, $return, strtotime("+2 hours")); } return $return; } }
/** * Commit changes to this timetabled train * @since Version 3.9 * @return $this */ public function commit() { $this->validate(); if (isset($this->mckey)) { deleteMemcacheObject($this->mckey); } $data = array("provider" => $this->provider, "train_number" => $this->number, "train_name" => "", "train_desc" => "", "gauge_id" => 0, "meta" => json_encode($this->meta), "commodity" => $this->commodity); if ($this->Organisation instanceof Organisation) { $data['operator_id'] = $this->Organisation->id; } if (filter_var($this->id, FILTER_VALIDATE_INT)) { $where = array("id = ?" => $this->id); $this->db->update("timetable_trains", $data, $where); } else { $this->db->insert("timetable_trains", $data); $this->id = $this->db->lastInsertId(); } return $this; }
/** * Commit changes to the database * @since Version 3.2 * @version 3.8.7 * @return boolean */ public function commit() { if (!$this->validate()) { return false; } if (RP_DEBUG) { global $site_debug; $debug_timer_start = microtime(true); } $this->flushMemcached(); $data = array("name" => $this->name, "desc" => $this->desc, "introduced" => $this->introduced, "wheel_arrangement_id" => $this->wheel_arrangement_id, "loco_type_id" => $this->type_id, "manufacturer_id" => $this->manufacturer_id, "flickr_tag" => $this->flickr_tag, "flickr_image_id" => $this->flickr_image_id, "length" => $this->length, "weight" => $this->weight, "axle_load" => $this->axle_load, "tractive_effort" => $this->tractive_effort, "model" => $this->model, "download_id" => empty($this->download_id) ? 0 : $this->download_id, "slug" => empty($this->slug) ? "" : $this->slug, "meta" => json_encode($this->meta)); if (empty($this->date_added)) { $data['date_added'] = time(); } else { $data['date_modified'] = time(); } if ($this->Asset instanceof \Railpage\Assets\Asset) { $data['asset_id'] = $this->Asset->id; } foreach ($data as $key => $val) { if (is_null($val)) { $data[$key] = ""; } } if ($this->id) { // Update $where = array("id = ?" => $this->id); $this->db->update("loco_class", $data, $where); $verb = "Update"; } else { $this->db->insert("loco_class", $data); $this->id = $this->db->lastInsertId(); $this->createSlug(); $this->commit(); $this->url = new Url($this->makeClassURL($this->slug)); $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); $verb = "Insert"; } if (RP_DEBUG) { $site_debug[] = "Zend_DB: SUCCESS " . $verb . " loco class ID " . $this->id . " in " . round(microtime(true) - $debug_timer_start, 5) . "s"; } deleteMemcacheObject("railpage:loco.class.bytype=all"); return true; }
/** * Add wheel arrangement * @since Version 3.4 * @return boolean * @param string $title * @param string $arrangement */ public function addWheelArrangement($title = "", $arrangement = false) { deleteMemcacheObject("railpage:loco.wheelarrangements"); $data = array("title" => $title, "arrangement" => $arrangement); $this->db->insert("wheel_arrangements", $data); return true; }
/** * Update the featured image for this album * @since Version 3.8.7 * @return int */ public function updateFeaturedImage() { $data = array("featured_photo" => $this->featured_photo_id); /* * Update the featured photo by album images */ if (!isset($data['featured_photo']) || !filter_var($data['featured_photo'], FILTER_VALIDATE_INT) || $data['featured_photo'] == 1) { foreach ($this->getImages() as $Image) { if (!$Image->hidden) { $data['featured_photo'] = $Image->id; $this->db->update("gallery_mig_album", $data, array("id = ?" => $this->id)); deleteMemcacheObject($this->mckey); #setMemcacheObject($this->mckey, $data, strtotime("+1 year")); break; } } } /** * Update the featured photo by sub-album images */ if (!isset($data['featured_photo']) || !filter_var($data['featured_photo'], FILTER_VALIDATE_INT) || $data['featured_photo'] == 1) { foreach ($this->yieldAlbums() as $Album) { foreach ($Album->getImages() as $Image) { if (!$Image->hidden) { $data['featured_photo'] = $Image->id; $this->db->update("gallery_mig_album", $data, array("id = ?" => $this->id)); deleteMemcacheObject($this->mckey); #setMemcacheObject($this->mckey, $data, strtotime("+1 year")); break; } } if (isset($data['featured_photo']) && filter_var($data['featured_photo'], FILTER_VALIDATE_INT)) { break; } } } return $data['featured_photo']; }
/** * Approve a story * @version 3.0 * @since Version 3.0 * * @param int|\Railpage\Users\User $user_id * * @return mixed */ public function approve($user_id) { if (!$this->id || !$this->db) { return false; } if ($user_id instanceof User) { $user_id = $user_id->id; } if (!filter_var($user_id, FILTER_VALIDATE_INT)) { $user_id = false; } if ($this->pending) { $query = "SELECT u.username AS informant, p.geo_lat, p.geo_lon, p.subject AS title, p.story AS hometext, p.storyext AS bodytext, p.topic, p.source FROM nuke_queue p, nuke_users u WHERE u.user_id = p.uid AND p.qid = ?"; if ($data = $this->db->fetchRow($query, $this->id)) { $data['approved'] = 1; $data['aid'] = $user_id; $data['time'] = new \Zend_Db_Expr('NOW()'); $this->db->insert("nuke_stories", $data); $id = $this->db->lastInsertId(); $this->db->delete("nuke_queue", array("qid = ?" => $this->id)); $this->id = $id; } } else { $this->approved = 1; $this->staff_user_id = $user_id; $this->setStaff(UserFactory::CreateUser($this->staff_user_id)); $this->date = new DateTime(); $this->commit(); } /** * Flush the Memcache store for the news topic */ if ($this->Topic instanceof Topic) { deleteMemcacheObject($this->Topic->mckey); } return true; }