Split code.

Signed-off-by: Hugues Peccatte <hugues.peccatte@gmail.com>
This commit is contained in:
Hugues Peccatte 2015-08-09 19:36:09 +02:00
parent 4e2310d546
commit 6a73312c9f

View File

@ -105,6 +105,7 @@ function PMA_RTN_handleEditor()
$_REQUEST['item_original_type'],
array('PROCEDURE', 'FUNCTION')
);
if (!$isProcOrFunc) {
$errors[] = sprintf(
__('Invalid routine type: "%s"'),
@ -117,28 +118,7 @@ function PMA_RTN_handleEditor()
$_REQUEST['item_original_name']
);
if (! defined('PMA_DRIZZLE') || ! PMA_DRIZZLE) {
if (isset($GLOBALS['proc_priv']) && $GLOBALS['proc_priv']
&& isset($GLOBALS['flush_priv']) && $GLOBALS['flush_priv']
) {
// Backup the Old Privileges before dropping
// if $_REQUEST['item_adjust_privileges'] set
$privilegesBackup = array();
if (isset($_REQUEST['item_adjust_privileges'])
&& ! empty($_REQUEST['item_adjust_privileges'])
) {
$privilegesBackupQuery = 'SELECT * FROM ' . PMA_Util::backquote('mysql')
. '.' . PMA_Util::backquote('procs_priv')
. ' where Routine_name = "' . $_REQUEST['item_original_name']
. '" AND Routine_type = "' . $_REQUEST['item_original_type']
. '";';
$privilegesBackup = $GLOBALS['dbi']->fetchResult(
$privilegesBackupQuery, 0
);
}
}
}
$privilegesBackup = PMA_RTN_backupPrivileges();
$drop_routine = "DROP {$_REQUEST['item_original_type']} "
. PMA_Util::backquote($_REQUEST['item_original_name'])
@ -152,75 +132,13 @@ function PMA_RTN_handleEditor()
. '<br />'
. __('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)
)
. '<br />'
. __('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);
$errors = checkResult(
$result,
__(
'Sorry, we failed to restore'
. ' the dropped routine.'
),
$create_routine,
$errors
);
} else {
// Default value
$resultAdjust = false;
if (! defined('PMA_DRIZZLE') || ! PMA_DRIZZLE) {
if (isset($GLOBALS['proc_priv']) && $GLOBALS['proc_priv']
&& isset($GLOBALS['flush_priv']) && $GLOBALS['flush_priv']
) {
// Insert all the previous privileges
// but with the new name and the new type
foreach ($privilegesBackup as $priv) {
$adjustProcPrivilege = 'INSERT INTO '
. PMA_Util::backquote('mysql') . '.'
. PMA_Util::backquote('procs_priv')
. ' VALUES("' . $priv[0] . '", "'
. $priv[1] . '", "' . $priv[2] . '", "'
. $_REQUEST['item_name'] . '", "'
. $_REQUEST['item_type'] . '", "'
. $priv[5] . '", "'
. $priv[6] . '", "'
. $priv[7] . '");';
$resultAdjust = $GLOBALS['dbi']->query(
$adjustProcPrivilege
);
}
}
}
if ($resultAdjust) {
// Flush the Privileges
$flushPrivQuery = 'FLUSH PRIVILEGES;';
$GLOBALS['dbi']->query($flushPrivQuery);
$message = PMA_Message::success(
__(
'Routine %1$s has been modified. Privileges have been adjusted.'
)
);
} 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;
}
list($errors, $message, $sql_query) = PMA_RTN_createRoutine(
$routine_query,
$errors,
$create_routine,
$privilegesBackup,
$drop_routine
);
}
}
} else {
@ -359,6 +277,154 @@ function PMA_RTN_handleEditor()
}
}
}
}
/**
* Backup the privileges
*
* @return array
*/
function PMA_RTN_backupPrivileges()
{
if (defined('PMA_DRIZZLE') && PMA_DRIZZLE) {
return array();
}
if (!(isset($GLOBALS['proc_priv']) && $GLOBALS['proc_priv']
&& isset($GLOBALS['flush_priv'])
&& $GLOBALS['flush_priv']
)) {
return array();
}
// Backup the Old Privileges before dropping
// if $_REQUEST['item_adjust_privileges'] set
if (!isset($_REQUEST['item_adjust_privileges'])
|| empty($_REQUEST['item_adjust_privileges'])
) {
return array();
}
$privilegesBackupQuery = 'SELECT * FROM ' . PMA_Util::backquote(
'mysql'
)
. '.' . PMA_Util::backquote('procs_priv')
. ' where Routine_name = "' . $_REQUEST['item_original_name']
. '" AND Routine_type = "' . $_REQUEST['item_original_type']
. '";';
$privilegesBackup = $GLOBALS['dbi']->fetchResult(
$privilegesBackupQuery,
0
);
return $privilegesBackup;
}
/**
* Create the routine
*
* @param string $routine_query Query to create routine
* @param array $errors Errors
* @param string $create_routine Query to restore routine
* @param $privilegesBackup
* @param $drop_routine
*
* @return array
*/
function PMA_RTN_createRoutine(
$routine_query,
$errors,
$create_routine,
$privilegesBackup,
$drop_routine
) {
$result = $GLOBALS['dbi']->tryQuery($routine_query);
if (!$result) {
$errors[] = sprintf(
__('The following query has failed: "%s"'),
htmlspecialchars($routine_query)
)
. '<br />'
. __('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);
$errors = checkResult(
$result,
__(
'Sorry, we failed to restore'
. ' the dropped routine.'
),
$create_routine,
$errors
);
return array($errors, $message, $sql_query);
}
// Default value
$resultAdjust = false;
if (!defined('PMA_DRIZZLE') || !PMA_DRIZZLE) {
if (isset($GLOBALS['proc_priv']) && $GLOBALS['proc_priv']
&& isset($GLOBALS['flush_priv'])
&& $GLOBALS['flush_priv']
) {
// Insert all the previous privileges
// but with the new name and the new type
foreach ($privilegesBackup as $priv) {
$adjustProcPrivilege = 'INSERT INTO '
. PMA_Util::backquote('mysql') . '.'
. PMA_Util::backquote('procs_priv')
. ' VALUES("' . $priv[0] . '", "'
. $priv[1] . '", "' . $priv[2] . '", "'
. $_REQUEST['item_name'] . '", "'
. $_REQUEST['item_type'] . '", "'
. $priv[5] . '", "'
. $priv[6] . '", "'
. $priv[7] . '");';
$resultAdjust = $GLOBALS['dbi']->query(
$adjustProcPrivilege
);
}
}
}
$message = PMA_RTN_flushPrivileges($resultAdjust);
$sql_query = $drop_routine . $routine_query;
return array($errors, $message, $sql_query);
}
/**
* @param $resultAdjust
*
* @return PMA_Message
*/
function PMA_RTN_flushPrivileges($resultAdjust)
{
if ($resultAdjust) {
// Flush the Privileges
$flushPrivQuery = 'FLUSH PRIVILEGES;';
$GLOBALS['dbi']->query($flushPrivQuery);
$message = PMA_Message::success(
__(
'Routine %1$s has been modified. Privileges have been adjusted.'
)
);
} else {
$message = PMA_Message::success(
__('Routine %1$s has been modified.')
);
}
$message->addParam(
PMA_Util::backquote($_REQUEST['item_name'])
);
return $message;
} // end PMA_RTN_handleEditor()
/**