Advertisement
Guest User

oc

a guest
Mar 12th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * ownCloud - Updater plugin
  5.  *
  6.  * @author Victor Dubiniuk
  7.  * @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
  8.  *
  9.  * This file is licensed under the Affero General Public License version 3 or
  10.  * later.
  11.  */
  12.  
  13. namespace OCA\Updater;
  14.  
  15. abstract class Location {
  16.  
  17.     // Path to current instance sources
  18.     protected $oldBase;
  19.     // Path to new instance sources
  20.     protected $newBase;
  21.     // Type of sources (3rdparty | apps | core)
  22.     protected $type = 'generic';
  23.     // Already moved items are collected here;
  24.     protected $done = array();
  25.  
  26.     public function __construct($oldBase, $newBase) {
  27.         $this->oldBase = $oldBase;
  28.         $this->newBase = $newBase;
  29.     }
  30.  
  31.     // Move sources
  32.     public function update($tmpDir = '') {
  33.         Helper::mkdir($tmpDir, true);
  34.         $collected = $this->collect();
  35.  
  36.         try {
  37.             foreach ($collected['old'] as $src) {
  38.                 $dst = str_replace($this->oldBase, $tmpDir, $src);
  39.                 Helper::move($src, $dst);
  40.  
  41.                 // ! reverted intentionally
  42.                 $this->done [] = array(
  43.                     'dst' => $src,
  44.                     'src' => $dst
  45.                 );
  46.             }
  47.  
  48.             foreach ($collected['new'] as $src) {
  49.                 $dst = str_replace($this->newBase, $this->oldBase, $src);
  50.                 Helper::move($src, $dst);
  51.             }
  52.  
  53.             $this->finalize();
  54.         } catch (\Exception $e) {
  55.             $this->rollback(true);
  56.             throw $e;
  57.         }
  58.     }
  59.  
  60.     // Extra steps needed
  61.     protected function finalize() {
  62.         //App::log('Success: ' . $this->type, \OC_Log::INFO);
  63.     }
  64.  
  65.     // Move sources back on Error
  66.     public final function rollback($log = false) {
  67.         if ($log) {
  68.             App::log('Something went wrong for ' . $this->type . '. Rolling back.');
  69.         }
  70.         foreach ($this->done as $item) {
  71.             Helper::copyr($item['src'], $item['dst'], false);
  72.         }
  73.     }
  74.  
  75.     // Aggregate current sources
  76.     public function collect($dryRun = false) {
  77.         $oldSources = $this->filterOld(
  78.                 Helper::scandir($this->oldBase)
  79.         );
  80.  
  81.         $collected = array(
  82.             'old' => $this->toAbsolute($this->oldBase, $oldSources),
  83.             'new' => array()
  84.         );
  85.  
  86.         if (!$dryRun) {
  87.             $newSources = $this->filterNew(
  88.                     Helper::scandir($this->newBase)
  89.             );
  90.             $collected['new'] = $this->toAbsolute($this->newBase, $newSources);
  91.         }
  92.  
  93.         return $collected;
  94.     }
  95.  
  96.     protected function toAbsolute($base, $pathArray) {
  97.         $result = array();
  98.         foreach ($pathArray as $path) {
  99.             // There is a little sense to make these entries absolute
  100.             if (!in_array($path, array('.', '..'))) {
  101.                 $result [$path] = realpath($base . '/' . $path);
  102.             }
  103.         }
  104.         return $result;
  105.     }
  106.  
  107.     // Filter input
  108.     protected function filterOld($pathArray) {
  109.         return $pathArray;
  110.     }
  111.  
  112.     // Filter input
  113.     protected function filterNew($pathArray) {
  114.         return $pathArray;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement