Esempio n. 1
0
 /**
  * Revert a file
  *
  * @access public
  * @author Cédric Alfonsi, <*****@*****.**>
  * @param  File resource
  * @param  int revision
  * @param  string msg
  * @return boolean
  * @see core_kernel_versioning_File::revert()
  */
 public function revert(core_kernel_file_File $resource, $revision = null, $msg = "")
 {
     $returnValue = (bool) false;
     if ($resource->getRepository()->authenticate()) {
         //no revision, revert local change
         if (is_null($revision)) {
             $returnValue = svn_revert($resource->getAbsolutePath());
         } else {
             $path = realpath($resource->getAbsolutePath());
             common_Logger::i('svn_revert ' . $path);
             //get the svn revision number
             $log = svn_log($path);
             $oldRevision = count($log) - $revision;
             if (isset($log[$oldRevision])) {
                 $svnRevision = $log[$oldRevision];
                 $svnRevisionNumber = $svnRevision['rev'];
                 //destroy the existing version
                 helpers_VersionedFile::rmWorkingCopy($path);
                 //replace with the target revision
                 if ($resource->update($svnRevisionNumber)) {
                     if (is_file($path)) {
                         //get old content
                         $content = $resource->getFileContent();
                         //update to the current version
                         $resource->update();
                         //set the new content
                         $resource->setContent($content);
                         //commit the change
                     }
                     if (is_dir($path)) {
                         $i = 10;
                         do {
                             $tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('versionedFolder');
                             common_Logger::i(__LINE__ . ' ' . $tmpDir);
                             $exists = file_exists($tmpDir);
                             $i--;
                         } while ($exists || !$i);
                         helpers_VersionedFile::cpWorkingCopy($path, $tmpDir);
                         $resource->update();
                         //@TODO: empty the current folder $path, to delete no longer versioned files
                         helpers_VersionedFile::cpWorkingCopy($tmpDir, $path);
                     }
                     if ($resource->commit($msg)) {
                         $returnValue = true;
                     } else {
                         @helpers_VersionedFile::rmWorkingCopy($path);
                         $resource->update();
                     }
                 } else {
                     @helpers_VersionedFile::rmWorkingCopy($path);
                     $resource->update();
                 }
             }
         }
     }
     return (bool) $returnValue;
 }