getConfig('allowexportalldb'); $sOutput=_outputDBDescription($sDbName, $bAllowExportAllDb); if ($bEchoOutput) { echo $sOutput; } if (!is_null($sFileName)) { $oFile=fopen($sFileName,'w'); fwrite($oFile,$sOutput); } else { $oFile=null; } _outputDBData($bAllowExportAllDb, $bEchoOutput, $sFileName, $oFile); if (!is_null($sFileName)) { fclose($oFile); } } function _outputDBDescription($sDbName, $bAllowExportAllDb) { $sOutput= '--' . "\n"; $sOutput.= '-- LimeSurvey Database Dump of `' . $sDbName . '`' . "\n"; if (!$bAllowExportAllDb) { $sOutput= '-- Only prefixed tables with: ' . Yii::app()->db->tablePrefix . "\n"; } $sOutput.= '-- Date of Dump: ' . dateShift(date('d-M-Y'), 'd-M-Y', Yii::app()->getConfig('timeadjust')) . "\n"; $sOutput.= '--' . "\n"; return $sOutput; } function _outputDBData($bAllowExportAllDb, $bEchoOutput, $sFileName, $oFile) { $aTables = Yii::app()->db->getSchema()->getTables(); foreach ($aTables as $sTableName => $oTableData) { if ($bAllowExportAllDb && Yii::app()->db->tablePrefix == substr($sTableName, 0, strlen(Yii::app()->db->tablePrefix))) { $sOutput=_outputTableDescription($sTableName); if ($bEchoOutput) { echo $sOutput; } if (!is_null($sFileName)) { fwrite($oFile,$sOutput); } _outputTableData($sTableName, $oTableData, $bEchoOutput, $sFileName, $oFile); } } } /** * Outputs the table structure in sql format */ function _outputTableDescription($sTableName) { $sOutput= "\n".'-- --------------------------------------------------------'."\n\n"; $sOutput.= '--'."\n"; $sOutput.= '-- Table structure for table `'.$sTableName.'`'."\n"; $sOutput.= '--'."\n\n"; $sOutput.= 'DROP TABLE IF EXISTS `'.$sTableName.'`;'."\n"; $aCreateTable = Yii::app()->db->createCommand('SHOW CREATE TABLE '.Yii::app()->db->quoteTableName($sTableName))->queryRow(); $sOutput.= $aCreateTable['Create Table'].';'."\n\n"; return $sOutput; } /** * Outputs the table data in sql format */ function _outputTableData($sTableName, $oTableData, $bEchoOutput, $sFileName, $oFile) { $sOutput= '--'."\n"; $sOutput.= '-- Dumping data for table `'.$sTableName.'`'."\n"; $sOutput.= '--'."\n\n"; $iNbRecords = _countNumberOfEntries($sTableName); if ($iNbRecords > 0) { $iMaxNbRecords = _getMaxNbRecords(); $aFieldNames = array_keys($oTableData->columns); for ($i = 0; $i < ceil($iNbRecords / $iMaxNbRecords); $i++) { $aRecords = Yii::app()->db->createCommand() ->select() ->from($sTableName) ->limit(intval($iMaxNbRecords), ($i != 0 ? ($i * $iMaxNbRecords) + 1 : null)) ->query()->readAll(); $sOutput.=_outputRecords($sTableName, $aFieldNames, $aRecords); if ($bEchoOutput) { echo $sOutput; } if (!is_null($sFileName)) { fwrite($oFile,$sOutput); } $sOutput=''; } $sOutput.= "\n"; } if ($bEchoOutput) { echo $sOutput; } if (!is_null($sFileName)) { fwrite($oFile,$sOutput); } } function _outputRecords($sTableName, $aFieldNames, $aRecords) { $i=0; $sOutput=''; foreach ($aRecords as $aRecord) { if ($i==0){ $sOutput.= 'INSERT INTO `' . $sTableName . "` ("; foreach ($aFieldNames as $sFieldName) { $sOutput.= '`'.$sFieldName.'`,'; } $sOutput=substr($sOutput,0,-1); $sOutput.=") VALUES\n"; } $sOutput.= '('; foreach ($aFieldNames as $sFieldName) { if (isset($aRecord[$sFieldName]) && !is_null($aRecord[$sFieldName])) { $sValue= addcslashes($aRecord[$sFieldName],"'"); $sValue = preg_replace("#\n#", "\\n", $sValue); $sOutput.= "'" . $sValue . "'"; } else { $sOutput.= 'NULL'; } if (end($aFieldNames) != $sFieldName) { $sOutput.= ', '; } } $i++; if ($i==200 || (end($aRecords) == $aRecord)) { $sOutput.= ');' . "\n"; $i=0; } else { $sOutput.= '),' . "\n"; } } return $sOutput; } function _countNumberOfEntries($sTableName) { $aNumRows = Yii::app()->db->createCommand('SELECT COUNT(*) FROM ' . Yii::app()->db->quoteTableName($sTableName))->queryRow(); $iNumRows = $aNumRows['COUNT(*)']; return $iNumRows; } function _getMaxNbRecords() { $iMaxRecords = (int)Yii::app()->getConfig('maxdumpdbrecords'); if ($iMaxRecords < 1) { $iMaxRecords = 2500; return $iMaxRecords; // default } return $iMaxRecords; } /** * Get the database name */ function _getDbName() { // Yii doesn't give us a good way to get the database name preg_match('/dbname=([^;]*)/', Yii::app()->db->getSchema()->getDbConnection()->connectionString, $aMatches); $sDbName = $aMatches[1]; return $sDbName; }