MutationDigitale / craft3-translate

Craft CMS plugin for static translations in CP
Other
12 stars 7 forks source link

DbMessageSource seems to be registered too soon #51

Closed goraxan closed 1 year ago

goraxan commented 1 year ago

This error happens if during the Craft install process (while executing the craft install/craft command) one plugin tries to read localized content. That is the case with SEOMatic, which is installed before craft3-translate, at this particular moment https://github.com/nystudio107/craft-seomatic/blob/v4/src/migrations/Install.php#L49C30-L49C30 which eventually tries to read the localized site name https://github.com/nystudio107/craft-seomatic/blob/v4/src/models/MetaSiteVars.php#L144 This error is thrown:

    > installing seomatic ...     > create table {{%seomatic_metabundles}} ... done (time: 0.004s)
    > create index idx_nhsedwsybbesrrtxmxkwxtturlmmdqspwhgy on {{%seomatic_metabundles}} (sourceBundleType) ... done (time: 0.003s)
    > create index idx_rlwjspuclxbzpzfpkxhejklzzdilopaesfqw on {{%seomatic_metabundles}} (sourceId) ... done (time: 0.003s)
    > create index idx_wdozydwhuhpjtwvolgljcchieosrjwoaaoij on {{%seomatic_metabundles}} (sourceSiteId) ... done (time: 0.004s)
    > create index idx_fvdaaenveulhkkgwrrhszekbvwtmbtegdsxz on {{%seomatic_metabundles}} (sourceHandle) ... done (time: 0.003s)
    > add foreign key fk_dophcndeyoyiijzkujrfyjjabxhqcblacqbu: {{%seomatic_metabundles}} (sourceSiteId) references {{%sites}} (id) ... done (time: 0.013s)
