예제 #1
0
 /**
  * Execute the query and log some informations
  *
  * @see DbCore::query()
  */
 public function query($sql)
 {
     $explain = false;
     if (preg_match('/^\\s*explain\\s+/i', $sql) || strpos($sql, _DB_PREFIX_ . 'modules_perfs')) {
         $explain = true;
     }
     if (!$explain) {
         $uniqSql = preg_replace('/[\'"][a-f0-9]{32}[\'"]/', '<span style="color:blue">XX</span>', $sql);
         $uniqSql = preg_replace('/[0-9]+/', '<span style="color:blue">XX</span>', $uniqSql);
         if (!isset($this->uniqQueries[$uniqSql])) {
             $this->uniqQueries[$uniqSql] = 0;
         }
         $this->uniqQueries[$uniqSql]++;
         // No cache for query
         if ($this->disableCache && !stripos($sql, 'SQL_NO_CACHE')) {
             $sql = preg_replace('/^\\s*select\\s+/i', 'SELECT SQL_NO_CACHE ', trim($sql));
         }
         // Get tables in query
         preg_match_all('/(from|join)\\s+`?' . _DB_PREFIX_ . '([a-z0-9_-]+)/ui', $sql, $matches);
         foreach ($matches[2] as $table) {
             if (!isset($this->tables[$table])) {
                 $this->tables[$table] = 0;
             }
             $this->tables[$table]++;
         }
         $start = microtime(true);
     }
     // Execute query
     $result = parent::query($sql);
     if (!$explain) {
         $end = microtime(true);
         $stack = debug_backtrace(false);
         while (preg_match('@[/\\\\]classes[/\\\\]db[/\\\\]@i', $stack[0]['file'])) {
             array_shift($stack);
         }
         $stack_light = array();
         foreach ($stack as $call) {
             $stack_light[] = array('file' => isset($call['file']) ? $call['file'] : 'undefined', 'line' => isset($call['line']) ? $call['line'] : 'undefined');
         }
         $this->queries[] = array('query' => $sql, 'time' => $end - $start, 'stack' => $stack_light);
     }
     return $result;
 }
예제 #2
0
파일: Hook.php 프로젝트: M03G/PrestaShop
 /**
  * Triggers CLDR download
  *
  * @param Event $event
  * @throws \Exception
  * @throws \PrestaShopDatabaseException
  */
 public static function init(Event $event)
 {
     $event->getIO()->write("Init CLDR data download...");
     $root_dir = realpath('');
     $cldr_update = new Update($root_dir . '/translations/');
     $cldr_update->init();
     // If settings file exist
     if (file_exists($root_dir . '/config/settings.inc.php')) {
         //load prestashop config to get locale env
         require $root_dir . '/config/config.inc.php';
         //get each defined languages and fetch cldr datas
         $langs = \DbCore::getInstance()->executeS('SELECT * FROM ' . _DB_PREFIX_ . 'lang');
         foreach ($langs as $lang) {
             $language_code = explode('-', $lang['language_code']);
             if (empty($language_code[1])) {
                 $language_code[1] = $language_code[0];
             }
             $cldr_update->fetchLocale($language_code['0'] . '-' . strtoupper($language_code[1]));
         }
     }
     $event->getIO()->write("Finished...");
 }
 protected static function loadSlaveServers()
 {
     static $is_loaded = null;
     if ($is_loaded !== null) {
         return;
     }
     // Add here your slave(s) server(s) in this file
     if (file_exists(_PS_ROOT_DIR_ . '/config/db_slave_server.inc.php')) {
         self::$_servers = array_merge(self::$_servers, require _PS_ROOT_DIR_ . '/config/db_slave_server.inc.php');
     }
     $is_loaded = true;
 }
예제 #4
0
 /**
  * Get Db object instance
  *
  * @param bool $master Decides whether the connection to be returned by the master server or the slave server
  * @return Db instance
  */
 public static function getInstance($master = true)
 {
     static $id = 0;
     // This MUST not be declared with the class members because some defines (like _DB_SERVER_) may not exist yet (the constructor can be called directly with params)
     if (!self::$_servers) {
         self::$_servers = array(array('server' => _DB_SERVER_, 'user' => _DB_USER_, 'password' => _DB_PASSWD_, 'database' => _DB_NAME_));
     }
     $total_servers = count(self::$_servers);
     if ($master || $total_servers == 1) {
         $id_server = 0;
     } else {
         $id++;
         $id_server = $total_servers > 2 && $id % $total_servers != 0 ? $id : 1;
     }
     if (!isset(self::$instance[$id_server])) {
         $class = Db::getClass();
         self::$instance[$id_server] = new $class(self::$_servers[$id_server]['server'], self::$_servers[$id_server]['user'], self::$_servers[$id_server]['password'], self::$_servers[$id_server]['database']);
     }
     return self::$instance[$id_server];
 }
예제 #5
0
 /**
  * Execute the query and log some informations
  *
  * @see DbCore::query()
  */
 public function query($sql)
 {
     $explain = false;
     if (preg_match('/^\\s*explain\\s+/i', $sql)) {
         $explain = true;
     }
     if (!$explain) {
         $uniqSql = preg_replace('/[0-9]+/', '<span style="color:blue">XX</span>', $sql);
         if (!isset($this->uniqQueries[$uniqSql])) {
             $this->uniqQueries[$uniqSql] = 0;
         }
         $this->uniqQueries[$uniqSql]++;
         // No cache for query
         if ($this->disableCache) {
             $sql = preg_replace('/^\\s*select\\s+/i', 'SELECT SQL_NO_CACHE ', trim($sql));
         }
         // Get tables in quer
         preg_match_all('/(from|join)\\s+`?' . _DB_PREFIX_ . '([a-z0-9_-]+)/ui', $sql, $matches);
         foreach ($matches[2] as $table) {
             if (!isset($this->tables[$table])) {
                 $this->tables[$table] = 0;
             }
             $this->tables[$table]++;
         }
         // Execute query
         $start = microtime(true);
     }
     $result = parent::query($sql);
     if (!$explain) {
         $end = microtime(true);
         // Save details
         $timeSpent = $end - $start;
         $trace = debug_backtrace(false);
         while (preg_match('@[/\\\\]classes[/\\\\]db[/\\\\]@i', $trace[0]['file'])) {
             array_shift($trace);
         }
         $this->queries[] = array('query' => $sql, 'time' => $timeSpent, 'file' => $trace[0]['file'], 'line' => $trace[0]['line']);
     }
     return $result;
 }
예제 #6
0
    /**
     * Get combination images ids
     *
     * @param int $idAttribute
     *
     * @return array
     */
    public function getImages($idAttribute)
    {
        return \DbCore::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
			SELECT a.`id_image` as id
			FROM `' . _DB_PREFIX_ . 'product_attribute_image` a
			' . \ShopCore::addSqlAssociation('product_attribute', 'a') . '
			WHERE a.`id_product_attribute` = ' . (int) $idAttribute . '
		');
    }
