+
+ /**
+ * update the "vid" (verb) field in the item table
+ *
+ * @return bool "true" when the job is done
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ * @throws \ImagickException
+ */
+ private static function update1347()
+ {
+ // Was the script completed?
+ if (DI::config()->get("system", "post_update_version") >= 1347) {
+ return true;
+ }
+
+ if (!DBStructure::existsTable('item-activity') || !DBStructure::existsTable('item')) {
+ DI::config()->set('system', 'post_update_version', 1347);
+ return true;
+ }
+
+ $id = DI::config()->get("system", "post_update_version_1347_id", 0);
+
+ Logger::info('Start', ['item' => $id]);
+
+ $start_id = $id;
+ $rows = 0;
+
+ $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
+ FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
+ LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
+ WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", GRAVITY_ACTIVITY, $id);
+
+ if (DBA::errorNo() != 0) {
+ Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
+ return false;
+ }
+
+ while ($item = DBA::fetch($items)) {
+ $id = $item['id'];
+ $verb = $item['item-verb'];
+ if (empty($verb)) {
+ $verb = $item['verb'];
+ }
+ if (empty($verb) && is_int($item['activity'])) {
+ $verb = Item::ACTIVITIES[$item['activity']];
+ }
+ if (empty($verb)) {
+ continue;
+ }
+
+ DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
+ ++$rows;
+ }
+ DBA::close($items);
+
+ DI::config()->set("system", "post_update_version_1347_id", $id);
+
+ Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
+
+ if ($start_id == $id) {
+ DI::config()->set("system", "post_update_version", 1347);
+ Logger::info('Done');
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * update the "gsid" (global server id) field in the contact table
+ *
+ * @return bool "true" when the job is done
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ * @throws \ImagickException
+ */
+ private static function update1348()
+ {
+ // Was the script completed?
+ if (DI::config()->get("system", "post_update_version") >= 1348) {
+ return true;
+ }
+
+ $id = DI::config()->get("system", "post_update_version_1348_id", 0);
+
+ Logger::info('Start', ['contact' => $id]);
+
+ $start_id = $id;
+ $rows = 0;
+ $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
+ $params = ['order' => ['id'], 'limit' => 10000];
+ $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
+
+ if (DBA::errorNo() != 0) {
+ Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
+ return false;
+ }
+
+ while ($contact = DBA::fetch($contacts)) {
+ $id = $contact['id'];
+
+ DBA::update('contact',
+ ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
+ ['id' => $contact['id']]);
+
+ ++$rows;
+ }
+ DBA::close($contacts);
+
+ DI::config()->set("system", "post_update_version_1348_id", $id);
+
+ Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
+
+ if ($start_id == $id) {
+ DI::config()->set("system", "post_update_version", 1348);
+ Logger::info('Done');
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * update the "gsid" (global server id) field in the apcontact table
+ *
+ * @return bool "true" when the job is done
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ * @throws \ImagickException
+ */
+ private static function update1349()
+ {
+ // Was the script completed?
+ if (DI::config()->get("system", "post_update_version") >= 1349) {
+ return true;
+ }
+
+ $id = DI::config()->get("system", "post_update_version_1349_id", '');
+
+ Logger::info('Start', ['apcontact' => $id]);
+
+ $start_id = $id;
+ $rows = 0;
+ $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
+ $params = ['order' => ['url'], 'limit' => 10000];
+ $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
+
+ if (DBA::errorNo() != 0) {
+ Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
+ return false;
+ }
+
+ while ($apcontact = DBA::fetch($apcontacts)) {
+ $id = $apcontact['url'];
+
+ DBA::update('apcontact',
+ ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
+ ['url' => $apcontact['url']]);
+
+ ++$rows;
+ }
+ DBA::close($apcontacts);
+
+ DI::config()->set("system", "post_update_version_1349_id", $id);
+
+ Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
+
+ if ($start_id == $id) {
+ DI::config()->set("system", "post_update_version", 1349);
+ Logger::info('Done');
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Remove orphaned photo entries
+ *
+ * @return bool "true" when the job is done
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ * @throws \ImagickException
+ */
+ private static function update1383()
+ {
+ // Was the script completed?
+ if (DI::config()->get("system", "post_update_version") >= 1383) {
+ return true;
+ }
+
+ Logger::info('Start');
+
+ $deleted = 0;
+ $avatar = [4 => 'photo', 5 => 'thumb', 6 => 'micro'];
+
+ $photos = DBA::select('photo', ['id', 'contact-id', 'resource-id', 'scale'], ["`contact-id` != ? AND `album` = ?", 0, Photo::CONTACT_PHOTOS]);
+ while ($photo = DBA::fetch($photos)) {
+ $delete = !in_array($photo['scale'], [4, 5, 6]);
+
+ if (!$delete) {
+ // Check if there is a contact entry with that photo
+ $delete = !DBA::exists('contact', ["`id` = ? AND `" . $avatar[$photo['scale']] . "` LIKE ?",
+ $photo['contact-id'], '%' . $photo['resource-id'] . '%']);
+ }
+
+ if ($delete) {
+ Photo::delete(['id' => $photo['id']]);
+ $deleted++;
+ }
+ }
+ DBA::close($photos);
+
+ DI::config()->set("system", "post_update_version", 1383);
+ Logger::info('Done', ['deleted' => $deleted]);
+ return true;
+ }
+
+ /**
+ * update the "hash" field in the photo table
+ *
+ * @return bool "true" when the job is done
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ * @throws \ImagickException
+ */
+ private static function update1384()
+ {
+ // Was the script completed?
+ if (DI::config()->get("system", "post_update_version") >= 1384) {
+ return true;
+ }
+
+ $condition = ["`hash` IS NULL"];
+ Logger::info('Start', ['rest' => DBA::count('photo', $condition)]);
+
+ $rows = 0;
+ $photos = DBA::select('photo', [], $condition, ['limit' => 10000]);
+
+ if (DBA::errorNo() != 0) {
+ Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
+ return false;
+ }
+
+ while ($photo = DBA::fetch($photos)) {
+ $img = Photo::getImageForPhoto($photo);
+ if (!empty($img)) {
+ $md5 = md5($img->asString());
+ } else {
+ $md5 = '';
+ }
+ DBA::update('photo', ['hash' => $md5], ['id' => $photo['id']]);
+ ++$rows;
+ }
+ DBA::close($photos);
+
+ Logger::info('Processed', ['rows' => $rows]);
+
+ if ($rows <= 100) {
+ DI::config()->set("system", "post_update_version", 1384);
+ Logger::info('Done');
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * update the "hash" field in the photo table
+ *
+ * @return bool "true" when the job is done
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ * @throws \ImagickException
+ */
+ private static function update1400()
+ {
+ // Was the script completed?
+ if (DI::config()->get("system", "post_update_version") >= 1400) {
+ return true;
+ }
+
+ if (!DBStructure::existsTable('item')) {
+ DI::config()->set("system", "post_update_version", 1400);
+ return true;
+ }
+
+ $condition = ["`extid` != ? AND EXISTS(SELECT `id` FROM `post-user` WHERE `uri-id` = `item`.`uri-id` AND `uid` = `item`.`uid` AND `external-id` IS NULL)", ''];
+ Logger::info('Start', ['rest' => DBA::count('item', $condition)]);
+
+ $rows = 0;
+ $items = DBA::select('item', ['uri-id', 'uid', 'extid'], $condition, ['order' => ['id'], 'limit' => 10000]);
+
+ if (DBA::errorNo() != 0) {
+ Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
+ return false;
+ }
+
+ while ($item = DBA::fetch($items)) {
+ Post::update(['external-id' => ItemURI::getIdByURI($item['extid'])], ['uri-id' => $item['uri-id'], 'uid' => $item['uid']]);
+ ++$rows;
+ }
+ DBA::close($items);
+
+ Logger::info('Processed', ['rows' => $rows]);
+
+ if ($rows <= 100) {
+ DI::config()->set("system", "post_update_version", 1400);
+ Logger::info('Done');
+ return true;
+ }
+
+ return false;
+ }