Exemple #1
0
 /**
  * Initialise the DB
  *
  * @return $this
  */
 protected function _initDb()
 {
     if (!is_null(self::$_db)) {
         return $this;
     }
     self::$_db = false;
     /**
      * Before connecting to the database
      * Map the WordPress table names with the table prefix
      */
     $wordpressEntities = (array) Mage::app()->getConfig()->getNode()->wordpress->database->before_connect->tables;
     $tablePrefix = $this->getTablePrefix();
     foreach ($wordpressEntities as $entity => $table) {
         Mage::getSingleton('core/resource')->setMappedTableName((string) $table->table, $tablePrefix . $table->table);
     }
     if ($this->getBlogId() > 1) {
         $networkTablePrefix = $this->getTablePrefix() . $this->getBlogId() . '_';
         $entities = (array) Mage::app()->getConfig()->getNode()->wordpress->database->before_connect->tables_mu;
         foreach ($entities as $entity => $table) {
             Mage::getSingleton('core/resource')->setMappedTableName((string) $table->table, $networkTablePrefix . $table->table);
         }
     }
     if (!Mage::getStoreConfigFlag('wordpress/database/is_shared', $this->getStore()->getId())) {
         // If database not shared, connect to WP database
         $configs = array('model' => 'mysql4', 'active' => '1', 'host' => '', 'username' => '', 'password' => '', 'dbname' => '', 'charset' => 'utf8');
         foreach ($configs as $key => $defaultValue) {
             if ($value = Mage::getStoreConfig('wordpress/database/' . $key, $this->getStore()->getId())) {
                 $configs[$key] = $value;
             }
         }
         foreach (array('username', 'password', 'dbname') as $field) {
             if (isset($configs[$field])) {
                 $configs[$field] = Mage::helper('core')->decrypt($configs[$field]);
             }
         }
         if (!isset($configs['host']) || !$configs['host']) {
             return $this->addError('Database host not defined.');
         }
         try {
             $connection = Mage::getSingleton('core/resource')->createConnection('wordpress', 'pdo_mysql', $configs);
             if (!is_object($connection)) {
                 return $this;
             }
             $connection->getConnection();
             if (!$connection->isConnected()) {
                 return $this->addError('Unable to connect to WordPress database.');
             }
         } catch (Exception $e) {
             return $this->addError($e->getMessage());
         }
         $db = $connection;
     } else {
         $db = Mage::getSingleton('core/resource')->getConnection('core_read');
     }
     try {
         $db->fetchOne($db->select()->from(Mage::getSingleton('core/resource')->getTableName('wordpress/post'), 'ID')->limit(1));
     } catch (Exception $e) {
         return $this->addError($e->getMessage())->addError(sprintf('Unable to query WordPress database. Is the table prefix (%s) correct?', $tablePrefix));
     }
     $db->query('SET NAMES UTF8');
     $wordpressEntities = (array) Mage::app()->getConfig()->getNode()->wordpress->database->after_connect->tables;
     foreach ($wordpressEntities as $entity => $table) {
         Mage::getSingleton('core/resource')->setMappedTableName((string) $table->table, $tablePrefix . $table->table);
     }
     self::$_db = $db;
     return $this;
 }