Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
114
core/lib/Drupal/Core/Batch/BatchStorage.php
Normal file
114
core/lib/Drupal/Core/Batch/BatchStorage.php
Normal file
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Batch\BatchStorage.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Batch;
|
||||
|
||||
use Drupal\Core\Database\Connection;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Drupal\Core\Access\CsrfTokenGenerator;
|
||||
|
||||
class BatchStorage implements BatchStorageInterface {
|
||||
|
||||
/**
|
||||
* The database connection.
|
||||
*
|
||||
* @var \Drupal\Core\Database\Connection
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* The session.
|
||||
*
|
||||
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
|
||||
*/
|
||||
protected $session;
|
||||
|
||||
/**
|
||||
* The CSRF token generator.
|
||||
*
|
||||
* @var \Drupal\Core\Access\CsrfTokenGenerator
|
||||
*/
|
||||
protected $csrfToken;
|
||||
|
||||
/**
|
||||
* Constructs the database batch storage service.
|
||||
*
|
||||
* @param \Drupal\Core\Database\Connection $connection
|
||||
* The database connection.
|
||||
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
|
||||
* The session.
|
||||
* @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token
|
||||
* The CSRF token generator.
|
||||
*/
|
||||
public function __construct(Connection $connection, SessionInterface $session, CsrfTokenGenerator $csrf_token) {
|
||||
$this->connection = $connection;
|
||||
$this->session = $session;
|
||||
$this->csrfToken = $csrf_token;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load($id) {
|
||||
// Ensure that a session is started before using the CSRF token generator.
|
||||
$this->session->start();
|
||||
$batch = $this->connection->query("SELECT batch FROM {batch} WHERE bid = :bid AND token = :token", array(
|
||||
':bid' => $id,
|
||||
':token' => $this->csrfToken->get($id),
|
||||
))->fetchField();
|
||||
if ($batch) {
|
||||
return unserialize($batch);
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete($id) {
|
||||
$this->connection->delete('batch')
|
||||
->condition('bid', $id)
|
||||
->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function update(array $batch) {
|
||||
$this->connection->update('batch')
|
||||
->fields(array('batch' => serialize($batch)))
|
||||
->condition('bid', $batch['id'])
|
||||
->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function cleanup() {
|
||||
// Cleanup the batch table and the queue for failed batches.
|
||||
$this->connection->delete('batch')
|
||||
->condition('timestamp', REQUEST_TIME - 864000, '<')
|
||||
->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function create(array $batch) {
|
||||
// Ensure that a session is started before using the CSRF token generator.
|
||||
$this->session->start();
|
||||
$this->connection->insert('batch')
|
||||
->fields(array(
|
||||
'bid' => $batch['id'],
|
||||
'timestamp' => REQUEST_TIME,
|
||||
'token' => $this->csrfToken->get($batch['id']),
|
||||
'batch' => serialize($batch),
|
||||
))
|
||||
->execute();
|
||||
}
|
||||
|
||||
}
|
55
core/lib/Drupal/Core/Batch/BatchStorageInterface.php
Normal file
55
core/lib/Drupal/Core/Batch/BatchStorageInterface.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Batch\BatchStorageInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Batch;
|
||||
|
||||
/**
|
||||
* Defines a common interface for batch storage operations.
|
||||
*/
|
||||
interface BatchStorageInterface {
|
||||
|
||||
/**
|
||||
* Loads a batch.
|
||||
*
|
||||
* @param int $id
|
||||
* The ID of the batch to load.
|
||||
*
|
||||
* @return array
|
||||
* An array representing the batch, or FALSE if no batch was found.
|
||||
*/
|
||||
public function load($id);
|
||||
|
||||
/**
|
||||
* Creates and saves a batch.
|
||||
*
|
||||
* @param array $batch
|
||||
* The array representing the batch to create.
|
||||
*/
|
||||
public function create(array $batch);
|
||||
|
||||
/**
|
||||
* Updates a batch.
|
||||
*
|
||||
* @param array $batch
|
||||
* The array representing the batch to update.
|
||||
*/
|
||||
public function update(array $batch);
|
||||
|
||||
/**
|
||||
* Deletes a batch.
|
||||
*
|
||||
* @param int $id
|
||||
* The ID of the batch to delete.
|
||||
*/
|
||||
public function delete($id);
|
||||
|
||||
/**
|
||||
* Cleans up failed or old batches.
|
||||
*/
|
||||
public function cleanup();
|
||||
|
||||
}
|
58
core/lib/Drupal/Core/Batch/Percentage.php
Normal file
58
core/lib/Drupal/Core/Batch/Percentage.php
Normal file
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Batch\Percentage.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Batch;
|
||||
|
||||
/**
|
||||
* Helper methods for the batch system.
|
||||
*/
|
||||
class Percentage {
|
||||
|
||||
/**
|
||||
* Formats the percent completion for a batch set.
|
||||
*
|
||||
* @param int $total
|
||||
* The total number of operations.
|
||||
* @param int $current
|
||||
* The number of the current operation. This may be a floating point number
|
||||
* rather than an integer in the case of a multi-step operation that is not
|
||||
* yet complete; in that case, the fractional part of $current represents the
|
||||
* fraction of the operation that has been completed.
|
||||
*
|
||||
* @return string
|
||||
* The properly formatted percentage, as a string. We output percentages
|
||||
* using the correct number of decimal places so that we never print "100%"
|
||||
* until we are finished, but we also never print more decimal places than
|
||||
* are meaningful.
|
||||
*
|
||||
* @see _batch_process()
|
||||
*/
|
||||
public static function format($total, $current) {
|
||||
if (!$total || $total == $current) {
|
||||
// If $total doesn't evaluate as true or is equal to the current set, then
|
||||
// we're finished, and we can return "100".
|
||||
$percentage = '100';
|
||||
}
|
||||
else {
|
||||
// We add a new digit at 200, 2000, etc. (since, for example, 199/200
|
||||
// would round up to 100% if we didn't).
|
||||
$decimal_places = max(0, floor(log10($total / 2.0)) - 1);
|
||||
do {
|
||||
// Calculate the percentage to the specified number of decimal places.
|
||||
$percentage = sprintf('%01.' . $decimal_places . 'f', round($current / $total * 100, $decimal_places));
|
||||
// When $current is an integer, the above calculation will always be
|
||||
// correct. However, if $current is a floating point number (in the case
|
||||
// of a multi-step batch operation that is not yet complete), $percentage
|
||||
// may be erroneously rounded up to 100%. To prevent that, we add one
|
||||
// more decimal place and try again.
|
||||
$decimal_places++;
|
||||
} while ($percentage == '100');
|
||||
}
|
||||
return $percentage;
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue