diff --git a/classes/install/Install.inc.php b/classes/install/Install.inc.php
index f2ac8ea..207253e 100644
--- a/classes/install/Install.inc.php
+++ b/classes/install/Install.inc.php
@@ -65,6 +65,12 @@ class Install extends PKPInstall {
$this->executeSQL(sprintf('INSERT INTO users (user_id, username, first_name, last_name, password, email, date_registered, date_last_login) VALUES (%d, \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\')', 1, $this->getParam('adminUsername'), $this->getParam('adminUsername'), $this->getParam('adminUsername'), Validation::encryptCredentials($this->getParam('adminUsername'), $this->getParam('adminPassword'), $this->getParam('encryption')), $this->getParam('adminEmail'), Core::getCurrentDate(), Core::getCurrentDate()));
$this->executeSQL(sprintf('INSERT INTO roles (user_id, role_id) VALUES (%d, %d)', 0, 1, ROLE_ID_SITE_ADMIN));
+ // Install email template list and data for each locale
+ $emailTemplateDao =& DAORegistry::getDAO('EmailTemplateDAO');
+ $this->executeSQL($emailTemplateDao->installEmailTemplates($emailTemplateDao->getMainEmailTemplatesFilename(), true));
+ foreach ($this->installedLocales as $locale) {
+ $this->executeSQL($emailTemplateDao->installEmailTemplateData($emailTemplateDao->getMainEmailTemplateDataFilename($locale), true));
+ }
} else {
// Add initial site data
$locale = $this->getParam('locale');
@@ -80,6 +86,13 @@ class Install extends PKPInstall {
return false;
}
+ // Install email template list and data for each locale
+ $emailTemplateDao =& DAORegistry::getDAO('EmailTemplateDAO');
+ $emailTemplateDao->installEmailTemplates($emailTemplateDao->getMainEmailTemplatesFilename());
+ foreach ($this->installedLocales as $locale) {
+ $emailTemplateDao->installEmailTemplateData($emailTemplateDao->getMainEmailTemplateDataFilename($locale));
+ }
+
// Install site settings
$siteSettingsDao =& DAORegistry::getDAO('SiteSettingsDAO');
$siteSettingsDao->installSettings('registry/siteSettings.xml', array(
diff --git a/classes/mail/EmailTemplate.inc.php b/classes/mail/EmailTemplate.inc.php
deleted file mode 100644
index 4371cc2..0000000
--- a/classes/mail/EmailTemplate.inc.php
+++ /dev/null
@@ -1,193 +0,0 @@
-getData('emailId');
- }
-
- /**
- * Set ID of email template.
- * @param $emailId int
- */
- function setEmailId($emailId) {
- return $this->setData('emailId', $emailId);
- }
-
- /**
- * Get key of email template.
- * @return string
- */
- function getEmailKey() {
- return $this->getData('emailKey');
- }
-
- /**
- * Set key of email template.
- * @param $emailKey string
- */
- function setEmailKey($emailKey) {
- return $this->setData('emailKey', $emailKey);
- }
-
- /**
- * Get the enabled setting of email template.
- * @return boolean
- */
- function getEnabled() {
- return $this->getData('enabled');
- }
-
- /**
- * Set the enabled setting of email template.
- * @param $enabled boolean
- */
- function setEnabled($enabled) {
- return $this->setData('enabled', $enabled);
- }
-
- /**
- * Check if email template is allowed to be disabled.
- * @return boolean
- */
- function getCanDisable() {
- return $this->getData('canDisable');
- }
-
- /**
- * Set whether or not email template is allowed to be disabled.
- * @param $canDisable boolean
- */
- function setCanDisable($canDisable) {
- return $this->setData('canDisable', $canDisable);
- }
-
-}
-
-
-/**
- * Email template with data for all supported locales.
- */
-class EmailTemplate extends BaseEmailTemplate {
-
- /** @var $localeData array of localized email template data */
- var $localeData;
-
- /**
- * Constructor.
- */
- function EmailTemplate() {
- parent::BaseEmailTemplate();
- $this->localeData = array();
- }
-
- /**
- * Add a new locale to store data for.
- * @param $locale string
- */
- function addLocale($locale) {
- $this->localeData[$locale] = array();
- }
-
- /**
- * Get set of supported locales for this template.
- * @return array
- */
- function getLocales() {
- return array_keys($this->localeData);
- }
-
- //
- // Get/set methods
- //
-
- /**
- * Get description of email template.
- * @param $locale string
- * @return string
- */
- function getDescription($locale) {
- return isset($this->localeData[$locale]['description']) ? $this->localeData[$locale]['description'] : '';
- }
-
- /**
- * Set description of email template.
- * @param $locale string
- * @param $description string
- */
- function setDescription($locale, $description) {
- $this->localeData[$locale]['description'] = $description;
- }
-
- /**
- * Get subject of email template.
- * @param $locale string
- * @return string
- */
- function getSubject($locale = null) {
- if ($locale === null) $locale = Locale::getLocale();
- return isset($this->localeData[$locale]['subject']) ? $this->localeData[$locale]['subject'] : '';
- }
-
- /**
- * Set subject of email template.
- * @param $locale string
- * @param $subject string
- */
- function setSubject($locale, $subject) {
- $this->localeData[$locale]['subject'] = $subject;
- }
-
- /**
- * Get body of email template.
- * @param $locale string
- * @return string
- */
- function getBody($locale = null) {
- if ($locale === null) $locale = Locale::getLocale();
- return isset($this->localeData[$locale]['body']) ? $this->localeData[$locale]['body'] : '';
- }
-
- /**
- * Set body of email template.
- * @param $locale string
- * @param $body string
- */
- function setBody($locale, $body) {
- $this->localeData[$locale]['body'] = $body;
- }
-}
-
-?>
diff --git a/classes/mail/EmailTemplateDAO.inc.php b/classes/mail/EmailTemplateDAO.inc.php
index 14dc059..7c65aa2 100644
--- a/classes/mail/EmailTemplateDAO.inc.php
+++ b/classes/mail/EmailTemplateDAO.inc.php
@@ -1,167 +1,54 @@
retrieve(
- 'SELECT d.email_key, d.can_edit, d.can_disable, d.enabled
- FROM email_templates AS d
- WHERE d.email_key = ?',
- $emailKey
- );
-
- $returner = null;
- if ($result->RecordCount() != 0) {
- $returner =& $this->_returnEmailTemplateFromRow($result->GetRowAssoc(false));
- }
-
- $result->Close();
- unset($result);
-
+ function &getBaseEmailTemplate($emailKey) {
+ $returner =& parent::getBaseEmailTemplate($emailKey, 0, 0);
return $returner;
}
/**
- * Internal function to return an email template object from a row.
- * @param $row array
- * @return EmailTemplate
- */
- function &_returnEmailTemplateFromRow(&$row) {
- $emailTemplate = new EmailTemplate();
- $emailTemplate->setEmailKey($row['email_key']);
- $emailTemplate->setEnabled($row['enabled']);
- $emailTemplate->setCanDisable($row['can_disable']);
-
- if (!HookRegistry::call('EmailTemplateDAO::_returnEmailTemplateFromRow', array(&$emailTemplate, &$row))) {
- $result =& $this->retrieve(
- 'SELECT d.locale, d.description, d.subject, d.body
- FROM email_templates_data AS d
- WHERE d.email_key = ?',
- $row['email_key']
- );
-
- while (!$result->EOF) {
- $dataRow =& $result->GetRowAssoc(false);
- $emailTemplate->addLocale($dataRow['locale']);
- $emailTemplate->setSubject($dataRow['locale'], $dataRow['subject']);
- $emailTemplate->setBody($dataRow['locale'], $dataRow['body']);
- $emailTemplate->setDescription($dataRow['locale'], $dataRow['description']);
- $result->MoveNext();
- }
- $result->Close();
- unset($result);
- }
-
- return $emailTemplate;
- }
-
- /**
- * Insert a new base email template.
- * @param $emailTemplate BaseEmailTemplate
- */
- function insertBaseEmailTemplate(&$emailTemplate) {
- return $this->update(
- 'INSERT INTO email_templates
- (email_key, enabled)
- VALUES
- (?, ?)',
- array(
- $emailTemplate->getEmailKey(),
- $emailTemplate->getEnabled() == null ? 0 : 1
- )
- );
- $emailTemplate->setEmailId($this->getInsertEmailId());
- return $emailTemplate->getEmailId();
- }
-
- /**
- * Update an existing base email template.
- * @param $emailTemplate BaseEmailTemplate
+ * Retrieve localized email template by key.
+ * @param $emailKey string
+ * @return LocaleEmailTemplate
*/
- function updateBaseEmailTemplate(&$emailTemplate) {
- return $this->update(
- 'UPDATE email_templates
- SET enabled = ?
- WHERE email_key = ?',
- array(
- $emailTemplate->getEnabled() == null ? 0 : 1,
- $emailTemplate->getEmailKey()
- )
- );
- }
-
- /**
- * Insert a new localized email template.
- * @param $emailTemplate EmailTemplate
- */
- function insertEmailTemplate(&$emailTemplate) {
- $this->insertBaseEmailTemplate($emailTemplate);
- return $this->updateEmailTemplateData($emailTemplate);
+ function &getLocaleEmailTemplate($emailKey) {
+ $returner =& parent::getLocaleEmailTemplate($emailKey, 0, 0);
+ return $returner;
}
/**
- * Insert/update locale-specific email template data.
- * @param $emailTemplate LocaleEmailTemplate
+ * Retrieve an email template by key.
+ * @param $emailKey string
+ * @param $locale string
+ * @return EmailTemplate
*/
- function updateEmailTemplateData(&$emailTemplate) {
- foreach ($emailTemplate->getLocales() as $locale) {
- $result =& $this->retrieve(
- 'SELECT COUNT(*) FROM email_templates_data
- WHERE email_key = ? AND locale = ?',
- array($emailTemplate->getEmailKey(), $locale)
- );
-
- if ($result->fields[0] == 0) {
- $this->update(
- 'INSERT INTO email_templates_data
- (email_key, locale, subject, body)
- VALUES
- (?, ?, ?, ?)',
- array($emailTemplate->getEmailKey(), $locale, $emailTemplate->getSubject($locale), $emailTemplate->getBody($locale))
- );
-
- } else {
- $this->update(
- 'UPDATE email_templates_data
- SET subject = ?,
- body = ?
- WHERE email_key = ? AND locale = ?',
- array($emailTemplate->getSubject($locale), $emailTemplate->getBody($locale), $emailTemplate->getEmailKey(), $locale)
- );
- }
-
- $result->Close();
- unset($result);
- }
+ function &getEmailTemplate($emailKey, $locale) {
+ $returner =& parent::getEmailTemplate($emailKey, $locale, 0, 0);
+ return $returner;
}
/**
@@ -169,38 +56,36 @@ class EmailTemplateDAO extends DAO {
* @param $emailKey string
*/
function deleteEmailTemplateByKey($emailKey) {
- return $this->update(
- 'DELETE FROM email_templates WHERE email_key = ?',
- $emailKey
- );
+ return parent::deleteEmailTemplateByKey($emailKey, 0, 0);
}
/**
- * Get the ID of the last inserted email template.
- * @return int
+ * Retrieve all email templates.
+ * @param $locale string
+ * @param $rangeInfo object optional
+ * @return array Email templates
*/
- function getInsertEmailId() {
- return $this->getInsertId('email_templates', 'emailId');
+ function &getEmailTemplates($locale, $rangeInfo = null) {
+ $returner =& parent::getEmailTemplates($locale, 0, 0, $rangeInfo);
+ return $returner;
}
/**
- * Delete all email templates for a specific locale.
- * @param $locale string
+ * Check if a template exists with the given email key.
+ * @param $emailKey string
+ * @return boolean
*/
- function deleteEmailTemplatesByLocale($locale) {
- $this->update(
- 'DELETE FROM email_templates_data WHERE locale = ?', $locale
- );
+ function templateExistsByKey($emailKey) {
+ return parent::templateExistsByKey($emailKey, 0, 0);
}
/**
- * Delete all default email templates for a specific locale.
- * @param $locale string
+ * Check if a custom template exists with the given email key for a journal.
+ * @param $emailKey string
+ * @return boolean
*/
- function deleteDefaultEmailTemplatesByLocale($locale) {
- /*$this->update(
- 'DELETE FROM email_templates_default_data WHERE locale = ?', $locale
- );*/ // Not used in Harvester, but called from PKP. FIXME
+ function customTemplateExistsByKey($emailKey) {
+ return parent::customTemplateExistsByKey($emailKey, 0, 0);
}
}
diff --git a/dbscripts/xml/data/locale/en_US/email_templates_data.xml b/dbscripts/xml/data/locale/en_US/email_templates_data.xml
deleted file mode 100644
index 03a42c1..0000000
--- a/dbscripts/xml/data/locale/en_US/email_templates_data.xml
+++ /dev/null
@@ -1,81 +0,0 @@
-
-
-
-
-
-
-
- en_US
-
-
-
- PASSWORD_RESET_CONFIRM
- Password Reset Confirmation
- We have received a request to reset your password for the {$siteTitle} web site.
-
-If you did not make this request, please ignore this email and your password will not be changed. If you wish to reset your password, click on the below URL.
-
-Reset my password: {$url}
-
-{$principalContactSignature}
- This email is sent to a registered user when they indicate that they have forgotten their password or are unable to login. It provides a URL they can follow to reset their password.
-
-
- PASSWORD_RESET
- Password Reset
- Your password has been successfully reset for use with the {$siteTitle} web site. Please retain this username and password, as it is necessary for all work with the site.
-
-Your username: {$username}
-Your new password: {$password}
-
-{$principalContactSignature}
- This email is sent to a registered user when they have successfully reset their password following the process described in the PASSWORD_RESET_CONFIRM email.
-
-
- USER_REGISTER
- Journal Registration
- {$userFullName}
-
-You have now been registered as a user. We have included your username and password in this email, which are needed for all work with this website. At any point, you can ask to be removed from the list of users by contacting me.
-
-Username: {$username}
-Password: {$password}
-
-Thank you,
-{$principalContactSignature}
- This email is sent to a newly registered user to welcome them to the system and provide them with a record of their username and password.
-
-
- USER_VALIDATE
- Validate Your Account
- {$userFullName}
-
-You have created an account, but before you can start using it, you need to validate your email account. To do this, simply follow the link below:
-
-{$activateUrl}
-
-Thank you,
-{$principalContactSignature}
- This email is sent to a newly registered user to welcome them to the system and provide them with a record of their username and password.
-
-
- NEW_ARCHIVE_NOTIFY
- Archive Added: {$archiveTitle}
- A new archive, "{$archiveTitle}", has been submitted for addition to {$siteTitle}. To index this archive, log in at {$loginUrl} or use the command-line harvester tool.
- This email is sent to the site administrator to notify them about a new archive submission that needs to be indexed.
-
-
-
diff --git a/dbscripts/xml/data/locale/te_ST/email_templates_data.xml b/dbscripts/xml/data/locale/te_ST/email_templates_data.xml
deleted file mode 100644
index 96681f6..0000000
--- a/dbscripts/xml/data/locale/te_ST/email_templates_data.xml
+++ /dev/null
@@ -1,31 +0,0 @@
-
-
-
-
-
-
-
- en_US
-
-
-
- NEW_ARCHIVE_NOTIFY
- Archive Added: {$archiveTitle}
- A new archive, "{$archiveTitle}", has been submitted for addition to {$siteTitle}. To index this archive, log in at {$loginUrl} or use the command-line harvester tool.
- This email is sent to the site administrator to notify them about a new archive submission that needs to be indexed.
-
-
-
diff --git a/dbscripts/xml/harvester2_schema.xml b/dbscripts/xml/harvester2_schema.xml
index c6c853a..1aa66b8 100644
--- a/dbscripts/xml/harvester2_schema.xml
+++ b/dbscripts/xml/harvester2_schema.xml
@@ -212,61 +212,6 @@
-
-
-
-
-
-
-
- Unique identifier for this email.
-
-
-
-
-
-
-
-
-
-
-
-
-
- Email templates.
-
- email_key
-
-
-
-
-
-
-
-
- Unique identifier for this email.
-
-
-
-
-
-
-
-
-
-
- Default data for email templates.
-
-
-
diff --git a/dbscripts/xml/install.xml b/dbscripts/xml/install.xml
index 4c5d624..a4ee1be 100644
--- a/dbscripts/xml/install.xml
+++ b/dbscripts/xml/install.xml
@@ -23,8 +23,6 @@
-
-
diff --git a/locale/en_US/emailTemplates.xml b/locale/en_US/emailTemplates.xml
new file mode 100644
index 0000000..58f25f3
--- /dev/null
+++ b/locale/en_US/emailTemplates.xml
@@ -0,0 +1,64 @@
+
+
+
+
+
+ Password Reset Confirmation
+ We have received a request to reset your password for the {$siteTitle} web site.
+
+If you did not make this request, please ignore this email and your password will not be changed. If you wish to reset your password, click on the below URL.
+
+Reset my password: {$url}
+
+{$principalContactSignature}
+ This email is sent to a registered user when they indicate that they have forgotten their password or are unable to login. It provides a URL they can follow to reset their password.
+
+
+ Password Reset
+ Your password has been successfully reset for use with the {$siteTitle} web site. Please retain this username and password, as it is necessary for all work with the site.
+
+Your username: {$username}
+Your new password: {$password}
+
+{$principalContactSignature}
+ This email is sent to a registered user when they have successfully reset their password following the process described in the PASSWORD_RESET_CONFIRM email.
+
+
+ Journal Registration
+ {$userFullName}
+
+You have now been registered as a user. We have included your username and password in this email, which are needed for all work with this website. At any point, you can ask to be removed from the list of users by contacting me.
+
+Username: {$username}
+Password: {$password}
+
+Thank you,
+{$principalContactSignature}
+ This email is sent to a newly registered user to welcome them to the system and provide them with a record of their username and password.
+
+
+ Validate Your Account
+ {$userFullName}
+
+You have created an account, but before you can start using it, you need to validate your email account. To do this, simply follow the link below:
+
+{$activateUrl}
+
+Thank you,
+{$principalContactSignature}
+ This email is sent to a newly registered user to welcome them to the system and provide them with a record of their username and password.
+
+
+ Archive Added: {$archiveTitle}
+ A new archive, "{$archiveTitle}", has been submitted for addition to {$siteTitle}. To index this archive, log in at {$loginUrl} or use the command-line harvester tool.
+ This email is sent to the site administrator to notify them about a new archive submission that needs to be indexed.
+
+
diff --git a/registry/emailTemplates.xml b/registry/emailTemplates.xml
new file mode 100644
index 0000000..cd902ec
--- /dev/null
+++ b/registry/emailTemplates.xml
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/tools/localeCheck.php b/tools/localeCheck.php
deleted file mode 100644
index 2e04494..0000000
--- a/tools/localeCheck.php
+++ /dev/null
@@ -1,331 +0,0 @@
-locales = $args;
- }
-
- /**
- * Print command usage information.
- */
- function usage() {
- echo "Script to test locales for consistency\n"
- . "Usage: {$this->scriptName} [localeName (optional)] ...\n";
- }
-
- /**
- * Test locales.
- */
- function execute() {
- // Flush the file cache just to be certain we're using
- // the most recent stuff
- import('lib.pkp.classes.cache.CacheManager');
- $cacheManager =& CacheManager::getManager();
- $cacheManager->flush('locale');
-
- // Load plugins so that their locale data is included too
- $plugins = array();
- foreach (PluginRegistry::getCategories() as $category) {
- echo "Loading plugin category \"$category\"...\n";
- $morePlugins = PluginRegistry::loadCategory($category);
- if (is_array($morePlugins)) $plugins += $morePlugins;
- }
-
- foreach (Locale::getAllLocales() as $locale => $name) {
- if (!empty($this->locales) && !in_array($locale, $this->locales)) continue;
- if ($locale != MASTER_LOCALE) {
- echo "Testing locale \"$name\" ($locale) against reference locale " . MASTER_LOCALE . ".\n";
- $this->testLocale($locale, MASTER_LOCALE, $plugins);
- $this->testEmails($locale, MASTER_LOCALE);
- }
- }
- }
-
- function testEmails($locale, $referenceLocale) {
- import('lib.pkp.classes.install.Installer'); // Bring in data dir
-
- $errors = array(
- );
-
- $xmlParser = new XMLParser();
- $referenceEmails =& $xmlParser->parse(
- INSTALLER_DATA_DIR . "/data/locale/$referenceLocale/email_templates_data.xml"
- );
- $emails =& $xmlParser->parse(
- INSTALLER_DATA_DIR . "/data/locale/$locale/email_templates_data.xml"
- );
- $emailsTable =& $emails->getChildByName('table');
- $referenceEmailsTable =& $referenceEmails->getChildByName('table');
- $matchedReferenceEmails = array();
-
- // Pass 1: For all translated emails, check that they match
- // against reference translations.
- for ($emailIndex = 0; ($email =& $emailsTable->getChildByName('row', $emailIndex)) !== null; $emailIndex++) {
- // Extract the fields from the email to be tested.
- $fields = $this->extractFields($email);
-
- // Locate the reference email and extract its fields.
- for ($referenceEmailIndex = 0; ($referenceEmail =& $referenceEmailsTable->getChildByName('row', $referenceEmailIndex)) !== null; $referenceEmailIndex++) {
- $referenceFields = $this->extractFields($referenceEmail);
- if ($referenceFields['email_key'] == $fields['email_key']) break;
- }
-
- // Check if a matching reference email was found.
- if (!isset($referenceEmail) || $referenceEmail === null) {
- $errors[EMAIL_ERROR_EXTRA_EMAIL][] = array(
- 'key' => $fields['email_key']
- );
- continue;
- }
-
- // We've successfully found a matching reference email.
- // Compare it against the translation.
- $bodyParams = $this->getParameterNames($fields['body']);
- $referenceBodyParams = $this->getParameterNames($referenceFields['body']);
- if ($bodyParams !== $referenceBodyParams) {
- $errors[EMAIL_ERROR_DIFFERING_PARAMS][] = array(
- 'key' => $fields['email_key'],
- 'mismatch' => array_diff($bodyParams, $referenceBodyParams)
- );
- }
-
- $subjectParams = $this->getParameterNames($fields['subject']);
- $referenceSubjectParams = $this->getParameterNames($referenceFields['subject']);
-
- if ($subjectParams !== $referenceSubjectParams) {
- $errors[EMAIL_ERROR_DIFFERING_PARAMS][] = array(
- 'key' => $fields['email_key'],
- 'mismatch' => array_diff($subjectParams, $referenceSubjectParams)
- );
- }
-
- $matchedReferenceEmails[] = $fields['email_key'];
-
- unset($email);
- unset($referenceEmail);
- }
-
- // Pass 2: Make sure that there are no missing translations.
- for ($referenceEmailIndex = 0; ($referenceEmail =& $referenceEmailsTable->getChildByName('row', $referenceEmailIndex)) !== null; $referenceEmailIndex++) {
- // Extract the fields from the email to be tested.
- $referenceFields = $this->extractFields($referenceEmail);
- if (!in_array($referenceFields['email_key'], $matchedReferenceEmails)) {
- $errors[EMAIL_ERROR_MISSING_EMAIL][] = array(
- 'key' => $referenceFields['email_key']
- );
- }
- }
-
- $this->displayEmailErrors($locale, $errors);
- }
-
- function extractFields(&$node) {
- $returner = array();
- foreach ($node->getChildren() as $field) if ($field->getName() === 'field') {
- $returner[$field->getAttribute('name')] = $field->getValue();
- }
- return $returner;
- }
-
- function displayEmailErrors($locale, $errors) {
- ksort($errors);
- echo "\nERROR REPORT FOR EMAILS IN \"$locale\":\n";
- echo "-----------------------------------\n";
- foreach ($errors as $type => $errorList) {
- if (!empty($errorList)) switch ($type) {
- case EMAIL_ERROR_MISSING_EMAIL:
- echo "The following messages are missing from the emails file and need translation.\n";
- foreach ($errorList as $error) echo " - " . $error['key'] . "\n";
- break;
- case EMAIL_ERROR_EXTRA_EMAIL:
- echo "\nThe following emails are not in the master translation and may be deleted:\n";
- foreach ($errorList as $error) echo " - " . $error['key'] . "\n";
- break;
- case EMAIL_ERROR_DIFFERING_PARAMS:
- echo "\nThe following emails are missing parameters or have extra parameters and need\n";
- echo "correcting against the master translation:\n";
- foreach ($errorList as $error) {
- echo " - " . $error['key'] . "\n";
- echo " Mismatching parameter(s): " . implode(', ', $error['mismatch']) . "\n";
- }
- break;
- default: die("Unknown error type \"$type\"!\n");
- }
- }
- }
-
- function testLocale($locale, $referenceLocale, $plugins) {
- $errors = array(
- LOCALE_ERROR_MISSING_KEY => array(),
- LOCALE_ERROR_EXTRA_KEY => array(),
- LOCALE_ERROR_SUSPICIOUS_LENGTH => array(),
- LOCALE_ERROR_DIFFERING_PARAMS => array()
- );
-
- $localeCache =& Locale::_getCache($locale);
- $referenceLocaleCache =& Locale::_getCache($referenceLocale);
-
- $localeContents =& $localeCache->getContents();
- $referenceContents =& $referenceLocaleCache->getContents();
-
- // Add locale data for plugins
- foreach ($plugins as $plugin) {
- $pluginCache = $plugin->_getCache($locale);
- $referencePluginCache = $plugin->_getCache($referenceLocale);
- $localeContents += $pluginCache->getContents();
- $referenceContents += $referencePluginCache->getContents();
- }
-
- foreach ($referenceContents as $key => $referenceValue) {
- if (!isset($localeContents[$key])) {
- $errors[LOCALE_ERROR_MISSING_KEY][] = array(
- 'key' => $key,
- 'locale' => $locale
- );
- continue;
- }
- $value = $localeContents[$key];
-
- // Watch for suspicious lengths.
- if (!$this->checkLengths($referenceValue, $value)) {
- $errors[LOCALE_ERROR_SUSPICIOUS_LENGTH][] = array(
- 'key' => $key,
- 'locale' => $locale,
- 'reference' => $referenceValue,
- 'value' => $value
- );
- }
-
- $referenceParams = $this->getParameterNames($referenceValue);
- $params = $this->getParameterNames($value);
- if ($referenceParams !== $params) {
- $errors[LOCALE_ERROR_DIFFERING_PARAMS][] = array(
- 'key' => $key,
- 'locale' => $locale,
- 'mismatch' => array_diff($referenceParams, $params)
- );
- }
- // After processing a key, remove it from the list;
- // this way, the remainder at the end of the loop
- // will be extra unnecessary keys.
- unset($localeContents[$key]);
- }
-
- // Leftover keys are extraneous.
- foreach ($localeContents as $key => $value) {
- $errors[LOCALE_ERROR_EXTRA_KEY][] = array(
- 'key' => $key,
- 'locale' => $locale
- );
- }
-
- $this->displayLocaleErrors($locale, $errors);
- }
-
- function displayLocaleErrors($locale, $errors) {
- ksort($errors);
- echo "\nERROR REPORT FOR LOCALE STRINGS IN \"$locale\":\n";
- echo "---------------------------------------\n";
- foreach ($errors as $type => $errorList) {
- if (!empty($errorList)) switch ($type) {
- case LOCALE_ERROR_MISSING_KEY:
- echo "The following keys are missing from a locale file and need to be translated.\n";
- foreach ($errorList as $error) echo " - " . $error['key'] . "\n";
- break;
- case LOCALE_ERROR_EXTRA_KEY:
- echo "\nThe following keys are not in the master translation and may be deleted:\n";
- foreach ($errorList as $error) echo " - " . $error['key'] . "\n";
- break;
- case LOCALE_ERROR_SUSPICIOUS_LENGTH:
- echo "\nThe following keys have suspicious lengths compared with the master translation\n";
- echo "and may need checking:\n";
- foreach ($errorList as $error) {
- $reference = $this->truncate($error['reference'], 65);
- $value = $this->truncate($error['value'], 65);
- echo " - " . $error['key'] . "\n";
- echo " \"" . $reference . "\" vs.\n";
- echo " \"" . $value . "\" ($locale)\n";
- }
- break;
- case LOCALE_ERROR_DIFFERING_PARAMS:
- echo "\nThe following keys are missing parameters or have extra parameters and need\n";
- echo "correcting against the master translation:\n";
- foreach ($errorList as $error) {
- echo " - " . $error['key'] . "\n";
- echo " Mismatching parameter(s): " . implode(', ', $error['mismatch']) . "\n";
- }
- break;
- default: die("Unknown error type \"$type\"!\n");
- }
- }
- }
-
- function truncate($value, $length = 80, $ellipsis = '...') {
- if (String::strlen($value) > $length) {
- $value = String::substr($value, 0, $length - String::strlen($ellipsis));
- return $value . $ellipsis;
- }
- return $value;
- }
-
- function checkLengths($reference, $value) {
- $referenceLength = String::strlen($reference);
- $length = String::strlen($value);
- $lengthDifference = abs($referenceLength - $length);
- if ($referenceLength == 0) return false;
- if ($lengthDifference / $referenceLength > 1 && $lengthDifference > 10) return false;
- return true;
- }
-
- /**
- * Given a locale string, get the list of parameter references of the
- * form {$myParameterName}.
- * @param $source string
- * @return array
- */
- function getParameterNames($source) {
- $matches = null;
- String::regexp_match_get('/({\$[^}]+})/' /* '/{\$[^}]+})/' */, $source, $matches);
- array_shift($matches); // Knock the top element off the array
- return $matches;
- }
-}
-
-$tool = new localeCheck(isset($argv) ? $argv : array());
-$tool->execute();
-
-?>