first commit
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\MO\Generate\Process;
|
||||
|
||||
|
||||
use WPML\Utils\Pager;
|
||||
use WPML\ST\MO\Generate\MultiSite\Executor;
|
||||
|
||||
class MultiSiteProcess implements Process {
|
||||
/** @var Executor */
|
||||
private $multiSiteExecutor;
|
||||
|
||||
/** @var SingleSiteProcess */
|
||||
private $singleSiteProcess;
|
||||
|
||||
/** @var Status */
|
||||
private $status;
|
||||
|
||||
/** @var Pager */
|
||||
private $pager;
|
||||
|
||||
/** @var SubSiteValidator */
|
||||
private $subSiteValidator;
|
||||
|
||||
/**
|
||||
* @param Executor $multiSiteExecutor
|
||||
* @param SingleSiteProcess $singleSiteProcess
|
||||
* @param Status $status
|
||||
* @param Pager $pager
|
||||
* @param SubSiteValidator $subSiteValidator
|
||||
*/
|
||||
public function __construct(
|
||||
Executor $multiSiteExecutor,
|
||||
SingleSiteProcess $singleSiteProcess,
|
||||
Status $status,
|
||||
Pager $pager,
|
||||
SubSiteValidator $subSiteValidator
|
||||
) {
|
||||
$this->multiSiteExecutor = $multiSiteExecutor;
|
||||
$this->singleSiteProcess = $singleSiteProcess;
|
||||
$this->status = $status;
|
||||
$this->pager = $pager;
|
||||
$this->subSiteValidator = $subSiteValidator;
|
||||
}
|
||||
|
||||
|
||||
public function runAll() {
|
||||
$this->multiSiteExecutor->withEach( $this->runIfSetupComplete( [ $this->singleSiteProcess, 'runAll' ] ) );
|
||||
$this->status->markComplete( true );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int Is completed
|
||||
*/
|
||||
public function runPage() {
|
||||
$remaining = $this->pager->iterate( $this->multiSiteExecutor->getSiteIds(), function ( $siteId ) {
|
||||
return $this->multiSiteExecutor->executeWith(
|
||||
$siteId,
|
||||
$this->runIfSetupComplete( function () {
|
||||
// no more remaining pages which means that process is done
|
||||
return $this->singleSiteProcess->runPage() === 0;
|
||||
} )
|
||||
);
|
||||
} );
|
||||
|
||||
if ( $remaining === 0 ) {
|
||||
$this->multiSiteExecutor->executeWith( Executor::MAIN_SITE_ID, function () {
|
||||
$this->status->markComplete( true );
|
||||
} );
|
||||
}
|
||||
|
||||
return $remaining;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getPagesCount() {
|
||||
$isCompletedForAllSites = $this->multiSiteExecutor->executeWith(
|
||||
Executor::MAIN_SITE_ID,
|
||||
[ $this->status, 'isCompleteForAllSites' ]
|
||||
);
|
||||
if ( $isCompletedForAllSites ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $this->multiSiteExecutor->getSiteIds()->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isCompleted() {
|
||||
return $this->getPagesCount() === 0;
|
||||
}
|
||||
|
||||
|
||||
private function runIfSetupComplete( $callback ) {
|
||||
return function () use ( $callback ) {
|
||||
if ( $this->subSiteValidator->isValid() ) {
|
||||
return $callback();
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\MO\Generate\Process;
|
||||
|
||||
|
||||
interface Process {
|
||||
|
||||
public function runAll();
|
||||
|
||||
/**
|
||||
* @return int Remaining
|
||||
*/
|
||||
public function runPage();
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getPagesCount();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isCompleted();
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\MO\Generate\Process;
|
||||
|
||||
use WPML\ST\MO\File\ManagerFactory;
|
||||
use WPML\ST\MO\Generate\MultiSite\Condition;
|
||||
use WPML\Utils\Pager;
|
||||
use function WPML\Container\make;
|
||||
|
||||
class ProcessFactory {
|
||||
const FILES_PAGER = 'wpml-st-mo-generate-files-pager';
|
||||
const FILES_PAGE_SIZE = 20;
|
||||
const SITES_PAGER = 'wpml-st-mo-generate-sites-pager';
|
||||
|
||||
/** @var Condition */
|
||||
private $multiSiteCondition;
|
||||
|
||||
/**
|
||||
* @param Condition $multiSiteCondition
|
||||
*/
|
||||
public function __construct( Condition $multiSiteCondition = null ) {
|
||||
$this->multiSiteCondition = $multiSiteCondition ?: new Condition();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Process
|
||||
* @throws \WPML\Auryn\InjectionException
|
||||
*/
|
||||
public function create() {
|
||||
$singleSiteProcess = self::createSingle();
|
||||
|
||||
if ( $this->multiSiteCondition->shouldRunWithAllSites() ) {
|
||||
return make( MultiSiteProcess::class,
|
||||
[ ':singleSiteProcess' => $singleSiteProcess, ':pager' => new Pager( self::SITES_PAGER, 1 ) ]
|
||||
);
|
||||
} else {
|
||||
return $singleSiteProcess;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $isBackgroundProcess
|
||||
*
|
||||
* @return SingleSiteProcess
|
||||
* @throws \WPML\Auryn\InjectionException
|
||||
*/
|
||||
public static function createSingle( $isBackgroundProcess = false ) {
|
||||
return make(
|
||||
SingleSiteProcess::class,
|
||||
[
|
||||
':pager' => new Pager( self::FILES_PAGER, self::FILES_PAGE_SIZE ),
|
||||
':manager' => ManagerFactory::create(),
|
||||
':migrateAdminTexts' => \WPML_Admin_Texts::get_migrator(),
|
||||
':status' => self::createStatus( $isBackgroundProcess ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $isBackgroundProcess
|
||||
*
|
||||
* @return mixed|\Mockery\MockInterface|Status
|
||||
* @throws \WPML\Auryn\InjectionException
|
||||
*/
|
||||
public static function createStatus( $isBackgroundProcess = false ) {
|
||||
return make( Status::class, [
|
||||
':optionPrefix' => $isBackgroundProcess ? Status::class . '_background' : null
|
||||
] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\MO\Generate\Process;
|
||||
|
||||
|
||||
use WPML\ST\MO\File\Manager;
|
||||
use WPML\ST\MO\Generate\DomainsAndLanguagesRepository;
|
||||
use WPML\Utils\Pager;
|
||||
|
||||
class SingleSiteProcess implements Process {
|
||||
|
||||
CONST TIMEOUT = 5;
|
||||
|
||||
/** @var DomainsAndLanguagesRepository */
|
||||
private $domainsAndLanguagesRepository;
|
||||
|
||||
/** @var Manager */
|
||||
private $manager;
|
||||
|
||||
/** @var Status */
|
||||
private $status;
|
||||
|
||||
/** @var Pager */
|
||||
private $pager;
|
||||
|
||||
/** @var callable */
|
||||
private $migrateAdminTexts;
|
||||
|
||||
/**
|
||||
* @param DomainsAndLanguagesRepository $domainsAndLanguagesRepository
|
||||
* @param Manager $manager
|
||||
* @param Status $status
|
||||
* @param Pager $pager
|
||||
* @param callable $migrateAdminTexts
|
||||
*/
|
||||
public function __construct(
|
||||
DomainsAndLanguagesRepository $domainsAndLanguagesRepository,
|
||||
Manager $manager,
|
||||
Status $status,
|
||||
Pager $pager,
|
||||
callable $migrateAdminTexts
|
||||
) {
|
||||
$this->domainsAndLanguagesRepository = $domainsAndLanguagesRepository;
|
||||
$this->manager = $manager;
|
||||
$this->status = $status;
|
||||
$this->pager = $pager;
|
||||
$this->migrateAdminTexts = $migrateAdminTexts;
|
||||
}
|
||||
|
||||
|
||||
public function runAll() {
|
||||
call_user_func( $this->migrateAdminTexts );
|
||||
$this->getDomainsAndLanguages()->each( function ( $row ) {
|
||||
$this->manager->add( $row->domain, $row->locale );
|
||||
} );
|
||||
|
||||
$this->status->markComplete();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int Remaining
|
||||
*/
|
||||
public function runPage() {
|
||||
if ( $this->pager->getProcessedCount() === 0 ) {
|
||||
call_user_func( $this->migrateAdminTexts );
|
||||
}
|
||||
|
||||
$domains = $this->getDomainsAndLanguages();;
|
||||
$remaining = $this->pager->iterate( $domains, function ( $row ) {
|
||||
$this->manager->add( $row->domain, $row->locale );
|
||||
|
||||
return true;
|
||||
}, self::TIMEOUT );
|
||||
|
||||
if ( $remaining === 0 ) {
|
||||
$this->status->markComplete();
|
||||
}
|
||||
|
||||
return $remaining;
|
||||
}
|
||||
|
||||
public function getPagesCount() {
|
||||
if ( $this->status->isComplete() ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$domains = $this->getDomainsAndLanguages();
|
||||
|
||||
if ( $domains->count() === 0 ) {
|
||||
$this->status->markComplete();
|
||||
}
|
||||
|
||||
return $domains->count();
|
||||
}
|
||||
|
||||
private function getDomainsAndLanguages() {
|
||||
return DomainsAndLanguagesRepository::hasTranslationFilesTable()
|
||||
? $this->domainsAndLanguagesRepository->get()
|
||||
: wpml_collect();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isCompleted() {
|
||||
return $this->getPagesCount() === 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\MO\Generate\Process;
|
||||
|
||||
class Status {
|
||||
/** @var \SitePress */
|
||||
private $sitepress;
|
||||
|
||||
/** @var string */
|
||||
private $optionPrefix;
|
||||
|
||||
/**
|
||||
* @param \SitePress $sitepress
|
||||
* @param string|null $optionPrefix
|
||||
*/
|
||||
public function __construct( \SitePress $sitepress, $optionPrefix = null ) {
|
||||
$this->sitepress = $sitepress;
|
||||
$this->optionPrefix = $optionPrefix ?: self::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $allSites
|
||||
*/
|
||||
public function markComplete( $allSites = false ) {
|
||||
$settings = $this->sitepress->get_setting( 'st', [] );
|
||||
$settings[ $this->getOptionName( $allSites ) ] = true;
|
||||
$this->sitepress->set_setting( 'st', $settings, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $allSites
|
||||
*/
|
||||
public function markIncomplete( $allSites = false ) {
|
||||
$settings = $this->sitepress->get_setting( 'st', [] );
|
||||
unset( $settings[ $this->getOptionName( $allSites ) ] );
|
||||
$this->sitepress->set_setting( 'st', $settings, true );
|
||||
}
|
||||
|
||||
public function markIncompleteForAll() {
|
||||
$this->markIncomplete( true );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isComplete() {
|
||||
$st_settings = $this->sitepress->get_setting( 'st', [] );
|
||||
|
||||
return isset( $st_settings[ $this->getOptionName( false ) ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isCompleteForAllSites() {
|
||||
$st_settings = $this->sitepress->get_setting( 'st', [] );
|
||||
|
||||
return isset( $st_settings[ $this->getOptionName( true ) ] );
|
||||
}
|
||||
|
||||
private function getOptionName( $allSites ) {
|
||||
return $allSites ? $this->optionPrefix . '_has_run_all_sites' : $this->optionPrefix . '_has_run';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\MO\Generate\Process;
|
||||
|
||||
use function WPML\Container\make;
|
||||
|
||||
class SubSiteValidator {
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid() {
|
||||
global $sitepress;
|
||||
|
||||
return $sitepress->is_setup_complete() && $this->hasTranslationFilesTable();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
private function hasTranslationFilesTable() {
|
||||
return make( \WPML_Upgrade_Schema::class )->does_table_exist( 'icl_mo_files_domains' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user