Post update script for setting the uri-id (#5573)

* Post update is working now

* Changed text.
This commit is contained in:
Michael Vogel 2018-08-06 18:40:41 +02:00 committed by Tobias Diekershoff
parent a74e2ce333
commit c032011143
3 changed files with 87 additions and 4 deletions

View File

@ -75,8 +75,9 @@ function friendica_content(App $a)
{ {
$o = '<h1>Friendica</h1>' . PHP_EOL; $o = '<h1>Friendica</h1>' . PHP_EOL;
$o .= '<p>'; $o .= '<p>';
$o .= L10n::t('This is Friendica, version') . ' <strong>' . FRIENDICA_VERSION . '</strong> '; $o .= L10n::t('This is Friendica, version %s that is running at the web location %s. The database version is %s, the post update version is %s.',
$o .= L10n::t('running at web location') . ' ' . System::baseUrl(); '<strong>' . FRIENDICA_VERSION . '</strong>', System::baseUrl(), '<strong>' . DB_UPDATE_VERSION . '</strong>',
'<strong>' . Config::get("system", "post_update_version") . '</strong>');
$o .= '</p>' . PHP_EOL; $o .= '</p>' . PHP_EOL;
$o .= '<p>'; $o .= '<p>';

View File

@ -7,6 +7,7 @@ namespace Friendica\Database;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\ItemURI;
use Friendica\Model\PermissionSet; use Friendica\Model\PermissionSet;
use Friendica\Database\DBA; use Friendica\Database\DBA;
@ -34,6 +35,9 @@ class PostUpdate
if (!self::update1279()) { if (!self::update1279()) {
return; return;
} }
if (!self::update1281()) {
return;
}
} }
/** /**
@ -363,4 +367,81 @@ class PostUpdate
$item['language'] = json_encode($lang_arr); $item['language'] = json_encode($lang_arr);
} }
/**
* @brief update item-uri data. Prerequisite for the next item structure update.
*
* @return bool "true" when the job is done
*/
private static function update1281()
{
// Was the script completed?
if (Config::get("system", "post_update_version") >= 1281) {
return true;
}
$id = Config::get("system", "post_update_version_1281_id", 0);
logger("Start from item " . $id, LOGGER_DEBUG);
$fields = ['id', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id', 'thr-parent', 'thr-parent-id'];
$start_id = $id;
$rows = 0;
$condition = ["`id` > ?", $id];
$params = ['order' => ['id'], 'limit' => 10000];
$items = DBA::select('item', $fields, $condition, $params);
while ($item = DBA::fetch($items)) {
$id = $item['id'];
if (empty($item['uri'])) {
// Should not happen
continue;
} elseif (empty($item['uri-id'])) {
$item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
}
if (empty($item['parent-uri'])) {
$item['parent-uri-id'] = $item['uri-id'];
} elseif (empty($item['parent-uri-id'])) {
$item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
}
// Very old items don't have this field
if (empty($item['thr-parent'])) {
$item['thr-parent-id'] = $item['parent-uri-id'];
} elseif (empty($item['thr-parent-id'])) {
$item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
}
unset($item['id']);
unset($item['guid']);
unset($item['uri']);
unset($item['parent-uri']);
unset($item['thr-parent']);
DBA::update('item', $item, ['id' => $id]);
++$rows;
}
DBA::close($items);
Config::set("system", "post_update_version_1281_id", $id);
logger("Processed rows: " . $rows . " - last processed item: " . $id, LOGGER_DEBUG);
if ($start_id == $id) {
logger("Updating item-uri in item-activity", LOGGER_DEBUG);
DBA::e("UPDATE `item-activity` INNER JOIN `item-uri` ON `item-uri`.`uri` = `item-activity`.`uri` SET `item-activity`.`uri-id` = `item-uri`.`id` WHERE `item-activity`.`uri-id` IS NULL");
logger("Updating item-uri in item-content", LOGGER_DEBUG);
DBA::e("UPDATE `item-content` INNER JOIN `item-uri` ON `item-uri`.`uri` = `item-content`.`uri` SET `item-content`.`uri-id` = `item-uri`.`id` WHERE `item-content`.`uri-id` IS NULL");
Config::set("system", "post_update_version", 1281);
logger("Done", LOGGER_DEBUG);
return true;
}
return false;
}
} }

View File

@ -1888,7 +1888,7 @@ class Item extends BaseObject
} }
$fields = ['uri' => $item['uri'], 'activity' => $activity_index, $fields = ['uri' => $item['uri'], 'activity' => $activity_index,
'uri-hash' => $item['uri-hash']]; 'uri-hash' => $item['uri-hash'], 'uri-id' => $item['uri-id']];
// We just remove everything that is content // We just remove everything that is content
foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) { foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) {
@ -1927,7 +1927,8 @@ class Item extends BaseObject
*/ */
private static function insertContent(&$item) private static function insertContent(&$item)
{ {
$fields = ['uri' => $item['uri'], 'uri-plink-hash' => $item['uri-hash']]; $fields = ['uri' => $item['uri'], 'uri-plink-hash' => $item['uri-hash'],
'uri-id' => $item['uri-id']];
foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) { foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) {
if (isset($item[$field])) { if (isset($item[$field])) {