예제 #7
0
function importCategories($file, $blindMode)
{
    $date = date('Y-m-d H:i:s');
    $xml = new XMLReader();
    $xml->open($file);
    $depth = array();
    $parentId = null;
    $parents = array();
    $depth = 0;
    $db = DbCore::getInstance();
    $table = 'category';
    $query = new DbQuery();
    $query->select('id_category');
    $query->from($table, 'p');
    $query->where("p.is_root_category = 1");
    $rootCategory = (int) $db->getValue($query);
    while ($xml->read()) {
        if ($xml->nodeType == XmlReader::ELEMENT && $xml->name == 'ctg:name') {
            $xml->read();
            $name = $xml->value;
        }
        if ($xml->nodeType == XmlReader::ELEMENT && $xml->name == 'ctg:description') {
            $xml->read();
            $description = $xml->value;
        }
        if ($xml->nodeType == XmlReader::ELEMENT && $xml->name == 'ctg:sequence') {
            $xml->read();
            $sequence = $xml->value;
        }
        if ($xml->nodeType == XmlReader::ELEMENT && $xml->name == 'ctg:displayed') {
            $xml->read();
            $displayed = $xml->value == 'true';
        }
        if ($xml->nodeType == XmlReader::ELEMENT && $xml->name == 'ctg:id') {
            $xml->read();
            $id = (int) $xml->value;
        }
        if ($xml->nodeType == XmlReader::ELEMENT && $xml->name == 'ctg:internetParams') {
            // hack - we insert category here
            $parent = $rootCategory;
            // root category
            if (isset($parents[$depth])) {
                $parent = $parents[$depth];
            }
            if ($blindMode) {
                try {
                    // hack - we can not save category unless it already exists
                    $db->insert('category', array('id_category' => $id, 'id_parent' => $parent, 'date_add' => $date));
                } catch (Exception $e) {
                }
            } else {
                try {
                    $category = new Category();
                    $category->id = $id;
                    $category->id_category = $id;
                    $category->name = $name;
                    $category->link_rewrite = createUrlSlug($name);
                    $category->description = $description;
                    $category->position = $sequence;
                    $category->id_parent = $parent;
                    $category->active = (int) $displayed;
                    $category->doNotRegenerateNTree = true;
                    $category->save();
                    $db->update('category_shop', array('position' => $sequence), "id_category = '" . $db->escape($id) . "'");
                } catch (Exception $e) {
                    echo $e->getMessage() . " {$id} {$parent}\n";
                }
            }
            // debug
            //echo str_repeat('+', $depth) . ' ' . $id . " ($parent)\n";
        }
        if ($xml->nodeType == XmlReader::ELEMENT && $xml->name == 'ctg:subCategories') {
            $depth++;
            if (!isset($parents[$depth])) {
                $parents[$depth] = $id;
            }
        }
        if ($xml->nodeType == XmlReader::END_ELEMENT && $xml->name == 'ctg:subCategories') {
            unset($parents[$depth]);
            $depth--;
        }
    }
    $xml->close();
    if ($blindMode) {
        $db->update('category', array('date_add' => $date), "date_add = '0000-00-00 00:00:00'");
    }
}
예제 #8
0
파일: process.php 프로젝트: M03G/PrestaShop
 /**
  * Install Cldr Datas
  */
 public function installCldrDatas()
 {
     $cldrUpdate = new Update(_PS_TRANSLATIONS_DIR_);
     $cldrUpdate->init();
     //get each defined languages and fetch cldr datas
     $langs = \DbCore::getInstance()->executeS('SELECT * FROM ' . _DB_PREFIX_ . 'lang');
     foreach ($langs as $lang) {
         $language_code = explode('-', $lang['language_code']);
         if (count($language_code) == 1) {
             $cldrUpdate->fetchLocale($language_code['0']);
         } else {
             $cldrUpdate->fetchLocale($language_code['0'] . '-' . Tools::strtoupper($language_code[1]));
         }
     }
 }
예제 #9
0
if (!empty($content)) {
    $file = "stock" . date("_Y-m-d_H-i-s") . ".xml";
    file_put_contents($file, $content);
}
$dom = new DomDocument();
$dom->load($file);
$xpath = new DOMXPath($dom);
// version 1.0
//$roots = $xpath->query('//rsp:responsePackItem[@state=\'ok\']/lst:listStock[@state=\'ok\']');
// version 2.0
$roots = $xpath->query('//rsp:responsePackItem/lStk:listStock');
//var_dump($roots->length);
//var_dump($roots);
$shopId = 1;
// administration
$db = DbCore::getInstance();
$query = new DbQuery();
$query->select("id_lang");
$query->from("lang");
$query->where("iso_code = 'cs'");
$langId = $db->getValue($query);
$date = date('Y-m-d H:i:s');
if ($roots->length > 0) {
    for ($i = 0; $i < $roots->length; $i++) {
        $products = $xpath->query('./lStk:stock', $roots->item($i));
        for ($j = 0; $j < $products->length; $j++) {
            $node = $products->item($j);
            $id = $xpath->query('./stk:stockHeader/stk:id', $node)->item(0)->nodeValue;
            $name = $xpath->query('./stk:stockHeader/stk:name', $node)->item(0)->nodeValue;
            $shortName = @$xpath->query('./stk:stockHeader/stk:shortName', $node)->item(0)->nodeValue;
            $nameComplement = @$xpath->query('./stk:stockHeader/stk:nameComplement', $node)->item(0)->nodeValue;