<commit_before><?php defined('SYSPATH') OR die('No direct script access.');

/**
 *
 * @package	BoomCMS
 * @category	Models
 * @author	<NAME>
 * @copyright	Hoop Associates
 *
 */
class Boom_Model_Page extends Model_Taggable
{
	/**
	 * Properties to create relationships with Kohana's ORM
	 */
	protected $_belongs_to = array(
		'mptt'		=>	array('model' => 'Page_MPTT', 'foreign_key' => 'id'),
	);

	protected $_created_column = array(
		'column'	=>	'created_time',
		'format'	=>	TRUE,
	);

	protected $_has_one = array(
		'version'		=>	array('model' => 'Page_Version', 'foreign_key' => 'page_id'),
	);

	protected $_has_many = array(
		'versions'	=> array('model' => 'Page_Version', 'foreign_key' => 'page_id'),
		'urls'		=> array('model' => 'Page_URL', 'foreign_key' => 'page_id'),
		'tags'	=> array('model' => 'Tag', 'through' => 'pages_tags'),
	);

	protected $_table_columns = array(
		'id'						=>	'',
		'sequence'					=>	'',
		'visible'					=>	'',
		'visible_from'				=>	'',
		'visible_to'				=>	'',
		'internal_name'				=>	'',
		'external_indexing'			=>	'',
		'internal_indexing'			=>	'',
		'visible_in_nav'				=>	true,
		'visible_in_nav_cms'			=>	true,
		'children_visible_in_nav'		=>	true,
		'children_visible_in_nav_cms'	=>	true,
		'children_template_id'		=>	'',
		'children_url_prefix'			=>	'',
		'children_ordering_policy'		=>	'',
		'children_prompt_for_template'	=>	'',
		'grandchild_template_id'		=>	'',
		'keywords'				=>	'',
		'description'				=>	'',
		'created_by'				=>	'',
		'created_time'				=>	'',
		'primary_uri'				=>	'',
	);

	protected $_table_name = 'pages';

	/**
	 * Child ordering policy value for manual
	 * @var	integer
	 */
	const CHILD_ORDER_MANUAL = 1;

	/**
	 * Child ordering policy value for alphabetic
	 * @var	integer
	 */
	const CHILD_ORDER_ALPHABETIC = 2;

	/**
	 * Child ordering policy value for date
	 * @var	integer
	 */
	const CHILD_ORDER_DATE = 4;

	/**
	 * Child ordering policy for ascending.
	 * @var	integer
	 */
	const CHILD_ORDER_ASC = 8;

	/**
	 * Child ordering policy for descending.
	 * @var	integer
	 */
	const CHILD_ORDER_DESC = 16;

	/**
	 * Cached result for self::url()
	 *
	 * @access	private
	 * @var		string
	 */
	private $_url;

	/**
	 * Updates a page's children with the same values as the current page.
	 *
	 * @param array $columns
	 * @param array $expected
	 *
	 * @return \Boom_Model_Page
	 * @throws Exception
	 */
	public function cascade_to_children(array $columns, array $expected = array())
	{
		// Page must be loaded.
		if ( ! $this->_loaded)
		{
			throw new Exception("Cannot call ".__CLASS__."::".__METHOD__." on an unloaded object.");
		}

		// If no expected columns have been given then use all the columns
		// Except ID and internal_name which must be unique.
		if (empty($expected))
		{
			$expected = array_diff(array_keys($this->_object), array('id', 'internal_name'));
		}

		// Don't update any columns which we aren't expecting.
		$columns = array_intersect($expected, $columns);

		// Get the values from the current object.
		$values = Arr::extract($this->_object, $columns);

		/**
		 *  The template_id property is a special case.
		 *
		 * If the parent has a child_template_id property set then we should use that.
		 * Otherwise we should use the parent's template_id property.
		 */
		if (isset($values['template_id']))
		{
			$values['template_id'] = ($this->_object['children_template_id'] == 0)? $this->version()->template_id : $this->_object['children_template_id'];
		}

		if ( ! empty($values))
		{
			// Run the query to update the children of the current page with their new values.
			DB::update('pages')
				->where('id', 'IN', DB::select('id')
					->from('page_mptt')
					->where('parent_id', '=', $this->id)
				)
				->set($values)
				->execute($this->_db);
		}

		return $this;
	}

	/**
	 * Delete any feature boxes which have this page as the target.
	 *
	 */
	public function delete_from_feature_boxes()
	{
		DB::delete('pages_chunks')
			->where('type', '=', Chunk::FEATURE)
			->where('chunk_id', 'IN',
				DB::select('id')
					->from('chunk_features')
					->where('target_page_id', '=', $this->id)
			)
			->execute($this->db);

		DB::delete('chunk_features')
			->where('target_page_id', '=', $this->id)
			->execute($this->db);
	}

	public function delete_from_linksets()
	{
		DB::delete('chunk_linkset_links')
			->where('target_page_id', '=', $this->id)
			->execute($this->id);
	}

	public function get_author_names_as_string()
	{
		$authors = $this->get_tags_with_name_like('Author/%');

		if ( ! empty($authors))
		{
			foreach ($authors as & $author)
			{
				$author = htmlentities(str_replace('Author/', '', $author->name), ENT_QUOTES);
			}
<commit_msg>Delete page from linksets when it's deleted<commit_after><?php defined('SYSPATH') OR die('No direct script access.');

/**
 *
 * @package	BoomCMS
 * @category	Models
 * @author	<NAME>
 * @copyright	Hoop Associates
 *
 */
class Boom_Model_Page extends Model_Taggable
{
	/**
	 * Properties to create relationships with Kohana's ORM
	 */
	protected $_belongs_to = array(
		'mptt'		=>	array('model' => 'Page_MPTT', 'foreign_key' => 'id'),
	);

	protected $_created_column = array(
		'column'	=>	'created_time',
		'format'	=>	TRUE,
	);

	protected $_has_one = array(
		'version'		=>	array('model' => 'Page_Version', 'foreign_key' => 'page_id'),
	);

	protected $_has_many = array(
		'versions'	=> array('model' => 'Page_Version', 'foreign_key' => 'page_id'),
		'urls'		=> array('model' => 'Page_URL', 'foreign_key' => 'page_id'),
		'tags'	=> array('model' => 'Tag', 'through' => 'pages_tags'),
	);

	protected $_table_columns = array(
		'id'						=>	'',
		'sequence'					=>	'',
		'visible'					=>	'',
		'visible_from'				=>	'',
		'visible_to'				=>	'',
		'internal_name'				=>	'',
		'external_indexing'			=>	'',
		'internal_indexing'			=>	'',
		'visible_in_nav'				=>	true,
		'visible_in_nav_cms'			=>	true,
		'children_visible_in_nav'		=>	true,
		'children_visible_in_nav_cms'	=>	true,
		'children_template_id'		=>	'',
		'children_url_prefix'			=>	'',
		'children_ordering_policy'		=>	'',
		'children_prompt_for_template'	=>	'',
		'grandchild_template_id'		=>	'',
		'keywords'				=>	'',
		'description'				=>	'',
		'created_by'				=>	'',
		'created_time'				=>	'',
		'primary_uri'				=>	'',
	);

	protected $_table_name = 'pages';

	/**
	 * Child ordering policy value for manual
	 * @var	integer
	 */
	const CHILD_ORDER_MANUAL = 1;

	/**
	 * Child ordering policy value for alphabetic
	 * @var	integer
	 */
	const CHILD_ORDER_ALPHABETIC = 2;

	/**
	 * Child ordering policy value for date
	 * @var	integer
	 */
	const CHILD_ORDER_DATE = 4;

	/**
	 * Child ordering policy for ascending.
	 * @var	integer
	 */
	const CHILD_ORDER_ASC = 8;

	/**
	 * Child ordering policy for descending.
	 * @var	integer
	 */
	const CHILD_ORDER_DESC = 16;

	/**
	 * Cached result for self::url()
	 *
	 * @access	private
	 * @var		string
	 */
	private $_url;

	/**
	 * Updates a page's children with the same values as the current page.
	 *
	 * @param array $columns
	 * @param array $expected
	 *
	 * @return \Boom_Model_Page
	 * @throws Exception
	 */
	public function cascade_to_children(array $columns, array $expected = array())
	{
		// Page must be loaded.
		if ( ! $this->_loaded)
		{
			throw new Exception("Cannot call ".__CLASS__."::".__METHOD__." on an unloaded object.");
		}

		// If no expected columns have been given then use all the columns
		// Except ID and internal_name which must be unique.
		if (empty($expected))
		{
			$expected = array_diff(array_keys($this->_object), array('id', 'internal_name'));
		}

		// Don't update any columns which we aren't expecting.
		$columns = array_intersect($expected, $columns);

		// Get the values from the current object.
		$values = Arr::extract($this->_object, $columns);

		/**
		 *  The template_id property is a special case.
		 *
		 * If the parent has a child_template_id property set then we should use that.
		 * Otherwise we should use the parent's template_id property.
		 */
		if (isset($values['template_id']))
		{
			$values['template_id'] = ($this->_object['children_template_id'] == 0)? $this->version()->template_id : $this->_object['children_template_id'];
		}

		if ( ! empty($values))
		{
			// Run the query to update the children of the current page with their new values.
			DB::update('pages')
				->where('id', 'IN', DB::select('id')
					->from('page_mptt')
					->where('parent_id', '=', $this->id)
				)
				->set($values)
				->execute($this->_db);
		}

		return $this;
	}

	/**
	 * Delete any feature boxes which have this page as the target.
	 *
	 */
	public function delete_from_feature_boxes()
	{
		DB::delete('page_chunks')
			->where('type', '=', Chunk::FEATURE)
			->where('chunk_id', 'IN',
				DB::select('id')
					->from('chunk_features')
					->where('target_page_id', '=', $this->id)
			)
			->execute($this->_db);

		DB::delete('chunk_features')
			->where('target_page_id', '=', $this->id)
			->execute($this->_db);
	}

	public function delete_from_linksets()
	{
		DB::delete('chunk_linkset_links')
			->where('target_page_id', '=', $this->id)
			->execute($this->_db);
	}

	public function get_author_names_as_string()
	{
		$authors = $this->get_tags_with_name_like('Author/%');

		if ( ! empty($authors))
		{
			foreach ($authors as & $author)
			{
				$author = htmlentities(str_replace('Author/', '', $author->name), ENT_QUOTES);
			}

{'fraction_non_alphanumeric': 0.17456438589035275, 'fraction_numerical': 0.0014874628134296643, 'mean_word_length': 3.505983724269986, 'pattern_counts': {'":': 2, '<': 7, '<?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': '212300', 'n_tokens_mistral': 3792, 'n_tokens_neox': 3302, 'n_words': 740}