Post update script for setting the uri-id (#5573)
authorMichael Vogel <icarus@dabo.de>
Mon, 6 Aug 2018 16:40:41 +0000 (18:40 +0200)
committerTobias Diekershoff <tobias.diekershoff@gmx.net>
Mon, 6 Aug 2018 16:40:41 +0000 (18:40 +0200)
* Post update is working now

* Changed text.

mod/friendica.php
src/Database/PostUpdate.php
src/Model/Item.php

index 05a76b4..f2cb5c5 100644 (file)
@@ -75,8 +75,9 @@ function friendica_content(App $a)
 {
        $o = '<h1>Friendica</h1>' . PHP_EOL;
        $o .= '<p>';
-       $o .= L10n::t('This is Friendica, version') . ' <strong>' . FRIENDICA_VERSION . '</strong> ';
-       $o .= L10n::t('running at web location') . ' ' . System::baseUrl();
+       $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.',
+               '<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>';
index 5cc892d..8805961 100644 (file)
@@ -7,6 +7,7 @@ namespace Friendica\Database;
 use Friendica\Core\Config;
 use Friendica\Model\Contact;
 use Friendica\Model\Item;
+use Friendica\Model\ItemURI;
 use Friendica\Model\PermissionSet;
 use Friendica\Database\DBA;
 
@@ -34,6 +35,9 @@ class PostUpdate
                if (!self::update1279()) {
                        return;
                }
+               if (!self::update1281()) {
+                       return;
+               }
        }
 
        /**
@@ -363,4 +367,81 @@ class PostUpdate
 
                $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;
+       }
 }
index d082d02..6d0b190 100644 (file)
@@ -1888,7 +1888,7 @@ class Item extends BaseObject
                }
 
                $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
                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)
        {
-               $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) {
                        if (isset($item[$field])) {