Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1040 lines
35 KiB

7 months ago
10 hours ago
3 years ago
3 years ago
3 years ago
2 years ago
9 months ago
8 months ago
8 months ago
3 months ago
2 months ago
10 hours ago
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2010-2021, the Friendica project
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. * Automatic post-databse structure change updates
  21. *
  22. * These functions are responsible for doing critical post update changes to the data (not the structure) in the database.
  23. *
  24. * Database structure changes are done in static/dbstructure.config.php
  25. *
  26. * For non-critical database migrations, please add a method in the Database\PostUpdate class
  27. *
  28. * If there is a need for a post update to a structure change, update this file
  29. * by adding a new function at the end with the number of the new DB_UPDATE_VERSION.
  30. *
  31. * The numbered script in this file has to be exactly like the DB_UPDATE_VERSION
  32. *
  33. * Example:
  34. * You are currently on version 4711 and you are preparing changes that demand an update script.
  35. *
  36. * 1. Create a function "update_4712()" here in the update.php
  37. * 2. Apply the needed structural changes in static/dbStructure.php
  38. * 3. Set DB_UPDATE_VERSION in static/dbstructure.config.php to 4712.
  39. *
  40. * If you need to run a script before the database update, name the function "pre_update_4712()"
  41. */
  42. use Friendica\Core\Logger;
  43. use Friendica\Core\Update;
  44. use Friendica\Core\Worker;
  45. use Friendica\Database\Database;
  46. use Friendica\Database\DBA;
  47. use Friendica\Database\DBStructure;
  48. use Friendica\DI;
  49. use Friendica\Model\Contact;
  50. use Friendica\Model\Item;
  51. use Friendica\Model\ItemURI;
  52. use Friendica\Model\Notification;
  53. use Friendica\Model\Photo;
  54. use Friendica\Model\Post;
  55. use Friendica\Model\Profile;
  56. use Friendica\Model\Storage;
  57. use Friendica\Security\PermissionSet\Depository\PermissionSet;
  58. use Friendica\Worker\Delivery;
  59. // Post-update script of PR 5751
  60. function update_1298()
  61. {
  62. $keys = ['gender', 'marital', 'sexual'];
  63. foreach ($keys as $translateKey) {
  64. $allData = DBA::select('profile', ['id', $translateKey]);
  65. $allLangs = DI::l10n()->getAvailableLanguages();
  66. $success = 0;
  67. $fail = 0;
  68. foreach ($allData as $key => $data) {
  69. $toTranslate = $data[$translateKey];
  70. if ($toTranslate != '') {
  71. foreach ($allLangs as $key => $lang) {
  72. $a = new \stdClass();
  73. $a->strings = [];
  74. // First we get the the localizations
  75. if (file_exists("view/lang/$lang/strings.php")) {
  76. include "view/lang/$lang/strings.php";
  77. }
  78. if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
  79. include "addon/morechoice/lang/$lang/strings.php";
  80. }
  81. $localizedStrings = $a->strings;
  82. unset($a);
  83. $key = array_search($toTranslate, $localizedStrings);
  84. if ($key !== false) {
  85. break;
  86. }
  87. // defaulting to empty string
  88. $key = '';
  89. }
  90. if ($key == '') {
  91. $fail++;
  92. } else {
  93. DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
  94. Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
  95. 'was' => $data[$translateKey]]);
  96. Contact::updateSelfFromUserID($data['id']);
  97. Profile::publishUpdate($data['id']);
  98. $success++;
  99. }
  100. }
  101. }
  102. Logger::notice($translateKey . " fix completed", ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
  103. }
  104. return Update::SUCCESS;
  105. }
  106. function update_1309()
  107. {
  108. $queue = DBA::select('queue', ['id', 'cid', 'guid']);
  109. while ($entry = DBA::fetch($queue)) {
  110. $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
  111. if (!DBA::isResult($contact)) {
  112. continue;
  113. }
  114. $item = Post::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
  115. if (!DBA::isResult($item)) {
  116. continue;
  117. }
  118. $deliver_options = ['priority' => PRIORITY_MEDIUM, 'dont_fork' => true];
  119. Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
  120. Logger::info('Added delivery worker', ['item' => $item['id'], 'contact' => $entry['cid']]);
  121. DBA::delete('queue', ['id' => $entry['id']]);
  122. }
  123. return Update::SUCCESS;
  124. }
  125. function update_1315()
  126. {
  127. if (DBStructure::existsTable('item-delivery-data')) {
  128. DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
  129. }
  130. return Update::SUCCESS;
  131. }
  132. function update_1318()
  133. {
  134. DBA::update('profile', ['marital' => "In a relation"], ['marital' => "Unavailable"]);
  135. DBA::update('profile', ['marital' => "Single"], ['marital' => "Available"]);
  136. Worker::add(PRIORITY_LOW, 'ProfileUpdate');
  137. return Update::SUCCESS;
  138. }
  139. function update_1323()
  140. {
  141. $users = DBA::select('user', ['uid']);
  142. while ($user = DBA::fetch($users)) {
  143. if (Contact::updateSelfFromUserID($user['uid'])) {
  144. Profile::publishUpdate($user['uid']);
  145. }
  146. }
  147. DBA::close($users);
  148. return Update::SUCCESS;
  149. }
  150. function update_1327()
  151. {
  152. $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
  153. while ($contact = DBA::fetch($contacts)) {
  154. Contact\User::setBlocked($contact['id'], $contact['uid'], $contact['blocked']);
  155. Contact\User::setIgnored($contact['id'], $contact['uid'], $contact['readonly']);
  156. }
  157. DBA::close($contacts);
  158. return Update::SUCCESS;
  159. }
  160. function update_1330()
  161. {
  162. $currStorage = DI::config()->get('storage', 'class', '');
  163. // set the name of the storage instead of the classpath as config
  164. if (!empty($currStorage)) {
  165. /** @var Storage\IStorage $currStorage */
  166. if (!DI::config()->set('storage', 'name', $currStorage::getName())) {
  167. return Update::FAILED;
  168. }
  169. // try to delete the class since it isn't needed. This won't work with config files
  170. DI::config()->delete('storage', 'class');
  171. }
  172. // Update attachments and photos
  173. if (!DBA::e("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
  174. !DBA::e("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
  175. return Update::FAILED;
  176. };
  177. return Update::SUCCESS;
  178. }
  179. function update_1332()
  180. {
  181. $condition = ["`is-default` IS NOT NULL"];
  182. $profiles = DBA::select('profile', [], $condition);
  183. while ($profile = DBA::fetch($profiles)) {
  184. Profile::migrate($profile);
  185. }
  186. DBA::close($profiles);
  187. DBA::update('contact', ['profile-id' => null], ['`profile-id` IS NOT NULL']);
  188. return Update::SUCCESS;
  189. }
  190. function update_1347()
  191. {
  192. foreach (Item::ACTIVITIES as $index => $activity) {
  193. DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], Database::INSERT_IGNORE);
  194. }
  195. return Update::SUCCESS;
  196. }
  197. function pre_update_1348()
  198. {
  199. if (!DBA::exists('contact', ['id' => 0])) {
  200. DBA::insert('contact', ['nurl' => '']);
  201. $lastid = DBA::lastInsertId();
  202. if ($lastid != 0) {
  203. DBA::update('contact', ['id' => 0], ['id' => $lastid]);
  204. }
  205. }
  206. // The tables "permissionset" and "tag" could or could not exist during the update.
  207. // This depends upon the previous version. Depending upon this situation we have to add
  208. // the "0" values before adding the foreign keys - or after would be sufficient.
  209. update_1348();
  210. if (DBStructure::existsTable('auth_codes') && DBStructure::existsTable('clients')) {
  211. DBA::e("DELETE FROM `auth_codes` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
  212. }
  213. if (DBStructure::existsTable('tokens') && DBStructure::existsTable('clients')) {
  214. DBA::e("DELETE FROM `tokens` WHERE NOT `client_id` IN (SELECT `client_id` FROM `clients`)");
  215. }
  216. return Update::SUCCESS;
  217. }
  218. function update_1348()
  219. {
  220. // Insert a permissionset with id=0
  221. // Inserting it without an ID and then changing the value to 0 tricks the auto increment
  222. if (!DBA::exists('permissionset', ['id' => 0])) {
  223. DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']);
  224. $lastid = DBA::lastInsertId();
  225. if ($lastid != 0) {
  226. DBA::update('permissionset', ['id' => 0], ['id' => $lastid]);
  227. }
  228. }
  229. if (!DBA::exists('tag', ['id' => 0])) {
  230. DBA::insert('tag', ['name' => '']);
  231. $lastid = DBA::lastInsertId();
  232. if ($lastid != 0) {
  233. DBA::update('tag', ['id' => 0], ['id' => $lastid]);
  234. }
  235. }
  236. return Update::SUCCESS;
  237. }
  238. function update_1349()
  239. {
  240. if (!DBStructure::existsTable('item-activity')) {
  241. return Update::SUCCESS;
  242. }
  243. $correct = true;
  244. foreach (Item::ACTIVITIES as $index => $activity) {
  245. if (!DBA::exists('verb', ['id' => $index + 1, 'name' => $activity])) {
  246. $correct = false;
  247. }
  248. }
  249. if (!$correct) {
  250. // The update failed - but it cannot be recovered, since the data doesn't match our expectation
  251. // This means that we can't use this "shortcut" to fill the "vid" field and we have to rely upon
  252. // the postupdate. This is not fatal, but means that it will take some longer time for the system
  253. // to fill all data.
  254. return Update::SUCCESS;
  255. }
  256. if (!DBA::e("UPDATE `item` INNER JOIN `item-activity` ON `item`.`uri-id` = `item-activity`.`uri-id`
  257. SET `vid` = `item-activity`.`activity` + 1 WHERE `gravity` = ? AND (`vid` IS NULL OR `vid` = 0)", GRAVITY_ACTIVITY)) {
  258. return Update::FAILED;
  259. }
  260. return Update::SUCCESS;
  261. }
  262. function update_1351()
  263. {
  264. if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id` SET `thread`.`uri-id` = `item`.`uri-id`")) {
  265. return Update::FAILED;
  266. }
  267. return Update::SUCCESS;
  268. }
  269. function pre_update_1354()
  270. {
  271. if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
  272. && !DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])
  273. && !DBA::e("ALTER TABLE `contact` CHANGE `ffi_keyword_blacklist` `ffi_keyword_denylist` text null")) {
  274. return Update::FAILED;
  275. }
  276. return Update::SUCCESS;
  277. }
  278. function update_1354()
  279. {
  280. if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
  281. && DBStructure::existsColumn('contact', ['ffi_keyword_denylist'])) {
  282. if (!DBA::e("UPDATE `contact` SET `ffi_keyword_denylist` = `ffi_keyword_blacklist`")) {
  283. return Update::FAILED;
  284. }
  285. // When the data had been copied then the main task is done.
  286. // Having the old field removed is only beauty but not crucial.
  287. // So we don't care if this was successful or not.
  288. DBA::e("ALTER TABLE `contact` DROP `ffi_keyword_blacklist`");
  289. }
  290. return Update::SUCCESS;
  291. }
  292. function update_1357()
  293. {
  294. if (!DBA::e("UPDATE `contact` SET `failed` = true WHERE `success_update` < `failure_update` AND `failed` IS NULL")) {
  295. return Update::FAILED;
  296. }
  297. if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `success_update` > `failure_update` AND `failed` IS NULL")) {
  298. return Update::FAILED;
  299. }
  300. if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `updated` > `failure_update` AND `failed` IS NULL")) {
  301. return Update::FAILED;
  302. }
  303. if (!DBA::e("UPDATE `contact` SET `failed` = false WHERE `last-item` > `failure_update` AND `failed` IS NULL")) {
  304. return Update::FAILED;
  305. }
  306. if (!DBA::e("UPDATE `gserver` SET `failed` = true WHERE `last_contact` < `last_failure` AND `failed` IS NULL")) {
  307. return Update::FAILED;
  308. }
  309. if (!DBA::e("UPDATE `gserver` SET `failed` = false WHERE `last_contact` > `last_failure` AND `failed` IS NULL")) {
  310. return Update::FAILED;
  311. }
  312. return Update::SUCCESS;
  313. }
  314. function pre_update_1358()
  315. {
  316. if (!DBA::e("DELETE FROM `contact-relation` WHERE NOT `relation-cid` IN (SELECT `id` FROM `contact`) OR NOT `cid` IN (SELECT `id` FROM `contact`)")) {
  317. return Update::FAILED;
  318. }
  319. return Update::SUCCESS;
  320. }
  321. function pre_update_1363()
  322. {
  323. Photo::delete(["`contact-id` != ? AND NOT `contact-id` IN (SELECT `id` FROM `contact`)", 0]);
  324. return Update::SUCCESS;
  325. }
  326. function pre_update_1364()
  327. {
  328. if (!DBA::e("DELETE FROM `2fa_recovery_codes` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  329. return Update::FAILED;
  330. }
  331. if (!DBA::e("DELETE FROM `2fa_app_specific_password` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  332. return Update::FAILED;
  333. }
  334. if (!DBA::e("DELETE FROM `attach` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  335. return Update::FAILED;
  336. }
  337. if (DBStructure::existsTable('clients') && !DBA::e("DELETE FROM `clients` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  338. return Update::FAILED;
  339. }
  340. if (!DBA::e("DELETE FROM `conv` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  341. return Update::FAILED;
  342. }
  343. if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  344. return Update::FAILED;
  345. }
  346. if (!DBA::e("DELETE FROM `group` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  347. return Update::FAILED;
  348. }
  349. if (!DBA::e("DELETE FROM `intro` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  350. return Update::FAILED;
  351. }
  352. if (!DBA::e("DELETE FROM `manage` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  353. return Update::FAILED;
  354. }
  355. if (!DBA::e("DELETE FROM `manage` WHERE NOT `mid` IN (SELECT `uid` FROM `user`)")) {
  356. return Update::FAILED;
  357. }
  358. if (!DBA::e("DELETE FROM `mail` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  359. return Update::FAILED;
  360. }
  361. if (!DBA::e("DELETE FROM `mailacct` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  362. return Update::FAILED;
  363. }
  364. if (!DBA::e("DELETE FROM `notify` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  365. return Update::FAILED;
  366. }
  367. if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  368. return Update::FAILED;
  369. }
  370. if (!DBA::e("DELETE FROM `pconfig` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  371. return Update::FAILED;
  372. }
  373. if (!DBA::e("DELETE FROM `profile` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  374. return Update::FAILED;
  375. }
  376. if (DBStructure::existsTable('profile_check') && !DBA::e("DELETE FROM `profile_check` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  377. return Update::FAILED;
  378. }
  379. if (!DBA::e("DELETE FROM `profile_field` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  380. return Update::FAILED;
  381. }
  382. if (!DBA::e("DELETE FROM `push_subscriber` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  383. return Update::FAILED;
  384. }
  385. if (!DBA::e("DELETE FROM `register` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  386. return Update::FAILED;
  387. }
  388. if (!DBA::e("DELETE FROM `search` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  389. return Update::FAILED;
  390. }
  391. if (DBStructure::existsTable('tokens') && !DBA::e("DELETE FROM `tokens` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  392. return Update::FAILED;
  393. }
  394. if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  395. return Update::FAILED;
  396. }
  397. if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  398. return Update::FAILED;
  399. }
  400. if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `receiver-uid` IN (SELECT `uid` FROM `user`)")) {
  401. return Update::FAILED;
  402. }
  403. if (!DBA::e("DELETE FROM `event` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
  404. return Update::FAILED;
  405. }
  406. if (!DBA::e("DELETE FROM `fsuggest` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
  407. return Update::FAILED;
  408. }
  409. if (!DBA::e("DELETE FROM `group_member` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
  410. return Update::FAILED;
  411. }
  412. if (!DBA::e("DELETE FROM `intro` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
  413. return Update::FAILED;
  414. }
  415. if (DBStructure::existsTable('profile_check') && !DBA::e("DELETE FROM `profile_check` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
  416. return Update::FAILED;
  417. }
  418. if (!DBA::e("DELETE FROM `user-contact` WHERE NOT `cid` IN (SELECT `id` FROM `contact`)")) {
  419. return Update::FAILED;
  420. }
  421. if (!DBA::e("DELETE FROM `group_member` WHERE NOT `gid` IN (SELECT `id` FROM `group`)")) {
  422. return Update::FAILED;
  423. }
  424. if (!DBA::e("DELETE FROM `gserver-tag` WHERE NOT `gserver-id` IN (SELECT `id` FROM `gserver`)")) {
  425. return Update::FAILED;
  426. }
  427. if (DBStructure::existsTable('user-item') && !DBA::e("DELETE FROM `user-item` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
  428. return Update::FAILED;
  429. }
  430. return Update::SUCCESS;
  431. }
  432. function pre_update_1365()
  433. {
  434. if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `notify-id` IN (SELECT `id` FROM `notify`)")) {
  435. return Update::FAILED;
  436. }
  437. if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
  438. return Update::FAILED;
  439. }
  440. return Update::SUCCESS;
  441. }
  442. function update_1375()
  443. {
  444. if (!DBA::e("UPDATE `item` SET `thr-parent` = `parent-uri`, `thr-parent-id` = `parent-uri-id` WHERE `thr-parent` = ''")) {
  445. return Update::FAILED;
  446. }
  447. return Update::SUCCESS;
  448. }
  449. function pre_update_1376()
  450. {
  451. // Insert a user with uid=0
  452. DBStructure::checkInitialValues();
  453. if (!DBA::e("DELETE FROM `item` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  454. return Update::FAILED;
  455. }
  456. if (!DBA::e("DELETE FROM `event` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  457. return Update::FAILED;
  458. }
  459. if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  460. return Update::FAILED;
  461. }
  462. if (!DBA::e("DELETE FROM `permissionset` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  463. return Update::FAILED;
  464. }
  465. if (!DBA::e("DELETE FROM `openwebauth-token` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  466. return Update::FAILED;
  467. }
  468. if (!DBA::e("DELETE FROM `post-category` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  469. return Update::FAILED;
  470. }
  471. Photo::delete(["NOT `uid` IN (SELECT `uid` FROM `user`)"]);
  472. if (!DBA::e("DELETE FROM `contact` WHERE NOT `uid` IN (SELECT `uid` FROM `user`)")) {
  473. return Update::FAILED;
  474. }
  475. return Update::SUCCESS;
  476. }
  477. function pre_update_1377()
  478. {
  479. DBStructure::checkInitialValues();
  480. if (!DBA::e("DELETE FROM `item` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
  481. return Update::FAILED;
  482. }
  483. if (!DBA::e("DELETE FROM `item` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
  484. return Update::FAILED;
  485. }
  486. if (!DBA::e("UPDATE `item` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
  487. return Update::FAILED;
  488. }
  489. if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `author-id` IN (SELECT `id` FROM `contact`)")) {
  490. return Update::FAILED;
  491. }
  492. if (DBStructure::existsTable('thread') && !DBA::e("DELETE FROM `thread` WHERE NOT `owner-id` IN (SELECT `id` FROM `contact`)")) {
  493. return Update::FAILED;
  494. }
  495. if (DBStructure::existsTable('thread') && !DBA::e("UPDATE `thread` SET `contact-id` = `owner-id` WHERE NOT `contact-id` IN (SELECT `id` FROM `contact`)")) {
  496. return Update::FAILED;
  497. }
  498. if (!DBA::e("UPDATE `notify` SET `uri-id` = NULL WHERE `uri-id` = 0")) {
  499. return Update::FAILED;
  500. }
  501. if (DBStructure::existsTable('diaspora-interaction') && !DBA::e("DELETE FROM `diaspora-interaction` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
  502. return Update::FAILED;
  503. }
  504. if (DBStructure::existsTable('item-activity') && !DBA::e("DELETE FROM `item-activity` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
  505. return Update::FAILED;
  506. }
  507. if (DBStructure::existsTable('item-content') && !DBA::e("DELETE FROM `item-content` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
  508. return Update::FAILED;
  509. }
  510. if (!DBA::e("DELETE FROM `notify` WHERE `uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
  511. return Update::FAILED;
  512. }
  513. if (!DBA::e("UPDATE `notify` SET `parent-uri-id` = NULL WHERE `parent-uri-id` = 0")) {
  514. return Update::FAILED;
  515. }
  516. if (!DBA::e("DELETE FROM `notify` WHERE `parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
  517. return Update::FAILED;
  518. }
  519. if (!DBA::e("UPDATE `notify-threads` SET `master-parent-uri-id` = NULL WHERE `master-parent-uri-id` = 0")) {
  520. return Update::FAILED;
  521. }
  522. if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-uri-id` NOT IN (SELECT `id` FROM `item-uri`)")) {
  523. return Update::FAILED;
  524. }
  525. if (!DBA::e("DELETE FROM `notify-threads` WHERE `master-parent-item` NOT IN (SELECT `id` FROM `item`)")) {
  526. return Update::FAILED;
  527. }
  528. return Update::SUCCESS;
  529. }
  530. function update_1380()
  531. {
  532. if (!DBA::e("UPDATE `notify` INNER JOIN `item` ON `item`.`id` = `notify`.`iid` SET `notify`.`uri-id` = `item`.`uri-id` WHERE `notify`.`uri-id` IS NULL AND `notify`.`otype` IN (?, ?)",
  533. Notification\ObjectType::ITEM, Notification\ObjectType::PERSON)) {
  534. return Update::FAILED;
  535. }
  536. if (!DBA::e("UPDATE `notify` INNER JOIN `item` ON `item`.`id` = `notify`.`parent` SET `notify`.`parent-uri-id` = `item`.`uri-id` WHERE `notify`.`parent-uri-id` IS NULL AND `notify`.`otype` IN (?, ?)",
  537. Notification\ObjectType::ITEM, Notification\ObjectType::PERSON)) {
  538. return Update::FAILED;
  539. }
  540. return Update::SUCCESS;
  541. }
  542. function pre_update_1395()
  543. {
  544. if (DBStructure::existsTable('post-user') && !DBStructure::existsColumn('post-user', ['id']) && !DBA::e("DROP TABLE `post-user`")) {
  545. return Update::FAILED;
  546. }
  547. return Update::SUCCESS;
  548. }
  549. function update_1395()
  550. {
  551. if (!DBA::e("INSERT INTO `post-user`(`id`, `uri-id`, `uid`, `contact-id`, `unseen`, `origin`, `psid`)
  552. SELECT `id`, `uri-id`, `uid`, `contact-id`, `unseen`, `origin`, `psid` FROM `item`
  553. ON DUPLICATE KEY UPDATE `contact-id` = `item`.`contact-id`, `unseen` = `item`.`unseen`, `origin` = `item`.`origin`, `psid` = `item`.`psid`")) {
  554. return Update::FAILED;
  555. }
  556. if (DBStructure::existsTable('user-item') && !DBA::e("INSERT INTO `post-user`(`uri-id`, `uid`, `hidden`, `notification-type`)
  557. SELECT `uri-id`, `user-item`.`uid`, `hidden`,`notification-type` FROM `user-item`
  558. INNER JOIN `item` ON `item`.`id` = `user-item`.`iid`
  559. ON DUPLICATE KEY UPDATE `hidden` = `user-item`.`hidden`, `notification-type` = `user-item`.`notification-type`")) {
  560. return Update::FAILED;
  561. }
  562. return Update::SUCCESS;
  563. }
  564. function update_1396()
  565. {
  566. if (!DBStructure::existsTable('item-content')) {
  567. return Update::SUCCESS;
  568. }
  569. if (DBStructure::existsColumn('item-content', ['raw-body'])) {
  570. if (!DBA::e("INSERT IGNORE INTO `post-content`(`uri-id`, `title`, `content-warning`, `body`, `raw-body`,
  571. `location`, `coord`, `language`, `app`, `rendered-hash`, `rendered-html`,
  572. `object-type`, `object`, `target-type`, `target`, `resource-id`, `plink`)
  573. SELECT `item-content`.`uri-id`, `item-content`.`title`, `item-content`.`content-warning`,
  574. `item-content`.`body`, `item-content`.`raw-body`, `item-content`.`location`, `item-content`.`coord`,
  575. `item-content`.`language`, `item-content`.`app`, `item-content`.`rendered-hash`,
  576. `item-content`.`rendered-html`, `item-content`.`object-type`, `item-content`.`object`,
  577. `item-content`.`target-type`, `item-content`.`target`, `item`.`resource-id`, `item-content`.`plink`
  578. FROM `item-content` INNER JOIN `item` ON `item`.`uri-id` = `item-content`.`uri-id`")) {
  579. return Update::FAILED;
  580. }
  581. } else {
  582. if (!DBA::e("INSERT IGNORE INTO `post-content`(`uri-id`, `title`, `content-warning`, `body`,
  583. `location`, `coord`, `language`, `app`, `rendered-hash`, `rendered-html`,
  584. `object-type`, `object`, `target-type`, `target`, `resource-id`, `plink`)
  585. SELECT `item-content`.`uri-id`, `item-content`.`title`, `item-content`.`content-warning`,
  586. `item-content`.`body`, `item-content`.`location`, `item-content`.`coord`,
  587. `item-content`.`language`, `item-content`.`app`, `item-content`.`rendered-hash`,
  588. `item-content`.`rendered-html`, `item-content`.`object-type`, `item-content`.`object`,
  589. `item-content`.`target-type`, `item-content`.`target`, `item`.`resource-id`, `item-content`.`plink`
  590. FROM `item-content` INNER JOIN `item` ON `item`.`uri-id` = `item-content`.`uri-id`")) {
  591. return Update::FAILED;
  592. }
  593. }
  594. return Update::SUCCESS;
  595. }
  596. function update_1397()
  597. {
  598. if (!DBA::e("INSERT INTO `post-user-notification`(`uri-id`, `uid`, `notification-type`)
  599. SELECT `uri-id`, `uid`, `notification-type` FROM `post-user` WHERE `notification-type` != 0
  600. ON DUPLICATE KEY UPDATE `uri-id` = `post-user`.`uri-id`, `uid` = `post-user`.`uid`, `notification-type` = `post-user`.`notification-type`")) {
  601. return Update::FAILED;
  602. }
  603. if (!DBStructure::existsTable('user-item')) {
  604. return Update::SUCCESS;
  605. }
  606. if (!DBA::e("INSERT INTO `post-user-notification`(`uri-id`, `uid`, `notification-type`)
  607. SELECT `uri-id`, `user-item`.`uid`, `notification-type` FROM `user-item`
  608. INNER JOIN `item` ON `item`.`id` = `user-item`.`iid` WHERE `notification-type` != 0
  609. ON DUPLICATE KEY UPDATE `notification-type` = `user-item`.`notification-type`")) {
  610. return Update::FAILED;
  611. }
  612. if (!DBStructure::existsTable('thread')) {
  613. return Update::SUCCESS;
  614. }
  615. if (!DBA::e("INSERT IGNORE INTO `post-thread-user`(`uri-id`, `uid`, `pinned`, `starred`, `ignored`, `wall`, `pubmail`, `forum_mode`)
  616. SELECT `thread`.`uri-id`, `thread`.`uid`, `user-item`.`pinned`, `thread`.`starred`,
  617. `thread`.`ignored`, `thread`.`wall`, `thread`.`pubmail`, `thread`.`forum_mode`
  618. FROM `thread` LEFT JOIN `user-item` ON `user-item`.`iid` = `thread`.`iid`")) {
  619. return Update::FAILED;
  620. }
  621. return Update::SUCCESS;
  622. }
  623. function update_1398()
  624. {
  625. if (!DBStructure::existsTable('thread')) {
  626. return Update::SUCCESS;
  627. }
  628. if (!DBA::e("INSERT IGNORE INTO `post-thread` (`uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented`)
  629. SELECT `uri-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `changed`, `commented` FROM `thread`")) {
  630. return Update::FAILED;
  631. }
  632. if (!DBStructure::existsTable('thread')) {
  633. return Update::SUCCESS;
  634. }
  635. if (!DBA::e("UPDATE `post-thread-user` INNER JOIN `thread` ON `thread`.`uid` = `post-thread-user`.`uid` AND `thread`.`uri-id` = `post-thread-user`.`uri-id`
  636. SET `post-thread-user`.`mention` = `thread`.`mention`")) {
  637. return Update::FAILED;
  638. }
  639. return Update::SUCCESS;
  640. }
  641. function update_1399()
  642. {
  643. if (!DBA::e("UPDATE `post-thread-user` INNER JOIN `post-user` ON `post-user`.`uid` = `post-thread-user`.`uid` AND `post-user`.`uri-id` = `post-thread-user`.`uri-id`
  644. SET `post-thread-user`.`contact-id` = `post-user`.`contact-id`, `post-thread-user`.`unseen` = `post-user`.`unseen`,
  645. `post-thread-user`.`hidden` = `post-user`.`hidden`, `post-thread-user`.`origin` = `post-user`.`origin`,
  646. `post-thread-user`.`psid` = `post-user`.`psid`, `post-thread-user`.`post-user-id` = `post-user`.`id`")) {
  647. return Update::FAILED;
  648. }
  649. return Update::SUCCESS;
  650. }
  651. function update_1400()
  652. {
  653. if (!DBA::e("INSERT IGNORE INTO `post` (`uri-id`, `parent-uri-id`, `thr-parent-id`, `owner-id`, `author-id`, `network`,
  654. `created`, `received`, `edited`, `gravity`, `causer-id`, `post-type`, `vid`, `private`, `visible`, `deleted`, `global`)
  655. SELECT `uri-id`, `parent-uri-id`, `thr-parent-id`, `owner-id`, `author-id`, `network`, `created`, `received`, `edited`,
  656. `gravity`, `causer-id`, `post-type`, `vid`, `private`, `visible`, `deleted`, `global` FROM `item`")) {
  657. return Update::FAILED;
  658. }
  659. if (!DBA::e("UPDATE `post-user` INNER JOIN `item` ON `item`.`uri-id` = `post-user`.`uri-id` AND `item`.`uid` = `post-user`.`uid`
  660. INNER JOIN `event` ON `item`.`event-id` = `event`.`id` AND `event`.`id` != 0
  661. SET `post-user`.`event-id` = `item`.`event-id`")) {
  662. return Update::FAILED;
  663. }
  664. if (!DBA::e("UPDATE `post-user` INNER JOIN `item` ON `item`.`uri-id` = `post-user`.`uri-id` AND `item`.`uid` = `post-user`.`uid`
  665. SET `post-user`.`wall` = `item`.`wall`, `post-user`.`parent-uri-id` = `item`.`parent-uri-id`,
  666. `post-user`.`thr-parent-id` = `item`.`thr-parent-id`,
  667. `post-user`.`created` = `item`.`created`, `post-user`.`edited` = `item`.`edited`,
  668. `post-user`.`received` = `item`.`received`, `post-user`.`gravity` = `item`.`gravity`,
  669. `post-user`.`network` = `item`.`network`, `post-user`.`owner-id` = `item`.`owner-id`,
  670. `post-user`.`author-id` = `item`.`author-id`, `post-user`.`causer-id` = `item`.`causer-id`,
  671. `post-user`.`post-type` = `item`.`post-type`, `post-user`.`vid` = `item`.`vid`,
  672. `post-user`.`private` = `item`.`private`, `post-user`.`global` = `item`.`global`,
  673. `post-user`.`visible` = `item`.`visible`, `post-user`.`deleted` = `item`.`deleted`")) {
  674. return Update::FAILED;
  675. }
  676. if (!DBA::e("INSERT IGNORE INTO `post-thread-user` (`uri-id`, `owner-id`, `author-id`, `causer-id`, `network`,
  677. `created`, `received`, `changed`, `commented`, `uid`, `wall`, `contact-id`, `unseen`, `hidden`, `origin`, `psid`, `post-user-id`)
  678. SELECT `uri-id`, `owner-id`, `author-id`, `causer-id`, `network`, `created`, `received`, `received`, `received`,
  679. `uid`, `wall`, `contact-id`, `unseen`, `hidden`, `origin`, `psid`, `id`
  680. FROM `post-user` WHERE `gravity` = 0 AND NOT EXISTS(SELECT `uri-id` FROM `post-thread-user` WHERE `post-user-id` = `post-user`.id)")) {
  681. return Update::FAILED;
  682. }
  683. if (!DBA::e("UPDATE `post-thread-user` INNER JOIN `post-thread` ON `post-thread-user`.`uri-id` = `post-thread`.`uri-id`
  684. SET `post-thread-user`.`owner-id` = `post-thread`.`owner-id`, `post-thread-user`.`author-id` = `post-thread`.`author-id`,
  685. `post-thread-user`.`causer-id` = `post-thread`.`causer-id`, `post-thread-user`.`network` = `post-thread`.`network`,
  686. `post-thread-user`.`created` = `post-thread`.`created`, `post-thread-user`.`received` = `post-thread`.`received`,
  687. `post-thread-user`.`changed` = `post-thread`.`changed`, `post-thread-user`.`commented` = `post-thread`.`commented`")) {
  688. return Update::FAILED;
  689. }
  690. return Update::SUCCESS;
  691. }
  692. function pre_update_1403()
  693. {
  694. // Necessary before a primary key change
  695. if (DBStructure::existsTable('parsed_url') && !DBA::e("DROP TABLE `parsed_url`")) {
  696. return Update::FAILED;
  697. }
  698. return Update::SUCCESS;
  699. }
  700. function update_1404()
  701. {
  702. $tasks = DBA::select('workerqueue', ['id', 'command', 'parameter'], ['command' => ['notifier', 'delivery', 'apdelivery', 'done' => false]]);
  703. while ($task = DBA::fetch($tasks)) {
  704. $parameters = json_decode($task['parameter'], true);
  705. if (is_array($parameters) && count($parameters) && in_array($parameters[0], [Delivery::MAIL, Delivery::SUGGESTION, Delivery::REMOVAL, Delivery::RELOCATION])) {
  706. continue;
  707. }
  708. switch (strtolower($task['command'])) {
  709. case 'notifier':
  710. if (count($parameters) == 3) {
  711. continue 2;
  712. }
  713. $item = DBA::selectFirst('item', ['uid', 'uri-id'], ['id' => $parameters[1]]);
  714. if (!DBA::isResult($item)) {
  715. continue 2;
  716. }
  717. $parameters[1] = $item['uri-id'];
  718. $parameters[2] = $item['uid'];
  719. break;
  720. case 'delivery':
  721. if (count($parameters) == 4) {
  722. continue 2;
  723. }
  724. $item = DBA::selectFirst('item', ['uid', 'uri-id'], ['id' => $parameters[1]]);
  725. if (!DBA::isResult($item)) {
  726. continue 2;
  727. }
  728. $parameters[1] = $item['uri-id'];
  729. $parameters[3] = $item['uid'];
  730. break;
  731. case 'apdelivery':
  732. if (count($parameters) == 6) {
  733. continue 2;
  734. }
  735. if (empty($parameters[4])) {
  736. $parameters[4] = [];
  737. }
  738. $item = DBA::selectFirst('item', ['uri-id'], ['id' => $parameters[1]]);
  739. if (!DBA::isResult($item)) {
  740. continue 2;
  741. }
  742. $parameters[5] = $item['uri-id'];
  743. break;
  744. default:
  745. continue 2;
  746. }
  747. DBA::update('workerqueue', ['parameter' => json_encode($parameters)], ['id' => $task['id']]);
  748. return Update::SUCCESS;
  749. }
  750. }
  751. function update_1407()
  752. {
  753. if (!DBA::e("UPDATE `post` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
  754. return Update::FAILED;
  755. }
  756. if (!DBA::e("UPDATE `post-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
  757. return Update::FAILED;
  758. }
  759. if (!DBA::e("UPDATE `post-thread` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
  760. return Update::FAILED;
  761. }
  762. if (!DBA::e("UPDATE `post-thread-user` SET `causer-id` = NULL WHERE `causer-id` = 0")) {
  763. return Update::FAILED;
  764. }
  765. return Update::SUCCESS;
  766. }
  767. function update_1413()
  768. {
  769. if (!DBA::e("UPDATE `post-user` SET `post-reason` = `post-type` WHERE `post-type` >= 64 and `post-type` <= 75")) {
  770. return Update::FAILED;
  771. }
  772. }
  773. function update_1419()
  774. {
  775. $mails = DBA::select('mail', ['id', 'from-url', 'uri', 'parent-uri', 'guid'], [], ['order' => ['id']]);
  776. while ($mail = DBA::fetch($mails)) {
  777. $fields = [];
  778. $fields['author-id'] = Contact::getIdForURL($mail['from-url'], 0, false);
  779. if (empty($fields['author-id'])) {
  780. continue;
  781. }
  782. $fields['uri-id'] = ItemURI::insert(['uri' => $mail['uri'], 'guid' => $mail['guid']]);
  783. $fields['parent-uri-id'] = ItemURI::getIdByURI($mail['parent-uri']);
  784. $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'guid'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
  785. if (!empty($reply)) {
  786. $fields['thr-parent'] = $reply['uri'];
  787. if (!empty($reply['uri-id'])) {
  788. $fields['thr-parent-id'] = $reply['uri-id'];
  789. } else {
  790. $fields['thr-parent-id'] = ItemURI::insert(['uri' => $reply['uri'], 'guid' => $reply['guid']]);
  791. }
  792. }
  793. DBA::update('mail', $fields, ['id' => $mail['id']]);
  794. }
  795. return Update::SUCCESS;
  796. }
  797. function update_1429()
  798. {
  799. if (!DBA::e("UPDATE `contact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
  800. return Update::FAILED;
  801. }
  802. if (!DBA::e("UPDATE `fcontact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
  803. return Update::FAILED;
  804. }
  805. if (!DBA::e("UPDATE `apcontact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) {
  806. return Update::FAILED;
  807. }
  808. DI::config()->set("system", "post_update_version", 1423);
  809. return Update::SUCCESS;
  810. }
  811. function update_1434()
  812. {
  813. $name = DI::config()->get('storage', 'name');
  814. // in case of an empty config, set "Database" as default storage backend
  815. if (empty($name)) {
  816. DI::config()->set('storage', 'name', Storage\Database::getName());
  817. }
  818. // In case of a Using deprecated storage class value, set the right name for it
  819. if (stristr($name, 'Friendica\Model\Storage\\')) {
  820. DI::config()->set('storage', 'name', substr($name, 24));
  821. }
  822. return Update::SUCCESS;
  823. }
  824. function update_1435()
  825. {
  826. $contacts = DBA::select('contact', [], ["`uid` != ?", 0]);
  827. while ($contact = DBA::fetch($contacts)) {
  828. Contact\User::insertForContactArray($contact);
  829. }
  830. }
  831. function update_1438()
  832. {
  833. DBA::update('photo', ['photo-type' => Photo::USER_AVATAR], ['profile' => true]);
  834. DBA::update('photo', ['photo-type' => Photo::CONTACT_AVATAR], ["NOT `profile` AND NOT `contact-id` IS NULL AND `contact-id` != ?", 0]);
  835. DBA::update('photo', ['photo-type' => Photo::DEFAULT], ["NOT `profile` AND (`contact-id` IS NULL OR `contact-id` = ?) AND `photo-type` IS NULL AND `album` != ?", 0, Photo::CONTACT_PHOTOS]);
  836. }
  837. function update_1439()
  838. {
  839. $intros = DBA::select('intro', ['id', 'fid'], ["NOT `fid` IS NULL AND `fid` != ?", 0]);
  840. while ($intro = DBA::fetch($intros)) {
  841. $fcontact = DBA::selectFirst('fcontact', ['url'], ['id' => $intro['fid']]);
  842. if (!empty($fcontact['url'])) {
  843. $id = Contact::getIdForURL($fcontact['url']);
  844. if (!empty($id)) {
  845. DBA::update('intro',['suggest-cid' => $id], ['id' => $intro['id']]);
  846. }
  847. }
  848. }
  849. DBA::close($intros);
  850. }
  851. function update_1440()
  852. {
  853. // Fix wrong public permissionset
  854. DBA::p("UPDATE `profile_field` SET `psid` = ? WHERE psid IN (SELECT `id` FROM `permissionset` WHERE `id` != ? AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '')", PermissionSet::PUBLIC, PermissionSet::PUBLIC);
  855. DBA::delete('permissionset', ["`id` != ? AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = ''", PermissionSet::PUBLIC]);
  856. return Update::SUCCESS;
  857. }