Артем

Артем

С нами с 12 марта 2017; Место в рейтинге пользователей: #46
Андрей
07 мая 2019, 09:05
2
+1
Я себе такой скрипт написал, просто запускаешь после установки модх через консоль

php modx_config.php

Часть с установкой пакетов взял из заметки Василия Наумкина.
Просто в массивах указываете нужные параметры и запускаете.
Не забываем указать логин и пароль администратора в конфигурации.

<?php

set_time_limit(0);
ini_set('max_execution_time', 0);

// API

require_once 'config.core.php';
require_once MODX_CORE_PATH.'model/modx/modx.class.php';
$modx = new modX();
$modx->initialize('web');
$modx->getService('error','error.modError', '', '');

$cache = $modx->getCacheManager();

function installPackage($packageName, $provider = 1) {
	global $modx;

	$provider = $modx->getObject('transport.modTransportProvider', $provider);
	$provider->getClient();
	$modx->getVersionData();
	$productVersion = $modx->version['code_name'].'-'.$modx->version['full_version'];

	$response = $provider->request('package','GET',array(
		'supports' => $productVersion,
		'query' => $packageName
	));

	if (!empty($response)) {

		$foundPackages = simplexml_load_string($response->response);

		foreach($foundPackages as $foundPackage) {
			/* @var modTransportPackage $foundPackage */
			if($foundPackage->name == $packageName) {
				$sig = explode('-',$foundPackage->signature);
				$versionSignature = explode('.',$sig[1]);
				$url = $foundPackage->location;

				if (!download($url, $modx->getOption('core_path').'packages/'.$foundPackage->signature.'.transport.zip')) {
					return array(
						'success' => 0,
						'message' => 'Could not download package <b>'.$packageName.'</b>.',
					);
				}

				$package = $modx->newObject('transport.modTransportPackage');
				$package->set('signature',$foundPackage->signature);
				$package->fromArray(array(
					'created' => date('Y-m-d h:i:s'),
					'updated' => null,
					'state' => 1,
					'workspace' => 1,
					'provider' => $provider->id,
					'source' => $foundPackage->signature.'.transport.zip',
					'package_name' => $sig[0],
					'version_major' => $versionSignature[0],
					'version_minor' => !empty($versionSignature[1]) ? $versionSignature[1] : 0,
					'version_patch' => !empty($versionSignature[2]) ? $versionSignature[2] : 0,
				));

				if (!empty($sig[2])) {
					$r = preg_split('/([0-9]+)/',$sig[2],-1,PREG_SPLIT_DELIM_CAPTURE);

					if (is_array($r) && !empty($r)) {
						$package->set('release',$r[0]);
						$package->set('release_index',(isset($r[1]) ? $r[1] : '0'));
					} else {
						$package->set('release',$sig[2]);
					}
				}

				if ($package->save() && $package->install()) {
					return array(
						'success' => 1,
						'message' => '<b>'.$packageName.'</b> was successfully installed',
					);
				}
				else {
					return array(
						'success' => 0,
						'message' => 'Could not save package <b>'.$packageName.'</b>',
					);
				}
				break;
			}
		}
	}
	else {
		return array(
			'success' => 0,
			'message' => 'Could not find <b>'.$packageName.'</b> in MODX repository',
		);
	}
	return true;
}

function download($src, $dst) {
	if (ini_get('allow_url_fopen')) {
		$file = @file_get_contents($src);
	}
	else if (function_exists('curl_init')) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $src);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT,180);
		$safeMode = @ini_get('safe_mode');
		$openBasedir = @ini_get('open_basedir');
		if (empty($safeMode) && empty($openBasedir)) {
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		}

		$file = curl_exec($ch);
		curl_close($ch);
	}
	else {
		return false;
	}
	file_put_contents($dst, $file);

	return file_exists($dst);
}

// LOGIN

$_user = [
	'username' => 'ваш_логи',
	'password' => 'ваш_пароль',
	'rememberme' => true,
	'login_context' => 'mgr'
];
$response = $modx->runProcessor('/security/login', $_user);
$user = $modx->getObject('modUser', array('username' => $_user['username']));
$modx->user = $user;


print "LOGIN: Done!\n";

// FOLDERS

$_folders = [
	'userdata/',
	'templates/'
];

foreach ($_folders as $folder) {
	$cache->writeTree(MODX_BASE_PATH.$folder);
}

print "FOLDERS: Done!\n";

// RENAME ht.access

rename(MODX_BASE_PATH.'ht.access', MODX_BASE_PATH.'.htaccess');
rename(MODX_BASE_PATH.'core/docs/changelog.txt', MODX_BASE_PATH.'core/docs/_changelog.txt');

print "RENAME: Done!\n";

// MEDIA SOURCES

$_media_sources = [
	[
		'name' => 'Userdata',
		'basePath' => 'userdata/',
		'baseUrl' => 'userdata/'
	]
];

