updater.inc

Classes used for updating various files in the Drupal webroot. These classes use a FileTransfer object to actually perform the operations. Normally, the FileTransfer is provided when the site owner is redirected to authorize.php as part of a multistep process.

File

drupal/includes/updater.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Classes used for updating various files in the Drupal webroot. These
  5. * classes use a FileTransfer object to actually perform the operations.
  6. * Normally, the FileTransfer is provided when the site owner is redirected to
  7. * authorize.php as part of a multistep process.
  8. */
  9. /**
  10. * Interface for a class which can update a Drupal project.
  11. *
  12. * An Updater currently serves the following purposes:
  13. * - It can take a given directory, and determine if it can operate on it.
  14. * - It can move the contents of that directory into the appropriate place
  15. * on the system using FileTransfer classes.
  16. * - It can return a list of "next steps" after an update or install.
  17. * - In the future, it will most likely perform some of those steps as well.
  18. */
  19. interface DrupalUpdaterInterface {
  20. /**
  21. * Checks if the project is installed.
  22. *
  23. * @return bool
  24. */
  25. public function isInstalled();
  26. /**
  27. * Returns the system name of the project.
  28. *
  29. * @param string $directory
  30. * A directory containing a project.
  31. */
  32. public static function getProjectName($directory);
  33. /**
  34. * @return string
  35. * An absolute path to the default install location.
  36. */
  37. public function getInstallDirectory();
  38. /**
  39. * Determine if the Updater can handle the project provided in $directory.
  40. *
  41. * @todo: Provide something more rational here, like a project spec file.
  42. *
  43. * @param string $directory
  44. *
  45. * @return bool
  46. * TRUE if the project is installed, FALSE if not.
  47. */
  48. public static function canUpdateDirectory($directory);
  49. /**
  50. * Actions to run after an install has occurred.
  51. */
  52. public function postInstall();
  53. /**
  54. * Actions to run after an update has occurred.
  55. */
  56. public function postUpdate();
  57. }
  58. /**
  59. * Base class for Updaters used in Drupal.
  60. */
  61. class Updater {
  62. /**
  63. * @var string $source Directory to install from.
  64. */
  65. public $source;
  66. public function __construct($source) {
  67. $this->source = $source;
  68. $this->name = self::getProjectName($source);
  69. $this->title = self::getProjectTitle($source);
  70. }
  71. /**
  72. * Return an Updater of the appropriate type depending on the source.
  73. *
  74. * If a directory is provided which contains a module, will return a
  75. * ModuleUpdater.
  76. *
  77. * @param string $source
  78. * Directory of a Drupal project.
  79. *
  80. * @return Updater
  81. */
  82. public static function factory($source) {
  83. if (is_dir($source)) {
  84. $updater = self::getUpdaterFromDirectory($source);
  85. }
  86. else {
  87. throw new UpdaterException(t('Unable to determine the type of the source directory.'));
  88. }
  89. return new $updater($source);
  90. }
  91. /**
  92. * Determine which Updater class can operate on the given directory.
  93. *
  94. * @param string $directory
  95. * Extracted Drupal project.
  96. *
  97. * @return string
  98. * The class name which can work with this project type.
  99. */
  100. public static function getUpdaterFromDirectory($directory) {
  101. // Gets a list of possible implementing classes.
  102. $updaters = drupal_get_updaters();
  103. foreach ($updaters as $updater) {
  104. $class = $updater['class'];
  105. if (call_user_func(array($class, 'canUpdateDirectory'), $directory)) {
  106. return $class;
  107. }
  108. }
  109. throw new UpdaterException(t('Cannot determine the type of project.'));
  110. }
  111. /**
  112. * Figure out what the most important (or only) info file is in a directory.
  113. *
  114. * Since there is no enforcement of which info file is the project's "main"
  115. * info file, this will get one with the same name as the directory, or the
  116. * first one it finds. Not ideal, but needs a larger solution.
  117. *
  118. * @param string $directory
  119. * Directory to search in.
  120. *
  121. * @return string
  122. * Path to the info file.
  123. */
  124. public static function findInfoFile($directory) {
  125. $info_files = file_scan_directory($directory, '/.*\.info$/');
  126. if (!$info_files) {
  127. return FALSE;
  128. }
  129. foreach ($info_files as $info_file) {
  130. if (drupal_substr($info_file->filename, 0, -5) == drupal_basename($directory)) {
  131. // Info file Has the same name as the directory, return it.
  132. return $info_file->uri;
  133. }
  134. }
  135. // Otherwise, return the first one.
  136. $info_file = array_shift($info_files);
  137. return $info_file->uri;
  138. }
  139. /**
  140. * Get the name of the project directory (basename).
  141. *
  142. * @todo: It would be nice, if projects contained an info file which could
  143. * provide their canonical name.
  144. *
  145. * @param string $directory
  146. *
  147. * @return string
  148. * The name of the project.
  149. */
  150. public static function getProjectName($directory) {
  151. return drupal_basename($directory);
  152. }
  153. /**
  154. * Return the project name from a Drupal info file.
  155. *
  156. * @param string $directory
  157. * Directory to search for the info file.
  158. *
  159. * @return string
  160. * The title of the project.
  161. */
  162. public static function getProjectTitle($directory) {
  163. $info_file = self::findInfoFile($directory);
  164. $info = drupal_parse_info_file($info_file);
  165. if (empty($info)) {
  166. throw new UpdaterException(t('Unable to parse info file: %info_file.', array('%info_file' => $info_file)));
  167. }
  168. if (empty($info['name'])) {
  169. throw new UpdaterException(t("The info file (%info_file) does not define a 'name' attribute.", array('%info_file' => $info_file)));
  170. }
  171. return $info['name'];
  172. }
  173. /**
  174. * Store the default parameters for the Updater.
  175. *
  176. * @param array $overrides
  177. * An array of overrides for the default parameters.
  178. *
  179. * @return array
  180. * An array of configuration parameters for an update or install operation.
  181. */
  182. protected function getInstallArgs($overrides = array()) {
  183. $args = array(
  184. 'make_backup' => FALSE,
  185. 'install_dir' => $this->getInstallDirectory(),
  186. 'backup_dir' => $this->getBackupDir(),
  187. );
  188. return array_merge($args, $overrides);
  189. }
  190. /**
  191. * Updates a Drupal project, returns a list of next actions.
  192. *
  193. * @param FileTransfer $filetransfer
  194. * Object that is a child of FileTransfer. Used for moving files
  195. * to the server.
  196. * @param array $overrides
  197. * An array of settings to override defaults; see self::getInstallArgs().
  198. *
  199. * @return array
  200. * An array of links which the user may need to complete the update
  201. */
  202. public function update(&$filetransfer, $overrides = array()) {
  203. try {
  204. // Establish arguments with possible overrides.
  205. $args = $this->getInstallArgs($overrides);
  206. // Take a Backup.
  207. if ($args['make_backup']) {
  208. $this->makeBackup($args['install_dir'], $args['backup_dir']);
  209. }
  210. if (!$this->name) {
  211. // This is bad, don't want to delete the install directory.
  212. throw new UpdaterException(t('Fatal error in update, cowardly refusing to wipe out the install directory.'));
  213. }
  214. // Make sure the installation parent directory exists and is writable.
  215. $this->prepareInstallDirectory($filetransfer, $args['install_dir']);
  216. // Note: If the project is installed in sites/all, it will not be
  217. // deleted. It will be installed in sites/default as that will override
  218. // the sites/all reference and not break other sites which are using it.
  219. if (is_dir($args['install_dir'] . '/' . $this->name)) {
  220. // Remove the existing installed file.
  221. $filetransfer->removeDirectory($args['install_dir'] . '/' . $this->name);
  222. }
  223. // Copy the directory in place.
  224. $filetransfer->copyDirectory($this->source, $args['install_dir']);
  225. // Make sure what we just installed is readable by the web server.
  226. $this->makeWorldReadable($filetransfer, $args['install_dir'] . '/' . $this->name);
  227. // Run the updates.
  228. // @TODO: decide if we want to implement this.
  229. $this->postUpdate();
  230. // For now, just return a list of links of things to do.
  231. return $this->postUpdateTasks();
  232. }
  233. catch (FileTransferException $e) {
  234. throw new UpdaterFileTransferException(t('File Transfer failed, reason: !reason', array('!reason' => strtr($e->getMessage(), $e->arguments))));
  235. }
  236. }
  237. /**
  238. * Installs a Drupal project, returns a list of next actions.
  239. *
  240. * @param FileTransfer $filetransfer
  241. * Object that is a child of FileTransfer.
  242. * @param array $overrides
  243. * An array of settings to override defaults; see self::getInstallArgs().
  244. *
  245. * @return array
  246. * An array of links which the user may need to complete the install.
  247. */
  248. public function install(&$filetransfer, $overrides = array()) {
  249. try {
  250. // Establish arguments with possible overrides.
  251. $args = $this->getInstallArgs($overrides);
  252. // Make sure the installation parent directory exists and is writable.
  253. $this->prepareInstallDirectory($filetransfer, $args['install_dir']);
  254. // Copy the directory in place.
  255. $filetransfer->copyDirectory($this->source, $args['install_dir']);
  256. // Make sure what we just installed is readable by the web server.
  257. $this->makeWorldReadable($filetransfer, $args['install_dir'] . '/' . $this->name);
  258. // Potentially enable something?
  259. // @TODO: decide if we want to implement this.
  260. $this->postInstall();
  261. // For now, just return a list of links of things to do.
  262. return $this->postInstallTasks();
  263. }
  264. catch (FileTransferException $e) {
  265. throw new UpdaterFileTransferException(t('File Transfer failed, reason: !reason', array('!reason' => strtr($e->getMessage(), $e->arguments))));
  266. }
  267. }
  268. /**
  269. * Make sure the installation parent directory exists and is writable.
  270. *
  271. * @param FileTransfer $filetransfer
  272. * Object which is a child of FileTransfer.
  273. * @param string $directory
  274. * The installation directory to prepare.
  275. */
  276. public function prepareInstallDirectory(&$filetransfer, $directory) {
  277. // Make the parent dir writable if need be and create the dir.
  278. if (!is_dir($directory)) {
  279. $parent_dir = dirname($directory);
  280. if (!is_writable($parent_dir)) {
  281. @chmod($parent_dir, 0755);
  282. // It is expected that this will fail if the directory is owned by the
  283. // FTP user. If the FTP user == web server, it will succeed.
  284. try {
  285. $filetransfer->createDirectory($directory);
  286. $this->makeWorldReadable($filetransfer, $directory);
  287. }
  288. catch (FileTransferException $e) {
  289. // Probably still not writable. Try to chmod and do it again.
  290. // @todo: Make a new exception class so we can catch it differently.
  291. try {
  292. $old_perms = substr(sprintf('%o', fileperms($parent_dir)), -4);
  293. $filetransfer->chmod($parent_dir, 0755);
  294. $filetransfer->createDirectory($directory);
  295. $this->makeWorldReadable($filetransfer, $directory);
  296. // Put the permissions back.
  297. $filetransfer->chmod($parent_dir, intval($old_perms, 8));
  298. }
  299. catch (FileTransferException $e) {
  300. $message = t($e->getMessage(), $e->arguments);
  301. $throw_message = t('Unable to create %directory due to the following: %reason', array('%directory' => $directory, '%reason' => $message));
  302. throw new UpdaterException($throw_message);
  303. }
  304. }
  305. // Put the parent directory back.
  306. @chmod($parent_dir, 0555);
  307. }
  308. }
  309. }
  310. /**
  311. * Ensure that a given directory is world readable.
  312. *
  313. * @param FileTransfer $filetransfer
  314. * Object which is a child of FileTransfer.
  315. * @param string $path
  316. * The file path to make world readable.
  317. * @param bool $recursive
  318. * If the chmod should be applied recursively.
  319. */
  320. public function makeWorldReadable(&$filetransfer, $path, $recursive = TRUE) {
  321. if (!is_executable($path)) {
  322. // Set it to read + execute.
  323. $new_perms = substr(sprintf('%o', fileperms($path)), -4, -1) . "5";
  324. $filetransfer->chmod($path, intval($new_perms, 8), $recursive);
  325. }
  326. }
  327. /**
  328. * Perform a backup.
  329. *
  330. * @todo Not implemented.
  331. */
  332. public function makeBackup(&$filetransfer, $from, $to) {
  333. }
  334. /**
  335. * Return the full path to a directory where backups should be written.
  336. */
  337. public function getBackupDir() {
  338. return file_stream_wrapper_get_instance_by_scheme('temporary')->getDirectoryPath();
  339. }
  340. /**
  341. * Perform actions after new code is updated.
  342. */
  343. public function postUpdate() {
  344. }
  345. /**
  346. * Perform actions after installation.
  347. */
  348. public function postInstall() {
  349. }
  350. /**
  351. * Return an array of links to pages that should be visited post operation.
  352. *
  353. * @return array
  354. * Links which provide actions to take after the install is finished.
  355. */
  356. public function postInstallTasks() {
  357. return array();
  358. }
  359. /**
  360. * Return an array of links to pages that should be visited post operation.
  361. *
  362. * @return array
  363. * Links which provide actions to take after the update is finished.
  364. */
  365. public function postUpdateTasks() {
  366. return array();
  367. }
  368. }
  369. /**
  370. * Exception class for the Updater class hierarchy.
  371. *
  372. * This is identical to the base Exception class, we just give it a more
  373. * specific name so that call sites that want to tell the difference can
  374. * specifically catch these exceptions and treat them differently.
  375. */
  376. class UpdaterException extends Exception {
  377. }
  378. /**
  379. * Child class of UpdaterException that indicates a FileTransfer exception.
  380. *
  381. * We have to catch FileTransfer exceptions and wrap those in t(), since
  382. * FileTransfer is so low-level that it doesn't use any Drupal APIs and none
  383. * of the strings are translated.
  384. */
  385. class UpdaterFileTransferException extends UpdaterException {
  386. }

Classes

Namesort descending Description
Updater Base class for Updaters used in Drupal.
UpdaterException Exception class for the Updater class hierarchy.
UpdaterFileTransferException Child class of UpdaterException that indicates a FileTransfer exception.

Interfaces

Namesort descending Description
DrupalUpdaterInterface Interface for a class which can update a Drupal project.