Commit 8f94da57 authored by alexpott's avatar alexpott

Issue #2010246 by tim.plunkett, tkuldeep17, plopesc, InternetDevels,...

Issue #2010246 by tim.plunkett, tkuldeep17, plopesc, InternetDevels, pfrenssen, googletorp: Convert update_manager_install_form, update_manager_update_form, update_manager_update_ready_form to the new form interface.
parent 2d915cb4
<?php
/**
* @file
* Contains \Drupal\update\Form\UpdateForm.
*/
namespace Drupal\update\Form;
/**
* Temporary form controller for update module.
*/
class UpdateForm {
/**
* Wraps update_manager_install_form().
*
* @todo Remove update_manager_install_form().
*/
public function reportInstall() {
module_load_include('manager.inc', 'update');
return \Drupal::formBuilder()->getForm('update_manager_install_form', 'report');
}
/**
* Wraps update_manager_update_form().
*
* @todo Remove update_manager_update_form().
*/
public function reportUpdate() {
module_load_include('manager.inc', 'update');
return \Drupal::formBuilder()->getForm('update_manager_update_form', 'report');
}
/**
* Wraps update_manager_install_form().
*
* @todo Remove update_manager_install_form().
*/
public function moduleInstall() {
module_load_include('manager.inc', 'update');
return \Drupal::formBuilder()->getForm('update_manager_install_form', 'module');
}
/**
* Wraps update_manager_update_form().
*
* @todo Remove update_manager_update_form().
*/
public function moduleUpdate() {
module_load_include('manager.inc', 'update');
return \Drupal::formBuilder()->getForm('update_manager_update_form', 'module');
}
/**
* Wraps update_manager_install_form().
*
* @todo Remove update_manager_install_form().
*/
public function themeInstall() {
module_load_include('manager.inc', 'update');
return \Drupal::formBuilder()->getForm('update_manager_install_form', 'theme');
}
/**
* Wraps update_manager_update_form().
*
* @todo Remove update_manager_update_form().
*/
public function themeUpdate() {
module_load_include('manager.inc', 'update');
return \Drupal::formBuilder()->getForm('update_manager_update_form', 'theme');
}
/**
* Wraps update_manager_update_ready_form().
*
* @todo Remove update_manager_update_ready_form().
*/
public function confirmUpdates() {
module_load_include('manager.inc', 'update');
return \Drupal::formBuilder()->getForm('update_manager_update_ready_form');
}
}
<?php
/**
* @file
* Contains \Drupal\update\Form\UpdateManagerInstall.
*/
namespace Drupal\update\Form;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\FileTransfer\Local;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Updater\Updater;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Configure update settings for this site.
*/
class UpdateManagerInstall extends FormBase {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a new UpdateManagerInstall.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(ModuleHandlerInterface $module_handler) {
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'update_manager_install_form';
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('module_handler')
);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$this->moduleHandler->loadInclude('update', 'inc', 'update.manager');
if (!_update_manager_check_backends($form, 'install')) {
return $form;
}
$form['help_text'] = array(
'#prefix' => '<p>',
'#markup' => $this->t('You can find <a href="@module_url">modules</a> and <a href="@theme_url">themes</a> on <a href="@drupal_org_url">drupal.org</a>. The following file extensions are supported: %extensions.', array(
'@module_url' => 'http://drupal.org/project/modules',
'@theme_url' => 'http://drupal.org/project/themes',
'@drupal_org_url' => 'http://drupal.org',
'%extensions' => archiver_get_extensions(),
)),
'#suffix' => '</p>',
);
$form['project_url'] = array(
'#type' => 'url',
'#title' => $this->t('Install from a URL'),
'#description' => $this->t('For example: %url', array('%url' => 'http://ftp.drupal.org/files/projects/name.tar.gz')),
);
$form['information'] = array(
'#prefix' => '<strong>',
'#markup' => $this->t('Or'),
'#suffix' => '</strong>',
);
$form['project_upload'] = array(
'#type' => 'file',
'#title' => $this->t('Upload a module or theme archive to install'),
'#description' => $this->t('For example: %filename from your local computer', array('%filename' => 'name.tar.gz')),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Install'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
$uploaded_file = $this->getRequest()->files->get('files[project_upload]', NULL, TRUE);
if (!($form_state['values']['project_url'] XOR !empty($uploaded_file))) {
$this->setFormError('project_url', $form_state, $this->t('You must either provide a URL or upload an archive file to install.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$local_cache = NULL;
if ($form_state['values']['project_url']) {
$local_cache = update_manager_file_get($form_state['values']['project_url']);
if (!$local_cache) {
drupal_set_message($this->t('Unable to retrieve Drupal project from %url.', array('%url' => $form_state['values']['project_url'])), 'error');
return;
}
}
elseif ($_FILES['files']['name']['project_upload']) {
$validators = array('file_validate_extensions' => array(archiver_get_extensions()));
if (!($finfo = file_save_upload('project_upload', $validators, NULL, 0, FILE_EXISTS_REPLACE))) {
// Failed to upload the file. file_save_upload() calls
// drupal_set_message() on failure.
return;
}
$local_cache = $finfo->getFileUri();
}
$directory = _update_manager_extract_directory();
try {
$archive = update_manager_archive_extract($local_cache, $directory);
}
catch (\Exception $e) {
drupal_set_message($e->getMessage(), 'error');
return;
}
$files = $archive->listContents();
if (!$files) {
drupal_set_message($this->t('Provided archive contains no files.'), 'error');
return;
}
// Unfortunately, we can only use the directory name to determine the
// project name. Some archivers list the first file as the directory (i.e.,
// MODULE/) and others list an actual file (i.e., MODULE/README.TXT).
$project = strtok($files[0], '/\\');
$archive_errors = $this->moduleHandler->invokeAll('verify_update_archive', array($project, $local_cache, $directory));
if (!empty($archive_errors)) {
drupal_set_message(array_shift($archive_errors), 'error');
// @todo: Fix me in D8: We need a way to set multiple errors on the same
// form element and have all of them appear!
if (!empty($archive_errors)) {
foreach ($archive_errors as $error) {
drupal_set_message($error, 'error');
}
}
return;
}
// Make sure the Updater registry is loaded.
drupal_get_updaters();
$project_location = $directory . '/' . $project;
try {
$updater = Updater::factory($project_location);
}
catch (\Exception $e) {
drupal_set_message($e->getMessage(), 'error');
return;
}
try {
$project_title = Updater::getProjectTitle($project_location);
}
catch (\Exception $e) {
drupal_set_message($e->getMessage(), 'error');
return;
}
if (!$project_title) {
drupal_set_message($this->t('Unable to determine %project name.', array('%project' => $project)), 'error');
}
if ($updater->isInstalled()) {
drupal_set_message($this->t('%project is already installed.', array('%project' => $project_title)), 'error');
return;
}
$project_real_location = drupal_realpath($project_location);
$arguments = array(
'project' => $project,
'updater_name' => get_class($updater),
'local_url' => $project_real_location,
);
// If the owner of the directory we extracted is the same as the owner of
// our configuration directory (e.g. sites/default) where we're trying to
// install the code, there's no need to prompt for FTP/SSH credentials.
// Instead, we instantiate a Drupal\Core\FileTransfer\Local and invoke
// update_authorize_run_install() directly.
if (fileowner($project_real_location) == fileowner(conf_path())) {
$this->moduleHandler->loadInclude('update', 'inc', 'update.authorize');
$filetransfer = new Local(DRUPAL_ROOT);
call_user_func_array('update_authorize_run_install', array_merge(array($filetransfer), $arguments));
}
// Otherwise, go through the regular workflow to prompt for FTP/SSH
// credentials and invoke update_authorize_run_install() indirectly with
// whatever FileTransfer object authorize.php creates for us.
else {
system_authorized_init('update_authorize_run_install', drupal_get_path('module', 'update') . '/update.authorize.inc', $arguments, $this->t('Update manager'));
$form_state['redirect'] = system_authorized_get_url();
}
}
}
This diff is collapsed.
<?php
/**
* @file
* Contains \Drupal\update\Form\UpdateReady.
*/
namespace Drupal\update\Form;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\FileTransfer\Local;
use Drupal\Core\Form\FormBase;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Updater\Updater;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Configure update settings for this site.
*/
class UpdateReady extends FormBase {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The state key value store.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* Constructs a new UpdateReady object.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The object that manages enabled modules in a Drupal installation.
* @param \Drupal\Core\State\StateInterface $state
* The state key value store.
*/
public function __construct(ModuleHandlerInterface $module_handler, StateInterface $state) {
$this->moduleHandler = $module_handler;
$this->state = $state;
}
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'update_manager_update_ready_form';
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('module_handler'),
$container->get('state')
);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$this->moduleHandler->loadInclude('update', 'inc', 'update.manager');
if (!_update_manager_check_backends($form, 'update')) {
return $form;
}
$form['backup'] = array(
'#prefix' => '<strong>',
'#markup' => $this->t('Back up your database and site before you continue. <a href="@backup_url">Learn how</a>.', array('@backup_url' => url('http://drupal.org/node/22281'))),
'#suffix' => '</strong>',
);
$form['maintenance_mode'] = array(
'#title' => $this->t('Perform updates with site in maintenance mode (strongly recommended)'),
'#type' => 'checkbox',
'#default_value' => TRUE,
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Continue'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
// Store maintenance_mode setting so we can restore it when done.
$_SESSION['maintenance_mode'] = $this->state->get('system.maintenance_mode');
if ($form_state['values']['maintenance_mode'] == TRUE) {
$this->state->set('system.maintenance_mode', TRUE);
}
if (!empty($_SESSION['update_manager_update_projects'])) {
// Make sure the Updater registry is loaded.
drupal_get_updaters();
$updates = array();
$directory = _update_manager_extract_directory();
$projects = $_SESSION['update_manager_update_projects'];
unset($_SESSION['update_manager_update_projects']);
$project_real_location = NULL;
foreach ($projects as $project => $url) {
$project_location = $directory . '/' . $project;
$updater = Updater::factory($project_location);
$project_real_location = drupal_realpath($project_location);
$updates[] = array(
'project' => $project,
'updater_name' => get_class($updater),
'local_url' => $project_real_location,
);
}
// If the owner of the last directory we extracted is the same as the
// owner of our configuration directory (e.g. sites/default) where we're
// trying to install the code, there's no need to prompt for FTP/SSH
// credentials. Instead, we instantiate a Drupal\Core\FileTransfer\Local
// and invoke update_authorize_run_update() directly.
if (fileowner($project_real_location) == fileowner(conf_path())) {
$this->moduleHandler->loadInclude('update', 'inc', 'update.authorize');
$filetransfer = new Local(DRUPAL_ROOT);
update_authorize_run_update($filetransfer, $updates);
}
// Otherwise, go through the regular workflow to prompt for FTP/SSH
// credentials and invoke update_authorize_run_update() indirectly with
// whatever FileTransfer object authorize.php creates for us.
else {
system_authorized_init('update_authorize_run_update', drupal_get_path('module', 'update') . '/update.authorize.inc', array($updates), $this->t('Update manager'));
$form_state['redirect'] = system_authorized_get_url();
}
}
}
}
This diff is collapsed.
......@@ -172,10 +172,6 @@ function update_manager_access() {
*/
function update_theme() {
return array(
'update_manager_update_form' => array(
'render element' => 'form',
'file' => 'update.manager.inc',
),
'update_last_check' => array(
'variables' => array('last' => 0),
'template' => 'update-last-check',
......
......@@ -25,7 +25,7 @@ update.manual_status:
update.report_install:
path: '/admin/reports/updates/install'
defaults:
_content: '\Drupal\update\Form\UpdateForm::reportInstall'
_form: '\Drupal\update\Form\UpdateManagerInstall'
_title: 'Install'
options:
_access_mode: 'ALL'
......@@ -36,7 +36,7 @@ update.report_install:
update.report_update:
path: '/admin/reports/updates/update'
defaults:
_content: '\Drupal\update\Form\UpdateForm::reportUpdate'
_form: '\Drupal\update\Form\UpdateManagerUpdate'
_title: 'Update'
options:
_access_mode: 'ALL'
......@@ -47,7 +47,7 @@ update.report_update:
update.module_install:
path: '/admin/modules/install'
defaults:
_content: '\Drupal\update\Form\UpdateForm::moduleInstall'
_form: '\Drupal\update\Form\UpdateManagerInstall'
_title: 'Install new module'
options:
_access_mode: 'ALL'
......@@ -58,7 +58,7 @@ update.module_install:
update.module_update:
path: '/admin/modules/update'
defaults:
_content: '\Drupal\update\Form\UpdateForm::moduleUpdate'
_form: '\Drupal\update\Form\UpdateManagerUpdate'
_title: 'Update'
options:
_access_mode: 'ALL'
......@@ -69,7 +69,7 @@ update.module_update:
update.theme_install:
path: '/admin/theme/install'
defaults:
_content: '\Drupal\update\Form\UpdateForm::themeInstall'
_form: '\Drupal\update\Form\UpdateManagerInstall'
_title: 'Install new theme'
options:
_access_mode: 'ALL'
......@@ -80,7 +80,7 @@ update.theme_install:
update.theme_update:
path: '/admin/theme/update'
defaults:
_content: '\Drupal\update\Form\UpdateForm::themeUpdate'
_form: '\Drupal\update\Form\UpdateManagerUpdate'
_title: 'Update'
options:
_access_mode: 'ALL'
......@@ -91,7 +91,7 @@ update.theme_update:
update.confirmation_page:
path: '/admin/update/ready'
defaults:
_content: '\Drupal\update\Form\UpdateForm::confirmUpdates'
_form: '\Drupal\update\Form\UpdateReady'
_title: 'Ready to update'
options:
_access_mode: 'ALL'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment