<commit_before><?php
	/**
	 * @package content
	 */

	/**
	 * The Publish page is where the majority of an Authors time will
	 * be spent in Symphony with adding, editing and removing entries
	 * from Sections. This Page controls the entries table as well as
	 * the Entry creation screens.
	 */
	require_once(TOOLKIT . '/class.administrationpage.php');
	require_once(TOOLKIT . '/class.entrymanager.php');
	require_once(TOOLKIT . '/class.sectionmanager.php');
	require_once(CONTENT . '/class.sortable.php');

	Class contentPublish extends AdministrationPage{

		public $_errors = array();

		public function sort(&$sort, &$order, $params) {
			$section = $params['current-section'];

			// If `?unsort` is appended to the URL, then sorting information are reverted
			// to their defaults
			if($params['unsort']) {
				$section->setSortingField($section->getDefaultSortingField(), false);
				$section->setSortingOrder('asc');

				redirect(Administration::instance()->getCurrentPageURL());
			}

			// By default, sorting information are retrieved from
			// the filesystem and stored inside the `Configuration` object
			if(is_null($sort) && is_null($order)) {
				$sort = $section->getSortingField();
				$order = $section->getSortingOrder();

				// Set the sorting in the `EntryManager` for subsequent use
				EntryManager::setFetchSorting($sort, $order);
			}
			else {
				// Ensure that this field is infact sortable, otherwise
				// fallback to IDs
				if(($field = FieldManager::fetch($sort)) instanceof Field && !$field->isSortable()) {
					$sort = $section->getDefaultSortingField();
				}

				// If the sort order or direction differs from what is saved,
				// update the config file and reload the page
				if($sort != $section->getSortingField() || $order != $section->getSortingOrder()){
					$section->setSortingField($sort, false);
					$section->setSortingOrder($order);

					redirect(Administration::instance()->getCurrentPageURL() . $params['filters']);
				}
			}

		}

		public function action(){
			$this->__switchboard('action');
		}

		public function __switchboard($type='view'){

			$function = ($type == 'action' ? '__action' : '__view') . ucfirst($this->_context['page']);

			if(!method_exists($this, $function)) {
				// If there is no action function, just return without doing anything
				if($type == 'action') return;

				Administration::instance()->errorPageNotFound();
			}

			$this->$function();
		}

		public function view(){
			$this->__switchboard();
		}

		public function __viewIndex(){
			if(!$section_id = SectionManager::fetchIDFromHandle($this->_context['section_handle'])) {
				Administration::instance()->throwCustomError(
					__('The Section, %s, could not be found.', array('<code>' . $this->_context['section_handle'] . '</code>')),
					__('Unknown Section'),
					Page::HTTP_STATUS_NOT_FOUND
				);
			}

			$section = SectionManager::fetch($section_id);

			$this->setPageType('table');
			$this->setTitle(__('%1$s &ndash; %2$s', array($section->get('name'), __('Symphony'))));

			$filters = array();
			$filter_querystring = $prepopulate_querystring = $where = $joins = NULL;
			$current_page = (isset($_REQUEST['pg']) && is_numeric($_REQUEST['pg']) ? max(1, intval($_REQUEST['pg'])) : 1);

			if(isset($_REQUEST['filter'])) {
				// legacy implementation, convert single filter to an array
				// split string in the form ?filter=handle:value
				if(!is_array($_REQUEST['filter'])) {
					list($field_handle, $filter_value) = explode(':', $_REQUEST['filter'], 2);
					$filters[$field_handle] = rawurldecode($filter_value);
				} else {
					$filters = $_REQUEST['filter'];
				}

				foreach($filters as $handle => $value) {
					$field_id = FieldManager::fetchFieldIDFromElementName(
						Symphony::Database()->cleanValue($handle),
						$section->get('id')
					);

					$field = FieldManager::fetch($field_id);

					if($field instanceof Field) {
						// For deprecated reasons, call the old, typo'd function name until the switch to the
						// properly named buildDSRetrievalSQL function.
						$field->buildDSRetrivalSQL(array($value), $joins, $where, false);
						$filter_querystring .= sprintf("filter[%s]=%s&amp;", $handle, rawurlencode($value));
						$prepopulate_querystring .= sprintf("prepopulate[%d]=%s&amp;", $field_id, rawurlencode($value));
					}
					else {
						unset($filters[$handle]);
					}
				}

				$filter_querystring = preg_replace("/&amp;$/", '', $filter_querystring);
				$prepopulate_querystring = preg_replace("/&amp;$/", '', $prepopulate_querystring);
			}

			Sortable::initialize($this, $entries, $sort, $order, array(
				'current-section' => $section,
				'filters' => ($filter_querystring ? "&amp;" . $filter_querystring : ''),
				'unsort' => isset($_REQUEST['unsort'])
			));

			$this->Form->setAttribute('action', Administration::instance()->getCurrentPageURL(). '?pg=' . $current_page.($filter_querystring ? "&amp;" . $filter_querystring : ''));

			$subheading_buttons = array(
				Widget::Anchor(__('Create New'), Administration::instance()->getCurrentPageURL().'new/'.($prepopulate_querystring ? '?' . $prepopulate_querystring : ''), __('Create a new entry'), 'create button', NULL, array('accesskey' => 'c'))
			);

			// Only show the Edit Section button if the Author is a developer. #938 ^BA
			if(Administration::instance()->Author->isDeveloper()) {
				array_unshift($subheading_buttons, Widget::Anchor(__('Edit Section'), SYMPHONY_URL . '/blueprints/sections/edit/' . $section_id . '/', __('Edit Section Configuration'), 'button'));
			}

			$this->appendSubheading($section->get('name'), $subheading_buttons);

			// Check that the filtered query fails that the filter is dropped and an
			// error is logged. #841 ^BA
			try {
				$entries = EntryManager::fetchByPage($current_page, $section_id, Symphony::Configuration()->get('pagination_maximum_rows', 'symphony'), $where, $joins);
			}
			catch(DatabaseException $ex) {
				$this->pageAlert(__('An error occurred while retrieving filtered entries. Showing all entries instead.'), Alert::ERROR);
				$filter_querystring = null;
				Symphony::Log()->pushToLog(sprintf(
						'%s - %s%s%s',
						$section->get('name') . ' Publish Index',
						$ex->getMessage(),
						($ex->getFile() ? " in file " .  $ex->getFile() : null),
						($ex->getLine() ? " on line " . $ex->getLine() : null)
					),
					E_NOTICE, true
				);
				$entries = EntryManager::fetchByPage($current_page, $section_id, Symphony::Configuration()->get('pagination_maximum_rows', 'symphony'));
			}<commit_msg>Add new delegate into contentPublish<commit_after><?php
	/**
	 * @package content
	 */

	/**
	 * The Publish page is where the majority of an Authors time will
	 * be spent in Symphony with adding, editing and removing entries
	 * from Sections. This Page controls the entries table as well as
	 * the Entry creation screens.
	 */
	require_once(TOOLKIT . '/class.administrationpage.php');
	require_once(TOOLKIT . '/class.entrymanager.php');
	require_once(TOOLKIT . '/class.sectionmanager.php');
	require_once(CONTENT . '/class.sortable.php');

	Class contentPublish extends AdministrationPage{

		public $_errors = array();

		public function sort(&$sort, &$order, $params) {
			$section = $params['current-section'];

			// If `?unsort` is appended to the URL, then sorting information are reverted
			// to their defaults
			if($params['unsort']) {
				$section->setSortingField($section->getDefaultSortingField(), false);
				$section->setSortingOrder('asc');

				redirect(Administration::instance()->getCurrentPageURL());
			}

			// By default, sorting information are retrieved from
			// the filesystem and stored inside the `Configuration` object
			if(is_null($sort) && is_null($order)) {
				$sort = $section->getSortingField();
				$order = $section->getSortingOrder();

				// Set the sorting in the `EntryManager` for subsequent use
				EntryManager::setFetchSorting($sort, $order);
			}
			else {
				// Ensure that this field is infact sortable, otherwise
				// fallback to IDs
				if(($field = FieldManager::fetch($sort)) instanceof Field && !$field->isSortable()) {
					$sort = $section->getDefaultSortingField();
				}

				// If the sort order or direction differs from what is saved,
				// update the config file and reload the page
				if($sort != $section->getSortingField() || $order != $section->getSortingOrder()){
					$section->setSortingField($sort, false);
					$section->setSortingOrder($order);

					redirect(Administration::instance()->getCurrentPageURL() . $params['filters']);
				}
			}

		}

		public function action(){
			$this->__switchboard('action');
		}

		public function __switchboard($type='view'){

			$function = ($type == 'action' ? '__action' : '__view') . ucfirst($this->_context['page']);

			if(!method_exists($this, $function)) {
				// If there is no action function, just return without doing anything
				if($type == 'action') return;

				Administration::instance()->errorPageNotFound();
			}

			$this->$function();
		}

		public function view(){
			$this->__switchboard();
		}

		public function __viewIndex(){
			if(!$section_id = SectionManager::fetchIDFromHandle($this->_context['section_handle'])) {
				Administration::instance()->throwCustomError(
					__('The Section, %s, could not be found.', array('<code>' . $this->_context['section_handle'] . '</code>')),
					__('Unknown Section'),
					Page::HTTP_STATUS_NOT_FOUND
				);
			}

			$section = SectionManager::fetch($section_id);

			$this->setPageType('table');
			$this->setTitle(__('%1$s &ndash; %2$s', array($section->get('name'), __('Symphony'))));

			$filters = array();
			$filter_querystring = $prepopulate_querystring = $where = $joins = NULL;
			$current_page = (isset($_REQUEST['pg']) && is_numeric($_REQUEST['pg']) ? max(1, intval($_REQUEST['pg'])) : 1);

			if(isset($_REQUEST['filter'])) {
				// legacy implementation, convert single filter to an array
				// split string in the form ?filter=handle:value
				if(!is_array($_REQUEST['filter'])) {
					list($field_handle, $filter_value) = explode(':', $_REQUEST['filter'], 2);
					$filters[$field_handle] = rawurldecode($filter_value);
				} else {
					$filters = $_REQUEST['filter'];
				}

				foreach($filters as $handle => $value) {
					$field_id = FieldManager::fetchFieldIDFromElementName(
						Symphony::Database()->cleanValue($handle),
						$section->get('id')
					);

					$field = FieldManager::fetch($field_id);

					if($field instanceof Field) {
						// For deprecated reasons, call the old, typo'd function name until the switch to the
						// properly named buildDSRetrievalSQL function.
						$field->buildDSRetrivalSQL(array($value), $joins, $where, false);
						$filter_querystring .= sprintf("filter[%s]=%s&amp;", $handle, rawurlencode($value));
						$prepopulate_querystring .= sprintf("prepopulate[%d]=%s&amp;", $field_id, rawurlencode($value));
					}
					else {
						unset($filters[$handle]);
					}
				}

				$filter_querystring = preg_replace("/&amp;$/", '', $filter_querystring);
				$prepopulate_querystring = preg_replace("/&amp;$/", '', $prepopulate_querystring);
			}

			Sortable::initialize($this, $entries, $sort, $order, array(
				'current-section' => $section,
				'filters' => ($filter_querystring ? "&amp;" . $filter_querystring : ''),
				'unsort' => isset($_REQUEST['unsort'])
			));

			$this->Form->setAttribute('action', Administration::instance()->getCurrentPageURL(). '?pg=' . $current_page.($filter_querystring ? "&amp;" . $filter_querystring : ''));

			$subheading_buttons = array(
				Widget::Anchor(__('Create New'), Administration::instance()->getCurrentPageURL().'new/'.($prepopulate_querystring ? '?' . $prepopulate_querystring : ''), __('Create a new entry'), 'create button', NULL, array('accesskey' => 'c'))
			);

			// Only show the Edit Section button if the Author is a developer. #938 ^BA
			if(Administration::instance()->Author->isDeveloper()) {
				array_unshift($subheading_buttons, Widget::Anchor(__('Edit Section'), SYMPHONY_URL . '/blueprints/sections/edit/' . $section_id . '/', __('Edit Section Configuration'), 'button'));
			}

			$this->appendSubheading($section->get('name'), $subheading_buttons);

            /**
             * Allows adjustments to be made to the SQL where and joins statements
             * before they are used to fetch the entries for the page
             *
             * @delegate AdjustPublishFiltering
             * @since Symphony 2.3.3
             * @param string $context
             * '/publish/'
             * @param integer $section_id
             * An array of the current columns, passed by reference
             * @param string $where
             * The current where statement, or null if not set
             * @param string $joins
             */
            Symphony::ExtensionManager()->notifyMembers('AdjustPublishFiltering', '/publish/', array('section-id' => $section_id, 'where' => &$where, 'joins' => &$joins));

			// Check that the filtered query fails that the filter is dropped and an
			// error is logged. #841 ^BA
			try {
				$entries = EntryManager::fetchByPage($current_page, $section_id, Symphony::Configuration()->get('pagination_maximum_rows', 'symphony'), $where, $joins);
			}
			catch(DatabaseException $ex) {
				$this->pageAlert(__('An error occurred while retrieving filtered entries. Showing all entries instead.'), Alert::ERROR);
				$filter_querystring = null;
				Symphony::Log()->pushToLog(sprintf(
						'%s - %s%s%s',
						$section->get('name') . ' Publish Index',
						$ex->getMessage(),
						($ex->getFile() ? " in file " .  $ex->getFile() : null),
						($ex->getLine() ? " on line " . $ex->getLine() : null)
					),
					E_NOTICE, true
				);
				$entries = EntryManager::fetchByPage($current_page, $section_id, Symphony::Configuration()->get('pagination_maximum_rows', 'symphony'));
			}
{'fraction_non_alphanumeric': 0.16715774344232842, 'fraction_numerical': 0.0017966223499820337, 'mean_word_length': 3.9190526687875575, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '112823', 'n_tokens_mistral': 4911, 'n_tokens_neox': 4035, 'n_words': 951}