<commit_before><?php

class ShareDraftController extends Controller {
	/**
	 * Controller for rendering draft pages.
	 *
	 * @config
	 *
	 * @var string
	 */
	private static $controller = 'Page_Controller';

	/**
	 * @var array
	 */
	private static $allowed_actions = array(
		'preview'
	);

	/**
	 * @var array
	 */
	private static $url_handlers = array(
		'$Key/$Token' => 'preview'
	);

	/**
	 * @param SS_HTTPRequest $request
	 *
	 * @return string|HTMLText
	 */
	public function preview(SS_HTTPRequest $request) {
		$key = $request->param('Key');
		$token = $request->param('Token');

		/**
		 * @var ShareToken $shareToken
		 */
		$shareToken = ShareToken::get()->filter('token', $token)->first();

		if(!$shareToken) {
			return $this->errorPage();
		}

		$page = $shareToken->Page();

		$latest = $page->Versions(null, 'Version DESC')->first();

		$controller = $this->getControllerFor($latest);

		if(!$shareToken->isExpired() && $page->generateKey($shareToken->Token) === $key) {
			Requirements::css(SHAREDRAFTCONTENT_DIR . '/css/top-bar.css');

			$rendered = $controller->render();

			$data = new ArrayData(array(
				'Page' => $page,
				'Latest' => $latest,
			));

			$include = (string) $data->renderWith('Includes/TopBar');

			return str_replace('</body>', $include . '</body>', (string) $rendered);<commit_msg>BUG Fix controller bypassing init<commit_after><?php

class ShareDraftController extends Controller {
	/**
	 * Controller for rendering draft pages.
	 *
	 * @config
	 *
	 * @var string
	 */
	private static $controller = 'Page_Controller';

	/**
	 * @var array
	 */
	private static $allowed_actions = array(
		'preview'
	);

	/**
	 * @var array
	 */
	private static $url_handlers = array(
		'$Key/$Token' => 'preview'
	);

	/**
	 * @param SS_HTTPRequest $request
	 *
	 * @return string|HTMLText
	 */
	public function preview(SS_HTTPRequest $request) {
		$key = $request->param('Key');
		$token = $request->param('Token');

		/**
		 * @var ShareToken $shareToken
		 */
		$shareToken = ShareToken::get()->filter('token', $token)->first();

		if(!$shareToken) {
			return $this->errorPage();
		}

		$page = Versioned::get_one_by_stage(
			'SiteTree',
			'Stage',
			sprintf('"SiteTree"."ID" = \'%d\'', $shareToken->PageID)
		);

		$latest = Versioned::get_latest_version('SiteTree', $shareToken->PageID);

		$controller = $this->getControllerFor($page);

		if(!$shareToken->isExpired() && $page->generateKey($shareToken->Token) === $key) {
			Requirements::css(SHAREDRAFTCONTENT_DIR . '/css/top-bar.css');

			// Temporarily un-secure the draft site and switch to draft
			$oldSecured = Session::get('unsecuredDraftSite');
			$oldMode = Versioned::get_reading_mode();
			$restore = function() use ($oldSecured, $oldMode) {
				Session::set('unsecuredDraftSite', $oldSecured);
				Versioned::set_reading_mode($oldMode);
			};

			// Process page inside an unsecured draft container
			try {
				Session::set('unsecuredDraftSite', true);
				Versioned::reading_stage('Stage');

				// Create mock request; Simplify request to single top level reqest
				$pageRequest = new SS_HTTPRequest('GET', $page->URLSegment);
				$pageRequest->match('$URLSegment//$Action/$ID/$OtherID', true);
				$rendered = $controller->handleRequest($pageRequest, $this->model);

				// Render draft heading
				$data = new ArrayData(array(
					'Page' => $page,
					'Latest' => $latest,
				));
				$include = (string) $data->renderWith('Includes/TopBar');
			} catch(Exception $ex) {
				$restore();
				throw $ex;
			}
			$restore();

			return str_replace('</body>', $include . '</body>', (string) $rendered->getBody());
{'fraction_non_alphanumeric': 0.1836734693877551, 'fraction_numerical': 0.0, 'mean_word_length': 3.8619302949061662, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 44, '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': '6528091', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1158, 'n_words': 235}