updated core to 7.58 (right after the site was hacked)

This commit is contained in:
2018-04-20 23:48:40 +02:00
parent 18f4aba146
commit 9344a61b61
711 changed files with 99690 additions and 480 deletions

View File

@@ -0,0 +1,12 @@
name = Batch example
description = An example outlining how a module can define batch operations.
package = Example modules
core = 7.x
files[] = batch_example.test
; Information added by Drupal.org packaging script on 2017-01-10
version = "7.x-1.x-dev"
core = "7.x"
project = "examples"
datestamp = "1484076787"

View File

@@ -0,0 +1,85 @@
<?php
/**
* @file
* Install, update, and uninstall functions for the batch_example module.
*/
/**
* Example of batch-driven update function.
*
* Because some update functions may require the batch API, the $sandbox
* provides a place to store state. When $sandbox['#finished'] == TRUE,
* calls to this update function are completed.
*
* The $sandbox param provides a way to store data during multiple invocations.
* When the $sandbox['#finished'] == 1, execution is complete.
*
* This dummy 'update' function changes no state in the system. It simply
* loads each node.
*
* To make this update function run again and again, execute the query
* "update system set schema_version = 0 where name = 'batch_example';"
* and then run /update.php.
*
* @ingroup batch_example
*/
function batch_example_update_7100(&$sandbox) {
$ret = array();
// Use the sandbox at your convenience to store the information needed
// to track progression between successive calls to the function.
if (!isset($sandbox['progress'])) {
// The count of nodes visited so far.
$sandbox['progress'] = 0;
// Total nodes that must be visited.
$sandbox['max'] = db_query('SELECT COUNT(nid) FROM {node}')->fetchField();
// A place to store messages during the run.
$sandbox['messages'] = array();
// Last node read via the query.
$sandbox['current_node'] = -1;
}
// Process nodes by groups of 10 (arbitrary value).
// When a group is processed, the batch update engine determines
// whether it should continue processing in the same request or provide
// progress feedback to the user and wait for the next request.
$limit = 10;
// Retrieve the next group of nids.
$result = db_select('node', 'n')
->fields('n', array('nid'))
->orderBy('n.nid', 'ASC')
->where('n.nid > :nid', array(':nid' => $sandbox['current_node']))
->extend('PagerDefault')
->limit($limit)
->execute();
foreach ($result as $row) {
// Here we actually perform a dummy 'update' on the current node.
$node = db_query('SELECT nid FROM {node} WHERE nid = :nid', array(':nid' => $row->nid))->fetchField();
// Update our progress information.
$sandbox['progress']++;
$sandbox['current_node'] = $row->nid;
}
// Set the "finished" status, to tell batch engine whether this function
// needs to run again. If you set a float, this will indicate the progress
// of the batch so the progress bar will update.
$sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? TRUE : ($sandbox['progress'] / $sandbox['max']);
// Set up a per-run message; Make a copy of $sandbox so we can change it.
// This is simply a debugging stanza to illustrate how to capture status
// from each pass through hook_update_N().
$sandbox_status = $sandbox;
// Don't want them in the output.
unset($sandbox_status['messages']);
$sandbox['messages'][] = t('$sandbox=') . print_r($sandbox_status, TRUE);
if ($sandbox['#finished']) {
// hook_update_N() may optionally return a string which will be displayed
// to the user.
$final_message = '<ul><li>' . implode('</li><li>', $sandbox['messages']) . "</li></ul>";
return t('The batch_example demonstration update did what it was supposed to do: @message', array('@message' => $final_message));
}
}

View File

