function testIncreaseMemoryLimitTo() {
if(!$this->canChangeMemory()) return;
ini_set('memory_limit', '64M');
// It can go up
increase_memory_limit_to('128M');
$this->assertEquals('128M', ini_get('memory_limit'));
// But not down
increase_memory_limit_to('64M');
$this->assertEquals('128M', ini_get('memory_limit'));
// Test the different kinds of syntaxes
increase_memory_limit_to(1024*1024*200);
$this->assertEquals(1024*1024*200, ini_get('memory_limit'));
increase_memory_limit_to('409600K');
$this->assertEquals('409600K', ini_get('memory_limit'));
increase_memory_limit_to('1G');
// If memory limit was left at 409600K, that means that the current testbox doesn't have
// 1G of memory available. That's okay; let's not report a failure for that.
if(ini_get('memory_limit') != '409600K') {
$this->assertEquals('1G', ini_get('memory_limit'));
}
// No argument means unlimited
increase_memory_limit_to();
$this->assertEquals(-1, ini_get('memory_limit'));
}
/**
* Perform migration
*
* @param string $base Absolute base path (parent of assets folder). Will default to BASE_PATH
* @return int Number of files successfully migrated
*/
public function run($base = null)
{
if (empty($base)) {
$base = BASE_PATH;
}
// Check if the File dataobject has a "Filename" field.
// If not, cannot migrate
if (!DB::get_schema()->hasField('File', 'Filename')) {
return 0;
}
// Set max time and memory limit
increase_time_limit_to();
increase_memory_limit_to();
// Loop over all files
$count = 0;
$filenameMap = $this->getFilenameArray();
foreach ($this->getFileQuery() as $file) {
// Get the name of the file to import
$filename = $filenameMap[$file->ID];
$success = $this->migrateFile($base, $file, $filename);
if ($success) {
$count++;
}
}
return $count;
}
/**
* Load the given file via {@link self::processAll()} and {@link self::processRecord()}.
* Optionally truncates (clear) the table before it imports.
*
* @return BulkLoader_Result See {@link self::processAll()}
*/
public function load($filepath)
{
// A small hack to allow model admin import form to work properly
if (!is_array($filepath) && isset($_FILES['_CsvFile'])) {
$filepath = $_FILES['_CsvFile'];
}
if (is_array($filepath)) {
$this->uploadFile = $filepath;
$filepath = $filepath['tmp_name'];
}
increase_time_limit_to();
increase_memory_limit_to('512M');
//get all instances of the to be imported data object
if ($this->deleteExistingRecords) {
DataObject::get($this->objectClass)->removeAll();
}
return $this->processAll($filepath);
}
function testIncreaseMemoryLimitTo()
{
ini_set('memory_limit', '64M');
// It can go up
increase_memory_limit_to('128M');
$this->assertEquals('128M', ini_get('memory_limit'));
// But not down
increase_memory_limit_to('64M');
$this->assertEquals('128M', ini_get('memory_limit'));
// Test the different kinds of syntaxes
increase_memory_limit_to(1024 * 1024 * 200);
$this->assertEquals(1024 * 1024 * 200, ini_get('memory_limit'));
increase_memory_limit_to('409600K');
$this->assertEquals('409600K', ini_get('memory_limit'));
increase_memory_limit_to('1G');
$this->assertEquals('1G', ini_get('memory_limit'));
// No argument means unlimited
increase_memory_limit_to();
$this->assertEquals(-1, ini_get('memory_limit'));
}
/**
* Uses {@link Director::test()} to perform in-memory HTTP requests
* on the passed-in URLs.
*
* @param array $urls Relative URLs
* @return array Result, keyed by URL. Keys:
* - "statuscode": The HTTP status code
* - "redirect": A redirect location (if applicable)
* - "path": The filesystem path where the cache has been written
*/
public function publishPages($urls)
{
$result = array();
//nest the config so we can make changes to the config and revert easily
Config::nest();
// Do we need to map these?
// Detect a numerically indexed arrays
if (is_numeric(join('', array_keys($urls)))) {
$urls = $this->urlsToPaths($urls);
}
// This can be quite memory hungry and time-consuming
// @todo - Make a more memory efficient publisher
increase_time_limit_to();
increase_memory_limit_to();
// Set the appropriate theme for this publication batch.
// This may have been set explicitly via StaticPublisher::static_publisher_theme,
// or we can use the last non-null theme.
$customTheme = Config::inst()->get('StaticPublisher', 'static_publisher_theme');
if ($customTheme) {
Config::inst()->update('SSViewer', 'theme', $customTheme);
}
// Ensure that the theme that is set gets used.
Config::inst()->update('SSViewer', 'theme_enabled', true);
$staticBaseUrl = Config::inst()->get('FilesystemPublisher', 'static_base_url');
if ($staticBaseUrl) {
Config::inst()->update('Director', 'alternate_base_url', $staticBaseUrl);
}
if ($this->fileExtension == 'php') {
Config::inst()->update('SSViewer', 'rewrite_hash_links', 'php');
}
if (Config::inst()->get('StaticPublisher', 'echo_progress')) {
echo $this->class . ": Publishing to " . $staticBaseUrl . "\n";
}
$files = array();
$i = 0;
$totalURLs = sizeof($urls);
foreach ($urls as $url => $path) {
$origUrl = $url;
$result[$origUrl] = array('statuscode' => null, 'redirect' => null, 'path' => null);
$i++;
if ($url && !is_string($url)) {
user_error("Bad url:" . var_export($url, true), E_USER_WARNING);
continue;
}
if (Config::inst()->get('StaticPublisher', 'echo_progress')) {
echo " * Publishing page {$i}/{$totalURLs}: {$url}\n";
flush();
}
Requirements::clear();
if ($url == "") {
$url = "/";
}
if (Director::is_relative_url($url)) {
$url = Director::absoluteURL($url);
}
$response = Director::test(str_replace('+', ' ', $url));
if (!$response) {
continue;
}
if ($response) {
$result[$origUrl]['statuscode'] = $response->getStatusCode();
}
Requirements::clear();
singleton('DataObject')->flushCache();
// Check for ErrorPages generating output - we want to handle this in a special way below.
$isErrorPage = false;
$pageObject = null;
if ($response && is_object($response) && (int) $response->getStatusCode() >= 400) {
$pageObject = SiteTree::get_by_link($url);
if ($pageObject && $pageObject instanceof ErrorPage) {
$isErrorPage = true;
}
}
// Skip any responses with a 404 status code unless it's the ErrorPage itself.
if (!$isErrorPage && is_object($response) && $response->getStatusCode() == '404') {
continue;
}
// Generate file content
// PHP file caching will generate a simple script from a template
if ($this->fileExtension == 'php') {
if (is_object($response)) {
if ($response->getStatusCode() == '301' || $response->getStatusCode() == '302') {
$content = $this->generatePHPCacheRedirection($response->getHeader('Location'));
} else {
$content = $this->generatePHPCacheFile($response->getBody(), HTTP::get_cache_age(), date('Y-m-d H:i:s'), $response->getHeader('Content-Type'));
}
} else {
$content = $this->generatePHPCacheFile($response . '', HTTP::get_cache_age(), date('Y-m-d H:i:s'), $response->getHeader('Content-Type'));
}
// HTML file caching generally just creates a simple file
//.........这里部分代码省略.........
public function load($filepath)
{
ini_set('max_execution_time', 3600);
increase_memory_limit_to('512M');
//get all instances of the to be imported data object
if ($this->deleteExistingRecords) {
$q = singleton($this->objectClass)->buildSQL();
$q->select = array('"ID"');
$ids = $q->execute()->column('ID');
foreach ($ids as $id) {
$obj = DataObject::get_by_id($this->objectClass, $id);
$obj->delete();
$obj->destroy();
unset($obj);
}
}
return $this->processAll($filepath);
}
/**
* Define the temporary folder if it wasn't defined yet
*/
if (!defined('TEMP_FOLDER')) {
define('TEMP_FOLDER', getTempFolder());
}
/**
* Priorities definition. These constants are used in calls to _t() as an optional argument
*/
define('PR_HIGH', 100);
define('PR_MEDIUM', 50);
define('PR_LOW', 10);
/**
* Ensure we have enough memory
*/
increase_memory_limit_to('64M');
///////////////////////////////////////////////////////////////////////////////
// INCLUDES
require_once "core/ManifestBuilder.php";
require_once "core/ClassInfo.php";
require_once 'core/Object.php';
require_once 'core/control/Director.php';
require_once 'filesystem/Filesystem.php';
require_once "core/Session.php";
///////////////////////////////////////////////////////////////////////////////
// MANIFEST
/**
* Include the manifest
*/
ManifestBuilder::include_manifest();
/**
function run($request)
{
set_time_limit(1200);
increase_memory_limit_to(-1);
$nextGetStatement = "";
//can we do the next step?
//IN general yes, but if the current one is not finished yet, then we do it again.
$canDoNext = true;
if (isset($_REQUEST["limit"])) {
$this->limit = intval($_REQUEST["limit"]);
}
if (isset($_REQUEST["start"])) {
$this->start = intval($_REQUEST["start"]);
}
//what is the current step?
$currentMethod = isset($_REQUEST["action"]) ? $_REQUEST["action"] : "";
if (in_array($currentMethod, $this->listOfMigrationTasks)) {
//are we doing the same one for a different limti?
if ($this->start) {
$this->DBAlterationMessageNow("this task is broken down into baby steps - we are now starting at .... " . $this->start . " processing " . $this->limit, "created");
}
$nextLimit = $this->{$currentMethod}();
if ($canDoNext && $nextLimit) {
$canDoNext = false;
//NEXT OPTION 1: do again with new limit
$nextGetStatement = "?action=" . $currentMethod . "&start=" . $nextLimit;
$nextDescription = "run next batch ...";
}
}
if ($canDoNext && !$nextGetStatement) {
//NEXT OPTION 2: start from the beginning
$nextGetStatement = "?fullmigration=1&action=" . $this->listOfMigrationTasks[0];
$nextDescription = "Start Migration by clicking on <i>'Next'</i> (this link) until all tasks have been completed.";
}
//retrieve data...
$this->retrieveInfoOnly = true;
$html = "";
$createListOfActions = false;
if (!$currentMethod) {
$createListOfActions = true;
}
if ($createListOfActions) {
$html .= "\r\n\t\t\t<p>Always make a backup of your database before running any migration tasks.</p>\r\n\t\t\t<ul>";
}
foreach ($this->listOfMigrationTasks as $key => $task) {
$explanation = $this->{$task}();
$explanation = str_replace(array("<h1>", "</h1>", "<p>", "</p>"), array("<strong>", "</strong>: ", "<span style=\"color: grey;\">", "</span>"), $explanation);
if ($task == $currentMethod) {
if ($canDoNext) {
$keyPlusOne = $key + 1;
if (isset($this->listOfMigrationTasks[$keyPlusOne]) && isset($_REQUEST["fullmigration"])) {
//NEXT OPTION 3: next action!
$action = $this->listOfMigrationTasks[$keyPlusOne];
$nextGetStatement = "?action=" . $action;
$nextDescription = $this->{$action}();
$nextDescription = str_replace(array("<h1>", "</h1>", "<p>", "</p>"), array("<strong>", "</strong>: ", "<span style=\"color: grey;\">", "</span>"), $nextDescription);
} else {
//NEXT OPTION 4: we have done all of them - no more next option...
$nextGetStatement = "";
$nextDescription = "";
}
}
}
if ($createListOfActions) {
$html .= "<li><a href=\"/dev/ecommerce/ecommercetaskmigration/?action=" . $task . "\">{$explanation} </a></li>";
}
}
if ($createListOfActions) {
$html .= "</ul>";
}
if ($nextGetStatement) {
$nextLink = "/dev/ecommerce/ecommercetaskmigration/" . $nextGetStatement;
if (isset($_REQUEST["fullmigration"])) {
$nextLink .= "&fullmigration=1";
}
echo "\r\n\t\t\t\t<hr style=\"margin-top: 50px;\"/>\r\n\t\t\t\t<h3><a href=\"{$nextLink}\">NEXT: {$nextDescription}</a></h3>";
if ($currentMethod) {
echo "\r\n\t\t\t\t<div style=\"width: 400px; height: 20px; padding-top: 20px; font-size: 11px; background: url(/ecommerce/images/loading.gif) no-repeat top left transparent\">\r\n\t\t\t\t\tNext step, if any - will load automatically in ten seconds.\r\n\t\t\t\t</div>\r\n\t\t\t\t<script type=\"text/javascript\">\r\n\t\t\t\t\tvar t = window.setTimeout(\r\n\t\t\t\t\t\tfunction(){\r\n\t\t\t\t\t\t\twindow.location = '{$nextLink}';\r\n\t\t\t\t\t\t},\r\n\t\t\t\t\t\t500\r\n\t\t\t\t\t);\r\n\t\t\t\t</script>\r\n\t\t\t\t<hr style=\"margin-bottom: 500px;\"/>\r\n\t\t\t";
}
}
echo $html;
}
public function exportTo($directory)
{
$directory = rtrim($directory, '/');
$links = $this->urlsToPaths($this->getLinks());
$files = array();
increase_time_limit_to();
increase_memory_limit_to();
// Make the output directory if it doesn't exist.
if (!is_dir($directory)) {
mkdir($directory, Filesystem::$folder_create_mask, true);
}
if ($this->theme) {
SSViewer::set_theme($this->theme);
} else {
SSViewer::set_theme(SSViewer::current_custom_theme());
}
if ($this->baseUrl && !$this->makeRelative) {
$originalBaseUrl = Director::baseURL();
Director::setBaseURL($this->baseUrl);
}
// Loop through each link that we're publishing, and create a static
// html file for each.
foreach ($links as $link => $path) {
Requirements::clear();
singleton('DataObject')->flushCache();
$response = Director::test($link);
$target = $directory . '/' . $path;
if (is_object($response)) {
if ($response->getStatusCode() == '301' || $response->getStatusCode() == '302') {
$absoluteURL = Director::absoluteURL($response->getHeader('Location'));
$content = "<meta http-equiv=\"refresh\" content=\"2; URL={$absoluteURL}\">";
} else {
$content = $response->getBody();
}
} else {
$content = (string) $response;
}
// Find any external content references inside the response, and add
// them to the copy array.
$externals = $this->externalReferencesFor($content);
if ($externals) {
foreach ($externals as $external) {
if (!Director::is_site_url($external)) {
continue;
}
$external = strtok($external, '?');
$external = Director::makeRelative($external);
if (file_exists(BASE_PATH . '/' . $external)) {
$files["{$directory}/{$external}"] = array('copy', BASE_PATH . '/' . $external);
}
}
}
// Append any anchor links which point to a relative site link
// with a .html extension.
$base = preg_quote(Director::baseURL());
$content = preg_replace('~<a(.+?)href="(' . $base . '[^"]*?)/?"~i', '<a$1href="$2.html"', $content);
$content = str_replace('/.html', '/index.html', $content);
// If we want to rewrite links to relative, then determine how many
// levels deep we are and rewrite the relevant attributes globally.
// Also, string the base tag.
if ($this->makeRelative) {
$content = preg_replace('~(src|href)="' . Director::protocolAndHost() . '~i', '$1="', $content);
if (($trimmed = trim($link, '/')) && strpos($trimmed, '/')) {
$prepend = str_repeat('../', substr_count($trimmed, '/'));
} else {
$prepend = './';
}
$base = preg_quote(Director::baseURL());
$content = preg_replace('~(href|src)="' . $base . '~i', '$1="' . $prepend, $content);
$content = preg_replace('~<base href="([^"]+)" />~', '', $content);
$content = preg_replace('~<base href="([^"]+)"><!--[if lte IE 6]></base><![endif]-->~', '', $content);
}
$files[$target] = array('create', $content);
}
// If we currently have a theme active, then copy all the theme
// assets across to the site.
if ($theme = SSViewer::current_theme()) {
$stack = array(THEMES_PATH . '/' . $theme);
// Build up a list of every file present in the current theme
// which is not a .ss template, and add it to the files array
while ($path = array_pop($stack)) {
foreach (scandir($path) as $file) {
if ($file[0] == '.' || $file[0] == '_') {
continue;
}
if (is_dir("{$path}/{$file}")) {
$stack[] = "{$path}/{$file}";
} else {
if (substr($file, -3) != '.ss') {
$loc = "{$path}/{$file}";
$to = $directory . '/' . substr($loc, strlen(BASE_PATH) + 1);
$files[$to] = array('copy', $loc);
}
}
}
}
}
// If theres a favicon.ico file in the site root, copy it across
if (file_exists(BASE_PATH . '/favicon.ico')) {
$files["{$directory}/favicon.ico"] = array('copy', BASE_PATH . '/favicon.ico');
//.........这里部分代码省略.........
请发表评论