diff -u b/core/includes/install.core.inc b/core/includes/install.core.inc --- b/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -1591,14 +1591,17 @@ if ($info['major']) { $core = $info['major'] . '.x'; $data = array( - 'name' => 'drupal', + 'id' => 'drupal', + 'name' => 'Drupal core', 'project_type' => 'module', 'core' => $core, 'version' => $version, 'server_pattern' => $install_state['server_pattern'], 'status' => 1, ); - \Drupal::service('locale.project')->set($data['name'], $data); + $project = new \Drupal\locale\LocaleTranslatableProject($data); + $project->setLangcode($langcode); + \Drupal::service('locale.project')->set($project); module_load_include('compare.inc', 'locale'); locale_translation_check_projects_local(array('drupal'), array($install_state['parameters']['langcode'])); } @@ -1606,17 +1609,14 @@ if ($info['major']) { $core = $info['major'] . '.x'; $data = array( - 'id' => 'drupal', - 'name' => 'Drupal core', + 'name' => 'drupal', 'project_type' => 'module', 'core' => $core, 'version' => $version, 'server_pattern' => $install_state['server_pattern'], 'status' => 1, ); - $project = new \Drupal\locale\LocaleTranslatableProject($data); - $project->setLangcode($langcode); - \Drupal::service('locale.project')->set($project); + \Drupal::service('locale.project')->set($data['name'], $data); module_load_include('compare.inc', 'locale'); locale_translation_check_projects_local(array('drupal'), array($install_state['parameters']['langcode'])); } diff -u b/core/modules/locale/locale.module b/core/modules/locale/locale.module --- b/core/modules/locale/locale.module +++ b/core/modules/locale/locale.module @@ -448,12 +448,12 @@ // built-in support for translation imports in the installer. if (!drupal_installation_attempted() && locale_translatable_language_list() && \Drupal::config('locale.settings')->get('translation.import_enabled')) { module_load_include('compare.inc', 'locale'); + $projects = locale_translation_build_projects(); // Update the list of translatable projects and start the import batch. // Only when new projects are added the update batch will be triggered. Not // each enabled module will introduce a new project. E.g. sub modules. - $projects = array_keys(locale_translation_build_projects()); - if ($list = array_intersect($list, $projects)) { + if ($list = array_intersect($list, array_keys($projects))) { module_load_include('fetch.inc', 'locale'); // Get translation status of the projects, download and update // translations. @@ -463,12 +463,12 @@ // built-in support for translation imports in the installer. if (!drupal_installation_attempted() && locale_translatable_language_list() && \Drupal::config('locale.settings')->get('translation.import_enabled')) { module_load_include('compare.inc', 'locale'); - $projects = locale_translation_build_projects(); // Update the list of translatable projects and start the import batch. // Only when new projects are added the update batch will be triggered. Not // each enabled module will introduce a new project. E.g. sub modules. - if ($list = array_intersect($list, array_keys($projects))) { + $projects = array_keys(locale_translation_build_projects()); + if ($list = array_intersect($list, $projects)) { module_load_include('fetch.inc', 'locale'); // Get translation status of the projects, download and update // translations. @@ -499,12 +499,7 @@ locale_translate_delete_translation_files($list, array()); // Remove translatable projects. - // Followup issue http://drupal.org/node/1842362 to replace the - // {locale_project} table. Then change this to a function call. \Drupal::service('locale.project')->deleteMultiple($list); - - // Clear the translation status. - locale_translation_status_delete_projects($list); } } @@ -514,7 +509,12 @@ locale_translate_delete_translation_files($list, array()); // Remove translatable projects. + // Followup issue http://drupal.org/node/1842362 to replace the + // {locale_project} table. Then change this to a function call. \Drupal::service('locale.project')->deleteMultiple($list); + + // Clear the translation status. + locale_translation_status_delete_projects($list); } } @@ -811,7 +811,7 @@ // Get file history from the database. $result = db_query('SELECT project, langcode, filename, version, uri, timestamp, last_checked FROM {locale_file}'); foreach ($result as $file) { - $file->type = $file->timestamp ? LOCALE_TRANSLATION_CURRENT : ''; + $file->type = $file->timestamp ? 'current' : ''; $history[$file->project][$file->langcode] = $file; } } @@ -826,7 +826,7 @@ // Get file history from the database. $result = db_query('SELECT project, langcode, filename, version, uri, timestamp, last_checked FROM {locale_file}'); foreach ($result as $file) { - $file->type = $file->timestamp ? 'current' : ''; + $file->type = $file->timestamp ? LOCALE_TRANSLATION_CURRENT : ''; $history[$file->project][$file->langcode] = $file; } } @@ -836,11 +836,10 @@ ->fields(array( 'version' => $file->version, 'timestamp' => $file->timestamp, - 'last_checked' => $file->last_checked, + 'last_checked' => $file->lastChecked, )) ->execute(); // The file history has changed, flush the static cache now. - // @todo Can we make this more fine grained? drupal_static_reset('locale_translation_get_file_history'); return $status; } @@ -851,10 +850,11 @@ ->fields(array( 'version' => $file->version, 'timestamp' => $file->timestamp, - 'last_checked' => $file->lastChecked, + 'last_checked' => $file->last_checked, )) ->execute(); // The file history has changed, flush the static cache now. + // @todo Can we make this more fine grained? drupal_static_reset('locale_translation_get_file_history'); return $status; } diff -u b/core/modules/locale/src/LocaleTranslatableProject.php b/core/modules/locale/src/LocaleTranslatableProject.php --- b/core/modules/locale/src/LocaleTranslatableProject.php +++ b/core/modules/locale/src/LocaleTranslatableProject.php @@ -44,42 +44,12 @@ protected $serverPattern = ''; /** - * Array of project states keyed by language. + * Array of project translation states keyed by language. */ protected $stateByLanguage = array(); /** - * The time the current project translation was updated. - * Zero if unknown or not translated. - */ - protected $timestamp; - - /** - * The time the current translation was last checked or updated. - */ - protected $lastChecked; - - /** - * The source which holds the most up to date translation. - */ - protected $latestSource; - - /** - * The timestamp of the most up to date translation source. - */ - protected $latestSourceTimestamp; - - /** - * Local translation source object. - */ - protected $localSource; - - /** - * Remote translation source object. - */ - protected $remoteSource; - - /** + * The current language code set. * @var string */ protected $langcode = ''; @@ -88,7 +58,6 @@ * @inheritdoc */ public function __construct($data = array()) { - $this->importData($data); } @@ -123,11 +92,11 @@ // if version or server pattern has changed. if ((isset($data['version']) && $old_version != $this->projectVersion) || (isset($data['server_pattern']) && $old_server_pattern != $this->serverPattern)) { - foreach (array_keys($this->stateByLanguage) as $langcode) { - $this->stateByLanguage[$langcode]->localSource = $this->buildLocalSource($langcode); - $this->stateByLanguage[$langcode]->remoteSource = $this->buildRemoteSource($langcode); - } - $this->clearTranslationStates(); + foreach ($this->stateByLanguage as $langcode => $state) { + $state->localSource = ProjectSource::create($this, $langcode); + $state->remoteSource = RemoteProjectSource::create($this, $langcode); + $state->clearTranslationStates(); + } } } @@ -152,12 +121,6 @@ $this->langcode = $langcode; $this->initLangcode($langcode); - $this->timestamp = &$this->stateByLanguage[$langcode]->timestamp; - $this->lastChecked = &$this->stateByLanguage[$langcode]->lastChecked; - $this->latestSource = &$this->stateByLanguage[$langcode]->latestSource; - $this->latestSourceTimestamp = &$this->stateByLanguage[$langcode]->latestSourceTimestamp; - $this->localSource = &$this->stateByLanguage[$langcode]->localSource; - $this->remoteSource = &$this->stateByLanguage[$langcode]->remoteSource; } /** @@ -184,7 +147,6 @@ * @inheritdoc */ public function removeLangcode($langcode) { - unset($this->stateByLanguage[$langcode]); } @@ -192,7 +154,6 @@ * @inheritdoc */ public function getStatus() { - return $this->status; } @@ -200,34 +161,17 @@ * @inheritdoc */ public function setStatus($status) { - $this->status = (bool) $status; } /** * @inheritdoc */ - public function clearTranslationStates() { - - foreach (array_keys($this->stateByLanguage) as $langcode) { - if ($this->stateByLanguage[$langcode]->latestSource == 'local' || $this->stateByLanguage[$langcode]->latestSource == 'remote') { - $this->stateByLanguage[$langcode]->latestSource = ''; - $this->stateByLanguage[$langcode]->latestSourceTimestamp = 0; - } - } - } - - /** - * @inheritdoc - */ public function getLocalSource() { $source = NULL; - if (!empty($this->localSource)) { - $source = clone $this->localSource; - $source->project = $this->id; - $source->langcode = $this->langcode; - $source->version = $this->projectVersion; + if (!empty($this->stateByLanguage[$this->langcode]->localSource)) { + $source = $this->stateByLanguage[$this->langcode]->localSource; } return $source; @@ -239,11 +183,8 @@ public function getRemoteSource() { $source = NULL; - if (!empty($this->remoteSource)) { - $source = clone $this->remoteSource; - $source->project = $this->id; - $source->langcode = $this->langcode; - $source->version = $this->projectVersion; + if (!empty($this->stateByLanguage[$this->langcode]->remoteSource)) { + $source = $this->stateByLanguage[$this->langcode]->localSource; } return $source; @@ -253,8 +194,7 @@ * @inheritdoc */ public function getLatestSourceTimestamp() { - - return $this->latestSourceTimestamp; + return $this->getCurrentState()->getLatestSourceTimestamp; } /** @@ -276,132 +216,87 @@ /** * @inheritdoc */ - public function remoteIsValid() { - - // Development release translations are not supported for remote sources. - if ($this->projectVersion && strpos('dev', $this->projectVersion) !== FALSE) { - return FALSE; - } - - return TRUE; + public function remoteIsLatest() { + return $this->getCurrentState()->remoteIsLatest(); } /** * @inheritdoc */ - public function remoteIsAvailable() { - - return $this->remoteIsValid() && $this->latestSource == 'remote'; + public function remoteIsValid() { + return $this->getCurrentState()->remoteIsValid(); } /** * @inheritdoc */ - public function remoteIsLatest() { - - return $this->latestSource == 'remote'; + public function remoteIsAvailable() { + return $this->getCurrentState()->remoteIsAvailable(); } /** * @inheritdoc */ public function localIsLatest() { - - return $this->latestSource == 'local'; + return $this->getCurrentState()->localIsLatest(); } /** * @inheritdoc */ public function setLocalSource($data) { + $this->getCurrentState()->setLocalSource($data); - if (isset($data->timestamp)) { - $this->localSource->filename = $data->filename; - $this->localSource->directory = $data->directory; - $this->localSource->uri = $data->uri; - $this->localSource->timestamp = $data->timestamp; - - // If the local translation is the most recent, we update the latestSource - // status. Otherwise only mark the current translation as checked. If remote - // and local have the same timestamp, the local source is chosen. - if ($data->timestamp > $this->latestSourceTimestamp || ($data->timestamp >= $this->latestSourceTimestamp && $this->latestSource == 'remote')) { - $this->latestSource = 'local'; - $this->latestSourceTimestamp = $data->timestamp; - } - else { - $this->lastChecked = REQUEST_TIME; - } - } - - $this->localSource->lastChecked = REQUEST_TIME; } /** * @inheritdoc */ public function setRemoteSource($data) { - - if (isset($data->timestamp)) { - $this->remoteSource->filename = $data->filename; - $this->remoteSource->uri = $data->uri; - $this->remoteSource->timestamp = $data->timestamp; - - // If the remote translation is the most recent, we update the - // latestSource status. Otherwise mark the current translation as checked. - if ($data->timestamp > $this->latestSourceTimestamp) { - $this->latestSource = 'remote'; - $this->latestSourceTimestamp = $data->timestamp; - } - else { - $this->lastChecked = REQUEST_TIME; - } - } - - $this->remoteSource->lastChecked = REQUEST_TIME; + $this->getCurrentState()->setRemoteSource($data); } /** * @inheritdoc */ public function setTimestamp($timestamp) { - - $this->timestamp = $timestamp; - $this->lastChecked = REQUEST_TIME; - - $this->latestSource = 'current'; - $this->latestSourceTimestamp = $timestamp; + $this->getCurrentState()->setTimestamp($timestamp); } /** * @inheritdoc */ public function isTranslated() { - - return $this->timestamp > 0; + return $this->getCurrentState()->isTranslated(); } /** * @inheritdoc */ public function hasUpdates() { - - return $this->latestSource == 'local' || $this->latestSource == 'remote'; + return $this->getCurrentState()->hasUpdates(); } /** * @inheritdoc */ public function hasNoTranslation() { - - return $this->latestSource == ''; + return $this->getCurrentState()->hasNoTranslation(); } /** * @inheritdoc */ public function isUpToDate() { + return $this->getCurrentState()->isUpToDate(); + } - return $this->latestSource == 'current'; + /** + * Returns the server pattern. + * @return string + */ + public function getServerPattern() { + return $this->serverPattern; } /** @@ -433,16 +328,16 @@ */ protected function initState($langcode) { - if (!isset($this->stateByLanguage[$langcode])) { - $state = new \stdClass(); - $state->timestamp = 0; - $state->lastChecked = 0; - $state->latestSource = ''; - $state->latestSourceTimestamp = 0; - $state->localSource = $this->buildLocalSource($langcode); - $state->remoteSource = $this->buildRemoteSource($langcode); - - $this->stateByLanguage[$langcode] = $state; + if (!$this->getCurrentState()) { + $data = array( + 'timestamp' => 0, + 'lastChecked' => 0, + 'latestSource' => '', + 'latestSourceTimestamp' => 0, + 'localSource' => LocalProjectSource::create($this, $langcode), + 'remoteSource' => RemoteProjectSource::create($this, $langcode), + ); + $this->setCurrentState(LocaleProjectTranslationState::create($data), $langcode); } } @@ -468,97 +363,20 @@ } - /** - * Builds a local translation source object. - * - * @param string $langcode - * Language code of the translation source. - * - * @return \stdClass - * Local source object. - */ - protected function buildLocalSource($langcode) { - - $filename = \Drupal::config('locale.settings')->get('translation.default_filename'); - $source = new \stdClass(); - if ($this->fileIsRemote($this->serverPattern)) { - $source->filename = $this->buildServerPattern($filename, $langcode); - $source->directory = 'translations://'; - $source->uri = $source->directory . $source->filename; - } - else { - $source->filename = $this->buildServerPattern(basename($this->serverPattern), $langcode); - $source->directory = $this->buildServerPattern(drupal_dirname($this->serverPattern), $langcode); - $source->uri = $source->directory . '/' . $source->filename; - } - - return $source; - } - - /** - * Builds a local translation source object. - * - * @param string $langcode - * Language code of the translation source. - * - * @return \stdClass - * Remote source object. + /**r + * Returns the current LocalProjectState depending on the language. + * @return LocaleProjectTranslationState */ - protected function buildRemoteSource($langcode) { - $source = NULL; - - if ($this->fileIsRemote($this->serverPattern)) { - $source = new \stdClass(); - $source->filename = $this->buildServerPattern(basename($this->serverPattern), $langcode); - $source->uri = $this->buildServerPattern($this->serverPattern, $langcode); - } - - return $source; + protected function getCurrentState() { + return isset($this->stateByLanguage[$this->langcode]) ? $this->stateByLanguage[$this->langcode] : null; } /** - * Builds uri of a translation file out of a server path replacement pattern. - * - * @param string $template - * Uri template string containing placeholders. Available placeholders: - * - "%project": Project name. - * - "%version": Project version. - * - "%core": Project core version. - * - "%language": Language code. + * @param LocaleProjectTranslationState $state + * The project translation state. * @param string $langcode - * Language code of the translation file. - * - * @return string - * File uri with replaced placeholders. + * The language code. */ - protected function buildServerPattern($template, $langcode) { - - $variables = array( - '%project' => $this->id, - '%version' => $this->projectVersion, - '%core' => $this->projectCore, - '%language' => $langcode, - ); - - return strtr($template, $variables); - } - - /** - * Returns whether the file URI is at a remote translation source. - * - * @param $uri - * File URI. May include a scheme. - * - * @return bool - * Returns TRUE if the file scheme indicates a remote file. FALSE if no - * scheme is supplied or files are stored at the local file system. - */ - protected function fileIsRemote($uri) { - - $scheme = file_uri_scheme($uri); - if ($scheme) { - return !drupal_realpath($scheme . '://'); - } - - return FALSE; + protected function setCurrentState($state, $langcode) { + $this->stateByLanguage[$langcode] = $state; } } diff -u b/core/modules/locale/src/LocaleTranslatableProjectInterface.php b/core/modules/locale/src/LocaleTranslatableProjectInterface.php --- b/core/modules/locale/src/LocaleTranslatableProjectInterface.php +++ b/core/modules/locale/src/LocaleTranslatableProjectInterface.php @@ -2,250 +2,147 @@ - /** * @file - * Contains Drupal\locale\LocaleTranslatableProjectInterface. + * Contains Drupal\locale\LocaleTranslatableProjectInterface */ - namespace Drupal\locale; + /** - * Defines the locale translatable project interface. + * Provides a translatable project that contains the interface translation + * status of an extension. */ interface LocaleTranslatableProjectInterface { /** - * Initializes the translation project. - * - * @param array $data - * Array of project data. - */ - public function __construct($data); - - /** * Returns the machine name of the project. * - * @return string + * @return * The unique project machine name. */ public function id(); /** - * Returns the human readable name of the project. + * Returns the human readable name of the project * - * @return string + * @return * The human readable project name. */ public function getName(); /** - * Update project data. - * - * @param array $data - * Array of project data. Uses the same data structure as __construct(). + * @inheritdoc */ - public function update($data); + public function hasNoTranslation(); /** - * Serialize the project to an array - * - * @return array - * Project properties to be stored. + * @inheritdoc */ - public function toArray(); + public function initLangcodeMultiple(array $langcodes); /** - * Sets the language this project is translated to. - * - * @param string $langcode - * Language code. + * @inheritdoc */ - public function setLangcode($langcode); + public function getStatus(); /** - * Initializes the project for a language. - * - * @param string $langcode - * Language code. + * @inheritdoc */ - public function initLangcode($langcode); + public function localIsLatest(); /** - * Initializes the project for multiple languages. - * - * @param array $langcodes - * Array of language codes. + * @inheritdoc */ - public function initLangcodeMultiple(array $langcodes); + public function toArray(); /** - * Deletes stored data of specified language. - * - * @param string $langcode - * Language code. + * @inheritdoc */ - public function removeLangcode($langcode); + public function setLangcode($langcode); /** - * Returns the project status. - * - * @return bool - * Returns TRUE if the project is enabled, FALSE otherwise. + * @inheritdoc */ - public function getStatus(); + public function removeLangcode($langcode); /** - * Sets the project status. - * - * @param bool $status - * A flag indicating the project status. TRUE: enabled, FALSE: disabled. + * @inheritdoc */ - public function setStatus($status); - /** - * Deletes translations states of the project for all languages. - */ - public function clearTranslationStates(); + public function hasUpdates(); /** - * Returns the local translation source. - * - * @return \stdClass|NULL - * Local source object or NULL if no remote translation source is available. + * @inheritdoc */ public function getLocalSource(); /** - * Returns the remote translation source. - * - * @return \stdClass|NULL - * Remote source object or NULL if no remote source is available. + * @inheritdoc */ - public function getRemoteSource(); + public function setLocalSource($data); /** - * Returns the timestamp of the most recent translation source. - * - * @return integer - * Timestamp of a translation source. 0 if no source is available. + * @inheritdoc */ - public function getLatestSourceTimestamp(); + public function isUpToDate(); /** - * Returns the version of the project. - * - * @return string - * Project release version string. + * @inheritdoc */ public function getVersion(); /** - * Overrides the project version. - * - * @param string $version - * Project release version string. + * @inheritdoc */ public function setVersion($version); /** - * Whether the remote source is valid and can be checked or downloaded. - * - * @return bool - * Returns FALSE if the project version is a dev release. No translations - * are available for dev releases. + * @inheritdoc */ - public function remoteIsValid(); + public function setRemoteSource($data); /** - * Whether a remote source file is available and can be downloaded. - * - * @return bool - * Returns FALSE if the project version is a dev release. No translations - * are available for dev releases. + * @inheritdoc */ - public function remoteIsAvailable(); + public function setStatus($status); - /** - * Returns whether the remote source is the latest translation source. - * - * @return bool - * Returns TRUE if the remote translation source is the latest or the only - * available source. FALSE if no remote source is available, the remote - * source is older or the current translation is up to date. - */ - public function remoteIsLatest(); /** - * Returns whether the local source is the latest translation source. - * - * @return bool - * Returns TRUE if the local translation source is the latest or the only - * available source. FALSE if no local source is available, the local - * source is older or the current translation is up to date. + * @inheritdoc */ - public function localIsLatest(); + public function isTranslated(); /** - * Updates the translation state with local source data. - * - * @param \stdClass $data - * Local translation source data. + * @inheritdoc */ - public function setLocalSource($data); + public function getRemoteSource(); /** - * Updates the translation state with remote source data. - * - * @param \stdClass $data - * Remote translation source data. + * @inheritdoc */ - public function setRemoteSource($data); + public function initLangcode($langcode); /** - * Updates the current translation state timestamp. - * - * @param int $timestamp - * Timestamp of a newly imported translation. + * @inheritdoc */ - public function setTimestamp($timestamp); + public function remoteIsLatest(); /** - * Whether this project is translated. - * - * The language the project is translated in, is set using setLanguage(). - * - * @return bool - * Returns TRUE if this project is translated. + * @inheritdoc */ - public function isTranslated(); + public function getLatestSourceTimestamp(); /** - * Whether translation updates are available for this language. - * - * The language the project is translated in, is set using setLanguage(). - * - * @return bool - * Returns TRUE if a local or remote translation update is available. + * @inheritdoc */ - public function hasUpdates(); + public function setTimestamp($timestamp); /** - * Whether the project was translated ever. - * - * The language the project is translated in, is set using setLanguage(). - * - * @return bool - * Returns TRUE if the project was not translated before and no translation - * sources are available. + * @inheritdoc */ - public function hasNoTranslation(); + public function update($data); /** - * Whether the project translation is up to date. - * - * @return bool - * Returns TRUE if the project is translated and the translation is of the - * latest version. Returns FALSE if more recent local or remote translations - * are available or the project has never been translated before. + * Returns the server pattern. + * @return string */ - public function isUpToDate(); - -} + public function getServerPattern(); +} \ No newline at end of file only in patch2: unchanged: --- /dev/null +++ b/core/modules/locale/src/AbstractProjectSource.php @@ -0,0 +1,61 @@ +latestSource = ''; + $this->latestSourceTimestamp = 0; + } + + /** + * Builds uri of a translation file out of a server path replacement pattern. + * + * @param string $template + * Uri template string containing placeholders. Available placeholders: + * - "%project": Project name. + * - "%version": Project version. + * - "%core": Project core version. + * - "%language": Language code. + * @param string $langcode + * Language code of the translation file. + * + * @return string + * File uri with replaced placeholders. + */ + public static function buildServerPattern($project, $langcode) { + $variables = array( + '%project' => $project->id, + '%version' => $project->projectVersion, + '%core' => $project->projectCore, + '%language' => $langcode, + ); + + return strtr($project->serverPattern, $variables); + } + + /** + * Returns whether the file URI is at a remote translation source. + * + * @param $uri + * File URI. May include a scheme. + * + * @return bool + * Returns TRUE if the file scheme indicates a remote file. FALSE if no + * scheme is supplied or files are stored at the local file system. + */ + public static function fileIsRemote($uri) { + + $scheme = file_uri_scheme($uri); + if ($scheme) { + return !drupal_realpath($scheme . '://'); + } + + return FALSE; + } +} \ No newline at end of file only in patch2: unchanged: --- /dev/null +++ b/core/modules/locale/src/LocalProjectSource.php @@ -0,0 +1,32 @@ +get('translation.default_filename'); + $source = new \stdClass(); + $server_pattern = $project->getServerPattern(); + if (self::fileIsRemote($server_pattern)) { + $source->filename = self::buildServerPattern($filename, $langcode); + $source->directory = 'translations://'; + $source->uri = $source->directory . $source->filename; + } + else { + $source->filename = self::buildServerPattern(basename($server_pattern), $langcode); + $source->directory = self::buildServerPattern(drupal_dirname($server_pattern), $langcode); + $source->uri = $source->directory . '/' . $source->filename; + } + + return $source; + } + +} \ No newline at end of file only in patch2: unchanged: --- /dev/null +++ b/core/modules/locale/src/LocaleProjectTranslationState.php @@ -0,0 +1,205 @@ + $property) { + $state->{$property_name} = $property; + } + + return $state; + } + + /** + * Returns the timestamp of the latest source. + * @return int + */ + public function getLatestSourceTimestamp() { + return $this->latestSourceTimestamp; + } + + /** + * Returns true if the latest source is remote. + * @return bool + */ + public function remoteIsLatest() { + return $this->latestSource == 'remote'; + } + + /** + * @inheritdoc + */ + public function remoteIsValid() { + $this->remoteSource->isValid(); + } + + /** + * @inheritdoc + */ + public function remoteIsAvailable() { + $this->remoteSource->isAvailable(); + } + + /** + * Returns true if the latest source is local. + * @return bool + */ + public function localIsLatest() { + return $this->latestSource == 'local'; + } + + /** + * Clears translation states for each local and remote source. + * @return void + */ + public function clearTranslationStates() { + if ($this->latestSource == 'local' || $this->latestSource == 'remote') { + $this->localSource->clearTranslationState(); + $this->remoteSource->clearTranslationState(); + } + } + + /** + * Set a new local source from the data array. + * @param $data + * An array containing the data for the local source. + */ + public function setLocalSource($data) { + if (isset($data->timestamp)) { + $local_source = $this->localSource; + $local_source->filename = $data->filename; + $local_source->directory = $data->directory; + $local_source->uri = $data->uri; + $local_source->timestamp = $data->timestamp; + + // If the local translation is the most recent, we update the latestSource + // status. Otherwise only mark the current translation as checked. If remote + // and local have the same timestamp, the local source is chosen. + if ($data->timestamp > $this->latestSourceTimestamp || ($data->timestamp >= $this->latestSourceTimestamp && $this->latestSource == 'remote')) { + $this->latestSource = 'local'; + $this->latestSourceTimestamp = $data->timestamp; + } + else { + $this->lastChecked = REQUEST_TIME; + } + } + + $this->localSource->lastChecked = REQUEST_TIME; + } + + /** + * Set a new remote source from the data array. + * @param $data + * An array containing the data for the local source. + */ + public function setRemoteSource($data) { + if (isset($data->timestamp)) { + $remote_source = $this->remoteSource; + $remote_source->filename = $data->filename; + $remote_source->uri = $data->uri; + $remote_source->timestamp = $data->timestamp; + + // If the remote translation is the most recent, we update the + // latestSource status. Otherwise mark the current translation as checked. + if ($data->timestamp > $this->latestSourceTimestamp) { + $this->latestSource = 'remote'; + $this->latestSourceTimestamp = $data->timestamp; + } + else { + $this->lastChecked = REQUEST_TIME; + } + } + + $this->remoteSource->lastChecked = REQUEST_TIME; + } + + /** + * Sets the time when the translation state was last checked. + * @param $timestamp + * The timestamp when the state was last checked. + */ + public function setTimestamp($timestamp) { + $this->timestamp = $timestamp; + $this->lastChecked = REQUEST_TIME; + + $this->latestSource = 'current'; + $this->latestSourceTimestamp = $timestamp; + } + + /** + * Returns true if the project is translated. + * @return bool + */ + public function isTranslated() { + return $this->timestamp > 0; + } + + /** + * Returns true if the latest source has updates. + * @return bool + */ + public function hasUpdates() { + return $this->latestSource == 'local'|| $this->latestSource == 'remote'; + } + + /** + * Returns true if we don't have translations available. + * @return bool + */ + public function hasNoTranslation() { + return $this->latestSource == ''; + } + + /** + * Returns true if the translations are up to date. + * @return bool + */ + public function isUpToDate() { + return $this->latestSource == 'current'; + } +} \ No newline at end of file only in patch2: unchanged: --- /dev/null +++ b/core/modules/locale/src/LocaleProjectTranslationStateInterface.php @@ -0,0 +1,86 @@ +getServerPattern(); + + if (self::fileIsRemote($server_pattern)) { + $source->filename = self::buildServerPattern(basename($server_pattern), $langcode); + $source->uri = self::buildServerPattern($server_pattern, $langcode); + } + + return $source; + } + + /** + * Returns TRUE if is valid. + * Development release translations are not supported for remote sources. + * @return bool + */ + public function isValid() { + return empty($this->projectVersion) || strpos('dev', $this->projectVersion) === FALSE; + } + + /** + * Return TRUE if remote translation is available. + * @return bool + */ + public function isAvailable() { + return !$this->projectVersion || strpos('dev', $this->projectVersion) === FALSE; + } + +} \ No newline at end of file