Exception: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.source_message' doesn't exist
The SQL being executed was: (SELECT `t1`.`message` AS `message`, `t2`.`translation` AS `translation`
FROM `source_message` `t1`, `message` `t2`
WHERE (`t1`.`id`=`t2`.`id`) AND (`t1`.`category`='site') AND (`t2`.`language`='en-US'))
UNION ALL ( SELECT `t1`.`message` AS `message`, `t2`.`translation` AS `translation`
FROM `source_message` `t1`, `message` `t2`
WHERE ((`t1`.`id`=`t2`.`id`) AND (`t1`.`category`='site') AND (`t2`.`language`='en')) AND (`t2`.`id` NOT IN (SELECT `id`
FROM `message`
WHERE `language`='en-US')) ) (/var/www/html/vendor/yiisoft/yii2/db/Schema.php:676)
#0 /var/www/html/vendor/yiisoft/yii2/db/Command.php(1307): yii\db\Schema->convertException(Object(PDOException), '(SELECT `t1`.`m...')
#1 /var/www/html/vendor/yiisoft/yii2/db/Command.php(1168): yii\db\Command->internalExecute('(SELECT `t1`.`m...')
#2 /var/www/html/vendor/yiisoft/yii2/db/Command.php(410): yii\db\Command->queryInternal('fetchAll', NULL)
#3 /var/www/html/vendor/yiisoft/yii2/i18n/DbMessageSource.php(161): yii\db\Command->queryAll()
#4 /var/www/html/vendor/mutation/translate/src/services/DbMessageSource.php(9): yii\i18n\DbMessageSource->loadMessagesFromDb('site', 'en-US')
#5 /var/www/html/vendor/yiisoft/yii2/i18n/DbMessageSource.php(132): mutation\translate\services\DbMessageSource->loadMessagesFromDb('site', 'en-US')
#6 /var/www/html/vendor/yiisoft/yii2/i18n/MessageSource.php(107): yii\i18n\DbMessageSource->loadMessages('site', 'en-US')
#7 /var/www/html/vendor/yiisoft/yii2/i18n/MessageSource.php(87): yii\i18n\MessageSource->translateMessage('site', 'Massimo Osti', 'en-US')
#8 /var/www/html/vendor/yiisoft/yii2/i18n/I18N.php(90): yii\i18n\MessageSource->translate('site', 'Massimo Osti', 'en-US')
#9 /var/www/html/vendor/craftcms/cms/src/i18n/I18N.php(300): yii\i18n\I18N->translate('site', 'Massimo Osti', Array, 'en-US')
#10 /var/www/html/vendor/yiisoft/yii2/BaseYii.php(538): craft\i18n\I18N->translate('site', 'Massimo Osti', Array, 'en-US')
#11 /var/www/html/vendor/nystudio107/craft-seomatic/src/models/MetaSiteVars.php(144): yii\BaseYii::t('site', 'Massimo Osti')
#12 /var/www/html/vendor/yiisoft/yii2/base/BaseObject.php(109): nystudio107\seomatic\models\MetaSiteVars->init()
#13 /var/www/html/vendor/craftcms/cms/src/base/Model.php(78): yii\base\BaseObject->__construct(Array)
#14 /var/www/html/vendor/nystudio107/craft-seomatic/src/models/MetaSiteVars.php(120): craft\base\Model->__construct(Array)
#15 /var/www/html/vendor/nystudio107/craft-seomatic/src/models/MetaBundle.php(173): nystudio107\seomatic\models\MetaSiteVars::create(Array)
#16 /var/www/html/vendor/nystudio107/craft-seomatic/src/models/MetaBundle.php(144): nystudio107\seomatic\models\MetaBundle->normalizeMetaBundleData(true)
#17 /var/www/html/vendor/nystudio107/craft-seomatic/src/services/MetaBundles.php(255): nystudio107\seomatic\models\MetaBundle::create(Array)
#18 /var/www/html/vendor/nystudio107/craft-seomatic/src/services/MetaBundles.php(1029): nystudio107\seomatic\services\MetaBundles->createGlobalMetaBundleForSite(1)
#19 /var/www/html/vendor/nystudio107/craft-seomatic/src/migrations/Install.php(191): nystudio107\seomatic\services\MetaBundles->createGlobalMetaBundles()
#20 /var/www/html/vendor/nystudio107/craft-seomatic/src/migrations/Install.php(49): nystudio107\seomatic\migrations\Install->insertDefaultData()
#21 /var/www/html/vendor/craftcms/cms/src/db/Migration.php(49): nystudio107\seomatic\migrations\Install->safeUp()
#22 /var/www/html/vendor/craftcms/cms/src/db/MigrationManager.php(233): craft\db\Migration->up(true)
#23 /var/www/html/vendor/craftcms/cms/src/base/Plugin.php(144): craft\db\MigrationManager->migrateUp(Object(nystudio107\seomatic\migrations\Install))
#24 /var/www/html/vendor/craftcms/cms/src/services/Plugins.php(533): craft\base\Plugin->install()
#25 /var/www/html/vendor/craftcms/cms/src/migrations/Install.php(1204): craft\services\Plugins->installPlugin('seomatic')
#26 /var/www/html/vendor/craftcms/cms/src/migrations/Install.php(1103): craft\migrations\Install->_installPlugins()
#27 /var/www/html/vendor/craftcms/cms/src/migrations/Install.php(77): craft\migrations\Install->insertDefaultData()
#28 /var/www/html/vendor/craftcms/cms/src/db/Migration.php(49): craft\migrations\Install->safeUp()
#29 /var/www/html/vendor/craftcms/cms/src/db/MigrationManager.php(233): craft\db\Migration->up(true)
#30 /var/www/html/vendor/craftcms/cms/src/console/controllers/InstallController.php(213): craft\db\MigrationManager->migrateUp(Object(craft\migrations\Install))

My guess is that the DbMessageSource is intercepting the yii\BaseYii::t calls before the plugin is installed (if I remove this line https://github.com/MutationDigitale/craft3-translate/blob/master/src/Translate.php#L69 the Craft installation finishes fine). Maybe the DbMessageSource should be initialized only after the plugin is fully enabled by using the https://docs.craftcms.com/api/v4/craft-services-plugins.html#method-ispluginenabled method?

goraxan commented 1 year ago

I can confirm that everything works as expected if the $this->initDbMessages(); (https://github.com/MutationDigitale/craft3-translate/blob/master/src/Translate.php#L69) is wrapped like this

        if(Craft::$app->getPlugins()->isPluginEnabled('translations-admin'))
            $this->initDbMessages();
smcyr commented 1 year ago

Good catch, thank you! It's fixed in version 3.0.3.