function write($replace) { // Prepare data. $this->source = pieCleanString($this->source); $this->source = preg_replace('/\\x0a{3,}/s', "\n\n", $this->source); $this->source = rtrim($this->source) . "\n\n"; $this->meta['size'] = strlen($this->source); if (!$this->meta['stamp']) { $this->meta['stamp'] = time(); } if (!$this->meta['type']) { $this->meta['type'] = 'full'; } // Write source and meta data to database. if (!($out = pieTempFile($this->name))) { return false; } fwrite($out, pieImplodePage($this->meta, $this->source)); // Append any previous versions if ($this->exists($this->name)) { if (!($in = fopen($this->_filePath($this->name), 'r'))) { fclose($out); unlink(pieTempName($this->name)); return false; } while ($info = $this->_nextVersion($in)) { if ($replace) { $replace = false; continue; } // Remove redundant data before updating. $source = $info['source']; unset($info['source']); unset($info['pages']); unset($info['files']); fwrite($out, pieImplodePage($info, $source)); } fclose($in); } // Activate new file fclose($out); touch(pieTempName($this->name), $this->meta['stamp']); return rename(pieTempName($this->name), $this->_filePath($this->name)); }
function replace($file) { if (($meta = $this->_current($file)) === false) { return false; } $path = $this->_mediaPath("{$file} " . $meta['stamp']); if (file_exists($path)) { unlink($path); } if (!($out = pieTempFile($file))) { return false; } if (!($in = fopen($this->_filePath($file), 'r'))) { return false; } $this->_nextVersion($in); while (!feof($in)) { $data = fread($in, BLOCKSIZE); fwrite($out, $data); } fclose($in); fclose($out); return rename(pieTempName($file), $this->_filePath($file)); }