<commit_before><?php
	/**
	 * News classes
	 * @since Version 3.0.1
	 * @version 3.0.1
	 * @author <NAME>
	 * @package Railpage
	 * @copyright Copyright (c) 2012 <NAME>
	 */
	 
	namespace Railpage\News;
	
	use Railpage\Module;
	use Exception;
	use DateTime;
	
	// Make sure the parent class is loaded
	#require_once(__DIR__ . DS . "class.news.base.php");
		
	/**
	 * News class
	 * @author <NAME>
	 * @version 3.0.1
	 * @package Railpage
	 * @copyright Copyright (c) 2011 <NAME>
	 * @since Version 3.0
	 */
	
	class News extends Base {
		
		/**
		 * Get story
		 * @version 3.0
		 * @since Version 3.0
		 * @return mixed
		 * @param int $id
		 * @param boolean $pending
		 */
		 
		public function getStory($id = false, $pending = false) {
			if (!$id || !$this->db) {
				return false;
			}
			
			$return = false;
			
			if ($pending) {
				// Get story from pending table
				$query = "SELECT u.username, p.qid as sid, p.uname as informant, p.subject as title, p.story as hometext, p.storyext as bodytext, p.topic, p.source, t.topicname, t.topicimage, t.topictext, p.timestamp as time FROM nuke_users u, nuke_queue p, nuke_topics t WHERE p.topic = t.topicid AND p.uid = u.user_id AND p.qid = '".$this->db->real_escape_string($id)."'";
			} else {
				$query = "SELECT s.*, t.topicname, t.topicimage, t.topictext, t.topicid FROM nuke_stories s, nuke_topics t WHERE s.topic = t.topicid AND s.sid = '".$this->db->real_escape_string($id)."'";
			}
			
			if ($rs = $this->db->query($query)) {
				$return = $rs->fetch_assoc(); 
				
				$whitespace_find = array("<p> </p>", "<p></p>", "<p>&nbsp;</p>");
				$whitespace_replace = array("", "", ""); 
		
				$return['hometext'] = str_replace($whitespace_find, $whitespace_replace, $return['hometext']); 
				$return['bodytext'] = str_replace($whitespace_find, $whitespace_replace, $return['bodytext']); 
			} else {
				trigger_error("News: unable to fetch story id ".$id); 
				trigger_error($this->db->error); 
			}
			
			return $return;
		}
		
		/**
		 * Get stories from topic
		 * @version 3.0
		 * @since Version 3.0
		 * @return mixed
		 * @param int $id
		 * @param int $page
		 * @param int $limit
		 * @param boolean $total
		 */
		 
		public function getStoriesFromTopic($id = false, $page = 0, $limit = 25, $total = true) {
			if (!$id || !$this->db) {
				return false;
			}
			
			$return = false;
			$query 	= "SELECT SQL_CALC_FOUND_ROWS s.*, t.topicname, t.topicimage, t.topictext, u.user_id AS informant_id FROM nuke_stories s, nuke_topics t, nuke_users u WHERE s.informant = u.username AND s.topic = t.topicid AND t.topicid = ".$this->db->real_escape_string($id)." ORDER BY s.time DESC LIMIT ".$this->db->real_escape_string($page * $limit).", ".$this->db->real_escape_string($limit); 
			
			if ($rs = $this->db->query($query)) {
				$total = $this->db->query("SELECT FOUND_ROWS() AS total"); 
				$total = $total->fetch_assoc(); 
				
				$return = array(); 
				$return['topic_id'] = $id;
				$return['total'] = $total['total']; 
				$return['page'] = $page; 
				$return['perpage'] = $limit; 
				
				require_once("includes/functions.php"); 
				
				while ($row = $rs->fetch_assoc()) {
					if (function_exists("relative_date")) {
						$row['time_relative'] = relative_date(strtotime($row['time']));
					} else {
						$row['time_relative'] = $row['time'];
					}
					
					$return['children'][] = $row; 
				}
			} else {
				trigger_error("News: unable to fetch stories from topic");
				trigger_error($this->db->error); 
				trigger_error($query); 
			}
			
			return $return;
		}
		
		/**
		 * Get JSON object for a news article
		 * @since Version 3.8.7
		 * @param int $article_id
		 * @return string
		 */
		
		public static function getArticleJSON($article_id) {
			$key = sprintf("json:railpage.news.article=%d", $article_id);
			
			if (!$json = getMemcacheObject($key)) {
				$Article = new Article($article_id);
				
				if (empty($Article->getParagraphs()) && !empty($Article->source)) {
					$Article->url->url = $Article->source;
				}
				
				$json = $Article->makeJSON();
			}
			
			return $json;
		}
		
		/**
		 * Wild stab in the dark guess at one of our topics based on the topic sent from an RSS feed
		 * @since Version 3.9
		 * @param string|array $topic
		 * @return \Railpage\News\Topic
		 */
		
		static public function guessTopic($topic) {
			
			/**
			 * Normalise the topic name
			 */
			
			if (is_array($topic)) {
				$topic = implode(", ", $topic);
			}
			
			$topic = trim($topic);
			
			if (preg_match("/tasrail/i", $topic)) {
				$topic = "tas";
			}
			
			if (preg_match("/puffingbillyworkshops/i", $topic)) {
				$topic = "narrow-gauge";
			}
			
			/**
			 * Attempt to find the topic in our existing list
			 */
			
			$Topic = new Topic($topic);
			
			if (filter_var($Topic->id, FILTER_VALIDATE_INT)) {
				return $Topic;
			}
			
			#printArray($topic);die;
			<commit_msg>Minor update to News::guessTopic()<commit_after><?php
	/**
	 * News classes
	 * @since Version 3.0.1
	 * @version 3.0.1
	 * @author <NAME>
	 * @package Railpage
	 * @copyright Copyright (c) 2012 <NAME>
	 */
	 
	namespace Railpage\News;
	
	use Railpage\Module;
	use Exception;
	use DateTime;
	
	// Make sure the parent class is loaded
	#require_once(__DIR__ . DS . "class.news.base.php");
		
	/**
	 * News class
	 * @author <NAME>
	 * @version 3.0.1
	 * @package Railpage
	 * @copyright Copyright (c) 2011 <NAME>
	 * @since Version 3.0
	 */
	
	class News extends Base {
		
		/**
		 * Get story
		 * @version 3.0
		 * @since Version 3.0
		 * @return mixed
		 * @param int $id
		 * @param boolean $pending
		 */
		 
		public function getStory($id = false, $pending = false) {
			if (!$id || !$this->db) {
				return false;
			}
			
			$return = false;
			
			if ($pending) {
				// Get story from pending table
				$query = "SELECT u.username, p.qid as sid, p.uname as informant, p.subject as title, p.story as hometext, p.storyext as bodytext, p.topic, p.source, t.topicname, t.topicimage, t.topictext, p.timestamp as time FROM nuke_users u, nuke_queue p, nuke_topics t WHERE p.topic = t.topicid AND p.uid = u.user_id AND p.qid = '".$this->db->real_escape_string($id)."'";
			} else {
				$query = "SELECT s.*, t.topicname, t.topicimage, t.topictext, t.topicid FROM nuke_stories s, nuke_topics t WHERE s.topic = t.topicid AND s.sid = '".$this->db->real_escape_string($id)."'";
			}
			
			if ($rs = $this->db->query($query)) {
				$return = $rs->fetch_assoc(); 
				
				$whitespace_find = array("<p> </p>", "<p></p>", "<p>&nbsp;</p>");
				$whitespace_replace = array("", "", ""); 
		
				$return['hometext'] = str_replace($whitespace_find, $whitespace_replace, $return['hometext']); 
				$return['bodytext'] = str_replace($whitespace_find, $whitespace_replace, $return['bodytext']); 
			} else {
				trigger_error("News: unable to fetch story id ".$id); 
				trigger_error($this->db->error); 
			}
			
			return $return;
		}
		
		/**
		 * Get stories from topic
		 * @version 3.0
		 * @since Version 3.0
		 * @return mixed
		 * @param int $id
		 * @param int $page
		 * @param int $limit
		 * @param boolean $total
		 */
		 
		public function getStoriesFromTopic($id = false, $page = 0, $limit = 25, $total = true) {
			if (!$id || !$this->db) {
				return false;
			}
			
			$return = false;
			$query 	= "SELECT SQL_CALC_FOUND_ROWS s.*, t.topicname, t.topicimage, t.topictext, u.user_id AS informant_id FROM nuke_stories s, nuke_topics t, nuke_users u WHERE s.informant = u.username AND s.topic = t.topicid AND t.topicid = ".$this->db->real_escape_string($id)." ORDER BY s.time DESC LIMIT ".$this->db->real_escape_string($page * $limit).", ".$this->db->real_escape_string($limit); 
			
			if ($rs = $this->db->query($query)) {
				$total = $this->db->query("SELECT FOUND_ROWS() AS total"); 
				$total = $total->fetch_assoc(); 
				
				$return = array(); 
				$return['topic_id'] = $id;
				$return['total'] = $total['total']; 
				$return['page'] = $page; 
				$return['perpage'] = $limit; 
				
				require_once("includes/functions.php"); 
				
				while ($row = $rs->fetch_assoc()) {
					if (function_exists("relative_date")) {
						$row['time_relative'] = relative_date(strtotime($row['time']));
					} else {
						$row['time_relative'] = $row['time'];
					}
					
					$return['children'][] = $row; 
				}
			} else {
				trigger_error("News: unable to fetch stories from topic");
				trigger_error($this->db->error); 
				trigger_error($query); 
			}
			
			return $return;
		}
		
		/**
		 * Get JSON object for a news article
		 * @since Version 3.8.7
		 * @param int $article_id
		 * @return string
		 */
		
		public static function getArticleJSON($article_id) {
			$key = sprintf("json:railpage.news.article=%d", $article_id);
			
			if (!$json = getMemcacheObject($key)) {
				$Article = new Article($article_id);
				
				if (empty($Article->getParagraphs()) && !empty($Article->source)) {
					$Article->url->url = $Article->source;
				}
				
				$json = $Article->makeJSON();
			}
			
			return $json;
		}
		
		/**
		 * Wild stab in the dark guess at one of our topics based on the topic sent from an RSS feed
		 * @since Version 3.9
		 * @param string|array $topic
		 * @return \Railpage\News\Topic
		 */
		
		static public function guessTopic($topic) {
			
			/**
			 * Normalise the topic name
			 */
			
			if (is_array($topic)) {
				$topic = implode(", ", $topic);
			}
			
			$topic = trim($topic);
			
			if (preg_match("/tasrail/i", $topic)) {
				$topic = "tas";
			}
			
			if (preg_match("/puffingbillyworkshops/i", $topic)) {
				$topic = "narrow-gauge";
			}
			
			if (preg_match("/yarratrams/i", $topic)) {
				$topic = "tram";
			}
			
			/**
			 * Attempt to find the topic in our existing list
			 */
			
			$Topic = new Topic($topic);
			
			if (filter_var($Topic->id, FILTER_VALIDATE_INT)) {
				return $Topic;
			}
			
			#printArray($topic);die;
			
{'fraction_non_alphanumeric': 0.16938011932450198, 'fraction_numerical': 0.007078572150874709, 'mean_word_length': 2.786370597243492, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6465097', 'n_tokens_mistral': 4284, 'n_tokens_neox': 3379, 'n_words': 826}