Skip to content
Extraits de code Groupes Projets
SqlDump.php 3,2 ko
Newer Older
  • Learn to ignore specific revisions
  •  * Piwik - free/libre analytics platform
    
     *
     * @link http://piwik.org
     * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
     */
    
    Thomas Steur's avatar
    Thomas Steur a validé
    namespace Piwik\Tests\Fixtures;
    
    use Piwik\Config;
    
    use Exception;
    
     * Reusable fixture. Loads a SQL dump into the DB.
    
    class SqlDump extends Fixture
    
        public $dateTime = '2012-09-03';
    
        public $period = 'day';
        public $idSite = 'all';
        public $tablesPrefix = 'piwik_';
    
        public $dumpUrl = "http://piwik-team.s3.amazonaws.com/generated-logs-one-day.sql.gz";
    
    
        public function setUp()
        {
            // drop all tables
    
            // download data dump if url supplied
            if (is_file($this->dumpUrl)) {
    
            } else {
                $dumpPath = PIWIK_INCLUDE_PATH . '/tmp/logdump.sql.gz';
    
                $bytesRead = $this->downloadDumpInPath($dumpPath);
    
                // sanity check
                if ($bytesRead <= 40 * 1024 * 1024) {
                    $str = "Could not download sql dump! You can manually download %s into %s";
                    throw new Exception(sprintf($str, $this->dumpUrl, $dumpPath));
    
    diosmosis's avatar
    diosmosis a validé
            if (substr($dumpPath, -3) === ".gz") {
                $deflatedDumpPath = PIWIK_INCLUDE_PATH . '/tmp/logdump.sql'; // TODO: should depend on name of URL
                exec("gunzip -c \"" . $dumpPath . "\" > \"$deflatedDumpPath\"", $output, $return);
                if ($return !== 0) {
                    throw new Exception("gunzip failed: " . implode("\n", $output));
                }
            } else {
                $deflatedDumpPath = $dumpPath;
    
            }
    
            // load the data into the correct database
    
            $user = Config::getInstance()->database['username'];
            $password = Config::getInstance()->database['password'];
    
            Config::getInstance()->database['tables_prefix'] = $this->tablesPrefix;
    
            $cmd = "mysql -u \"$user\" \"--password=$password\" {$this->dbName} < \"" . $deflatedDumpPath . "\" 2>&1";
    
            if ($return !== 0) {
                throw new Exception("Failed to load sql dump: " . implode("\n", $output));
            }
    
            // make sure archiving will be called
    
            Rules::setBrowserTriggerArchiving(true);
    
    
            $this->getTestEnvironment()->configOverride = array(
                'database' => array(
                    'tables_prefix' => $this->tablesPrefix
                )
            );
            $this->getTestEnvironment()->save();
    
    
        public function tearDown()
        {
            // empty
        }
    
    
        /**
         * @param $dumpPath
         * @return int
         */
        protected function downloadDumpInPath($dumpPath)
        {
            $bufferSize = 1024 * 1024;
    
            $dump = fopen($this->dumpUrl, 'rb');
            $outfile = fopen($dumpPath, 'wb');
            $bytesRead = 0;
            while (!feof($dump)) {
                fwrite($outfile, fread($dump, $bufferSize), $bufferSize);
                $bytesRead += $bufferSize;
            }
            fclose($dump);
            fclose($outfile);
            return $bytesRead;
        }