<commit_before><?php

/**
 * This class lets you export a static copy of your site.
 * It creates a huge number of folders each containing an index.html file.
 * This preserves the URL naming format.
 */
class StaticExporter extends Controller {
	function init() {
		parent::init();
		if(!$this->can('AdminCMS')) {
			$messageSet = array(
				'default' => _t('LeftAndMain.PERMDEFAULT'),
				'alreadyLoggedIn' => _t('LeftAndMain.PERMALREADY'),
				'logInAgain' => _t('LeftAndMain.PERMAGAIN'),
			);

			Security::permissionFailure($this, $messageSet);
		}
	}


	function Link($action = null) {
		return "StaticExporter/$action";
	}

	function index() {
		echo "<h1>"._t('StaticExporter.NAME','Static exporter')."</h1>";
		echo $this->StaticExportForm()->forTemplate();
	}

	function StaticExportForm() {
		return new Form($this, 'StaticExportForm', new FieldSet(
			new TextField('folder', _t('StaticExporter.FOLDEREXPORT','Folder to export to')),
			new TextField('baseurl', _t('StaticExporter.BASEURL','Base URL'))
		), new FieldSet(
			new FormAction('export', _t('StaticExporter.EXPORTTOTHAT','Export to that folder'))
		));
	}

	function export() {

		if($_REQUEST['baseurl']) {
			$base = $_REQUEST['baseurl'];
			if(substr($base,-1) != '/') $base .= '/';
			Director::setBaseURL($base);
		}

		$folder = $_REQUEST['folder'];
		if($folder && file_exists($folder)) {
			$pages = DataObject::get("SiteTree");
			foreach($pages as $page) {
				$subfolder = "$folder/$page->URLSegment";
				$contentfile = "$folder/$page->URLSegment/index.html";

				echo "<li>$page->URLSegment/index.html";

				// Make the folder
				if(!file_exists($subfolder)) {
					mkdir($subfolder);
					chmod($subfolder,02775);
				}

				// Run the page
				Requirements::clear();
				$controllerClass = "{$page->class}_Controller";
				if(class_exists($controllerClass)) {
					$controller = new $controllerClass($page);

					$response = $controller->run( array() );
					$pageContent = $response->getBody();
					
					// Write to file
					if($fh = fopen($contentfile, 'w')) {
						fwrite($fh, $pageContent);
						fclose($fh);
					}
				}
			}
		} else {
			echo _t('StaticExporter.ONETHATEXISTS',"Please specify a folder that exists");
		}

	}

}

?><commit_msg>Updated and improved StaticExporter<commit_after><?php

/**
 * This class lets you export a static copy of your site.
 * It creates a huge number of folders each containing an index.html file.
 * This preserves the URL naming format.
 */
class StaticExporter extends Controller {
	function init() {
		parent::init();
		if(!Permission::check('ADMIN')) {
			$messageSet = array(
				'default' => "Enter your email address and password to access the CMS.",
				'alreadyLoggedIn' => "I'm sorry, but you can't access that part of the CMS.  If you want to log in as someone else, do so below",
				'logInAgain' => "You have been logged out of the CMS.  If you would like to log in again, enter a username and password below.",
			);

			Security::permissionFailure($this, $messageSet);
			return;
		}
	}
		
	
	function Link($action = null) {
		return "StaticExporter/$action";
	}
	
	function index() {
		echo "<h1>Static exporter</h1>";
		echo $this->StaticExportForm()->forTemplate();
	}
	
	function StaticExportForm() {
		return new Form($this, 'StaticExportForm', new FieldSet(
			// new TextField('folder', 'Folder to export to'),
			new TextField('baseurl', 'Base URL')
		), new FieldSet(
			new FormAction('export', 'Export to that folder')
		));
	}
	
	function export() {
		
		if($_REQUEST['baseurl']) {
			$base = $_REQUEST['baseurl'];
			if(substr($base,-1) != '/') $base .= '/';
			Director::setBaseURL($base);
		}
		
		$folder = '/tmp/static-export/' . project();
		if(!project()) $folder .= 'site';
		if(!file_exists($folder)) mkdir($folder, 02775, true);

		$f1 = Director::baseFolder() . '/assets';
		$f2 = Director::baseFolder() . '/' . project();
		`cd $folder; ln -s $f1; ln -s $f2`;

		
		$baseFolder = basename($folder);
		
		if($folder && file_exists($folder)) {
			$pages = DataObject::get("SiteTree");
			foreach($pages as $page) {
				$subfolder = "$folder/$page->URLSegment";
				$contentfile = "$folder/$page->URLSegment/index.html";
				
				// Make the folder				
				if(!file_exists($subfolder)) {
					mkdir($subfolder, 02775);
				}
				
				// Run the page
				Requirements::clear();
				$controllerClass = "{$page->class}_Controller";
				if(class_exists($controllerClass)) {
					$controller = new $controllerClass($page);
					$pageContent = $controller->run( array() );
					
					// Write to file
					if($fh = fopen($contentfile, 'w')) {
						fwrite($fh, $pageContent->getBody());
						fclose($fh);
					}
				}
			}
			
			copy("$folder/home/index.html", "$folder/index.html");			

			`cd /tmp/static-export; tar -czhf $baseFolder.tar.gz $baseFolder`;

			$content = file_get_contents("/tmp/static-export/$baseFolder.tar.gz");
			Filesystem::removeFolder('/tmp/static-export');

			HTTP::sendFileToBrowser($content, "$baseFolder.tar.gz");
			return null;
			
		} else {
			echo "Please specify a folder that exists";
		}
			
	}
	
}

?>
{'fraction_non_alphanumeric': 0.17233294255568582, 'fraction_numerical': 0.0048847205939820245, 'mean_word_length': 3.5623885918003566, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 32, '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': '478383', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1620, 'n_words': 385}