foreach ($_media_sources as $config) {
	$source = $modx->runProcessor('source/create', [
		'name' => $config['name'],
		'class_key' => 'sources.modFileMediaSource'
	]);

	if ($source->isError()) {
		$modx->log(1, 'Failed to create Source. Message: '.$source->getMessage());
	}

	$source = $modx->getObject('modMediaSource', $source->response['object']['id']);
	$properties = $source->getProperties();
	$properties['basePath']['value'] = $config['basePath'];
	$properties['baseUrl']['value'] = $config['baseUrl'];
	$source->setProperties($properties);
	$source->save();
}

print "MEDIA SOURCES: Done!\n";

// PROVIDERS

$_providers = [
	[
		'name' => 'modstore.pro',
		'service_url' => 'https://modstore.pro/extras/',
		'username' => '',
		'api_key' => '',
		'description' => ''
	],
];

foreach ($_providers as $properties) {
	$provider = $modx->runProcessor('workspace/providers/create', $properties);

	if ($provider->isError()) {
		$modx->log(1, 'Failed to create Provider. Message: '.$provider->getMessage());
	}
}

print "PROVIDERS: Done!\n";

// PACKAGES

$_packages = [
	1 => [
		'translit',
		'FormIt',
		'ClientConfig',
		'MIGX',
		'CKEditor',
		'filetranslit',
		'Console',
		'simpleUpdater',
	],
	2 => [
		'pdoTools',
		'Ace',
		'phpThumbOn',
		'AjaxForm',
	]
];

foreach ($_packages as $provider => $packages) {
	foreach ($packages as $package) {
		installPackage($package, $provider);
		print $package.": Done!\n";
	}
}

print "PACKAGES: Done!\n";

// RESOURCES

$_resources = [
	[
		'pagetitle' => '404',
		'alias' => '404',
		'hidemenu' => 1,
		'published' => 1,
		'content' => '<p>Такой страницы не существует.</p>'
	],
	[
		'pagetitle' => 'sitemap.xml',
		'alias' => 'sitemap',
		'content_type' => 2,
		'template' => 0,
		'hidemenu' => 1,
		'published' => 1,
		'richtext' => 0,
		'content' => '[[!pdoSitemap]]'
	],
	[
		'pagetitle' => 'robots.txt',
		'alias' => 'robots',
		'content_type' => 3,
		'template' => 0,
		'hidemenu' => 1,
		'published' => 1,
		'richtext' => 0,
		'content' => "User-agent: *\nDisallow: /"
	]
];

foreach ($_resources as $properties) {
	$resource = $modx->runProcessor('resource/create', $properties);

	if ($resource->isError()) {
		$modx->log(1, 'Failed to create Resource. Message: '.$resource->getMessage());
	}
}

print "RESOURCES: Done!\n";

// OPTIONS

// get media source id
$default_media_source = $modx->getObject('modMediaSource', [
	'name' => $_media_sources[0]['name']
]);

$default_media_source = $default_media_source->toArray()['id'];

// get error page id
$error_page = $modx->getObject('modResource', [
	'pagetitle' => $_resources[0]['pagetitle']
]);

$error_page = $error_page->toArray()['id'];

$_options = [
	'site_name' => 'SITE_NAME',
	'emailsender' => 'ваша_почта',

	'server_protocol' => 'http',
	'friendly_alias_realtime' => 1,
	'friendly_alias_restrict_chars' => 'alphanumeric',
	'friendly_alias_translit' => 'russian',
	'friendly_urls' => 1,
	'friendly_urls_strict' => 1,
	'use_alias_path' => 1,
	'use_frozen_parent_uris' => 1,
	'confirm_navigation' => 0,
	'welcome_screen' => 0,
	'default_template' => 1,
	'link_tag_scheme' => 'abs',
	'log_snippet_not_found' => 0,
	'log_deprecated' => 0,
	'publish_default' => 1,
	'feed_modx_news_enabled' => 0,
	'feed_modx_security_enabled' => 0,
	'send_poweredby_header' => 0,

	'ace.font_size' => '14px',
	'ace.theme' => 'tomorrow_night',
	'pdotools_fenom_options' => '{"strip":true}',
	'pdotools_fenom_parser' => 1,

	'default_media_source' => $default_media_source,
	'error_page' => $error_page
];

foreach ($_options as $key => $value) {
	if ($option = $modx->getObject('modSystemSetting', $key)) {
		$option->set('value', $value);
		$option->save();
	}
}

print "OPTIONS: Done!\n";

// CONTENT TYPE

$content_type = $modx->getObject('modContentType', [
	'name' => 'HTML'
]);

if ($content_type) {
	$content_type->set('file_extensions', '/');
	$content_type->save();
}

print "CONTENT TYPE: Done!\n";

// CLEAR CACHE

$cache->refresh();

print "ALL Done!\n";