@@ -0,0 +1,319 @@
<?php
/**
* @file
* Outlines how a module can use the Batch API.
*/
/**
* @defgroup batch_example Example: Batch API
* @ingroup examples
* @{
* Outlines how a module can use the Batch API.
*
* Batches allow heavy processing to be spread out over several page
* requests, ensuring that the processing does not get interrupted
* because of a PHP timeout, while allowing the user to receive feedback
* on the progress of the ongoing operations. It also can prevent out of memory
* situations.
*
* The @link batch_example.install .install file @endlink also shows how the
* Batch API can be used to handle long-running hook_update_N() functions.
*
* Two harmless batches are defined:
* - batch 1: Load the node with the lowest nid 100 times.
* - batch 2: Load all nodes, 20 times and uses a progressive op, loading nodes
* by groups of 5.
* @see batch
*/
/**
* Implements hook_menu().
*/
function batch_example_menu() {
$items = array();
$items['examples/batch_example'] = array(
'title' => 'Batch example',
'description' => 'Example of Drupal batch processing',
'page callback' => 'drupal_get_form',
'page arguments' => array('batch_example_simple_form'),
'access callback' => TRUE,
);
return $items;
}
/**
* Form builder function to allow choice of which batch to run.
*/
function batch_example_simple_form() {
$form['description'] = array(
'#type' => 'markup',
'#markup' => t('This example offers two different batches. The first does 1000 identical operations, each completed in on run; the second does 20 operations, but each takes more than one run to operate if there are more than 5 nodes.'),
);
$form['batch'] = array(
'#type' => 'select',
'#title' => 'Choose batch',
'#options' => array(
'batch_1' => t('batch 1 - 1000 operations, each loading the same node'),
'batch_2' => t('batch 2 - 20 operations. each one loads all nodes 5 at a time'),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Go',
);
// If no nodes, prevent submission.
// Find out if we have a node to work with. Otherwise it won't work.
$nid = batch_example_lowest_nid();
if (empty($nid)) {
drupal_set_message(t("You don't currently have any nodes, and this example requires a node to work with. As a result, this form is disabled."));
$form['submit']['#disabled'] = TRUE;
}
return $form;
}
/**
* Submit handler.
*
* @param array $form
* Form API form.
* @param array $form_state
* Form API form.
*/
function batch_example_simple_form_submit($form, &$form_state) {
$function = 'batch_example_' . $form_state['values']['batch'];
// Reset counter for debug information.
$_SESSION['http_request_count'] = 0;
// Execute the function named batch_example_batch_1() or
// batch_example_batch_2().
$batch = $function();
batch_set($batch);
}
/**
* Batch 1 definition: Load the node with the lowest nid 1000 times.
*
* This creates an operations array defining what batch 1 should do, including
* what it should do when it's finished. In this case, each operation is the
* same and by chance even has the same $nid to operate on, but we could have
* a mix of different types of operations in the operations array.
*/
function batch_example_batch_1() {
$nid = batch_example_lowest_nid();
$num_operations = 1000;
drupal_set_message(t('Creating an array of @num operations', array('@num' => $num_operations)));
$operations = array();
// Set up an operations array with 1000 elements, each doing function
// batch_example_op_1.
// Each operation in the operations array means at least one new HTTP request,
// running Drupal from scratch to accomplish the operation. If the operation
// returns with $context['finished'] != TRUE, then it will be called again.
// In this example, $context['finished'] is always TRUE.
for ($i = 0; $i < $num_operations; $i++) {
// Each operation is an array consisting of
// - The function to call.
// - An array of arguments to that function.
$operations[] = array(
'batch_example_op_1',
array(
$nid,
t('(Operation @operation)', array('@operation' => $i)),
),
);
}
$batch = array(
'operations' => $operations,
'finished' => 'batch_example_finished',
);
return $batch;
}
/**
* Batch operation for batch 1: load a node.
*
* This is the function that is called on each operation in batch 1.
*/
function batch_example_op_1($nid, $operation_details, &$context) {
$node = node_load($nid, NULL, TRUE);
// Store some results for post-processing in the 'finished' callback.
// The contents of 'results' will be available as $results in the
// 'finished' function (in this example, batch_example_finished()).
$context['results'][] = $node->nid . ' : ' . check_plain($node->title);
// Optional message displayed under the progressbar.
$context['message'] = t('Loading node "@title"', array('@title' => $node->title)) . ' ' . $operation_details;
_batch_example_update_http_requests();
}
/**
* Batch 2 : Prepare a batch definition that will load all nodes 20 times.
*/
function batch_example_batch_2() {
$num_operations = 20;
// Give helpful information about how many nodes are being operated on.
$node_count = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')->fetchField();
drupal_set_message(
t('There are @node_count nodes so each of the @num operations will require @count HTTP requests.',
array(
'@node_count' => $node_count,
'@num' => $num_operations,
'@count' => ceil($node_count / 5),
)
)
);
$operations = array();
// 20 operations, each one loads all nodes.
for ($i = 0; $i < $num_operations; $i++) {
$operations[] = array(
'batch_example_op_2',
array(t('(Operation @operation)', array('@operation' => $i))),
);
}
$batch = array(
'operations' => $operations,
'finished' => 'batch_example_finished',
// Message displayed while processing the batch. Available placeholders are:
// @current, @remaining, @total, @percentage, @estimate and @elapsed.
// These placeholders are replaced with actual values in _batch_process(),
// using strtr() instead of t(). The values are determined based on the
// number of operations in the 'operations' array (above), NOT by the number
// of nodes that will be processed. In this example, there are 20
// operations, so @total will always be 20, even though there are multiple
// nodes per operation.
// Defaults to t('Completed @current of @total.').
'title' => t('Processing batch 2'),
'init_message' => t('Batch 2 is starting.'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Batch 2 has encountered an error.'),
);
return $batch;
}
/**
* Batch operation for batch 2 : load all nodes, 5 by five.
*
* After each group of 5 control is returned to the batch API for later
* continuation.
*/
function batch_example_op_2($operation_details, &$context) {
// Use the $context['sandbox'] at your convenience to store the
// information needed to track progression between successive calls.
if (empty($context['sandbox'])) {
$context['sandbox'] = array();
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_node'] = 0;
// Save node count for the termination message.
$context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')->fetchField();
}
// Process nodes by groups of 5 (arbitrary value).
// When a group of five is processed, the batch update engine determines
// whether it should continue processing in the same request or provide
// progress feedback to the user and wait for the next request.
// That way even though we're already processing at the operation level
// the operation itself is interruptible.
$limit = 5;
// Retrieve the next group of nids.
$result = db_select('node', 'n')
->fields('n', array('nid'))
->orderBy('n.nid', 'ASC')
->where('n.nid > :nid', array(':nid' => $context['sandbox']['current_node']))
->extend('PagerDefault')
->limit($limit)
->execute();
foreach ($result as $row) {
// Here we actually perform our dummy 'processing' on the current node.
$node = node_load($row->nid, NULL, TRUE);
// Store some results for post-processing in the 'finished' callback.
// The contents of 'results' will be available as $results in the
// 'finished' function (in this example, batch_example_finished()).
$context['results'][] = $node->nid . ' : ' . check_plain($node->title) . ' ' . $operation_details;
// Update our progress information.
$context['sandbox']['progress']++;
$context['sandbox']['current_node'] = $node->nid;
$context['message'] = check_plain($node->title);
}
// Inform the batch engine that we are not finished,
// and provide an estimation of the completion level we reached.
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = ($context['sandbox']['progress'] >= $context['sandbox']['max']);
}
_batch_example_update_http_requests();
}
/**
* Batch 'finished' callback used by both batch 1 and batch 2.
*/
function batch_example_finished($success, $results, $operations) {
if ($success) {
// Here we could do something meaningful with the results.
// We just display the number of nodes we processed...
drupal_set_message(t('@count results processed in @requests HTTP requests.', array('@count' => count($results), '@requests' => _batch_example_get_http_requests())));
drupal_set_message(t('The final result was "%final"', array('%final' => end($results))));
}
else {
// An error occurred.
// $operations contains the operations that remained unprocessed.
$error_operation = reset($operations);
drupal_set_message(
t('An error occurred while processing @operation with arguments : @args',
array(
'@operation' => $error_operation[0],
'@args' => print_r($error_operation[0], TRUE),
)
),
'error'
);
}
}
/**
* Utility function - simply queries and loads the lowest nid.
*
* @return int|NULL
* A nid or NULL if there are no nodes.
*/
function batch_example_lowest_nid() {
$select = db_select('node', 'n')
->fields('n', array('nid'))
->orderBy('n.nid', 'ASC')
->extend('PagerDefault')
->limit(1);
$nid = $select->execute()->fetchField();
return $nid;
}
/**
* Utility function to increment HTTP requests in a session variable.
*/
function _batch_example_update_http_requests() {
$_SESSION['http_request_count']++;
}
/**
* Utility function to count the HTTP requests in a session variable.
*
* @return int
* Number of requests.
*/
function _batch_example_get_http_requests() {
return !empty($_SESSION['http_request_count']) ? $_SESSION['http_request_count'] : 0;
}
/**
* @} End of "defgroup batch_example".
*/

View File

@@ -0,0 +1,60 @@
<?php
/**
* @file
* Test case for Testing the batch example module.
*
* This file contains the test cases to check if module is performing as
* expected.
*/
/**
* Functional tests for the Batch Example module.
*
* @ingroup batch_example
*/
class BatchExampleTestCase extends DrupalWebTestCase {
protected $webUser;
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Batch example functionality',
'description' => 'Verify the defined batches.',
'group' => 'Examples',
);
}
/**
* Enable modules and create user with specific permissions.
*/
public function setUp() {
parent::setUp('batch_example');
// Create user.
$this->webUser = $this->drupalCreateUser();
}
/**
* Login user, create 30 nodes and test both batch examples.
*/
public function testBatchExampleBasic() {
// Login the admin user.
$this->drupalLogin($this->webUser);
// Create 30 nodes.
for ($count = 0; $count < 30; $count++) {
$node = $this->drupalCreateNode();
}
// Launch Batch 1
$result = $this->drupalPost('examples/batch_example', array('batch' => 'batch_1'), t('Go'));
// Check that 1000 operations were performed.
$this->assertText('1000 results processed');
// Launch Batch 2
$result = $this->drupalPost('examples/batch_example', array('batch' => 'batch_2'), t('Go'));
// Check that 600 operations were performed.
$this->assertText('600 results processed');
}
}