class Updater {
public $source;
public function __construct($source) {
$this->source = $source;
$this->name = self::getProjectName($source);
$this->title = self::getProjectTitle($source);
}
public static function factory($source) {
if (is_dir($source)) {
$updater = self::getUpdaterFromDirectory($source);
}
else {
throw new UpdaterException(t('Unable to determine the type of the source directory.'));
}
return new $updater($source);
}
public static function getUpdaterFromDirectory($directory) {
$updaters = drupal_get_updaters();
foreach ($updaters as $updater) {
$class = $updater['class'];
if (call_user_func(array(
$class,
'canUpdateDirectory',
), $directory)) {
return $class;
}
}
throw new UpdaterException(t('Cannot determine the type of project.'));
}
public static function findInfoFile($directory) {
$info_files = file_scan_directory($directory, '/.*\\.info$/');
if (!$info_files) {
return FALSE;
}
foreach ($info_files as $info_file) {
if (drupal_substr($info_file->filename, 0, -5) == drupal_basename($directory)) {
return $info_file->uri;
}
}
$info_file = array_shift($info_files);
return $info_file->uri;
}
public static function getProjectName($directory) {
return drupal_basename($directory);
}
public static function getProjectTitle($directory) {
$info_file = self::findInfoFile($directory);
$info = drupal_parse_info_file($info_file);
if (empty($info)) {
throw new UpdaterException(t('Unable to parse info file: %info_file.', array(
'%info_file' => $info_file,
)));
}
if (empty($info['name'])) {
throw new UpdaterException(t("The info file (%info_file) does not define a 'name' attribute.", array(
'%info_file' => $info_file,
)));
}
return $info['name'];
}
protected function getInstallArgs($overrides = array()) {
$args = array(
'make_backup' => FALSE,
'install_dir' => $this
->getInstallDirectory(),
'backup_dir' => $this
->getBackupDir(),
);
return array_merge($args, $overrides);
}
public function update(&$filetransfer, $overrides = array()) {
try {
$args = $this
->getInstallArgs($overrides);
if ($args['make_backup']) {
$this
->makeBackup($args['install_dir'], $args['backup_dir']);
}
if (!$this->name) {
throw new UpdaterException(t('Fatal error in update, cowardly refusing to wipe out the install directory.'));
}
$this
->prepareInstallDirectory($filetransfer, $args['install_dir']);
if (is_dir($args['install_dir'] . '/' . $this->name)) {
$filetransfer
->removeDirectory($args['install_dir'] . '/' . $this->name);
}
$filetransfer
->copyDirectory($this->source, $args['install_dir']);
$this
->makeWorldReadable($filetransfer, $args['install_dir'] . '/' . $this->name);
$this
->postUpdate();
return $this
->postUpdateTasks();
} catch (FileTransferException $e) {
throw new UpdaterFileTransferException(t('File Transfer failed, reason: !reason', array(
'!reason' => strtr($e
->getMessage(), $e->arguments),
)));
}
}
public function install(&$filetransfer, $overrides = array()) {
try {
$args = $this
->getInstallArgs($overrides);
$this
->prepareInstallDirectory($filetransfer, $args['install_dir']);
$filetransfer
->copyDirectory($this->source, $args['install_dir']);
$this
->makeWorldReadable($filetransfer, $args['install_dir'] . '/' . $this->name);
$this
->postInstall();
return $this
->postInstallTasks();
} catch (FileTransferException $e) {
throw new UpdaterFileTransferException(t('File Transfer failed, reason: !reason', array(
'!reason' => strtr($e
->getMessage(), $e->arguments),
)));
}
}
public function prepareInstallDirectory(&$filetransfer, $directory) {
if (!is_dir($directory)) {
$parent_dir = dirname($directory);
if (!is_writable($parent_dir)) {
@chmod($parent_dir, 0755);
try {
$filetransfer
->createDirectory($directory);
$this
->makeWorldReadable($filetransfer, $directory);
} catch (FileTransferException $e) {
try {
$old_perms = substr(sprintf('%o', fileperms($parent_dir)), -4);
$filetransfer
->chmod($parent_dir, 0755);
$filetransfer
->createDirectory($directory);
$this
->makeWorldReadable($filetransfer, $directory);
$filetransfer
->chmod($parent_dir, intval($old_perms, 8));
} catch (FileTransferException $e) {
$message = t($e
->getMessage(), $e->arguments);
$throw_message = t('Unable to create %directory due to the following: %reason', array(
'%directory' => $directory,
'%reason' => $message,
));
throw new UpdaterException($throw_message);
}
}
@chmod($parent_dir, 0555);
}
}
}
public function makeWorldReadable(&$filetransfer, $path, $recursive = TRUE) {
if (!is_executable($path)) {
$new_perms = substr(sprintf('%o', fileperms($path)), -4, -1) . "5";
$filetransfer
->chmod($path, intval($new_perms, 8), $recursive);
}
}
public function makeBackup(&$filetransfer, $from, $to) {
}
public function getBackupDir() {
return file_stream_wrapper_get_instance_by_scheme('temporary')
->getDirectoryPath();
}
public function postUpdate() {
}
public function postInstall() {
}
public function postInstallTasks() {
return array();
}
public function postUpdateTasks() {
return array();
}
}