/** * Static method to decompress data * * @param string $data * @return mixed */ public static function decompress($data) { // Decompress the file if (@file_exists($data)) { $bz = bzopen($data, 'r'); $uncompressed = ''; // Read the uncompressed data. while (!feof($bz)) { $uncompressed .= bzread($bz, 4096); } // Close the Bzip2 compressed file and write // the data to the uncompressed file. bzclose($bz); if (stripos($data, '.tbz2') !== false) { $newFile = str_replace('.tbz2', '.tar', $data); } else { if (stripos($data, '.tbz') !== false) { $newFile = str_replace('.tbz', '.tar', $data); } else { $newFile = str_replace('.bz2', '', $data); } } file_put_contents($newFile, $uncompressed); return $newFile; // Else, decompress the string } else { return bzdecompress($data); } }
protected function getLines() { $handle = bzopen($this->file, "r"); if ($handle) { $decompressedData = ''; while (true) { do { if (feof($handle)) { bzclose($handle); return; } $decompressedData .= bzread($handle, 8192); $key = strpos($decompressedData, "\n"); } while ($key === false); do { $line = substr($decompressedData, 0, $key + 1); $decompressedData = substr_replace($decompressedData, '', 0, $key + 1); (yield $line); $key = strpos($decompressedData, "\n"); } while ($key !== false); } } else { throw new \Exception("не удалось открыть файл"); } }
/** * {@inheritdoc} */ protected function _read($length) { $data = bzread($this->_fileHandler, $length); if (false === $data) { throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase('Failed to read data from %1', [$this->_filePath])); } return $data; }
/** * Read data from bz archive * * @throws Mage_Exception * @param int $length * @return string */ protected function _read($length) { $data = bzread($this->_fileHandler, $length); if (false === $data) { throw new Mage_Exception('Failed to read data from ' . $this->_filePath); } return $data; }
function bzfile($file) { $bz = bzopen($file, "r"); $str = ""; while (!feof($bz)) { $str = $str . bzread($bz, 8192); bzclose($bz); } return $str; }
public function read($file) { $open = bzopen($file, 'r'); if (empty($open)) { throw new FileNotFoundException('Error', 'fileNotFound', $file); } $return = bzread($open, 8096); bzclose($open); return $return; }
function bunzip($infile, $outfile) { $string = null; $zp = bzopen($infile, "r"); while (!feof($zp)) { $string .= bzread($zp, 4096); } bzclose($zp); $fp = fopen($outfile, "w"); fwrite($fp, $string, strlen($string)); fclose($fp); }
function test_bzerror() { global $tmpfile; $f = fopen($tmpfile, "w"); fwrite($f, "this is a test"); fclose($f); $f = bzopen($tmpfile, "r"); bzread($f); $ret = bzerror($f); bzclose($f); unlink($tmpfile); VS($ret, array("errno" => -5, "errstr" => "DATA_ERROR_MAGIC")); }
function extractBzip2($src, $dest = false) { $bz = bzopen($src, "r"); $data = ''; while (!feof($bz)) { $data .= bzread($bz, 1024 * 1024); } bzclose($bz); if (empty($dest)) { return $data; } elseif (file_put_contents($dest, $data)) { return $dest; } return false; }
public function read($file = '', $length = 1024, $type = NULL) { if (!is_string($file) || empty($file)) { return Error::set('Error', 'stringParameter', '1.(file)'); } if (!is_numeric($length)) { return Error::set('Error', 'numericParameter', '2.(length)'); } $open = bzopen($file, 'r'); if (empty($open)) { return Error::set('Error', 'fileNotFound', $file); } $return = bzread($open, $length); bzclose($open); return $return; }
/** * @param InputInterface $input * @param OutputInterface $output * @return void * @throws Exception */ protected function execute(InputInterface $input, OutputInterface $output) { /** @var RenaApp $app */ $app = RenaApp::getInstance(); // Setup the url and cache path $url = "https://www.fuzzwork.co.uk/dump/"; $cache = __DIR__ . "/../../cache/update"; // Create the cache dir if it doesn't exist if (!file_exists($cache)) { mkdir($cache); } // Fetch the md5 $md5file = "mysql-latest.tar.bz2.md5"; $md5 = explode(" ", $app->cURL->getData($url . $md5file, 0))[0]; $lastSeenMD5 = $app->Storage->get("ccpdataMD5"); if ($lastSeenMD5 !== $md5 || !$input->getOption("force") === false) { $output->writeln("Updating to latest CCP data dump"); $dbFiles = array("dgmAttributeCategories", "dgmAttributeTypes", "dgmEffects", "dgmTypeAttributes", "dgmTypeEffects", "invFlags", "invGroups", "invTypes", "mapDenormalize", "mapRegions", "mapSolarSystems", "mapConstellations"); $type = ".sql.bz2"; foreach ($dbFiles as $file) { $output->writeln("Updating {$file}"); $dataURL = $url . "latest/" . $file . $type; try { file_put_contents("{$cache}/{$file}{$type}", $app->cURL->getData($dataURL, 0)); $sqlData = bzopen("{$cache}/{$file}{$type}", "r"); // Open the BZip data // Read the BZip data and append it to data $data = ""; while (!feof($sqlData)) { $data .= bzread($sqlData, 4096); } } catch (\Exception $e) { throw new \Exception($e->getMessage()); } // Since rena uses tokuDB, we'll replace InnoDB with TokuDB here, just because we can.. $data = str_replace("ENGINE=InnoDB", "ENGINE=TokuDB", $data); $dataParts = explode(";\n", $data); foreach ($dataParts as $qry) { $query = $qry . ";"; $app->Db->execute($query); } // Remove the stored BZip file from the drive - no need to store it.. unlink("{$cache}/{$file}.sql.bz2"); } $app->Storage->set("ccpdataMD5", $md5); } }
static function load_bz($file_name) { global $wgOfflineWikiPath; $path = "{$wgOfflineWikiPath}/{$file_name}"; if (strlen($file_name) < 1) { return null; } #strange that bzopen doesn't choke on dir. $bz = bzopen($path, "r"); if (!$bz) { return null; } $out = ""; while ($bz && !feof($bz)) { $out .= bzread($bz, 8192); } bzclose($bz); return $out; }
/** * Unpack file by BZIP2 compressor. * * @param string $source * @param string $destination * @return string */ public function unpack($source, $destination) { $data = ''; $bzPointer = bzopen($source, 'r'); if (empty($bzPointer)) { throw new Exception('Can\'t open BZ archive : ' . $source); } while (!feof($bzPointer)) { $data .= bzread($bzPointer, 131072); } bzclose($bzPointer); if (is_dir($destination)) { $file = $this->getFilename($source); $destination = $destination . $file; } echo $destination; $this->_writeFile($destination, $data); return $destination; }
/** * Décompression du contenu. Si path est donné les fichiers sont décompressés dans le répertoire en question, * sinon, ils sont retournés "en mémoire". * * @param filename, path where uncompress * @return buffer uncompressed or boolean for save success if path given */ public function uncompress($filename, $path = null) { if (!($bz = bzopen($filename, "r"))) { return null; } $decompressed_file = ''; while (!feof($bz)) { $decompressed_file .= bzread($bz, 4096); } bzclose($bz); if ($path) { if ($fp = fopen($path, 'w')) { fwrite($fp, $decompressed_file, strlen($decompressed_file)); fclose($fp); return true; } return false; } return $decompressed_file; }
function getFile($full_filename) { $extension = pathinfo($full_filename, PATHINFO_EXTENSION); if ($extension == "bz2") { $bz = bzopen($full_filename, "r"); while (!feof($bz)) $data .= bzread($bz, 4096); bzclose($bz); } else { $bz = fopen($full_filename, "r"); while (!feof($bz)) $data .= fread($bz, 4096); fclose($bz); } return $data; }
/** * */ public function createCCPDB() { $ccpDataURL = "https://www.fuzzwork.co.uk/dump/sqlite-latest.sqlite.bz2"; $ccpDataMD5URL = "https://www.fuzzwork.co.uk/dump/sqlite-latest.sqlite.bz2.md5"; $dbLocation = BASEDIR . "/config/database/"; $md5 = explode(" ", $this->curl->getData($ccpDataMD5URL))[0]; $lastSeenMd5 = $this->storage->get("ccpDataMd5"); // If the last seen md5, isn't equal the current seen md5, we'll update! if ($lastSeenMd5 !== $md5) { try { $this->log->notice("Updating CCP SQLite Database"); $this->log->notice("Downloading bz2 file, and writing it to {$dbLocation}ccpData.sqlite.bz2"); $downloadedData = $this->curl->getLargeData($ccpDataURL, "{$dbLocation}ccpData.sqlite.bz2"); if ($downloadedData == false) { $this->log->warn("Error: File not downloaded successfully!"); die; } $this->log->notice("Opening bz2 file"); $sqliteData = bzopen("{$dbLocation}ccpData.sqlite.bz2", "r"); $this->log->notice("Reading from bz2 file"); $data = ""; while (!feof($sqliteData)) { $data .= bzread($sqliteData, 4096); } $this->log->notice("Writing bz2 file contents into .sqlite file"); file_put_contents("{$dbLocation}ccpData.sqlite", $data); $this->log->notice("Deleting bz2 file"); unlink("{$dbLocation}ccpData.sqlite.bz2"); $this->log->notice("Creating mapCelestials view"); $this->ccpDB->execute("CREATE VIEW mapAllCelestials AS SELECT itemID, itemName, typeName, mapDenormalize.typeID, solarSystemName, mapDenormalize.solarSystemID, mapDenormalize.constellationID, mapDenormalize.regionID, mapRegions.regionName, orbitID, mapDenormalize.x, mapDenormalize.y, mapDenormalize.z FROM mapDenormalize JOIN invTypes ON (mapDenormalize.typeID = invTypes.typeID) JOIN mapSolarSystems ON (mapSolarSystems.solarSystemID = mapDenormalize.solarSystemID) JOIN mapRegions ON (mapDenormalize.regionID = mapRegions.regionID) JOIN mapConstellations ON (mapDenormalize.constellationID = mapConstellations.constellationID)"); $this->log->notice("CCP Database updated!"); $this->storage->set("ccpDataMd5", $md5); } catch (\Exception $e) { $this->log->warn("Error updating the CCPDatabase. Bot can't run"); die; } } }
function download($url, $file) { $tmp = $file . '.bz2'; $fp = fopen($tmp, "w"); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_FILE, $fp); curl_setopt($curl, CURLOPT_HEADER, 0); $result = curl_exec($curl); curl_close($curl); fclose($fp); if ($result == false) { return false; } $bz = bzopen($tmp, "r"); $fp = fopen($file, "w"); while (!feof($bz)) { fwrite($fp, bzread($bz, 4096)); } bzclose($bz); fclose($fp); return $result; }
public static function read($f3, $md5, $filename) { $path_temp = PFH_MD5::get_tmp_file_path($f3, $md5, $filename); // 移除多餘的快取資料 PFH_Archive_cache::clean($f3, $path_temp); if (is_file($path_temp) === FALSE) { $path = PFH_MD5::get_file_path($f3, $md5, $filename); $tmp_dir = PFH_MD5::get_tmp_dir_path($f3, $md5); //$tmp_file = PFH_MD5::get_tmp_file_path($f3, $md5); if (is_file($path)) { // 沒有壓縮的情況 return $path; } //if (is_file($path)) { if (is_file($path . ".zip")) { $archive = new ZipArchive(); //echo $path; $res = $archive->open($path . ".zip"); //$zip->extractTo($tmp_file); $archive->extractTo($tmp_dir); $archive->close(); } else { if (is_file($path . ".bz2")) { $in_file = bzopen($path . ".bz2", "r"); $out_file = fopen($path_temp, "w"); while ($buffer = bzread($in_file, 4096)) { fwrite($out_file, $buffer, 4096); } bzclose($in_file); fclose($out_file); } } } // 加入快取資料 PFH_Archive_cache::add($f3, $path_temp); return $path_temp; }
public function extract($archive = "", $outputDir = "") { $bzh = bzopen($archive, 'r') or die("Could not open {$archive}"); if (!is_dir($outputDir)) { mkdir($outputDir, 0700, true) or die("Could not create a {$outputDir} directory"); } $_archive = str_ireplace('.tbz', '.bz2', $archive); $tar_path = basename($_archive, '.bz2'); $lchar = substr($outputDir, -1); if ('/' == $lchar or '\\' == $lchar) { $tar_path = $outputDir . $tar_path; } else { $tar_path = $outputDir . "/" . $tar_path; } $fh = fopen($tar_path, "w"); while (!feof($bzh)) { $cstr = bzread($bzh, 4096); fwrite($fh, $cstr); } bzclose($bzh); fclose($fh); $this->tarHandler->extract($tar_path, $outputDir); echo "<font color=\"green\">Sucessfull!</font>"; }
/** * Decompresses the given content * * @param string $content * @return string */ public function decompress($content) { $archive = $this->getArchive(); if (file_exists($content)) { $archive = $content; } if (file_exists($archive)) { $file = bzopen($archive, 'r'); if (!$file) { require_once 'Zend/Filter/Exception.php'; throw new Zend_Filter_Exception("Error opening the archive '" . $content . "'"); } $compressed = bzread($file); bzclose($file); } else { $compressed = bzdecompress($content); } if (is_int($compressed)) { require_once 'Zend/Filter/Exception.php'; throw new Zend_Filter_Exception('Error during decompression'); } return $compressed; }
function _openAppend() { if (filesize($this->_tarname) == 0) { return $this->_openWrite(); } if ($this->_compress) { $this->_close(); if (!@rename($this->_tarname, $this->_tarname . ".tmp")) { $this->_error('Error while renaming \'' . $this->_tarname . '\' to temporary file \'' . $this->_tarname . '.tmp\''); return false; } if ($this->_compress_type == 'gz') { $v_temp_tar = @gzopen($this->_tarname . ".tmp", "rb"); } elseif ($this->_compress_type == 'bz2') { $v_temp_tar = @bzopen($this->_tarname . ".tmp", "rb"); } if ($v_temp_tar == 0) { $this->_error('Unable to open file \'' . $this->_tarname . '.tmp\' in binary read mode'); @rename($this->_tarname . ".tmp", $this->_tarname); return false; } if (!$this->_openWrite()) { @rename($this->_tarname . ".tmp", $this->_tarname); return false; } if ($this->_compress_type == 'gz') { $v_buffer = @gzread($v_temp_tar, 512); // ----- Read the following blocks but not the last one if (!@gzeof($v_temp_tar)) { do { $v_binary_data = pack("a512", $v_buffer); $this->_writeBlock($v_binary_data); $v_buffer = @gzread($v_temp_tar, 512); } while (!@gzeof($v_temp_tar)); } @gzclose($v_temp_tar); } elseif ($this->_compress_type == 'bz2') { $v_buffered_lines = array(); $v_buffered_lines[] = @bzread($v_temp_tar, 512); // ----- Read the following blocks but not the last one while (strlen($v_buffered_lines[] = @bzread($v_temp_tar, 512)) > 0) { $v_binary_data = pack("a512", array_shift($v_buffered_lines)); $this->_writeBlock($v_binary_data); } @bzclose($v_temp_tar); } if (!@unlink($this->_tarname . ".tmp")) { $this->_error('Error while deleting temporary file \'' . $this->_tarname . '.tmp\''); } } else { // ----- For not compressed tar, just add files before the last // 512 bytes block if (!$this->_openReadWrite()) { return false; } clearstatcache(); $v_size = filesize($this->_tarname); fseek($this->_file, $v_size - 512); } return true; }
/** * http://bugs.php.net/bug.php?id=29532 * bzip reads a maximum of 8192 bytes on windows systems * @todo this function is unused */ function getNextChunk($max_size = null) { if (null !== $max_size) { $size = min($max_size, $this->getChunkSize()); } else { $size = $this->getChunkSize(); } // $result = $this->handler->getNextChunk($size); $result = ''; switch ($this->getCompression()) { case 'application/bzip2': $result = ''; while (strlen($result) < $size - 8192 && ! feof($this->getHandle())) { $result .= bzread($this->getHandle(), $size); } break; case 'application/gzip': $result = gzread($this->getHandle(), $size); break; case 'application/zip': /* * if getNextChunk() is used some day, * replace this code by code similar to the one * in open() * include_once './libraries/unzip.lib.php'; $import_handle = new SimpleUnzip(); $import_handle->ReadFile($this->getName()); if ($import_handle->Count() == 0) { $this->_error_message = $GLOBALS['strNoFilesFoundInZip']; return false; } elseif ($import_handle->GetError(0) != 0) { $this->_error_message = $GLOBALS['strErrorInZipFile'] . ' ' . $import_handle->GetErrorMsg(0); return false; } else { $result = $import_handle->GetData(0); } */ break; case 'none': $result = fread($this->getHandle(), $size); break; default: return false; } echo $size . ' - '; echo strlen($result) . ' - '; echo (@$GLOBALS['__len__'] += strlen($result)) . ' - '; echo $this->_error_message; echo '<hr />'; if ($GLOBALS['charset_conversion']) { $result = PMA_convert_string($this->getCharset(), $GLOBALS['charset'], $result); } else { /** * Skip possible byte order marks (I do not think we need more * charsets, but feel free to add more, you can use wikipedia for * reference: <http://en.wikipedia.org/wiki/Byte_Order_Mark>) * * @todo BOM could be used for charset autodetection */ if ($this->getOffset() === 0) { // UTF-8 if (strncmp($result, "\xEF\xBB\xBF", 3) == 0) { $result = substr($result, 3); // UTF-16 BE, LE } elseif (strncmp($result, "\xFE\xFF", 2) == 0 || strncmp($result, "\xFF\xFE", 2) == 0) { $result = substr($result, 2); } } } $this->_offset += $size; if (0 === $result) { return true; } return $result; }
function _openAppend() { if (filesize($this->_tarname) == 0) { return $this->_openWrite(); } if ($this->_compress) { $this->_close(); if (!@rename($this->_tarname, $this->_tarname . ".tmp")) { $this->_error('Error while renaming \'' . $this->_tarname . '\' to temporary file \'' . $this->_tarname . '.tmp\''); return false; } if ($this->_compress_type == 'gz') { $v_temp_tar = @gzopen($this->_tarname . ".tmp", "rb"); } elseif ($this->_compress_type == 'bz2') { $v_temp_tar = @bzopen($this->_tarname . ".tmp", "r"); } if ($v_temp_tar == 0) { $this->_error('Unable to open file \'' . $this->_tarname . '.tmp\' in binary read mode'); @rename($this->_tarname . ".tmp", $this->_tarname); return false; } if (!$this->_openWrite()) { @rename($this->_tarname . ".tmp", $this->_tarname); return false; } if ($this->_compress_type == 'gz') { $end_blocks = 0; while (!@gzeof($v_temp_tar)) { $v_buffer = @gzread($v_temp_tar, 512); if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) { $end_blocks++; // do not copy end blocks, we will re-make them // after appending continue; } elseif ($end_blocks > 0) { for ($i = 0; $i < $end_blocks; $i++) { $this->_writeBlock(ARCHIVE_TAR_END_BLOCK); } $end_blocks = 0; } $v_binary_data = pack("a512", $v_buffer); $this->_writeBlock($v_binary_data); } @gzclose($v_temp_tar); } elseif ($this->_compress_type == 'bz2') { $end_blocks = 0; while (strlen($v_buffer = @bzread($v_temp_tar, 512)) > 0) { if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) { $end_blocks++; // do not copy end blocks, we will re-make them // after appending continue; } elseif ($end_blocks > 0) { for ($i = 0; $i < $end_blocks; $i++) { $this->_writeBlock(ARCHIVE_TAR_END_BLOCK); } $end_blocks = 0; } $v_binary_data = pack("a512", $v_buffer); $this->_writeBlock($v_binary_data); } @bzclose($v_temp_tar); } if (!@unlink($this->_tarname . ".tmp")) { $this->_error('Error while deleting temporary file \'' . $this->_tarname . '.tmp\''); } } else { // ----- For not compressed tar, just add files before the last // one or two 512 bytes block if (!$this->_openReadWrite()) { return false; } clearstatcache(); $v_size = filesize($this->_tarname); // We might have zero, one or two end blocks. // The standard is two, but we should try to handle // other cases. fseek($this->_file, $v_size - 1024); if (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) { fseek($this->_file, $v_size - 1024); } elseif (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) { fseek($this->_file, $v_size - 512); } } return true; }
/** * Read a file * * Handles user space streams appropriately otherwise any read will return 8192 * * @param integer $length Length of data to read * * @return mixed * * @see http://php.net/manual/en/function.fread.php * @since 11.1 */ public function read($length = 0) { if (!$this->_filesize && !$length) { // Get the filesize $this->filesize(); if (!$this->_filesize) { // Set it to the biggest and then wait until eof $length = -1; } else { $length = $this->_filesize; } } if (!$this->_fh) { $this->setError(JText::_('JLIB_FILESYSTEM_ERROR_STREAMS_FILE_NOT_OPEN')); return false; } $retval = false; // Capture PHP errors $php_errormsg = 'Error Unknown'; $track_errors = ini_get('track_errors'); ini_set('track_errors', true); $remaining = $length; do { // Do chunked reads where relevant switch ($this->processingmethod) { case 'bz': $res = $remaining > 0 ? bzread($this->_fh, $remaining) : bzread($this->_fh, $this->chunksize); break; case 'gz': $res = $remaining > 0 ? gzread($this->_fh, $remaining) : gzread($this->_fh, $this->chunksize); break; case 'f': default: $res = $remaining > 0 ? fread($this->_fh, $remaining) : fread($this->_fh, $this->chunksize); break; } if (!$res) { $this->setError($php_errormsg); $remaining = 0; // jump from the loop } else { if (!$retval) { $retval = ''; } $retval .= $res; if (!$this->eof()) { $len = strlen($res); $remaining -= $len; } else { // If it's the end of the file then we've nothing left to read; reset remaining and len $remaining = 0; $length = strlen($retval); } } } while ($remaining || !$length); // Restore error tracking to what it was before ini_set('track_errors', $track_errors); // Return the result return $retval; }
/** * Returns next part of imported file/buffer * * @param integer size of buffer to read (this is maximal size * function will return) * @return string part of file/buffer * @access public */ function PMA_importGetNextChunk($size = 32768) { global $import_file, $import_text, $finished, $compression, $import_handle, $offset, $charset_conversion, $charset_of_file, $charset, $read_multiply, $read_limit; // Add some progression while reading large amount of data if ($read_multiply <= 8) { $size *= $read_multiply; } else { $size *= 8; } $read_multiply++; // We can not read too much if ($size > $read_limit) { $size = $read_limit; } if (PMA_checkTimeout()) { return FALSE; } if ($finished) { return TRUE; } if ($import_file == 'none') { // Well this is not yet supported and tested, but should return content of textarea if (strlen($import_text) < $size) { $finished = TRUE; return $import_text; } else { $r = substr($import_text, 0, $size); $offset += $size; $import_text = substr($import_text, $size); return $r; } } switch ($compression) { case 'application/bzip2': $result = bzread($import_handle, $size); $finished = feof($import_handle); break; case 'application/gzip': $result = gzread($import_handle, $size); $finished = feof($import_handle); break; case 'application/zip': $result = substr($import_text, 0, $size); $import_text = substr($import_text, $size); $finished = empty($import_text); break; case 'none': $result = fread($import_handle, $size); $finished = feof($import_handle); break; } $offset += $size; if ($charset_conversion) { return PMA_convert_string($charset_of_file, $charset, $result); } else { return $result; } }
/** * Returns the content of a .bz2 compressed file as string * @author marcel senf <*****@*****.**> */ function bzfile($file) { $bz = bzopen($file, "r"); $str = ''; while (!feof($bz)) { //8192 seems to be the maximum buffersize? $str = $str . bzread($bz, 8192); } bzclose($bz); return $str; }
function read_dbpedia_properties($inFile, $outFile) { //create a file handle for the output file $out_fh = fopen($outFile, "w") or die("Cannot open {$outFile} for writting!\n"); $bz = bzopen($inFile, "r") or die("Could not open file {$inFile}!\n"); while (!feof($bz)) { $aLine = bzread($bz, 4096); preg_match("/(.*)property\\/meshnumberProperty(.*)/", $aLine, $meshnumberProp); preg_match("/(.*)property\\/meshid(.*)/", $aLine, $meshid); preg_match("/(.*)property\\/meshname(.*)/", $aLine, $meshname); preg_match("/(.*)property\\/iupacname(.*)/", $aLine, $iupacname); preg_match("/(.*)property\\/mgiid(.*)/", $aLine, $mgiid); preg_match("/(.*)property\\/symbol(.*)/", $aLine, $symbol); preg_match("/(.*)property\\/scop(.*)/", $aLine, $scop); preg_match("/(.*)property\\/interpro(.*)/", $aLine, $interpro); preg_match("/(.*)property\\/hgncid(.*)/", $aLine, $hgncid); preg_match("/(.*)property\\/kegg(.*)/", $aLine, $kegg); preg_match("/(.*)property\\/pdb(.*)/", $aLine, $pdb); preg_match("/(.*)property\\/pfam(.*)/", $aLine, $pfam); preg_match("/(.*)property\\/prosite(.*)/", $aLine, $prosite); preg_match("/(.*)property\\/inchi(.*)/", $aLine, $inchi); preg_match("/(.*)property\\/smiles(.*)/", $aLine, $smiles); preg_match("/(.*)property\\/casNumer(.*)/", $aLine, $casNumber); preg_match("/(.*)property\\/chebi(.*)/", $aLine, $chebi); preg_match("/(.*)property\\/ecnumber(.*)/", $aLine, $ecnumber); preg_match("/(.*)property\\/entrezgene(.*)/", $aLine, $entrezgene); preg_match("/(.*)property\\/omim(.*)/", $aLine, $omim); preg_match("/(.*)property\\/pubchem(.*)/", $aLine, $pubchem); preg_match("/(.*)property\\/refseq(.*)/", $aLine, $refseq); preg_match("/(.*)property\\/uniprot(.*)/", $aLine, $uniprot); preg_match("/(.*)property\\/drugbank(.*)/", $aLine, $drugbank); //check if a line matched if (count($meshnumberProp)) { //get the triple $t = getLiteralTripleFromString($meshnumberProp[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_mesh_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($meshid)) { //get the triple $t = getLiteralTripleFromString($meshid[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_mesh_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($meshname)) { //get the triple $t = getLiteralTripleFromString($meshname[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_mesh_name> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($iupacname)) { //get the triple $t = getLiteralTripleFromString($iupacname[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_iupac_name> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($mgiid)) { //get the triple $t = getLiteralTripleFromString($mgiid[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_mgi_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($symbol)) { //get the triple $t = getLiteralTripleFromString($symbol[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_symbol> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($scop)) { //get the triple $t = getLiteralTripleFromString($scop[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_scop_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($interpro)) { //get the triple $t = getLiteralTripleFromString($interpro[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_interpro_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($hgncid)) { //get the triple $t = getLiteralTripleFromString($hgncid[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_hgnc_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($kegg)) { //get the triple $t = getLiteralTripleFromString($kegg[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_kegg_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($pdb)) { //get the triple $t = getLiteralTripleFromString($pdb[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_pdb_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($pfam)) { //get the triple $t = getLiteralTripleFromString($pfam[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_pfam_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($prosite)) { //get the triple $t = getLiteralTripleFromString($prosite[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_prosite_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($inchi)) { //get the triple $t = getLiteralTripleFromString($inchi[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_inchi> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($smiles)) { //get the triple $t = getLiteralTripleFromString($smiles[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_smiles> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($casNumber)) { //get the triple $t = getLiteralTripleFromString($casNumber[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_cas> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($chebi)) { //get the triple $t = getLiteralTripleFromString($chebi[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_chebi_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($ecnumber)) { //get the triple $t = getLiteralTripleFromString($ecnumber[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_ec_number> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($entrezgene)) { //get the triple $t = getLiteralTripleFromString($entrezgene[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_entrez_gene_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($omim)) { //get the triple $t = getLiteralTripleFromString($omim[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_omim_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($pubchem)) { //get the triple $t = getLiteralTripleFromString($pubchem[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_pubchem_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($refseq)) { //get the triple $t = getLiteralTripleFromString($refseq[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_refseq_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($uniprot)) { //get the triple $t = getLiteralTripleFromString($uniprot[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_uniprot_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } elseif (count($drugbank)) { //get the triple $t = getLiteralTripleFromString($drugbank[0]); if (count($t) == 3) { //make a valid bio2rdf triple $triple = $t["subject"] . " <http://bio2rdf.org/dbpedia_vocabulary:has_drugbank_id> " . $t["object"] . ".\n"; //write the triple to the output file writeTripleToFile($triple, $out_fh); } } } fclose($out_fh); bzclose($bz); }
function fn_read($fp) { if ($this->SET['comp_method'] == 2) { return bzread($fp, 4096); } elseif ($this->SET['comp_method'] == 1) { return gzread($fp, 4096); } else { return fread($fp, 4096); } }
private function restore_backup_db($working_dir, $working_dir_localpath, &$import_table_prefix) { do_action('updraftplus_restore_db_pre'); # This is now a legacy option (at least on the front end), so we should not see it much $this->prior_upload_path = get_option('upload_path'); // There is a file backup.db(.gz) inside the working directory # The 'off' check is for badly configured setups - http://wordpress.org/support/topic/plugin-wp-super-cache-warning-php-safe-mode-enabled-but-safe-mode-is-off if (@ini_get('safe_mode') && 'off' != strtolower(@ini_get('safe_mode'))) { echo "<p>" . __('Warning: PHP safe_mode is active on your server. Timeouts are much more likely. If these happen, then you will need to manually restore the file via phpMyAdmin or another method.', 'updraftplus') . "</p><br/>"; } $db_basename = 'backup.db.gz'; if (!empty($this->ud_foreign)) { $plugins = apply_filters('updraftplus_accept_archivename', array()); if (empty($plugins[$this->ud_foreign])) { return new WP_Error('unknown', sprintf(__('Backup created by unknown source (%s) - cannot be restored.', 'updraftplus'), $this->ud_foreign)); } if (!file_exists($working_dir_localpath . '/' . $db_basename) && file_exists($working_dir_localpath . '/backup.db')) { $db_basename = 'backup.db'; } elseif (!file_exists($working_dir_localpath . '/' . $db_basename) && file_exists($working_dir_localpath . '/backup.db.bz2')) { $db_basename = 'backup.db.bz2'; } if (!file_exists($working_dir_localpath . '/' . $db_basename)) { $separatedb = empty($plugins[$this->ud_foreign]['separatedb']) ? false : true; $filtered_db_name = apply_filters('updraftplus_foreign_dbfilename', false, $this->ud_foreign, $this->ud_backup_info, $working_dir_localpath, $separatedb); if (is_string($filtered_db_name)) { $db_basename = $filtered_db_name; } } } // wp_filesystem has no gzopen method, so we switch to using the local filesystem (which is harmless, since we are performing read-only operations) if (false === $db_basename || !is_readable($working_dir_localpath . '/' . $db_basename)) { return new WP_Error('dbopen_failed', __('Failed to find database file', 'updraftplus') . " ({$working_dir}/" . $db_basename . ")"); } global $wpdb, $updraftplus; $this->skin->feedback('restore_database'); $is_plain = substr($db_basename, -3, 3) == '.db'; $is_bz2 = substr($db_basename, -7, 7) == '.db.bz2'; // Read-only access: don't need to go through WP_Filesystem if ($is_plain) { $dbhandle = fopen($working_dir_localpath . '/' . $db_basename, 'r'); } elseif ($is_bz2) { if (!function_exists('bzopen')) { $updraftplus->log_e("Your web server's PHP installation has these functions disabled: %s.", 'bzopen'); $updraftplus->log_e('Your hosting company must enable these functions before %s can work.', __('restoration', 'updraftplus')); } $dbhandle = bzopen($working_dir_localpath . '/' . $db_basename, 'r'); } else { $dbhandle = gzopen($working_dir_localpath . '/' . $db_basename, 'r'); } if (!$dbhandle) { return new WP_Error('dbopen_failed', __('Failed to open database file', 'updraftplus')); } $this->line = 0; if (true == $this->use_wpdb) { $updraftplus->log_e('Database access: Direct MySQL access is not available, so we are falling back to wpdb (this will be considerably slower)'); } else { $updraftplus->log("Using direct MySQL access; value of use_mysqli is: " . ($this->use_mysqli ? '1' : '0')); if ($this->use_mysqli) { @mysqli_query($this->mysql_dbh, 'SET SESSION query_cache_type = OFF;'); } else { @mysql_query('SET SESSION query_cache_type = OFF;', $this->mysql_dbh); } } // Find the supported engines - in case the dump had something else (case seen: saved from MariaDB with engine Aria; imported into plain MySQL without) $supported_engines = $wpdb->get_results("SHOW ENGINES", OBJECT_K); $this->errors = 0; $this->statements_run = 0; $this->insert_statements_run = 0; $this->tables_created = 0; $sql_line = ""; $sql_type = -1; $this->start_time = microtime(true); $old_wpversion = ''; $this->old_siteurl = ''; $this->old_home = ''; $this->old_content = ''; $old_table_prefix = defined('UPDRAFTPLUS_OVERRIDE_IMPORT_PREFIX') && UPDRAFTPLUS_OVERRIDE_IMPORT_PREFIX ? UPDRAFTPLUS_OVERRIDE_IMPORT_PREFIX : ''; $old_siteinfo = array(); $gathering_siteinfo = true; $this->create_forbidden = false; $this->drop_forbidden = false; $this->last_error = ''; $random_table_name = 'updraft_tmp_' . rand(0, 9999999) . md5(microtime(true)); # The only purpose in funnelling queries directly here is to be able to get the error number if ($this->use_wpdb) { $req = $wpdb->query("CREATE TABLE {$random_table_name}"); if (!$req) { $this->last_error = $wpdb->last_error; } $this->last_error_no = false; } else { if ($this->use_mysqli) { $req = mysqli_query($this->mysql_dbh, "CREATE TABLE {$random_table_name}"); } else { $req = mysql_unbuffered_query("CREATE TABLE {$random_table_name}", $this->mysql_dbh); } if (!$req) { $this->last_error = $this->use_mysqli ? mysqli_error($this->mysql_dbh) : mysql_error($this->mysql_dbh); $this->last_error_no = $this->use_mysqli ? mysqli_errno($this->mysql_dbh) : mysql_errno($this->mysql_dbh); } } if (!$req && ($this->use_wpdb || 1142 === $this->last_error_no)) { $this->create_forbidden = true; # If we can't create, then there's no point dropping $this->drop_forbidden = true; echo '<strong>' . __('Warning:', 'updraftplus') . '</strong> '; $updraftplus->log_e('Your database user does not have permission to create tables. We will attempt to restore by simply emptying the tables; this should work as long as a) you are restoring from a WordPress version with the same database structure, and b) Your imported database does not contain any tables which are not already present on the importing site.', ' (' . $this->last_error . ')'); } else { if ($this->use_wpdb) { $req = $wpdb->query("DROP TABLE {$random_table_name}"); if (!$req) { $this->last_error = $wpdb->last_error; } $this->last_error_no = false; } else { if ($this->use_mysqli) { $req = mysqli_query($this->mysql_dbh, "DROP TABLE {$random_table_name}"); } else { $req = mysql_unbuffered_query("DROP TABLE {$random_table_name}", $this->mysql_dbh); } if (!$req) { $this->last_error = $this->use_mysqli ? mysqli_error($this->mysql_dbh) : mysql_error($this->mysql_dbh); $this->last_error_no = $this->use_mysqli ? mysqli_errno($this->mysql_dbh) : mysql_errno($this->mysql_dbh); } } if (!$req && ($this->use_wpdb || $this->last_error_no === 1142)) { $this->drop_forbidden = true; echo '<strong>' . __('Warning:', 'updraftplus') . '</strong> '; $updraftplus->log_e('Your database user does not have permission to drop tables. We will attempt to restore by simply emptying the tables; this should work as long as you are restoring from a WordPress version with the same database structure (%s)', ' (' . $this->last_error . ')'); } } $restoring_table = ''; $this->max_allowed_packet = $updraftplus->get_max_packet_size(); $updraftplus->log("Entering maintenance mode"); $this->maintenance_mode(true); // N.B. There is no such function as bzeof() - we have to detect that another way while ($is_plain && !feof($dbhandle) || !$is_plain && ($is_bz2 || !$is_bz2 && !gzeof($dbhandle))) { // Up to 1Mb if ($is_plain) { $buffer = rtrim(fgets($dbhandle, 1048576)); } elseif ($is_bz2) { if (!isset($bz2_buffer)) { $bz2_buffer = ''; } $buffer = ''; if (strlen($bz2_buffer) < 524288) { $bz2_buffer .= bzread($dbhandle, 1048576); } if (bzerrno($dbhandle) !== 0) { $updraftplus->log("bz2 error: " . bzerrstr($dbhandle) . " (code: " . bzerrno($bzhandle) . ")"); break; } if (false !== $bz2_buffer && '' !== $bz2_buffer) { if (false !== ($p = strpos($bz2_buffer, "\n"))) { $buffer .= substr($bz2_buffer, 0, $p + 1); $bz2_buffer = substr($bz2_buffer, $p + 1); } else { $buffer .= $bz2_buffer; $bz2_buffer = ''; } } else { break; } $buffer = rtrim($buffer); } else { $buffer = rtrim(gzgets($dbhandle, 1048576)); } // Discard comments if (empty($buffer) || substr($buffer, 0, 1) == '#' || preg_match('/^--(\\s|$)/', substr($buffer, 0, 3))) { if ('' == $this->old_siteurl && preg_match('/^\\# Backup of: (http(.*))$/', $buffer, $matches)) { $this->old_siteurl = untrailingslashit($matches[1]); $updraftplus->log_e('<strong>Backup of:</strong> %s', htmlspecialchars($this->old_siteurl)); do_action('updraftplus_restore_db_record_old_siteurl', $this->old_siteurl); $this->save_configuration_bundle(); } elseif (false === $this->created_by_version && preg_match('/^\\# Created by UpdraftPlus version ([\\d\\.]+)/', $buffer, $matches)) { $this->created_by_version = trim($matches[1]); echo '<strong>' . __('Backup created by:', 'updraftplus') . '</strong> ' . htmlspecialchars($this->created_by_version) . '<br>'; $updraftplus->log('Backup created by: ' . $this->created_by_version); } elseif ('' == $this->old_home && preg_match('/^\\# Home URL: (http(.*))$/', $buffer, $matches)) { $this->old_home = untrailingslashit($matches[1]); if ($this->old_siteurl && $this->old_home != $this->old_siteurl) { echo '<strong>' . __('Site home:', 'updraftplus') . '</strong> ' . htmlspecialchars($this->old_home) . '<br>'; $updraftplus->log('Site home: ' . $this->old_home); } do_action('updraftplus_restore_db_record_old_home', $this->old_home); } elseif ('' == $this->old_content && preg_match('/^\\# Content URL: (http(.*))$/', $buffer, $matches)) { $this->old_content = untrailingslashit($matches[1]); echo '<strong>' . __('Content URL:', 'updraftplus') . '</strong> ' . htmlspecialchars($this->old_content) . '<br>'; $updraftplus->log('Content URL: ' . $this->old_content); do_action('updraftplus_restore_db_record_old_content', $this->old_content); } elseif ('' == $old_table_prefix && (preg_match('/^\\# Table prefix: (\\S+)$/', $buffer, $matches) || preg_match('/^-- Table Prefix: (\\S+)$/i', $buffer, $matches))) { # We also support backwpup style: # -- Table Prefix: wp_ $old_table_prefix = $matches[1]; echo '<strong>' . __('Old table prefix:', 'updraftplus') . '</strong> ' . htmlspecialchars($old_table_prefix) . '<br>'; $updraftplus->log("Old table prefix: " . $old_table_prefix); } elseif ($gathering_siteinfo && preg_match('/^\\# Site info: (\\S+)$/', $buffer, $matches)) { if ('end' == $matches[1]) { $gathering_siteinfo = false; // Sanity checks if (isset($old_siteinfo['multisite']) && !$old_siteinfo['multisite'] && is_multisite()) { // Just need to check that you're crazy if (!defined('UPDRAFTPLUS_EXPERIMENTAL_IMPORTINTOMULTISITE') || UPDRAFTPLUS_EXPERIMENTAL_IMPORTINTOMULTISITE != true) { return new WP_Error('multisite_error', $this->strings['multisite_error']); } // Got the needed code? if (!class_exists('UpdraftPlusAddOn_MultiSite') || !class_exists('UpdraftPlus_Addons_Migrator')) { return new WP_Error('missing_addons', __('To import an ordinary WordPress site into a multisite installation requires both the multisite and migrator add-ons.', 'updraftplus')); } } } elseif (preg_match('/^([^=]+)=(.*)$/', $matches[1], $kvmatches)) { $key = $kvmatches[1]; $val = $kvmatches[2]; echo '<strong>' . __('Site information:', 'updraftplus') . '</strong>' . ' ' . htmlspecialchars($key) . ' = ' . htmlspecialchars($val) . '<br>'; $updraftplus->log("Site information: " . $key . "=" . $val); $old_siteinfo[$key] = $val; if ('multisite' == $key) { if ($val) { $this->ud_backup_is_multisite = 1; } else { $this->ud_backup_is_multisite = 0; } } } } continue; } // Detect INSERT commands early, so that we can split them if necessary if (preg_match('/^\\s*(insert into \\`?([^\\`]*)\\`?\\s+(values|\\())/i', $sql_line . $buffer, $matches)) { $sql_type = 3; $insert_prefix = $matches[1]; } # Deal with case where adding this line will take us over the MySQL max_allowed_packet limit - must split, if we can (if it looks like consecutive rows) # ALlow a 100-byte margin for error (including searching/replacing table prefix) if (3 == $sql_type && $sql_line && strlen($sql_line . $buffer) > $this->max_allowed_packet - 100 && preg_match('/,\\s*$/', $sql_line) && preg_match('/^\\s*\\(/', $buffer)) { // Remove the final comma; replace with semi-colon $sql_line = substr(rtrim($sql_line), 0, strlen($sql_line) - 1) . ';'; if ('' != $old_table_prefix && $import_table_prefix != $old_table_prefix) { $sql_line = $updraftplus->str_replace_once($old_table_prefix, $import_table_prefix, $sql_line); } # Run the SQL command; then set up for the next one. $this->line++; echo __("Split line to avoid exceeding maximum packet size", 'updraftplus') . " (" . strlen($sql_line) . " + " . strlen($buffer) . " : " . $this->max_allowed_packet . ")<br>"; $updraftplus->log("Split line to avoid exceeding maximum packet size (" . strlen($sql_line) . " + " . strlen($buffer) . " : " . $this->max_allowed_packet . ")"); $do_exec = $this->sql_exec($sql_line, $sql_type, $import_table_prefix); if (is_wp_error($do_exec)) { return $do_exec; } # Reset, then carry on $sql_line = $insert_prefix . " "; } $sql_line .= $buffer; # Do we have a complete line yet? We used to just test the final character for ';' here (up to 1.8.12), but that was too unsophisticated if (3 == $sql_type && !preg_match('/\\)\\s*;$/', substr($sql_line, -3, 3)) || 3 != $sql_type && ';' != substr($sql_line, -1, 1)) { continue; } $this->line++; # We now have a complete line - process it if (3 == $sql_type && $sql_line && strlen($sql_line) > $this->max_allowed_packet) { $this->log_oversized_packet($sql_line); # Reset $sql_line = ''; $sql_type = -1; # If this is the very first SQL line of the options table, we need to bail; it's essential if (0 == $this->insert_statements_run && $restoring_table && $restoring_table == $import_table_prefix . 'options') { $updraftplus->log("Leaving maintenance mode"); $this->maintenance_mode(false); return new WP_Error('initial_db_error', sprintf(__('An error occurred on the first %s command - aborting run', 'updraftplus'), 'INSERT (options)')); } continue; } # The timed overhead of this is negligible if (preg_match('/^\\s*drop table if exists \\`?([^\\`]*)\\`?\\s*;/i', $sql_line, $matches)) { $sql_type = 1; if (!isset($printed_new_table_prefix)) { $import_table_prefix = $this->pre_sql_actions($import_table_prefix); if (false === $import_table_prefix || is_wp_error($import_table_prefix)) { return $import_table_prefix; } $printed_new_table_prefix = true; } $this->table_name = $matches[1]; // Legacy, less reliable - in case it was not caught before if ('' == $old_table_prefix && preg_match('/^([a-z0-9]+)_.*$/i', $this->table_name, $tmatches)) { $old_table_prefix = $tmatches[1] . '_'; echo '<strong>' . __('Old table prefix:', 'updraftplus') . '</strong> ' . htmlspecialchars($old_table_prefix) . '<br>'; $updraftplus->log("Old table prefix (detected from first table): {$old_table_prefix}"); } $this->new_table_name = $old_table_prefix ? $updraftplus->str_replace_once($old_table_prefix, $import_table_prefix, $this->table_name) : $this->table_name; if ('' != $old_table_prefix && $import_table_prefix != $old_table_prefix) { $sql_line = $updraftplus->str_replace_once($old_table_prefix, $import_table_prefix, $sql_line); } $this->tables_been_dropped[] = $this->new_table_name; } elseif (preg_match('/^\\s*create table \\`?([^\\`\\(]*)\\`?\\s*\\(/i', $sql_line, $matches)) { $sql_type = 2; $this->insert_statements_run = 0; $this->table_name = $matches[1]; // Legacy, less reliable - in case it was not caught before. We added it in here (CREATE) as well as in DROP because of SQL dumps which lack DROP statements. if ('' == $old_table_prefix && preg_match('/^([a-z0-9]+)_.*$/i', $this->table_name, $tmatches)) { $old_table_prefix = $tmatches[1] . '_'; echo '<strong>' . __('Old table prefix:', 'updraftplus') . '</strong> ' . htmlspecialchars($old_table_prefix) . '<br>'; $updraftplus->log("Old table prefix (detected from creating first table): {$old_table_prefix}"); } // MySQL 4.1 outputs TYPE=, but accepts ENGINE=; 5.1 onwards accept *only* ENGINE= $sql_line = $updraftplus->str_lreplace('TYPE=', 'ENGINE=', $sql_line); if (empty($printed_new_table_prefix)) { $import_table_prefix = $this->pre_sql_actions($import_table_prefix); if (false === $import_table_prefix || is_wp_error($import_table_prefix)) { return $import_table_prefix; } $printed_new_table_prefix = true; } $this->new_table_name = $old_table_prefix ? $updraftplus->str_replace_once($old_table_prefix, $import_table_prefix, $this->table_name) : $this->table_name; // This CREATE TABLE command may be the de-facto mark for the end of processing a previous table (which is so if this is not the first table in the SQL dump) if ($restoring_table) { # Attempt to reconnect if the DB connection dropped (may not succeed, of course - but that will soon become evident) $updraftplus->check_db_connection($this->wpdb_obj); // After restoring the options table, we can set old_siteurl if on legacy (i.e. not already set) if ($restoring_table == $import_table_prefix . 'options') { if ('' == $this->old_siteurl || '' == $this->old_home || '' == $this->old_content) { global $updraftplus_addons_migrator; if (isset($updraftplus_addons_migrator->new_blogid)) { switch_to_blog($updraftplus_addons_migrator->new_blogid); } if ('' == $this->old_siteurl) { $this->old_siteurl = untrailingslashit($wpdb->get_row("SELECT option_value FROM {$wpdb->options} WHERE option_name='siteurl'")->option_value); do_action('updraftplus_restore_db_record_old_siteurl', $this->old_siteurl); } if ('' == $this->old_home) { $this->old_home = untrailingslashit($wpdb->get_row("SELECT option_value FROM {$wpdb->options} WHERE option_name='home'")->option_value); do_action('updraftplus_restore_db_record_old_home', $this->old_home); } if ('' == $this->old_content) { $this->old_content = $this->old_siteurl . '/wp-content'; do_action('updraftplus_restore_db_record_old_content', $this->old_content); } if (isset($updraftplus_addons_migrator->new_blogid)) { restore_current_blog(); } } } if ($restoring_table != $this->new_table_name) { $this->restored_table($restoring_table, $import_table_prefix, $old_table_prefix); } } $engine = "(?)"; $engine_change_message = ''; if (preg_match('/ENGINE=([^\\s;]+)/', $sql_line, $eng_match)) { $engine = $eng_match[1]; if (isset($supported_engines[$engine])) { #echo sprintf(__('Requested table engine (%s) is present.', 'updraftplus'), $engine); if ('myisam' == strtolower($engine)) { $sql_line = preg_replace('/PAGE_CHECKSUM=\\d\\s?/', '', $sql_line, 1); } } else { $engine_change_message = sprintf(__('Requested table engine (%s) is not present - changing to MyISAM.', 'updraftplus'), $engine) . "<br>"; $sql_line = $updraftplus->str_lreplace("ENGINE={$eng_match}", "ENGINE=MyISAM", $sql_line); // Remove (M)aria options if ('maria' == strtolower($engine) || 'aria' == strtolower($engine)) { $sql_line = preg_replace('/PAGE_CHECKSUM=\\d\\s?/', '', $sql_line, 1); $sql_line = preg_replace('/TRANSACTIONAL=\\d\\s?/', '', $sql_line, 1); } } } $this->table_name = $matches[1]; echo '<strong>' . sprintf(__('Restoring table (%s)', 'updraftplus'), $engine) . ":</strong> " . htmlspecialchars($this->table_name); $logline = "Restoring table ({$engine}): " . $this->table_name; if ('' != $old_table_prefix && $import_table_prefix != $old_table_prefix) { echo ' - ' . __('will restore as:', 'updraftplus') . ' ' . htmlspecialchars($this->new_table_name); $logline .= " - will restore as: " . $this->new_table_name; $sql_line = $updraftplus->str_replace_once($old_table_prefix, $import_table_prefix, $sql_line); } $updraftplus->log($logline); $restoring_table = $this->new_table_name; echo '<br>'; if ($engine_change_message) { echo $engine_change_message; } } elseif (preg_match('/^\\s*(insert into \\`?([^\\`]*)\\`?\\s+(values|\\())/i', $sql_line, $matches)) { $sql_type = 3; if ('' != $old_table_prefix && $import_table_prefix != $old_table_prefix) { $sql_line = $updraftplus->str_replace_once($old_table_prefix, $import_table_prefix, $sql_line); } } elseif (preg_match('/^\\s*(\\/\\*\\!40000 )?(alter|lock) tables? \\`?([^\\`\\(]*)\\`?\\s+(write|disable|enable)/i', $sql_line, $matches)) { # Only binary mysqldump produces this pattern (LOCK TABLES `table` WRITE, ALTER TABLE `table` (DISABLE|ENABLE) KEYS) $sql_type = 4; if ('' != $old_table_prefix && $import_table_prefix != $old_table_prefix) { $sql_line = $updraftplus->str_replace_once($old_table_prefix, $import_table_prefix, $sql_line); } } elseif (preg_match('/^(un)?lock tables/i', $sql_line)) { # BackWPup produces these $sql_type = 5; } elseif (preg_match('/^(create|drop) database /i', $sql_line)) { # WPB2D produces these, as do some phpMyAdmin dumps $sql_type = 6; } elseif (preg_match('/^use /i', $sql_line)) { # WPB2D produces these, as do some phpMyAdmin dumps $sql_type = 7; } elseif (preg_match('#/\\*\\!40\\d+ SET NAMES (\\S+)#', $sql_line, $smatches)) { $sql_type = 8; $this->set_names = $smatches[1]; } else { # Prevent the previous value of $sql_type being retained for an unknown type $sql_type = 0; } // if (5 !== $sql_type) { if ($sql_type != 6 && $sql_type != 7) { $do_exec = $this->sql_exec($sql_line, $sql_type); if (is_wp_error($do_exec)) { return $do_exec; } } else { $updraftplus->log("Skipped SQL statement (unwanted type={$sql_type}): {$sql_line}"); } # Reset $sql_line = ''; $sql_type = -1; } $updraftplus->log("Leaving maintenance mode"); $this->maintenance_mode(false); if ($restoring_table) { $this->restored_table($restoring_table, $import_table_prefix, $old_table_prefix); } $time_taken = microtime(true) - $this->start_time; $updraftplus->log_e('Finished: lines processed: %d in %.2f seconds', $this->line, $time_taken); if ($is_plain) { fclose($dbhandle); } elseif ($is_bz2) { bzclose($dbhandle); } else { gzclose($dbhandle); } global $wp_filesystem; $wp_filesystem->delete($working_dir . '/' . $db_basename, false, 'f'); return true; }
function _read($p_len) { if ($this->_comptype == TarLib::COMPRESS_GZIP) { return @gzread($this->_fp, $p_len); } elseif ($this->_comptype == TarLib::COMPRESS_BZIP) { return @bzread($this->_fp, $p_len); } else { return @fread($this->_fp, $p_len); } }