[ticket/11162] Newlines to LF.

PHPBB3-11162
This commit is contained in:
Oleg Pudeyev 2012-12-05 10:40:42 -05:00
parent 1a411c5658
commit 94ebc57078

View file

@ -1,208 +1,208 @@
<?php <?php
/** /**
* *
* @package phpBB3 * @package phpBB3
* @copyright (c) 2012 phpBB Group * @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
* *
*/ */
/** /**
* @ignore * @ignore
*/ */
if (!defined('IN_PHPBB')) if (!defined('IN_PHPBB'))
{ {
exit; exit;
} }
/** /**
* Updates rows in given table from a set of values to a new value. * Updates rows in given table from a set of values to a new value.
* If this results in rows violating uniqueness constraints, the duplicate * If this results in rows violating uniqueness constraints, the duplicate
* rows are eliminated. * rows are eliminated.
* *
* The only supported table is bookmarks. * The only supported table is bookmarks.
* *
* @param dbal $db Database object * @param dbal $db Database object
* @param string $table Table on which to perform the update * @param string $table Table on which to perform the update
* @param string $column Column whose values to change * @param string $column Column whose values to change
* @param array $from_values An array of values that should be changed * @param array $from_values An array of values that should be changed
* @param int $to_value The new value * @param int $to_value The new value
* @return null * @return null
*/ */
function phpbb_update_rows_avoiding_duplicates($db, $table, $column, $from_values, $to_value) function phpbb_update_rows_avoiding_duplicates($db, $table, $column, $from_values, $to_value)
{ {
$sql = "SELECT $column, user_id $sql = "SELECT $column, user_id
FROM $table FROM $table
WHERE " . $db->sql_in_set($column, $from_values); WHERE " . $db->sql_in_set($column, $from_values);
$result = $db->sql_query($sql); $result = $db->sql_query($sql);
$old_user_ids = array(); $old_user_ids = array();
while ($row = $db->sql_fetchrow($result)) while ($row = $db->sql_fetchrow($result))
{ {
$old_user_ids[$row[$column]][] = $row['user_id']; $old_user_ids[$row[$column]][] = $row['user_id'];
} }
$db->sql_freeresult($result); $db->sql_freeresult($result);
$sql = "SELECT $column, user_id $sql = "SELECT $column, user_id
FROM $table FROM $table
WHERE $column = '" . (int) $to_value . "'"; WHERE $column = '" . (int) $to_value . "'";
$result = $db->sql_query($sql); $result = $db->sql_query($sql);
$new_user_ids = array(); $new_user_ids = array();
while ($row = $db->sql_fetchrow($result)) while ($row = $db->sql_fetchrow($result))
{ {
$new_user_ids[$row[$column]][] = $row['user_id']; $new_user_ids[$row[$column]][] = $row['user_id'];
} }
$db->sql_freeresult($result); $db->sql_freeresult($result);
$queries = array(); $queries = array();
$any_found = false; $any_found = false;
foreach ($from_values as $from_value) foreach ($from_values as $from_value)
{ {
if (!isset($old_user_ids[$from_value])) if (!isset($old_user_ids[$from_value]))
{ {
continue; continue;
} }
$any_found = true; $any_found = true;
if (empty($new_user_ids)) if (empty($new_user_ids))
{ {
$sql = "UPDATE $table $sql = "UPDATE $table
SET $column = " . (int) $to_value . " SET $column = " . (int) $to_value . "
WHERE $column = '" . $db->sql_escape($from_value) . "'"; WHERE $column = '" . $db->sql_escape($from_value) . "'";
$queries[] = $sql; $queries[] = $sql;
} }
else else
{ {
$different_user_ids = array_diff($old_user_ids[$from_value], $new_user_ids[$to_value]); $different_user_ids = array_diff($old_user_ids[$from_value], $new_user_ids[$to_value]);
if (!empty($different_user_ids)) if (!empty($different_user_ids))
{ {
$sql = "UPDATE $table $sql = "UPDATE $table
SET $column = " . (int) $to_value . " SET $column = " . (int) $to_value . "
WHERE $column = '" . $db->sql_escape($from_value) . "' WHERE $column = '" . $db->sql_escape($from_value) . "'
AND " . $db->sql_in_set('user_id', $different_user_ids); AND " . $db->sql_in_set('user_id', $different_user_ids);
$queries[] = $sql; $queries[] = $sql;
} }
} }
} }
if ($any_found) if ($any_found)
{ {
$db->sql_transaction('begin'); $db->sql_transaction('begin');
foreach ($queries as $sql) foreach ($queries as $sql)
{ {
$db->sql_query($sql); $db->sql_query($sql);
} }
$sql = "DELETE FROM $table $sql = "DELETE FROM $table
WHERE " . $db->sql_in_set($column, $from_values); WHERE " . $db->sql_in_set($column, $from_values);
$db->sql_query($sql); $db->sql_query($sql);
$db->sql_transaction('commit'); $db->sql_transaction('commit');
} }
} }
/** /**
* Updates rows in given table from a set of values to a new value. * Updates rows in given table from a set of values to a new value.
* If this results in rows violating uniqueness constraints, the duplicate * If this results in rows violating uniqueness constraints, the duplicate
* rows are merged respecting notify_status (0 takes precedence over 1). * rows are merged respecting notify_status (0 takes precedence over 1).
* *
* The only supported table is topics_watch. * The only supported table is topics_watch.
* *
* @param dbal $db Database object * @param dbal $db Database object
* @param string $table Table on which to perform the update * @param string $table Table on which to perform the update
* @param string $column Column whose values to change * @param string $column Column whose values to change
* @param array $from_values An array of values that should be changed * @param array $from_values An array of values that should be changed
* @param int $to_value The new value * @param int $to_value The new value
* @return null * @return null
*/ */
function phpbb_update_rows_avoiding_duplicates_notify_status($db, $table, $column, $from_values, $to_value) function phpbb_update_rows_avoiding_duplicates_notify_status($db, $table, $column, $from_values, $to_value)
{ {
$sql = "SELECT $column, user_id, notify_status $sql = "SELECT $column, user_id, notify_status
FROM $table FROM $table
WHERE " . $db->sql_in_set($column, $from_values); WHERE " . $db->sql_in_set($column, $from_values);
$result = $db->sql_query($sql); $result = $db->sql_query($sql);
$old_user_ids = array(); $old_user_ids = array();
while ($row = $db->sql_fetchrow($result)) while ($row = $db->sql_fetchrow($result))
{ {
$old_user_ids[(int) $row['notify_status']][$row[$column]][] = $row['user_id']; $old_user_ids[(int) $row['notify_status']][$row[$column]][] = $row['user_id'];
} }
$db->sql_freeresult($result); $db->sql_freeresult($result);
$sql = "SELECT $column, user_id $sql = "SELECT $column, user_id
FROM $table FROM $table
WHERE $column = '" . (int) $to_value . "'"; WHERE $column = '" . (int) $to_value . "'";
$result = $db->sql_query($sql); $result = $db->sql_query($sql);
$new_user_ids = array(); $new_user_ids = array();
while ($row = $db->sql_fetchrow($result)) while ($row = $db->sql_fetchrow($result))
{ {
$new_user_ids[$row[$column]][] = $row['user_id']; $new_user_ids[$row[$column]][] = $row['user_id'];
} }
$db->sql_freeresult($result); $db->sql_freeresult($result);
$queries = array(); $queries = array();
$any_found = false; $any_found = false;
$extra_updates = array( $extra_updates = array(
0 => 'notify_status = 0', 0 => 'notify_status = 0',
1 => '', 1 => '',
); );
foreach ($from_values as $from_value) foreach ($from_values as $from_value)
{ {
foreach ($extra_updates as $notify_status => $extra_update) { foreach ($extra_updates as $notify_status => $extra_update) {
if (!isset($old_user_ids[$notify_status][$from_value])) if (!isset($old_user_ids[$notify_status][$from_value]))
{ {
continue; continue;
} }
$any_found = true; $any_found = true;
if (empty($new_user_ids)) if (empty($new_user_ids))
{ {
$sql = "UPDATE $table $sql = "UPDATE $table
SET $column = " . (int) $to_value . " SET $column = " . (int) $to_value . "
WHERE $column = '" . $db->sql_escape($from_value) . "'"; WHERE $column = '" . $db->sql_escape($from_value) . "'";
$queries[] = $sql; $queries[] = $sql;
} }
else else
{ {
$different_user_ids = array_diff($old_user_ids[$notify_status][$from_value], $new_user_ids[$to_value]); $different_user_ids = array_diff($old_user_ids[$notify_status][$from_value], $new_user_ids[$to_value]);
if (!empty($different_user_ids)) if (!empty($different_user_ids))
{ {
$sql = "UPDATE $table $sql = "UPDATE $table
SET $column = " . (int) $to_value . " SET $column = " . (int) $to_value . "
WHERE $column = '" . $db->sql_escape($from_value) . "' WHERE $column = '" . $db->sql_escape($from_value) . "'
AND " . $db->sql_in_set('user_id', $different_user_ids); AND " . $db->sql_in_set('user_id', $different_user_ids);
$queries[] = $sql; $queries[] = $sql;
} }
if ($extra_update) { if ($extra_update) {
$same_user_ids = array_diff($old_user_ids[$notify_status][$from_value], $different_user_ids); $same_user_ids = array_diff($old_user_ids[$notify_status][$from_value], $different_user_ids);
if (!empty($same_user_ids)) if (!empty($same_user_ids))
{ {
$sql = "UPDATE $table $sql = "UPDATE $table
SET $extra_update SET $extra_update
WHERE $column = '" . (int) $to_value . "' WHERE $column = '" . (int) $to_value . "'
AND " . $db->sql_in_set('user_id', $same_user_ids); AND " . $db->sql_in_set('user_id', $same_user_ids);
$queries[] = $sql; $queries[] = $sql;
} }
} }
} }
} }
} }
if ($any_found) if ($any_found)
{ {
$db->sql_transaction('begin'); $db->sql_transaction('begin');
foreach ($queries as $sql) foreach ($queries as $sql)
{ {
$db->sql_query($sql); $db->sql_query($sql);
} }
$sql = "DELETE FROM $table $sql = "DELETE FROM $table
WHERE " . $db->sql_in_set($column, $from_values); WHERE " . $db->sql_in_set($column, $from_values);
$db->sql_query($sql); $db->sql_query($sql);
$db->sql_transaction('commit'); $db->sql_transaction('commit');
} }
} }