<commit_before><?php
	/**
	 * News classes
	 * @since Version 3.0.1
	 * @version 3.9
	 * @author <NAME>
	 * @package Railpage
	 * @copyright Copyright (c) 2012 <NAME>
	 */
	 
	namespace Railpage\News;
	
	use Exception;
	use Railpage\Url;
	use Railpage\Module;
	
	// Make sure the parent class is loaded
		
	/**
	 * News topics
	 * @since Version 3.0.1
	 * @version 3.0.1
	 * @author <NAME>
	 * @package Railpage
	 * @copyright Copyright (c) 2012 <NAME>
	 */
	
	class Topic extends Base {
		
		/** 
		 * Topic ID
		 * @since Version 3.0.1
		 * @version 3.0.1
		 * @var int $topic_id
		 */
		
		public $id;
		
		/** 
		 * Topic alias
		 * @since Version 3.0.1
		 * @version 3.0.1
		 * @var string $topic_alias
		 */
		
		public $alias;
		
		/** 
		 * Topic title
		 * @since Version 3.0.1
		 * @version 3.0.1
		 * @var int $topic_title
		 */
		
		public $title;
		
		/** 
		 * Topic image
		 * @since Version 3.0.1
		 * @version 3.0.1
		 * @var int $topic_image
		 */
		
		public $image;
		
		/**
		 * Descriptive text
		 * @since Version 3.8
		 * @var string $desc
		 */
		
		public $desc;
		
		/** 
		 * Constructor
		 * @since Version 3.0.1
		 * @version 3.0.1
		 * @param object $db
		 * @param int|string $topic_id
		 */
		
		public function __construct($topic_id = false) {
			parent::__construct();
			
			if (filter_var($topic_id, FILTER_VALIDATE_INT)) {
				$this->id = $topic_id;
			} elseif (is_string($topic_id)) {
				if (!$id = getMemcacheObject(sprintf("railpage:news.topic.name=%s", $topic_id))) {
					$id = $this->db->fetchOne("SELECT topicid FROM nuke_topics WHERE topicname = ?", $topic_id);
					
					if ($id) {
						setMemcacheObject(sprintf("railpage:news.topic.name=%s", $topic_id), $id);
					}
				}
				
				if (isset($id) && $id !== false) {
					$this->id = $id;
				}
			}
			
			if ($this->id) {
				$this->mckey = sprintf("railpage:news.topic=%d", $this->id);
				
				if ($this->db instanceof \sql_db) {
					$query = "SELECT * FROM nuke_topics WHERE topicid = '".$this->db->real_escape_string($this->id)."'";
					
					if ($rs = $this->db->query($query)) {
						if ($rs->num_rows == 1) {
							$row = $rs->fetch_assoc(); 
							
							$this->id 		= $row['topicid']; 
							$this->alias	= $row['topicname']; 
							$this->title 	= $row['topictext'];
							$this->image	= $row['topicimage']; 
							$this->desc		= $row['desc'];
						}
					} else {
						trigger_error(__CLASS__.": Could not retrieve topic ID ".$topic_id); 
						trigger_error($this->db->error); 
						trigger_error($query); 
						
						return false;
					}
				} else {
					if (!$row = getMemcacheObject($this->mckey)) {
						$query = "SELECT * FROM nuke_topics WHERE topicid = ?";
						
						$row = $this->db_readonly->fetchRow($query, $this->id);
						
						setMemcacheObject($this->mckey, $row, strtotime("+6 months"));
					}
					
					$this->id 		= $row['topicid']; 
					$this->alias	= $row['topicname']; 
					$this->title 	= $row['topictext'];
					$this->image	= $row['topicimage']; 
					$this->desc		= isset($row['desc']) ? $row['desc'] : "";
				}
			}
			
			if (!empty($this->alias)) {
				$this->url = new Url(sprintf("%s/t/%s", $this->Module->url, $this->alias));
			}
		}
		
		/**
		 * Get stories from topic
		 * @version 3.0
		 * @since Version 3.0
		 * @return mixed
		 * @param int $page
		 * @param int $limit
		 * @param boolean $total
		 */
		 
		public function stories($page = 0, $limit = 25, $total = true) {
			$return = false;
			$mckey = "railpage:topic_id=" . $this->id . ".stories.page=" . $page . ".limit=" . $limit . ".total=" . (int)$total;
			$mcexp = strtotime("+1 hour"); 
			
			if (!$return = getMemcacheObject($mckey)) {
				// Get it from Sphinx
				
				$Sphinx = $this->getSphinx();
			
				$query = $Sphinx->select("*")
						->from("idx_news_article")
						->orderBy("story_time_unix", "DESC")
						->limit($page * $limit, $limit)
						->where("topic_id", "=", $this->id)
						->where("story_active", "=", 1);
						
				$matches = $query->execute(); 
				
				$meta = $Sphinx->query("SHOW META");
				$meta = $meta->execute();
				
				if (is_array($matches) && count($matches)) {
					$return = array(
						"total" => $meta[1]['Value'],
						"children" => array(),
						"page" => $page,
						"perpage" => $limit,
						"topic_id" => $this->id
					);
					
					foreach ($matches as $id => $row) {
						
						$row['time_relative'] = time2str($row['story_time_unix']);
						$row['time'] = $row['story_time'];
						$row['title'] = format_topictitle($row['story_title']);
						
						// Match the first sentence
						$line = explode("\n", $row['story_blurb']); 
						$row['firstline'] = preg_replace('/([^?!.]*.).*/', '\\1', strip_tags($line[0]));
							
						if (empty($row['story_slug'])) {
							$row['slug'] = $this->createSlug($row['story_id']); 
						}
						
						$row['url'] = $this->makePermaLink($row['story_slug']); 
						$row['hometext'] = $row['story_blurb'];
						$row['bodytext'] = $row['story_body'];
						$row['featured_image'] = $row['story_image'];
						$row['informant'] = $row['username'];
						
						$return['children'][$id] = $row;
					}
				}
			} 
			
			if (!isset($return) || $return === false || !is_array($return)) {
				$query = "SELECT SQL_CALC_FOUND_ROWS s.*, t.topicname, t.topicimage, t.topictext, u.user_id AS informant_id FROM nuke_stories AS s LEFT JOIN nuke_topics AS t ON s.topic = t.topicid LEFT JOIN nuke_users AS u ON s.informant = u.username WHERE s.topic = ? AND s.approved = ? ORDER BY s.time DESC LIMIT ?, ?"; 
				
				$return = array(); 
				$return['total'] 	= 0;
				$return['children'] = array(); 
				$return['page'] 	= $page; 
				$return['perpage'] 	= $limit; 
				$return['topic_id'] = $this->id;
				
				if ($result = $this->db_readonly->fetchAll($query, array($this->id, "1", $page * $limit, $limit))) {
					$return['total'] 	= $this->db_readonly->fetchOne("SELECT FOUND_ROWS() AS total"); 
					
					foreach ($result as $row) {
						if (function_exists("relative_date")) {
							$row['time_relative'] = relative_date(strtotime($row['time']));
						} else {
							$row['time_relative'] = $row['time'];
						}
						
						$row['title'] = format_topictitle($row['title']);
						
						// Match the first sentence
						$line = explode("\n", $row['hometext']); 
						$row['firstline'] 	= preg_replace('/([^?!.]*.).*/', '\\1', strip_tags($line[0]));
							
						if (empty($row['slug'])) {
							$row['slug'] = $this->createSlug($row['sid']); 
						}
						
						$row['url'] = $this->makePermaLink($row['slug']); 
						
						$return['children'][] = $row; 
					}
				}
				
				setMemcacheObject($mckey, $return, $mcexp);
			}
			
			return $return;
		}
		
		/**
		 * Alias for $this->stories()
		 * @since Version 3.8.7<commit_msg>Bugfix for news viewtopic<commit_after><?php
	/**
	 * News classes
	 * @since Version 3.0.1
	 * @version 3.9
	 * @author <NAME>
	 * @package Railpage
	 * @copyright Copyright (c) 2012 <NAME>
	 */
	 
	namespace Railpage\News;
	
	use Exception;
	use Railpage\Url;
	use Railpage\Module;
	
	// Make sure the parent class is loaded
		
	/**
	 * News topics
	 * @since Version 3.0.1
	 * @version 3.0.1
	 * @author <NAME>
	 * @package Railpage
	 * @copyright Copyright (c) 2012 <NAME>
	 */
	
	class Topic extends Base {
		
		/** 
		 * Topic ID
		 * @since Version 3.0.1
		 * @version 3.0.1
		 * @var int $topic_id
		 */
		
		public $id;
		
		/** 
		 * Topic alias
		 * @since Version 3.0.1
		 * @version 3.0.1
		 * @var string $topic_alias
		 */
		
		public $alias;
		
		/** 
		 * Topic title
		 * @since Version 3.0.1
		 * @version 3.0.1
		 * @var int $topic_title
		 */
		
		public $title;
		
		/** 
		 * Topic image
		 * @since Version 3.0.1
		 * @version 3.0.1
		 * @var int $topic_image
		 */
		
		public $image;
		
		/**
		 * Descriptive text
		 * @since Version 3.8
		 * @var string $desc
		 */
		
		public $desc;
		
		/** 
		 * Constructor
		 * @since Version 3.0.1
		 * @version 3.0.1
		 * @param object $db
		 * @param int|string $topic_id
		 */
		
		public function __construct($topic_id = false) {
			parent::__construct();
			
			if (filter_var($topic_id, FILTER_VALIDATE_INT)) {
				$this->id = $topic_id;
			} elseif (is_string($topic_id)) {
				if (!$id = getMemcacheObject(sprintf("railpage:news.topic.name=%s", $topic_id))) {
					$id = $this->db->fetchOne("SELECT topicid FROM nuke_topics WHERE topicname = ?", $topic_id);
					
					if ($id) {
						setMemcacheObject(sprintf("railpage:news.topic.name=%s", $topic_id), $id);
					}
				}
				
				if (isset($id) && $id !== false) {
					$this->id = $id;
				}
			}
			
			if ($this->id) {
				$this->mckey = sprintf("railpage:news.topic=%d", $this->id);
				
				if ($this->db instanceof \sql_db) {
					$query = "SELECT * FROM nuke_topics WHERE topicid = '".$this->db->real_escape_string($this->id)."'";
					
					if ($rs = $this->db->query($query)) {
						if ($rs->num_rows == 1) {
							$row = $rs->fetch_assoc(); 
							
							$this->id 		= $row['topicid']; 
							$this->alias	= $row['topicname']; 
							$this->title 	= $row['topictext'];
							$this->image	= $row['topicimage']; 
							$this->desc		= $row['desc'];
						}
					} else {
						trigger_error(__CLASS__.": Could not retrieve topic ID ".$topic_id); 
						trigger_error($this->db->error); 
						trigger_error($query); 
						
						return false;
					}
				} else {
					if (!$row = getMemcacheObject($this->mckey)) {
						$query = "SELECT * FROM nuke_topics WHERE topicid = ?";
						
						$row = $this->db_readonly->fetchRow($query, $this->id);
						
						setMemcacheObject($this->mckey, $row, strtotime("+6 months"));
					}
					
					$this->id 		= $row['topicid']; 
					$this->alias	= $row['topicname']; 
					$this->title 	= $row['topictext'];
					$this->image	= $row['topicimage']; 
					$this->desc		= isset($row['desc']) ? $row['desc'] : "";
				}
			}
			
			if (!empty($this->alias)) {
				$this->url = new Url(sprintf("%s/t/%s", $this->Module->url, $this->alias));
			}
		}
		
		/**
		 * Get stories from topic
		 * @version 3.0
		 * @since Version 3.0
		 * @return mixed
		 * @param int $page
		 * @param int $limit
		 * @param boolean $total
		 */
		 
		public function stories($page = 0, $limit = 25, $total = true) {
			$return = false;
			$mckey = "railpage:topic_id=" . $this->id . ".stories.page=" . $page . ".limit=" . $limit . ".total=" . (int)$total;
			$mcexp = strtotime("+1 hour"); 
			
			if (!$return = getMemcacheObject($mckey)) {
				// Get it from Sphinx
				
				$Sphinx = $this->getSphinx();
			
				$query = $Sphinx->select("*")
						->from("idx_news_article")
						->orderBy("story_time_unix", "DESC")
						->limit($page * $limit, $limit)
						->where("topic_id", "=", $this->id)
						->where("story_active", "=", 1);
						
				$matches = $query->execute(); 
				
				$meta = $Sphinx->query("SHOW META");
				$meta = $meta->execute();
				
				if (is_array($matches) && count($matches)) {
					$return = array(
						"total" => $meta[1]['Value'],
						"children" => array(),
						"page" => $page,
						"perpage" => $limit,
						"topic_id" => $this->id
					);
					
					foreach ($matches as $id => $row) {
						
						$row['time_relative'] = time2str($row['story_time_unix']);
						$row['time'] = $row['story_time'];
						$row['title'] = format_topictitle($row['story_title']);
						
						// Match the first sentence
						$line = explode("\n", $row['story_blurb']); 
						$row['firstline'] = preg_replace('/([^?!.]*.).*/', '\\1', strip_tags($line[0]));
							
						if (empty($row['story_slug'])) {
							$row['slug'] = $this->createSlug($row['story_id']); 
						}
						
						$row['url'] = $this->makePermaLink($row['story_slug']); 
						$row['hometext'] = $row['story_blurb'];
						$row['bodytext'] = $row['story_body'];
						$row['featured_image'] = $row['story_image'];
						$row['informant'] = $row['username'];
						
						$return['children'][$id] = $row;
					}
				}
			} 
			
			if (!isset($return) || $return === false || !is_array($return)) {
				$query = "SELECT SQL_CALC_FOUND_ROWS s.*, t.topicname, t.topicimage, t.topictext, u.user_id AS informant_id FROM nuke_stories AS s LEFT JOIN nuke_topics AS t ON s.topic = t.topicid LEFT JOIN nuke_users AS u ON s.informant = u.username WHERE s.topic = ? AND s.approved = ? ORDER BY s.time DESC LIMIT ?, ?"; 
				
				$return = array(); 
				$return['total'] 	= 0;
				$return['children'] = array(); 
				$return['page'] 	= $page; 
				$return['perpage'] 	= $limit; 
				$return['topic_id'] = $this->id;
				
				if ($result = $this->db_readonly->fetchAll($query, array($this->id, "1", $page * $limit, $limit))) {
					$return['total'] 	= $this->db_readonly->fetchOne("SELECT FOUND_ROWS() AS total"); 
					
					foreach ($result as $row) {
						if (function_exists("relative_date")) {
							$row['time_relative'] = relative_date(strtotime($row['time']));
						} else {
							$row['time_relative'] = $row['time'];
						}
						
						$row['title'] = format_topictitle($row['title']);
						
						// Match the first sentence
						$line = explode("\n", $row['hometext']); 
						$row['firstline'] 	= preg_replace('/([^?!.]*.).*/', '\\1', strip_tags($line[0]));
							
						if (empty($row['slug'])) {
							$row['slug'] = $this->createSlug($row['sid']); 
						}
						
						$row['url'] = $this->makePermaLink($row['slug']); 
						$row['story_id'] = $row['sid'];
						
						$return['children'][] = $row; 
					}
				}
				
				setMemcacheObject($mckey, $return, $mcexp);
			}
			
			return $return;
		}
		
		/**
		 * Alias for $this->stories()
		 * @since Version 3.8.7
{'fraction_non_alphanumeric': 0.20205278592375367, 'fraction_numerical': 0.010703812316715543, 'mean_word_length': 2.4316981132075473, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 145, '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': '5787413', 'n_tokens_mistral': 6515, 'n_tokens_neox': 4776, 'n_words': 950}