123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- <?php
- interface DrupalUpdaterInterface {
-
- public function isInstalled();
-
- public static function getProjectName($directory);
-
- public function getInstallDirectory();
-
- public static function canUpdateDirectory($directory);
-
- public function postInstall();
-
- public function postUpdate();
- }
- 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();
- }
- }
- class UpdaterException extends Exception {
- }
- class UpdaterFileTransferException extends UpdaterException {
- }
|