Skip to content
Commits on Source (1)
FirePHPCore*
\ No newline at end of file
The master branch is empty. Please see 8.x-1.x and 7.x-1.x branches.
\ No newline at end of file
README.txt
==========
A module containing helper functions for Drupal developers and
inquisitive admins. This module can print a log of
all database queries for each page request at the bottom of each page. The
summary includes how many times each query was executed on a page, and how long
each query took.
It also offers
- a block for running custom PHP on a page
- a block for quickly accessing devel pages
- a block for masquerading as other users (useful for testing)
- reports memory usage at bottom of page
- more
This module is safe to use on a production site. Just be sure to only grant
'access development information' permission to developers.
Also a dpr() function is provided, which pretty prints arrays and strings.
Useful during development. Many other nice functions like dpm(), dvm().
AJAX developers in particular ought to install FirePHP Core from
http://www.firephp.org/ and put it in the devel directory.
This happens automatically when you enable via drush. You may also
use a drush command to download the library. If downloading by hand,
your path to fb.php should look like devel/FirePHPCore/lib/FirePHPCore/fb.php.
You can use svn checkout http://firephp.googlecode.com/svn/trunk/trunk/Libraries/FirePHPCore.
Then you can log php variables to the Firebug console. Is quite useful.
Included in this package is also:
- devel_node_access module which prints out the node_access records for a given node. Also offers hook_node_access_explain for all node access modules to implement. Handy.
- devel_generate.module which bulk creates nodes, users, comment, terms for development
Some nifty drush integration ships with devel and devel_generate. See drush help for details.
COMPATIBILITY NOTES
==================
- Modules that use AHAH may have incompatibility with the query log and other
footer info. Consider setting $GLOBALS['devel_shutdown'] = FALSE if you run into
any issues.
AUTHOR/MAINTAINER
======================
-moshe weitzman <weitzman at tejasa DOT com>
http://cyrve.com
Hans Salvisberg <drupal at salvisberg DOT com>
README
======
This module contains tools for developers using access control modules
to restrict access to some nodes. It is intended to help catch some
common mistakes and provide feedback to confirm that restricted nodes
are in fact visible only to the intended users.
Provides a summary page which queries the node_access table and
reports common mistakes such as the presence of Drupal's default entry
which grants all users read access to all nodes. Also reports the
presence of nodes not represented in node_access table. This may
occur when an access control module is installed after nodes have
already been created.
Provides a block which shows all node_access entries for the nodes
shown on a given page. This gives developers a quick check to see
that grants are provided as they should be. This block auto-enables
to the footer region. You may move it as desired.
If Views module is installed, allows browsing of nodes by realm,
including those nodes not in the node_access table (NULL realm).
WISHLIST
========
Things I'd like to see but haven't had time to do:
* Automatically solve common problems. I.e. delete the "all" realm
entry, and automatically save all nodes not in the node_access table.
* Nicer feedback indicating whether nodes are visible to the public or
not. I.e. use color coding or icons.
* Summary does not differentiate between view grants and other types
of grants. I personally use node_access only for view grants so I'm
not sure exactly what else it should show.
AUTHOR
======
Dave Cohen AKA yogadex on drupal.org
.dev-query, .dev-timer, .dev-memory-usage {
align: left;
direction: ltr;
padding-top: inherit;
}
.dev-query, .dev-timer, .dev-memory-usage table {
direction: ltr;
}
\ No newline at end of file
<?php
function devel_admin_settings() {
$form['queries'] = array('#type' => 'fieldset', '#title' => t('Query log'));
$description = t('Display a log of the database queries needed to generate the current page, and the execution time for each. Also, queries which are repeated during a single page view are summed in the # column, and printed in red since they are candidates for caching.');
if (!devel_is_compatible_optimizer()) {
$description = t('You must disable or upgrade the php Zend Optimizer extension in order to enable this feature. The minimum required version is 3.2.8. Earlier versions of Zend Optimizer are <a href="!url">horribly buggy and segfault your Apache</a> ... ', array('!url' => url('http://drupal.org/node/126098'))) . $description;
}
$form['queries']['devel_query_display'] = array('#type' => 'checkbox',
'#title' => t('Display query log'),
'#default_value' => variable_get('devel_query_display', 0),
'#description' => $description,
'#disabled' => !devel_is_compatible_optimizer(),
);
$form['queries']['settings'] = array(
'#type' => 'container',
'#states' => array(
// Hide the query log settings when not displaying query log.
'invisible' => array(
'input[name="devel_query_display"]' => array('checked' => FALSE),
),
),
);
$form['queries']['settings']['devel_query_sort'] = array('#type' => 'radios',
'#title' => t('Sort query log'),
'#default_value' => variable_get('devel_query_sort', DEVEL_QUERY_SORT_BY_SOURCE),
'#options' => array(t('by source'), t('by duration')),
'#description' => t('The query table can be sorted in the order that the queries were executed or by descending duration.'),
);
$form['queries']['settings']['devel_execution'] = array('#type' => 'textfield',
'#title' => t('Slow query highlighting'),
'#default_value' => variable_get('devel_execution', 5),
'#size' => 4,
'#maxlength' => 4,
'#description' => t('Enter an integer in milliseconds. Any query which takes longer than this many milliseconds will be highlighted in the query log. This indicates a possibly inefficient query, or a candidate for caching.'),
);
$form['xhprof'] = array(
'#type' => 'fieldset',
'#title' => 'XHProf',
'#description' => t('XHProf is a php extension which is essential for profiling your Drupal site. It pinpoints slow functions, and also memory hogging functions.'),
);
$description = extension_loaded('xhprof') ? t('Profile requests with the xhprof php extension.') : '<span class="warning">' . t('You must enable the <a href="!url">xhprof php extension</a> to use this feature.', array('!url' => url('http://techportal.ibuildings.com/2009/12/01/profiling-with-xhprof/'))) . '</span>';
$form['xhprof']['devel_xhprof_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enable profiling of all page views and <a href="!drush">drush</a> requests.', array('!drush' => url('http://drush.ws'))),
'#default_value' => variable_get('devel_xhprof_enabled', FALSE),
'#description' => $description,
'#disabled' => !extension_loaded('xhprof'),
);
$form['xhprof']['settings'] = array(
'#type' => 'container',
'#states' => array(
'invisible' => array(
'input[name="devel_xhprof_enabled"]' => array('checked' => FALSE),
),
),
);
$form['xhprof']['settings']['devel_xhprof_directory'] = array(
'#type' => 'textfield',
'#title' => 'xhprof directory',
'#description' => t('Location of the xhprof source code on your system, usually somewhere in /usr/local/share or /usr/share, include the leading forward slash.'),
'#default_value' => variable_get('devel_xhprof_directory', ''),
'#states' => array(
'invisible' => array(
'input[name="devel_xhprof_enabled"]' => array('checked' => FALSE),
),
),
);
$form['xhprof']['settings']['devel_xhprof_url'] = array(
'#type' => 'textfield',
'#title' => 'XHProf URL',
'#description' => t('Path to the publically accessible xhprof_html - required to display profiler reports. You will need to set this up outside Drupal, for example at http://xhprof.localhost/xhprof_html'),
'#default_value' => variable_get('devel_xhprof_url', ''),
'#states' => array(
'invisible' => array(
'input[name="devel_xhprof_enabled"]' => array('checked' => FALSE),
),
),
);
$form['devel_api_url'] = array('#type' => 'textfield',
'#title' => t('API Site'),
'#default_value' => variable_get('devel_api_url', 'api.drupal.org'),
'#description' => t('The base URL for your developer documentation links. You might change this if you run <a href="!url">api.module</a> locally.', array('!url' => url('http://drupal.org/project/api'))));
$form['dev_timer'] = array('#type' => 'checkbox',
'#title' => t('Display page timer'),
'#default_value' => variable_get('dev_timer', 0),
'#description' => t('Display page execution time in the query log box.'),
);
$form['dev_mem'] = array('#type' => 'checkbox',
'#title' => t('Display memory usage'),
'#default_value' => variable_get('dev_mem', 0),
'#description' => t('Display how much memory is used to generate the current page. This will show memory usage when devel_init() is called and when devel_exit() is called.'),
);
$form['devel_redirect_page'] = array('#type' => 'checkbox',
'#title' => t('Display redirection page'),
'#default_value' => variable_get('devel_redirect_page', 0),
'#description' => t('When a module executes drupal_goto(), the query log and other developer information is lost. Enabling this setting presents an intermediate page to developers so that the log can be examined before continuing to the destination page.'),
);
$form['devel_page_alter'] = array('#type' => 'checkbox',
'#title' => t('Display $page array'),
'#default_value' => variable_get('devel_page_alter', FALSE),
'#description' => t('Display $page array from <a href="http://api.drupal.org/api/function/hook_page_alter/7">hook_page_alter()</a> in the messages area of each page.'),
);
$form['devel_error_handler'] = array('#type' => 'radios',
'#title' => t('Error handler'),
'#default_value' => variable_get('devel_error_handler', DEVEL_ERROR_HANDLER_STANDARD),
'#options' => array(DEVEL_ERROR_HANDLER_NONE => t('None'), DEVEL_ERROR_HANDLER_STANDARD => t('Standard drupal')),
'#description' => t('Choose an error handler for your site. <em>Backtrace</em> prints nice debug information when an error is noticed, and you <a href="@choose">choose to show errors on screen</a>. <strong>Backtrace requires the <a href="@krumo">krumo library</a></strong>. <em>None</em> is a good option when stepping through the site in your debugger.', array('@krumo' => url('http://krumo.sourceforge.net'), '@choose' => url('admin/config/development/logging'))),
);
if (has_krumo()) {
$form['devel_error_handler']['#options'][DEVEL_ERROR_HANDLER_BACKTRACE] = t('Backtrace');
}
$options = drupal_map_assoc(array('default', 'blue', 'green', 'orange', 'white', 'disabled'));
$form['devel_krumo_skin'] = array(
'#type' => 'radios',
'#title' => t('Krumo display'),
'#description' => t('Select a skin for your debug messages or select <em>disabled</em> to display object and array output in standard PHP format.'),
'#options' => $options,
'#default_value' => variable_get('devel_krumo_skin', 'default'),
);
$form['devel_rebuild_theme_registry'] = array(
'#type' => 'checkbox',
'#title' => t('Rebuild the theme registry on every page load'),
'#description' => t('While creating new templates and theme_ overrides the theme registry needs to be rebuilt.'),
'#default_value' => variable_get('devel_rebuild_theme_registry', FALSE),
);
$form['devel_use_uncompressed_jquery'] = array(
'#type' => 'checkbox',
'#title' => t('Use uncompressed jQuery'),
'#default_value' => variable_get('devel_use_uncompressed_jquery', FALSE),
'#description' => t("Use a human-readable version of jQuery instead of the minified version that ships with Drupal, to make JavaScript debugging easier."),
);
return system_settings_form($form);
}
.dev-query, .dev-timer, .dev-memory-usage {
padding: 1em;
}
.devel-obj-output .field {
color: red;
}
.devel-obj-output dd {
display: block;
}
/**
* Query summary
*/
div.dev-query {
font-size:11px;
background:#fff;
border-top:3px solid #ccc;
color:#333;
/*padding:.5em;*/
}
div.dev-query .marker {
color: #f00;
font-weight: bold;
}
/**
* Querylog
*/
div.devel-querylog {
color:#333;
border-bottom:1px solid #eee;
font-size:11px;
line-height:100%;
padding-left:30em;
padding-right:2em;
position:relative;
overflow:hidden;
}
div.devel-querylog .marker {
color: #f00;
font-weight: bold;
}
div.devel-querylog-header {
border-top:3px solid #ccc;
background:#fff;
font-weight:bold;
}
div.devel-querylog-even {
background:#fff;
}
div.devel-querylog-odd {
background:#f8f8f8;
}
div.devel-querylog div.cell {
overflow:hidden;
padding: 1em .5em;
}
div.devel-querylog div.cell-1 {
position:absolute;
left:0px;
width:4em;
}
div.devel-querylog div.cell-2 {
position:absolute;
left:4em;
width:3em;
}
div.devel-querylog div.cell-3 {
position:absolute;
left:6em;
width:19em;
}
div.devel-querylog div.cell-4 {
position:absolute;
left:26em;
width:4em;
}
div.devel-querylog-even div.cell-5,
div.devel-querylog-odd div.cell-5 {
/*max-height:18em;*/
font-family: 'Andale Mono', monospace;
}
div.devel-querylog div.cell-6 {
position:absolute;
right:0em;
top:0em;
/*width:9em;*/
}
<?php
/**
* @file
* Drush integration for the devel module.
*/
/**
* Implements hook_drush_command().
*/
function devel_drush_command() {
$items['devel-download'] = array(
'description' => dt('Downloads the FirePHP library from http://firephp.org/.'),
'arguments' => array(
'path' => dt('Optional. A path to the download folder. If omitted Drush will use the default location (sites/all/libraries/firephp).'),
),
);
$items['devel-reinstall'] = array(
'description' => dt('Disable, Uninstall, and Install a list of projects.'),
'arguments' => array(
'path' => dt('A space separated list of project names.'),
),
'aliases' => array('dre'),
);
$items['fn-hook'] = array(
'description' => 'List implementations of a given hook and explore source of specified one.',
'arguments' => array(
'hook' => 'The name of the hook to explore.'
),
'aliases' => array('fnh', 'hook'),
);
$items['fn-view'] = array(
'description' => 'Show the source of specified function or method.',
'arguments' => array(
'function' => 'The name of the function or method to view.',
),
'options' => array(
'--pipe' => 'Output just the filename of the function',
),
'examples' => array(
'fn-view drupal_set_breadcrumb' => 'View the source code for function "drupal_set_breadcrumb"',
'vi `drush --pipe fn-view user_access`' => 'Edit the file that contains the function "user_access"',
'fn-view NodeController::load' => 'View the source code for method load in the class NodeController'
),
'aliases' => array('fnv'),
);
$items['devel-token'] = array(
'description' => dt('List available tokens'),
'aliases' => array('token'),
'core' => array(7), // Remove once 3.0 is released.
);
return $items;
}
/**
* Implementation of hook_drush_help().
*/
function devel_drush_help($section) {
switch ($section) {
case 'drush:devel-reinstall':
return dt('Disable, Uninstall, and Install a list of projects.');
case 'drush:devel-download':
return dt("Downloads the FirePHP library from http://firephp.org/. Places it in the devel module directory. Skips download if library already present. This all happens automatically if you enable devel using drush.");
}
}
/**
* A command callback. This is faster than 3 separate bootstraps.
*/
function drush_devel_reinstall() {
$projects = func_get_args();
$args = array_merge(array('pm-disable'), $projects);
call_user_func_array('drush_invoke', $args);
$args = array_merge(array('pm-uninstall'), $projects);
call_user_func_array('drush_invoke', $args);
$args = array_merge(array('pm-enable'), $projects);
call_user_func_array('drush_invoke', $args);
}
/**
* A command callback.
*/
function drush_devel_download() {
$args = func_get_args();
if (isset($args[0])) {
$path = $args[0];
}
else {
$path = drush_get_context('DRUSH_DRUPAL_ROOT');
if (module_exists('libraries')) {
$path .= '/' . libraries_get_path('FirePHPCore') . '/FirePHPCore';
}
else {
$path .= '/'. drupal_get_path('module', 'devel') . '/FirePHPCore';
}
}
if (is_dir($path)) {
drush_log('FirePHP already present. No download required.', 'ok');
}
elseif (drush_shell_exec('svn checkout http://firephp.googlecode.com/svn/branches/Library-FirePHPCore-0.3 ' . $path)) {
drush_log(dt('FirePHP has been checked out via svn to @path.', array('@path' => $path)), 'success');
}
else {
drush_log(dt('Drush was unable to checkout FirePHP to @path.', array('@path' => $path)), 'error');
}
}
/**
* Implements drush_MODULE_post_COMMAND().
*/
function drush_devel_post_pm_enable() {
$modules = func_get_args();
if (in_array('devel', $modules) && !drush_get_option('skip')) {
drush_devel_download();
}
}
/**
* Command handler. Show hook implementations
*/
function drush_devel_fn_hook($hook) {
// Get implementations in the .install files as well.
include_once './includes/install.inc';
drupal_load_updates();
if ($hook_implementations = module_implements($hook)) {
if ($choice = drush_choice(array_combine($hook_implementations, $hook_implementations), 'Enter the number of the hook implementation you wish to view.')) {
return drush_devel_fn_view($choice . "_$hook");
}
}
else {
drush_log(dt('No implementations.'), 'ok');
}
}
/**
* Command handler. Show source code of specified function or method.
*/
function drush_devel_fn_view($function_name) {
// Get implementations in the .install files as well.
include_once './includes/install.inc';
drupal_load_updates();
if (strpos($function_name, '::') === FALSE) {
if (!function_exists($function_name)) {
return drush_set_error(dt('Function not found'));
}
$reflect = new ReflectionFunction($function_name);
}
else {
list($class, $method) = explode('::', $function_name);
if (!method_exists($class, $method)) {
return drush_set_error(dt('Method not found'));
}
$reflect = new ReflectionMethod($class, $method);
}
$func_info = array('!file' => $reflect->getFileName(), '!startline' => $reflect->getStartLine(), '!endline' => $reflect->getEndLine());
//drush_print_pipe(dt("!file -line !startline", $func_info));
drush_print_pipe($reflect->getFileName());
drush_print(dt("// file: !file, lines !startline-!endline", $func_info));
_drush_devel_print_function($reflect->getFileName(), $reflect->getStartLine(), $reflect->getEndLine());
}
/**
* Command callback. List available tokens.
*/
function drush_devel_token() {
$rows[] = array(dt('Group'), dt('Token'), dt('Name'));
$all = token_info();
foreach ($all['tokens'] as $group => $tokens) {
foreach ($tokens as $key => $token) {
$rows[] = array($group, $key, $token['name']);
}
}
drush_print_table($rows, TRUE);
}
/**
* Print the specified function, including any
* doxygen-style comments that come before it.
*/
function _drush_devel_print_function($file, $start_line, $end_line) {
$line_num = 0;
$doxygen = NULL;
$fp = fopen( $file, 'r' );
while (!feof($fp) && ($line_num < ($start_line - 1))) {
$line = fgets($fp);
++$line_num;
if (substr($line,0,3) == '/**') {
$doxygen = $line;
}
elseif (isset($doxygen)) {
$doxygen .= $line;
if ($line_num + 1 == $start_line) {
drush_print(rtrim($doxygen));
}
if (strstr($line, '*/') !== FALSE) {
$doxygen = NULL;
}
}
}
while (!feof($fp) && ($line_num < $end_line)) {
$line = fgets($fp);
++$line_num;
drush_print(rtrim($line));
}
}
name = Devel
description = Various blocks, pages, and functions for developers.
package = Development
core = 7.x
configure = admin/config/development/devel
tags[] = developer
<?php
/**
* Implements hook_enable().
*/
function devel_enable() {
$t = get_t();
// New module weights in core: put devel as the very last in the chain.
db_update('system')
->fields(array(
'weight' => 88,
))
->condition('type', 'module')
->condition('name', 'devel')
->execute();
// Create a custom menu, if Menu module is enabled.
// @see devel_modules_installed()
if (module_exists('menu')) {
$menu = array(
'menu_name' => 'devel',
'title' => $t('Development'),
'description' => $t('Development link'),
);
menu_save($menu);
}
}
/**
* Implements hook_uninstall().
*/
function devel_uninstall() {
variable_del('devel_form_weights');
variable_del('devel_execution');
variable_del('dev_timer');
variable_del('devel_query_display');
variable_del('devel_redirect_page');
variable_del('devel_api_url');
variable_del('dev_mem');
variable_del('devel_error_handler');
variable_del('devel_switch_user_list_size');
variable_del('devel_switch_user_include_anon');
variable_del('devel_switch_user_show_form');
// Delete the development menu.
if (module_exists('menu')) {
if ($devel_menu = menu_load('devel')) {
menu_delete($devel_menu);
}
}
}
/**
* Remove feature for storing queries. Cleanup deprecated tables and variables.
*/
function devel_update_7000() {
db_drop_table('devel_queries');
db_drop_table('devel_times');
}
/**
* Rebuild the menus since everything is defined by devel_menu().
*/
function devel_update_7001() {
db_delete('menu_links')
->condition('module', 'devel')
->execute();
variable_set('menu_rebuild_needed', TRUE);
return t('Devel module menu links will be rebuilt.');
}
/**
* Remove deprecated variables - dev_query, devel_code_coverage
*/
function devel_update_7002() {
variable_del('dev_query'); // Sad trombone. http://drupalcode.org/viewvc/drupal/drupal/includes/database.mysql.inc?revision=1.2&view=markup
variable_del('devel_code_coverage');
}
/**
* As per issue #813132: change schablon.com to white for krumo.
*/
function devel_update_7003() {
if (variable_get('devel_krumo_skin', 'white') == 'schablon.com') {
variable_set('devel_krumo_skin', 'white');
}
}
(function ($) {
// Explain link in query log
Drupal.behaviors.devel_explain = {
attach: function() {
$('a.dev-explain').click(function () {
qid = $(this).attr("qid");
cell = $('#devel-query-' + qid);
$('.dev-explain', cell).load(Drupal.settings.basePath + '?q=devel/explain/' + Drupal.settings.devel.request_id + '/' + qid).show();
$('.dev-placeholders', cell).hide();
$('.dev-arguments', cell).hide();
return false;
});
}
}
// Arguments link in query log
Drupal.behaviors.devel_arguments = {
attach: function() {
$('a.dev-arguments').click(function () {
qid = $(this).attr("qid");
cell = $('#devel-query-' + qid);
$('.dev-arguments', cell).load(Drupal.settings.basePath + '?q=devel/arguments/' + Drupal.settings.devel.request_id + '/' + qid).show();
$('.dev-placeholders', cell).hide();
$('.dev-explain', cell).hide();
return false;
});
}
}
// Placeholders link in query log
Drupal.behaviors.devel_placeholders = {
attach: function() {
$('a.dev-placeholders').click(function () {
qid = $(this).attr("qid");
cell = $('#devel-query-' + qid);
$('.dev-explain', cell).hide();
$('.dev-arguments', cell).hide();
$('.dev-placeholders', cell).show();
return false;
});
}
}
})(jQuery);
This diff is collapsed.
<?php
// Menu callback that shows menu item.
function devel_menu_item() {
$item = menu_get_item($_GET['path']);
return kdevel_print_object($item);
}
/**
* Returns a list of all currently defined user functions in the current
* request lifecycle, with links their documentation.
*/
function devel_function_reference() {
$functions = get_defined_functions();
$version = devel_get_core_version(VERSION);
$ufunctions = $functions['user'];
sort($ufunctions);
$api = variable_get('devel_api_url', 'api.drupal.org');
foreach ($ufunctions as $function) {
$links[] = l($function, "http://$api/api/$version/function/$function");
}
return theme('item_list', array('items' => $links));
}
/**
* Menu callback; clears all caches, then redirects to the previous page.
*/
function devel_cache_clear() {
drupal_flush_all_caches();
drupal_set_message('Cache cleared.');
drupal_goto();
}
// A menu callback. Called by the AJAX link in query log.
function devel_querylog_explain($request_id, $qid) {
if (!is_numeric($request_id)) {
return MENU_ACCESS_DENIED;
}
$path = "temporary://devel_querylog/$request_id.txt";
$path = file_stream_wrapper_uri_normalize($path);
$queries = json_decode(file_get_contents($path));
$query = $queries[$qid];
$result = db_query('EXPLAIN ' . $query->query, (array)$query->args)->fetchAllAssoc('table');
$i = 1;
foreach ($result as $row) {
$row = (array)$row;
if ($i == 1) {
$header = array_keys($row);
}
$rows[] = array_values($row);
$i++;
}
// Print and return nothing thus avoiding page wrapper.
$output = theme('table', array('header' => $header, 'rows' => $rows));
print $output;
$GLOBALS['devel_shutdown'] = FALSE;
}
// A menu callback. Called by the AJAX link in query log.
function devel_querylog_arguments($request_id, $qid) {
if (!is_numeric($request_id)) {
return MENU_ACCESS_DENIED;
}
$path = "temporary://devel_querylog/$request_id.txt";
$path = file_stream_wrapper_uri_normalize($path);
$queries = json_decode(file_get_contents($path));
$query = $queries[$qid];
$conn = Database::getConnection();
$quoted = array();
foreach ((array)$query->args as $key => $val) {
$quoted[$key] = $conn->quote($val);
}
$output = strtr($query->query, $quoted);
// print and return nothing thus avoiding page wrapper.
print $output;
$GLOBALS['devel_shutdown'] = FALSE;
}
/**
* Menu callback; clear the database, resetting the menu to factory defaults.
*/
function devel_menu_rebuild() {
menu_rebuild();
drupal_set_message(t('The menu router has been rebuilt.'));
drupal_goto();
}
/**
* Display a dropdown of installed modules with the option to reinstall them.
*/
function devel_reinstall($form, &$form_state) {
$output = '';
$modules = module_list();
sort($modules);
$options = drupal_map_assoc($modules);
$form['list'] = array(
'#type' => 'checkboxes',
'#options' => $options,
'#description' => t('Uninstall and then install the selected modules. <code>hook_uninstall()</code> and <code>hook_install()</code> will be executed and the schema version number will be set to the most recent update number. You may have to manually clear out any existing tables first if the module doesn\'t implement <code>hook_uninstall()</code>.'),
);
$form['submit'] = array(
'#value' => t('Reinstall'),
'#type' => 'submit',
);
return $form;
}
/**
* Process reinstall menu form submissions.
*/
function devel_reinstall_submit($form, &$form_state) {
// require_once './includes/install.inc';
$modules = array_filter($form_state['values']['list']);
module_disable($modules, FALSE);
drupal_uninstall_modules($modules);
module_enable($modules, FALSE);
drupal_set_message(t('Uninstalled and installed: %names.', array('%names' => implode(', ', $modules))));
}
// Menu callback.
function devel_theme_registry() {
drupal_theme_initialize();
$hooks = theme_get_registry();
ksort($hooks);
return kprint_r($hooks, TRUE);
}
// Menu callback. $entity_type argument not currently used in the UI.
function devel_entity_info_page($entity_type = NULL) {
$info = entity_get_info($entity_type);
ksort($info);
return kprint_r($info, TRUE);
}
// Menu callback.
function devel_field_info_page() {
$info = field_info_fields();
$output = kprint_r($info, TRUE, t('Fields'));
$info = field_info_instances();
$output .= kprint_r($info, TRUE, t('Instances'));
$info = field_info_bundles();
$output .= kprint_r($info, TRUE, t('Bundles'));
return $output;
}
/**
* Menu callback; display all variables.
*/
function devel_variable_page() {
// We return our own $page so as to avoid blocks.
$output = drupal_get_form('devel_variable_form');
drupal_set_page_content($output);
$page = element_info('page');
return $page;
}
function devel_variable_form() {
$header = array(
'name' => array('data' => t('Name'), 'field' => 'name', 'sort' => 'asc'),
'value' => array('data' => t('Value'), 'field' => 'value'),
'length' => array('data' => t('Length'), 'field' => 'length'),
'edit' => array('data' => t('Operations')),
);
// TODO: we could get variables out of $conf but that would include hard coded ones too. ideally i would highlight overrridden/hard coded variables
$query = db_select('variable', 'v')->extend('TableSort');
$query->fields('v', array('name', 'value'));
switch (db_driver()) {
case 'mssql':
$query->addExpression("COL_LENGTH('{variable}', 'value')", 'length');
break;
case 'pgsql':
$query->addExpression("CONVERT(LENGTH(v.value), INTEGER)", 'length');
break;
default:
$query->addExpression("CONVERT(LENGTH(v.value), UNSIGNED INTEGER)", 'length');
break;
}
$result = $query
->orderByHeader($header)
->execute();
foreach ($result as $row) {
// $variables[$row->name] = '';
$options[$row->name]['name'] = check_plain($row->name);
if (merits_krumo($row->value)) {
$value = krumo_ob(variable_get($row->name, NULL));
}
else {
if (drupal_strlen($row->value) > 70) {
$value = check_plain(drupal_substr($row->value, 0, 65)) .'...';
}
else {
$value = check_plain($row->value);
}
}
$options[$row->name]['value'] = $value;
$options[$row->name]['length'] = $row->length;
$options[$row->name]['edit'] = l(t('Edit'), "devel/variable/edit/$row->name");
}
$form['variables'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No variables.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
);
// krumo($form);
return $form;
}
function devel_variable_form_submit($form, &$form_state) {
$deletes = array_filter($form_state['values']['variables']);
array_walk($deletes, 'variable_del');
if (count($deletes)) {
drupal_set_message(format_plural(count($deletes), 'One variable deleted.', '@count variables deleted.'));
}
}
function devel_variable_edit($form, &$form_state, $name) {
$value = variable_get($name, 'not found');
$form['name'] = array(
'#type' => 'value',
'#value' => $name
);
$form['value'] = array(
'#type' => 'item',
'#title' => t('Old value'),
'#markup' => dpr($value, TRUE),
);
if (is_string($value) || is_numeric($value)) {
$form['new'] = array(
'#type' => 'textarea',
'#title' => t('New value'),
'#default_value' => $value
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
}
else {
$api = variable_get('devel_api_url', 'api.drupal.org');
$form['new'] = array(
'#type' => 'item',
'#title' => t('New value'),
'#value' => t('Sorry, complex variable types may not be edited yet. Use the <em>Execute PHP</em> block and the <a href="@variable-set-doc">variable_set()</a> function.', array('@variable-set-doc' => "http://$api/api/HEAD/function/variable_set"))
);
}
drupal_set_title($name);
return $form;
}
function devel_variable_edit_submit($form, &$form_state) {
variable_set($form_state['values']['name'], $form_state['values']['new']);
drupal_set_message(t('Saved new value for %name.', array('%name' => $form_state['values']['name'])));
'devel/variable';
}
/**
* Menu callback: display the session.
*/
function devel_session() {
global $user;
$output = kprint_r($_SESSION, TRUE);
$headers = array(t('Session name'), t('Session ID'));
$output .= theme('table', array('headers' => $headers, 'rows' => array(array(session_name(), session_id()))));
return $output;
}
/**
* Menu callback; prints the loaded structure of the current node/user.
*/
function devel_load_object($type, $object, $name = NULL) {
$name = isset($name) ? $name : $type;
return kdevel_print_object($object, '$'. $name .'->');
}
/**
* Menu callback; prints the render structure of the current object (currently node or user).
*/
function devel_render_object($type, $object, $name = NULL) {
$name = isset($name) ? $name : $type;
$function = $type . '_view';
$build = $function($object);
return kdevel_print_object($build, '$'. $name .'->');
}
function devel_elements_page() {
return kdevel_print_object(module_invoke_all('element_info'));
}
function devel_phpinfo() {
print phpinfo();
drupal_exit();
}
<?php
/**
* Implements hook_rules_action_info().
*/
function devel_rules_action_info() {
return array(
'devel_debug' => array(
'base' => 'devel_rules_debug_action',
'label' => t('Debug value'),
'group' => t('Devel'),
'parameter' => array(
'value' => array('type' => 'unknown', 'label' => t('Value to debug')),
),
),
);
}
/**
* Rules action for debugging values.
*/
function devel_rules_debug_action($value) {
dpm($value);
}
<?php
/**
* @file
* Generate content, taxonomy, menu, and users via drush framework.
*/
/**
* Implementation of hook_drush_command().
*/
function devel_generate_drush_command() {
$items['generate-users'] = array(
'description' => 'Create users.',
'arguments' => array(
'number_users' => 'Number of users to generate.',
),
'options' => array(
'kill' => 'Delete all users before generating new ones.',
'roles' => 'A comma delimited list of role IDs which should be granted to the new users. No need to specify authenticated user role.',
),
'aliases' => array('genu'),
);
$items['generate-terms'] = array(
'description' => 'Create terms in specified vocabulary.',
'arguments' => array(
'machine_name' => 'Vocabulary machine name into which new terms will be inserted.',
'number_terms' => 'Number of terms to insert. Defaults to 10.',
),
'options' => array(
'kill' => 'Delete all terms in specified vocabulary before generating.',
'feedback' => 'An integer representing interval for insertion rate logging. Defaults to 500',
),
'aliases' => array('gent'),
);
$items['generate-vocabs'] = array(
'description' => 'Create vocabularies.',
'arguments' => array(
'num_vocabs' => 'Number of vocabularies to create. Defaults to 1.',
),
'options' => array(
'kill' => 'Delete all vocabularies before generating.',
),
'aliases' => array('genv'),
);
$items['generate-content'] = array(
'description' => 'Create content.',
'drupal dependencies' => array('devel_generate'),
'arguments' => array(
'number_nodes' => 'Number of nodes to generate.',
'maximum_comments' => 'Maximum number of comments to generate.',
),
'options' => array(
'kill' => 'Delete all content before generating new content.',
'types' => 'A comma delimited list of content types to create. Defaults to page,article.',
'feedback' => 'An integer representing interval for insertion rate logging. Defaults to 500',
'skip-fields' => 'A comma delimited list of fields to omit when generating random values',
'languages' => 'A comma-separated list of language codes',
),
'aliases' => array('genc'),
);
$items['generate-menus'] = array(
'description' => 'Create menus and menu items.',
'drupal dependencies' => array('devel_generate'), // Remove these once devel.module is moved down a directory. http://drupal.org/node/925246
'arguments' => array(
'number_menus' => 'Number of menus to generate. Defaults to 2.',
'number_links' => 'Number of links to generate. Defaults to 50.',
'max_depth' => 'Max link depth. Defaults to 3',
'max_width' => 'Max width of first level of links. Defaults to 8.',
),
'options' => array(
'kill' => 'Delete all previously generated menus and links before generating new menus and links.',
),
'aliases' => array('genm'),
);
return $items;
}
/**
* Command callback. Generate a number of users.
*/
function drush_devel_generate_users($num_users = NULL) {
if (drush_generate_is_number($num_users) == FALSE) {
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', t('Invalid number of users.'));
}
drush_generate_include_devel();
$roles = drush_get_option('roles') ? explode(',', drush_get_option('roles')) : array();
devel_create_users($num_users, drush_get_option('kill'), 0, $roles);
drush_log(t('Generated @number users.', array('@number' => $num_users)), 'success');
}
/**
* Command callback. Generate a number of terms in given vocabs.
*/
function drush_devel_generate_terms($vname = NULL, $num_terms = 10) {
// Try to convert machine name to a vocab ID
if (!$vocab = taxonomy_vocabulary_machine_name_load($vname)) {
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid vocabulary name: !name', array('!name' => $vname)));
}
if (drush_generate_is_number($num_terms) == FALSE) {
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of terms: !num', array('!num' => $num_terms)));
}
drush_generate_include_devel();
$vocabs[$vocab->vid] = $vocab;
devel_generate_term_data($vocabs, $num_terms, '12', drush_get_option('kill'));
drush_log(dt('Generated @num_terms terms.', array('@num_terms' => $num_terms)), 'success');
}
/**
* Command callback. Generate a number of vocabularies.
*/
function drush_devel_generate_vocabs($num_vocab = 1) {
if (drush_generate_is_number($num_vocab) == FALSE) {
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of vocabularies: !num.', array('!num' => $num_vocab)));
}
drush_generate_include_devel();
devel_generate_vocab_data($num_vocab, '12', drush_get_option('kill'));
drush_log(dt('Generated @num_vocab vocabularies.', array('@num_vocab' => $num_vocab)), 'success');
}
/**
* Command callback. Generate a number of nodes and comments.
*/
function drush_devel_generate_content($num_nodes = NULL, $max_comments = NULL) {
if (drush_generate_is_number($num_nodes) == FALSE) {
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of nodes'));
}
if (!empty($max_comments) && drush_generate_is_number($max_comments) == FALSE) {
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of comments.'));
}
$add_language = drush_get_option('languages');
if (!empty($add_language)) {
$add_language = explode(',', str_replace(' ', '', $add_language));
// Intersect with the enabled languages to make sure the language args
// passed are actually enabled.
$values['values']['add_language'] = array_flip(array_intersect($add_language, array_keys(locale_language_list())));
}
// Load user 1; is needed for creating *published* comments.
if ($max_comments) {
global $user;
$user_one = user_load(1);
$user = $user_one;
drupal_save_session(FALSE);
}
$values['values']['kill_content'] = drush_get_option('kill');
$values['values']['title_length'] = 6;
$values['values']['num_nodes'] = $num_nodes;
$values['values']['max_comments'] = $max_comments;
$values['values']['node_types'] = drupal_map_assoc(explode(',', drush_get_option('types', 'page,article')));
drush_generate_include_devel();
devel_generate_content($values);
drush_log(t('Generated @num_nodes nodes, @max_comments comments (or less) per node.', array('@num_nodes' => (int)$num_nodes, '@max_comments' => (int)$max_comments)), 'success');
}
/**
* Command callback. Generate a number of menus and menu links.
*/
function drush_devel_generate_menus($number_menus = 2, $number_links = 50, $max_depth = 3, $max_width = 8) {
if (drush_generate_is_number($number_menus) == FALSE) {
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of menus'));
}
if (drush_generate_is_number($number_links) == FALSE) {
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of links'));
}
if (drush_generate_is_number($max_depth) == FALSE || $max_depth > 9 || $max_depth < 1) {
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid maximum link depth. Use a value between 1 and 9'));
}
if (drush_generate_is_number($max_width) == FALSE || $max_width < 1) {
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid maximum menu width. Use a positive numeric value.'));
}
global $user;
$user_one = user_load(1);
$user = $user_one;
drupal_save_session(FALSE);
$kill = drush_get_option('kill');
drush_generate_include_devel();
$link_types = drupal_map_assoc(array('node', 'front', 'external'));
devel_generate_menu_data($number_menus, array(), $number_links, 12, $link_types, $max_depth, $max_width, $kill);
drush_log(t('Generated @number_menus menus, @number_links links.', array('@number_menus' => (int)$number_menus, '@number_links' => (int)$number_links)), 'success');
}
//////////////////////////////////////////////////////////////////////////////
// Helper functions
// Verify if param is a number.
function drush_generate_is_number($number) {
if ($number == NULL) return FALSE;
if (!is_numeric($number)) return FALSE;
return TRUE;
}
// Include devel_generate.inc.
function drush_generate_include_devel() {
$path = drupal_get_path('module', 'devel_generate');
require_once($path .'/devel_generate.inc');
}
<?php
/**
* @file
* Functions needed for devel_generate Fields API integration.
*/
/**
* Enrich the $object that is about to be saved with arbitrary
* information in each of its fields.
**/
function devel_generate_fields(&$object, $obj_type, $bundle) {
$field_types = field_info_field_types();
$instances = field_info_instances($obj_type, $bundle);
$skips = function_exists('drush_get_option') ? drush_get_option('skip-fields', '') : @$_REQUEST['skip-fields'];
foreach (explode(',', $skips) as $skip) {
unset($instances[$skip]);
}
foreach ($instances as $instance) {
$field_name = $instance['field_name'];
$field = field_info_field($field_name);
$object_field = array();
// If module handles own multiples, then only call its hook once.
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
$max = 0;
}
else {
switch ($field['cardinality']) {
case FIELD_CARDINALITY_UNLIMITED:
$max = rand(0, 3); //just an arbitrary number for 'unlimited'
break;
default:
$max = $field['cardinality'] - 1;
break;
}
}
for ($i = 0; $i <= $max; $i++) {
$module = $field_types[$field['type']]['module'];
// Include any support file that might exist for this field.
if (in_array($module, array('file', 'image', 'taxonomy', 'number', 'text', 'comment', 'list'))) {
// devel_generate implements on behalf of core and special friends.
module_load_include('inc', 'devel_generate', "$module.devel_generate");
}
else {
module_load_include('inc', $module, "$module.devel_generate");
}
$function = $module . '_devel_generate';
if (function_exists($function)) {
if ($result = $function($object, $field, $instance, $bundle)) {
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
// Fields that handle their own multiples will add their own deltas.
$object_field = $result;
}
else {
// When multiples are handled by the content module, add a delta for each result.
$object_field[$i] = $result;
}
}
}
}
// TODO: Completely overriding any existing $object->{$field['field_name']}
// is necessary here because the forum module has a bug where it
// initializes the property with incorrect data.
// @see http://drupal.org/node/652176
$object->{$field['field_name']} = array(
$object->language => $object_field,
);
}
}
/**
* A simple function to return multiple values for fields that use
* custom multiple value widgets but don't need any other special multiple
* values handling. This will call the field generation function
* a random number of times and compile the results into a node array.
*/
function devel_generate_multiple($function, $object, $field, $instance, $bundle) {
$object_field = array();
if (function_exists($function)) {
switch ($field['cardinality']) {
case FIELD_CARDINALITY_UNLIMITED:
$max = rand(0, 3); //just an arbitrary number for 'unlimited'
break;
default:
$max = $field['cardinality'] - 1;
break;
}
for ($i = 0; $i <= $max; $i++) {
$result = $function($object, $field, $instance, $bundle);
if (!empty($result)) {
$object_field[$i] = $result;
}
}
}
return $object_field;
}
This diff is collapsed.
name = Devel generate
description = Generate dummy users, nodes, and taxonomy terms.
package = Development
core = 7.x
dependencies[] = devel
tags[] = developer
<?php
/**
* Implementation of hook_menu().
*/
function devel_generate_menu() {
$items = array();
$items['admin/config/development/generate/user'] = array(
'title' => 'Generate users',
'description' => 'Generate a given number of users. Optionally delete current users.',
'page callback' => 'drupal_get_form',
'page arguments' => array('devel_generate_users_form'),
'access arguments' => array('administer users'),
);
$items['admin/config/development/generate/content'] = array(
'title' => 'Generate content',
'description' => 'Generate a given number of nodes and comments. Optionally delete current items.',
'page callback' => 'drupal_get_form',
'page arguments' => array('devel_generate_content_form'),
'access arguments' => array('administer nodes'),
);
if (module_exists('taxonomy')) {
$items['admin/config/development/generate/taxonomy'] = array(
'title' => 'Generate terms',
'description' => 'Generate a given number of terms. Optionally delete current terms.',
'page callback' => 'drupal_get_form',
'page arguments' => array('devel_generate_term_form'),
'access arguments' => array('administer taxonomy'),
);
$items['admin/config/development/generate/vocabs'] = array(
'title' => 'Generate vocabularies',
'description' => 'Generate a given number of vocabularies. Optionally delete current vocabularies.',
'page callback' => 'drupal_get_form',
'page arguments' => array('devel_generate_vocab_form'),
'access arguments' => array('administer taxonomy'),
);
}
$items['admin/config/development/generate/menu'] = array(
'title' => 'Generate menus',
'description' => 'Generate a given number of menus and menu links. Optionally delete current menus.',
'page callback' => 'drupal_get_form',
'page arguments' => array('devel_generate_menu_form'),
'access arguments' => array('administer menu'),
);
return $items;
}
function devel_generate_users_form() {
$form['num'] = array(
'#type' => 'textfield',
'#title' => t('How many users would you like to generate?'),
'#default_value' => 50,
'#size' => 10,
);
$form['kill_users'] = array(
'#type' => 'checkbox',
'#title' => t('Delete all users (except user id 1) before generating new users.'),
'#default_value' => FALSE,
);
$options = user_roles(TRUE);
unset($options[DRUPAL_AUTHENTICATED_RID]);
$form['roles'] = array(
'#type' => 'checkboxes',
'#title' => t('Which roles should the users receive?'),
'#description' => t('Users always receive the <em>authenticated user</em> role.'),
'#options' => $options,
);
$options = array(1 => t('Now'));
foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) {
$options[$interval] = format_interval($interval, 1) . ' ' . t('ago');
}
$form['time_range'] = array(
'#type' => 'select',
'#title' => t('How old should user accounts be?'),
'#description' => t('User ages will be distributed randomly from the current time, back to the selected time.'),
'#options' => $options,
'#default_value' => 604800,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Generate'),
);
return $form;
}
function devel_generate_users_form_submit($form_id, &$form_state) {
module_load_include('inc', 'devel_generate');
$values = $form_state['values'];
devel_create_users($values['num'], $values['kill_users'], $values['time_range'], $values['roles']);
}
function devel_generate_content_form() {
$options = array();
if (module_exists('content')) {
$types = content_types();
foreach ($types as $type) {
$warn = '';
if (count($type['fields'])) {
$warn = t('. This type contains CCK fields which will only be populated by fields that implement the content_generate hook.');
}
$options[$type['type']] = t($type['name']). $warn;
}
}
else {
$types = node_type_get_types();
$suffix = '';
foreach ($types as $type) {
if (module_exists('comment')) {
$default = variable_get('comment_' . $type->type, COMMENT_NODE_OPEN);
$map = array(t('Hidden'), t('Closed'), t('Open'));
$suffix = '<small>. ' . t('Comments: ') . $map[$default]. '</small>';
}
$options[$type->type] = t($type->name) . $suffix;
}
}
// we cannot currently generate valid polls.
unset($options['poll']);
if (empty($options)) {
drupal_set_message(t('You do not have any content types that can be generated. <a href="@create-type">Go create a new content type</a> already!</a>', array('@create-type' => url('admin/structure/types/add'))), 'error', FALSE);
return;
}
$form['node_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Content types'),
'#options' => $options,
'#default_value' => array_keys($options),
);
if (module_exists('checkall')) $form['node_types']['#checkall'] = TRUE;
$form['kill_content'] = array(
'#type' => 'checkbox',
'#title' => t('<strong>Delete all content</strong> in these content types before generating new content.'),
'#default_value' => FALSE,
);
$form['num_nodes'] = array(
'#type' => 'textfield',
'#title' => t('How many nodes would you like to generate?'),
'#default_value' => 50,
'#size' => 10,
);
$options = array(1 => t('Now'));
foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) {
$options[$interval] = format_interval($interval, 1) . ' ' . t('ago');
}
$form['time_range'] = array(
'#type' => 'select',
'#title' => t('How far back in time should the nodes be dated?'),
'#description' => t('Node creation dates will be distributed randomly from the current time, back to the selected time.'),
'#options' => $options,
'#default_value' => 604800,
);
$form['max_comments'] = array(
'#type' => module_exists('comment') ? 'textfield' : 'value',
'#title' => t('Maximum number of comments per node.'),
'#description' => t('You must also enable comments for the content types you are generating. Note that some nodes will randomly receive zero comments. Some will receive the max.'),
'#default_value' => 0,
'#size' => 3,
'#access' => module_exists('comment'),
);
$form['title_length'] = array(
'#type' => 'textfield',
'#title' => t('Max word length of titles'),
'#default_value' => 4,
'#size' => 10,
);
$form['add_alias'] = array(
'#type' => 'checkbox',
'#disabled' => !module_exists('path'),
'#description' => t('Requires path.module'),
'#title' => t('Add an url alias for each node.'),
'#default_value' => FALSE,
);
$form['add_statistics'] = array(
'#type' => 'checkbox',
'#title' => t('Add statistics for each node (node_counter table).'),
'#default_value' => TRUE,
'#access' => module_exists('statistics'),
);
unset($options);
$options[LANGUAGE_NONE] = t('Language neutral');
if (module_exists('locale')) {
$options += locale_language_list();
}
$form['add_language'] = array(
'#type' => 'select',
'#title' => t('Set language on nodes'),
'#multiple' => TRUE,
'#disabled' => !module_exists('locale'),
'#description' => t('Requires locale.module'),
'#options' => $options,
'#default_value' => array(LANGUAGE_NONE => LANGUAGE_NONE),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Generate'),
);
$form['#redirect'] = FALSE;
return $form;
}
function devel_generate_content_form_submit($form_id, &$form_state) {
module_load_include('inc', 'devel_generate', 'devel_generate');
$form_state['values']['node_types'] = array_filter($form_state['values']['node_types']);
if ($form_state['values']['num_nodes'] <= 50 && $form_state['values']['max_comments'] <= 10) {
module_load_include('inc', 'devel_generate');
devel_generate_content($form_state);
}
else {
module_load_include('inc', 'devel_generate', 'devel_generate_batch');
devel_generate_batch_content($form_state);
}
}
function devel_generate_term_form() {
$options = array();
foreach (taxonomy_get_vocabularies() as $vid => $vocab) {
$options[$vid] = $vocab->machine_name;
}
$form['vids'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Vocabularies'),
'#required' => TRUE,
'#options' => $options,
'#description' => t('Restrict terms to these vocabularies.'),
);
$form['num_terms'] = array(
'#type' => 'textfield',
'#title' => t('Number of terms?'),
'#default_value' => 10,
'#size' => 10,
);
$form['title_length'] = array(
'#type' => 'textfield',
'#title' => t('Max word length of term names'),
'#default_value' => 12,
'#size' => 10,
);
$form['kill_taxonomy'] = array(
'#type' => 'checkbox',
'#title' => t('Delete existing terms in specified vocabularies before generating new terms.'),
'#default_value' => FALSE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Generate'),
);
return $form;
}
function devel_generate_vocab_form() {
$form['num_vocabs'] = array(
'#type' => 'textfield',
'#title' => t('Number of vocabularies?'),
'#default_value' => 1,
'#size' => 10,
);
$form['title_length'] = array(
'#type' => 'textfield',
'#title' => t('Max word length of vocabulary names'),
'#default_value' => 12,
'#size' => 10,
);
$form['kill_taxonomy'] = array(
'#type' => 'checkbox',
'#title' => t('Delete existing vocabularies before generating new ones.'),
'#default_value' => FALSE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Generate'),
);
return $form;
}
function devel_generate_term_form_submit($form_id, &$form_state) {
module_load_include('inc', 'devel_generate');
$vocabs = taxonomy_vocabulary_load_multiple($form_state['values']['vids']);
devel_generate_term_data($vocabs, $form_state['values']['num_terms'], $form_state['values']['title_length'], $form_state['values']['kill_taxonomy']);
}
function devel_generate_vocab_form_submit($form_id, &$form_state) {
module_load_include('inc', 'devel_generate');
devel_generate_vocab_data($form_state['values']['num_vocabs'], $form_state['values']['title_length'], $form_state['values']['kill_taxonomy']);
}
function devel_generate_node_insert($node) {
if (isset($node->devel_generate)) {
$results = $node->devel_generate;
if (!empty($results['max_comments']) && $node->comment >= COMMENT_NODE_OPEN) {
devel_generate_add_comments($node, $results['users'], $results['max_comments'], $results['title_length']);
}
// Add an url alias. Cannot happen before save because we don't know the nid.
if (!empty($results['add_alias'])) {
$path = array(
'source' => 'node/' . $node->nid,
'alias' => 'node-' . $node->nid . '-' . $node->type,
);
path_save($path);
}
// Add node statistics.
if (!empty($results['add_statistics']) && module_exists('statistics')) {
devel_generate_add_statistics($node);
}
}
}
function devel_generate_set_message($msg, $type = 'status') {
$function = function_exists('drush_log') ? 'drush_log' : 'drupal_set_message';
$function($msg, $type);
}
function devel_generate_menu_form() {
$menu_enabled = module_exists('menu');
if ($menu_enabled) {
$menus = array('__new-menu__' => t('Create new menu(s)')) + menu_get_menus();
}
else {
$menus = menu_list_system_menus();
}
$form['existing_menus'] = array(
'#type' => 'checkboxes',
'#title' => t('Generate links for these menus'),
'#options' => $menus,
'#default_value' => array('__new-menu__'),
'#required' => TRUE,
);
if ($menu_enabled) {
$form['num_menus'] = array(
'#type' => 'textfield',
'#title' => t('Number of new menus to create'),
'#default_value' => 2,
'#size' => 10,
'#states' => array(
'visible' => array(
':input[name=existing_menus[__new-menu__]]' => array('checked' => TRUE),
),
),
);
}
$form['num_links'] = array(
'#type' => 'textfield',
'#title' => t('Number of links to generate'),
'#default_value' => 50,
'#size' => 10,
'#required' => TRUE,
);
$form['title_length'] = array(
'#type' => 'textfield',
'#title' => t('Max word length of menu and menu link names'),
'#default_value' => 12,
'#size' => 10,
'#required' => TRUE,
);
$form['link_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Types of links to generate'),
'#options' => array(
'node' => t('Nodes'),
'front' => t('Front page'),
'external' => t('External'),
),
'#default_value' => array('node', 'front', 'external'),
'#required' => TRUE,
);
$form['max_depth'] = array(
'#type' => 'select',
'#title' => t('Maximum link depth'),
'#options' => range(0, MENU_MAX_DEPTH),
'#default_value' => floor(MENU_MAX_DEPTH / 2),
'#required' => TRUE,
);
unset($form['max_depth']['#options'][0]);
$form['max_width'] = array(
'#type' => 'textfield',
'#title' => t('Maximum menu width'),
'#default_value' => 6,
'#size' => 10,
'#description' => t("Limit the width of the generated menu's first level of links to a certain number of items."),
'#required' => TRUE,
);
$form['kill'] = array(
'#type' => 'checkbox',
'#title' => t('Delete existing custom generated menus and menu links before generating new ones.'),
'#default_value' => FALSE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Generate'),
);
return $form;
}
function devel_generate_menu_form_submit($form_id, &$form_state) {
// If the create new menus checkbox is off, set the number of new menus to 0.
if (!isset($form_state['values']['existing_menus']['__new-menu__']) || !$form_state['values']['existing_menus']['__new-menu__']) {
$form_state['values']['num_menus'] = 0;
}
module_load_include('inc', 'devel_generate');
devel_generate_menu_data($form_state['values']['num_menus'], $form_state['values']['existing_menus'], $form_state['values']['num_links'], $form_state['values']['title_length'], $form_state['values']['link_types'], $form_state['values']['max_depth'], $form_state['values']['max_width'], $form_state['values']['kill']);
}
<?php
/**
* Devel Generate batch handling functions using the BatchAPI
*/
/**
* Functions called from FAPI:
*/
function devel_generate_batch_content($form_state) {
$operations = array();
// Setup the batch operations and save the variables.
$operations[] = array('devel_generate_batch_content_pre_node', array($form_state['values']));
// add the kill operation
if ($form_state['values']['kill_content']) {
$operations[] = array('devel_generate_batch_content_kill', array());
}
// add the operations to create the nodes
for ($num = 0; $num < $form_state['values']['num_nodes']; $num ++) {
$operations[] = array('devel_generate_batch_content_add_node', array());
}
// start the batch
$batch = array(
'title' => t('Generating Content'),
'operations' => $operations,
'finished' => 'devel_generate_batch_finished',
'file' => drupal_get_path('module', 'devel_generate') . '/devel_generate_batch.inc',
);
batch_set($batch);
}
/**
* Create Content Batch Functions:
*/
function devel_generate_batch_content_kill(&$context) {
module_load_include('inc', 'devel_generate', 'devel_generate');
devel_generate_content_kill($context['results']);
}
function devel_generate_batch_content_pre_node($vars, &$context) {
$context['results'] = $vars;
$context['results']['num_nids'] = 0;
module_load_include('inc', 'devel_generate', 'devel_generate');
devel_generate_content_pre_node($context['results']);
}
function devel_generate_batch_content_add_node(&$context) {
module_load_include('inc', 'devel_generate', 'devel_generate');
devel_generate_content_add_node($context['results']);
$context['results']['num_nids'] ++;
}
function devel_generate_batch_finished($success, $results, $operations) {
if ($success) {
$message = t('Finished @num_nids nodes created successfully.', array('@num_nids' => $results['num_nids']));
}
else {
$message = t('Finished with an error.');
}
drupal_set_message($message);
}