fetchResult(
"SELECT $columns FROM `INFORMATION_SCHEMA`.`ROUTINES` WHERE $where;"
);
echo PMA_RTE_getList('routine', $items);
/**
* Display the form for adding a new routine, if the user has the privileges.
*/
echo PMA_RTN_getFooterLinks();
/**
* Display a warning for users with PHP's old "mysql" extension.
*/
if ($GLOBALS['cfg']['Server']['extension'] === 'mysql') {
trigger_error(
__(
'You are using PHP\'s deprecated \'mysql\' extension, '
. 'which is not capable of handling multi queries. '
. '[strong]The execution of some stored routines may fail![/strong] '
. 'Please use the improved \'mysqli\' extension to '
. 'avoid any problems.'
),
E_USER_WARNING
);
}
} // end PMA_RTN_main()
/**
* This function parses a string containing one parameter of a routine,
* as returned by PMA_RTN_parseAllParameters() and returns an array containing
* the information about this parameter.
*
* @param string $value A string containing one parameter of a routine
*
* @return array Parsed information about the input parameter
*/
function PMA_RTN_parseOneParameter($value)
{
global $param_directions;
$retval = array(0 => '',
1 => '',
2 => '',
3 => '',
4 => '');
$parsed_param = PMA_SQP_parse($value);
$pos = 0;
if (in_array(strtoupper($parsed_param[$pos]['data']), $param_directions)) {
$retval[0] = strtoupper($parsed_param[0]['data']);
$pos++;
}
if ($parsed_param[$pos]['type'] == 'alpha_identifier'
|| $parsed_param[$pos]['type'] == 'quote_backtick'
) {
$retval[1] = PMA_Util::unQuote(
$parsed_param[$pos]['data']
);
$pos++;
}
$depth = 0;
$param_length = '';
$param_opts = array();
for ($i=$pos; $i<$parsed_param['len']; $i++) {
if (($parsed_param[$i]['type'] == 'alpha_columnType'
|| $parsed_param[$i]['type'] == 'alpha_functionName') && $depth == 0
) {
$retval[2] = strtoupper($parsed_param[$i]['data']);
} else if ($parsed_param[$i]['type'] == 'punct_bracket_open_round'
&& $depth == 0
) {
$depth = 1;
} else if ($parsed_param[$i]['type'] == 'punct_bracket_close_round'
&& $depth == 1
) {
$depth = 0;
} else if ($depth == 1) {
$param_length .= $parsed_param[$i]['data'];
} else if ($parsed_param[$i]['type'] == 'alpha_reservedWord'
&& strtoupper($parsed_param[$i]['data']) == 'CHARSET' && $depth == 0
) {
if ($parsed_param[$i+1]['type'] == 'alpha_charset'
|| $parsed_param[$i+1]['type'] == 'alpha_identifier'
) {
$param_opts[] = strtolower($parsed_param[$i+1]['data']);
}
} else if ($parsed_param[$i]['type'] == 'alpha_columnAttrib'
&& $depth == 0
) {
$param_opts[] = strtoupper($parsed_param[$i]['data']);
}
}
$retval[3] = $param_length;
sort($param_opts);
$retval[4] = implode(' ', $param_opts);
return $retval;
} // end PMA_RTN_parseOneParameter()
/**
* This function looks through the contents of a parsed
* SHOW CREATE [PROCEDURE | FUNCTION] query and extracts
* information about the routine's parameters.
*
* @param array $parsed_query Parsed query, returned by by PMA_SQP_parse()
* @param string $routine_type Routine type: 'PROCEDURE' or 'FUNCTION'
*
* @return array Information about the parameteres of a routine.
*/
function PMA_RTN_parseAllParameters($parsed_query, $routine_type)
{
$retval = array();
$retval['num'] = 0;
// First get the list of parameters from the query
$buffer = '';
$params = array();
$fetching = false;
$depth = 0;
for ($i=0; $i<$parsed_query['len']; $i++) {
if ($parsed_query[$i]['type'] == 'alpha_reservedWord'
&& $parsed_query[$i]['data'] == $routine_type
) {
$fetching = true;
} else if ($fetching == true
&& $parsed_query[$i]['type'] == 'punct_bracket_open_round'
) {
$depth++;
if ($depth > 1) {
$buffer .= $parsed_query[$i]['data'] . ' ';
}
} else if ($fetching == true
&& $parsed_query[$i]['type'] == 'punct_bracket_close_round'
) {
$depth--;
if ($depth > 0) {
$buffer .= $parsed_query[$i]['data'] . ' ';
} else {
break;
}
} else if ($parsed_query[$i]['type'] == 'punct_listsep' && $depth == 1) {
$params[] = $buffer;
$retval['num']++;
$buffer = '';
} else if ($fetching == true && $depth > 0) {
$buffer .= $parsed_query[$i]['data'] . ' ';
}
}
if (! empty($buffer)) {
$params[] = $buffer;
$retval['num']++;
}
// Now parse each parameter individually
foreach ($params as $key => $value) {
list($retval['dir'][],
$retval['name'][],
$retval['type'][],
$retval['length'][],
$retval['opts'][]) = PMA_RTN_parseOneParameter($value);
}
// Since some indices of $retval may be still undefined, we fill
// them each with an empty array to avoid E_ALL errors in PHP.
foreach (array('dir', 'name', 'type', 'length', 'opts') as $key => $index) {
if (! isset($retval[$index])) {
$retval[$index] = array();
}
}
return $retval;
} // end PMA_RTN_parseAllParameters()
/**
* This function looks through the contents of a parsed
* SHOW CREATE [PROCEDURE | FUNCTION] query and extracts
* information about the routine's definer.
*
* @param array $parsed_query Parsed query, returned by PMA_SQP_parse()
*
* @return string The definer of a routine.
*/
function PMA_RTN_parseRoutineDefiner($parsed_query)
{
$retval = '';
$fetching = false;
for ($i=0; $i<$parsed_query['len']; $i++) {
if ($parsed_query[$i]['type'] == 'alpha_reservedWord'
&& $parsed_query[$i]['data'] == 'DEFINER'
) {
$fetching = true;
} else if ($fetching == true
&& $parsed_query[$i]['type'] != 'quote_backtick'
&& substr($parsed_query[$i]['type'], 0, 5) != 'punct'
) {
break;
} else if ($fetching == true
&& $parsed_query[$i]['type'] == 'quote_backtick'
) {
$retval .= PMA_Util::unQuote(
$parsed_query[$i]['data']
);
} else if ($fetching == true && $parsed_query[$i]['type'] == 'punct_user') {
$retval .= $parsed_query[$i]['data'];
}
}
return $retval;
} // end PMA_RTN_parseRoutineDefiner()
/**
* Handles editor requests for adding or editing an item
*
* @return void
*/
function PMA_RTN_handleEditor()
{
global $_GET, $_POST, $_REQUEST, $GLOBALS, $db, $errors;
if (! empty($_REQUEST['editor_process_add'])
|| ! empty($_REQUEST['editor_process_edit'])
) {
/**
* Handle a request to create/edit a routine
*/
$sql_query = '';
$routine_query = PMA_RTN_getQueryFromRequest();
if (! count($errors)) { // set by PMA_RTN_getQueryFromRequest()
// Execute the created query
if (! empty($_REQUEST['editor_process_edit'])) {
$isProcOrFunc = in_array(
$_REQUEST['item_original_type'],
array('PROCEDURE', 'FUNCTION')
);
if (!$isProcOrFunc) {
$errors[] = sprintf(
__('Invalid routine type: "%s"'),
htmlspecialchars($_REQUEST['item_original_type'])
);
} else {
// Backup the old routine, in case something goes wrong
$create_routine = $GLOBALS['dbi']->getDefinition(
$db, $_REQUEST['item_original_type'],
$_REQUEST['item_original_name']
);
$drop_routine = "DROP {$_REQUEST['item_original_type']} "
. PMA_Util::backquote($_REQUEST['item_original_name'])
. ";\n";
$result = $GLOBALS['dbi']->tryQuery($drop_routine);
if (! $result) {
$errors[] = sprintf(
__('The following query has failed: "%s"'),
htmlspecialchars($drop_routine)
)
. '
'
. __('MySQL said: ') . $GLOBALS['dbi']->getError(null);
} else {
$result = $GLOBALS['dbi']->tryQuery($routine_query);
if (! $result) {
$errors[] = sprintf(
__('The following query has failed: "%s"'),
htmlspecialchars($routine_query)
)
. '
'
. __('MySQL said: ') . $GLOBALS['dbi']->getError(null);
// We dropped the old routine,
// but were unable to create the new one
// Try to restore the backup query
$result = $GLOBALS['dbi']->tryQuery($create_routine);
if (! $result) {
// OMG, this is really bad! We dropped the query,
// failed to create a new one
// and now even the backup query does not execute!
// This should not happen, but we better handle
// this just in case.
$errors[] = __(
'Sorry, we failed to restore'
. ' the dropped routine.'
)
. '
'
. __('The backed up query was:')
. "\"" . htmlspecialchars($create_routine) . "\""
. '
'
. __('MySQL said: ')
. $GLOBALS['dbi']->getError(null);
}
} else {
$message = PMA_Message::success(
__('Routine %1$s has been modified.')
);
$message->addParam(
PMA_Util::backquote($_REQUEST['item_name'])
);
$sql_query = $drop_routine . $routine_query;
}
}
}
} else {
// 'Add a new routine' mode
$result = $GLOBALS['dbi']->tryQuery($routine_query);
if (! $result) {
$errors[] = sprintf(
__('The following query has failed: "%s"'),
htmlspecialchars($routine_query)
)
. '
'
. __('MySQL said: ') . $GLOBALS['dbi']->getError(null);
} else {
$message = PMA_Message::success(
__('Routine %1$s has been created.')
);
$message->addParam(
PMA_Util::backquote($_REQUEST['item_name'])
);
$sql_query = $routine_query;
}
}
}
if (count($errors)) {
$message = PMA_Message::error(
__(
'One or more errors have occurred while'
. ' processing your request:'
)
);
$message->addString('