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.

522 lines
15KB

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2020, Friendica
  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\Addon;
  43. use Friendica\Core\Logger;
  44. use Friendica\Core\Update;
  45. use Friendica\Core\Worker;
  46. use Friendica\Database\DBA;
  47. use Friendica\Database\DBStructure;
  48. use Friendica\DI;
  49. use Friendica\Model\Contact;
  50. use Friendica\Model\GContact;
  51. use Friendica\Model\Item;
  52. use Friendica\Model\User;
  53. use Friendica\Model\Storage;
  54. use Friendica\Util\DateTimeFormat;
  55. use Friendica\Worker\Delivery;
  56. function update_1179()
  57. {
  58. if (DI::config()->get('system', 'no_community_page')) {
  59. DI::config()->set('system', 'community_page_style', CP_NO_COMMUNITY_PAGE);
  60. }
  61. // Update the central item storage with uid=0
  62. Worker::add(PRIORITY_LOW, "threadupdate");
  63. return Update::SUCCESS;
  64. }
  65. function update_1181()
  66. {
  67. // Fill the new fields in the term table.
  68. // deactivated, the "term" table is deprecated
  69. // Worker::add(PRIORITY_LOW, "TagUpdate");
  70. return Update::SUCCESS;
  71. }
  72. function update_1189()
  73. {
  74. if (strlen(DI::config()->get('system', 'directory_submit_url')) &&
  75. !strlen(DI::config()->get('system', 'directory'))) {
  76. DI::config()->set('system', 'directory', dirname(DI::config()->get('system', 'directory_submit_url')));
  77. DI::config()->delete('system', 'directory_submit_url');
  78. }
  79. return Update::SUCCESS;
  80. }
  81. function update_1191()
  82. {
  83. DI::config()->set('system', 'maintenance', 1);
  84. if (Addon::isEnabled('forumlist')) {
  85. Addon::uninstall('forumlist');
  86. }
  87. // select old formlist addon entries
  88. $r = q("SELECT `uid`, `cat`, `k`, `v` FROM `pconfig` WHERE `cat` = '%s' ",
  89. DBA::escape('forumlist')
  90. );
  91. // convert old forumlist addon entries in new config entries
  92. if (DBA::isResult($r)) {
  93. foreach ($r as $rr) {
  94. $uid = $rr['uid'];
  95. $family = $rr['cat'];
  96. $key = $rr['k'];
  97. $value = $rr['v'];
  98. if ($key === 'randomise') {
  99. DI::pConfig()->delete($uid, $family, $key);
  100. }
  101. if ($key === 'show_on_profile') {
  102. if ($value) {
  103. DI::pConfig()->set($uid, 'feature', 'forumlist_profile', $value);
  104. }
  105. DI::pConfig()->delete($uid, $family, $key);
  106. }
  107. if ($key === 'show_on_network') {
  108. if ($value) {
  109. DI::pConfig()->set($uid, 'feature', 'forumlist_widget', $value);
  110. }
  111. DI::pConfig()->delete($uid, $family, $key);
  112. }
  113. }
  114. }
  115. DI::config()->set('system', 'maintenance', 0);
  116. return Update::SUCCESS;
  117. }
  118. function update_1203()
  119. {
  120. $r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
  121. DBA::escape(User::ACCOUNT_TYPE_COMMUNITY),
  122. DBA::escape(User::PAGE_FLAGS_COMMUNITY),
  123. DBA::escape(User::PAGE_FLAGS_PRVGROUP)
  124. );
  125. }
  126. function update_1244()
  127. {
  128. // Sets legacy_password for all legacy hashes
  129. DBA::update('user', ['legacy_password' => true], ['SUBSTR(password, 1, 4) != "$2y$"']);
  130. // All legacy hashes are re-hashed using the new secure hashing function
  131. $stmt = DBA::select('user', ['uid', 'password'], ['legacy_password' => true]);
  132. while ($user = DBA::fetch($stmt)) {
  133. DBA::update('user', ['password' => User::hashPassword($user['password'])], ['uid' => $user['uid']]);
  134. }
  135. // Logged in users are forcibly logged out
  136. DBA::delete('session', ['1 = 1']);
  137. return Update::SUCCESS;
  138. }
  139. function update_1245()
  140. {
  141. $rino = DI::config()->get('system', 'rino_encrypt');
  142. if (!$rino) {
  143. return Update::SUCCESS;
  144. }
  145. DI::config()->set('system', 'rino_encrypt', 1);
  146. return Update::SUCCESS;
  147. }
  148. function update_1247()
  149. {
  150. // Removing hooks with the old name
  151. DBA::e("DELETE FROM `hook`
  152. WHERE `hook` LIKE 'plugin_%'");
  153. // Make sure we install the new renamed ones
  154. Addon::reload();
  155. }
  156. function update_1260()
  157. {
  158. DI::config()->set('system', 'maintenance', 1);
  159. DI::config()->set(
  160. 'system',
  161. 'maintenance_reason',
  162. DI::l10n()->t(
  163. '%s: Updating author-id and owner-id in item and thread table. ',
  164. DateTimeFormat::utcNow().' '.date('e')
  165. )
  166. );
  167. $items = DBA::p("SELECT `id`, `owner-link`, `owner-name`, `owner-avatar`, `network` FROM `item`
  168. WHERE `owner-id` = 0 AND `owner-link` != ''");
  169. while ($item = DBA::fetch($items)) {
  170. $contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
  171. 'photo' => $item['owner-avatar'], 'network' => $item['network']];
  172. $cid = Contact::getIdForURL($item['owner-link'], 0, false, $contact);
  173. if (empty($cid)) {
  174. continue;
  175. }
  176. Item::update(['owner-id' => $cid], ['id' => $item['id']]);
  177. }
  178. DBA::close($items);
  179. DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
  180. SET `thread`.`owner-id` = `item`.`owner-id` WHERE `thread`.`owner-id` = 0");
  181. $items = DBA::p("SELECT `id`, `author-link`, `author-name`, `author-avatar`, `network` FROM `item`
  182. WHERE `author-id` = 0 AND `author-link` != ''");
  183. while ($item = DBA::fetch($items)) {
  184. $contact = ['url' => $item['author-link'], 'name' => $item['author-name'],
  185. 'photo' => $item['author-avatar'], 'network' => $item['network']];
  186. $cid = Contact::getIdForURL($item['author-link'], 0, false, $contact);
  187. if (empty($cid)) {
  188. continue;
  189. }
  190. Item::update(['author-id' => $cid], ['id' => $item['id']]);
  191. }
  192. DBA::close($items);
  193. DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
  194. SET `thread`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0");
  195. DI::config()->set('system', 'maintenance', 0);
  196. return Update::SUCCESS;
  197. }
  198. function update_1261()
  199. {
  200. // This fixes the results of an issue in the develop branch of 2018-05.
  201. DBA::update('contact', ['blocked' => false, 'pending' => false], ['uid' => 0, 'blocked' => true, 'pending' => true]);
  202. return Update::SUCCESS;
  203. }
  204. function update_1278()
  205. {
  206. DI::config()->set('system', 'maintenance', 1);
  207. DI::config()->set(
  208. 'system',
  209. 'maintenance_reason',
  210. DI::l10n()->t(
  211. '%s: Updating post-type.',
  212. DateTimeFormat::utcNow().' '.date('e')
  213. )
  214. );
  215. Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
  216. Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
  217. DI::config()->set('system', 'maintenance', 0);
  218. return Update::SUCCESS;
  219. }
  220. function update_1288()
  221. {
  222. // Updates missing `uri-id` values
  223. DBA::e("UPDATE `item-activity` INNER JOIN `item` ON `item`.`iaid` = `item-activity`.`id` SET `item-activity`.`uri-id` = `item`.`uri-id` WHERE `item-activity`.`uri-id` IS NULL OR `item-activity`.`uri-id` = 0");
  224. DBA::e("UPDATE `item-content` INNER JOIN `item` ON `item`.`icid` = `item-content`.`id` SET `item-content`.`uri-id` = `item`.`uri-id` WHERE `item-content`.`uri-id` IS NULL OR `item-content`.`uri-id` = 0");
  225. return Update::SUCCESS;
  226. }
  227. // Post-update script of PR 5751
  228. function update_1298()
  229. {
  230. $keys = ['gender', 'marital', 'sexual'];
  231. foreach ($keys as $translateKey) {
  232. $allData = DBA::select('profile', ['id', $translateKey]);
  233. $allLangs = DI::l10n()->getAvailableLanguages();
  234. $success = 0;
  235. $fail = 0;
  236. foreach ($allData as $key => $data) {
  237. $toTranslate = $data[$translateKey];
  238. if ($toTranslate != '') {
  239. foreach ($allLangs as $key => $lang) {
  240. $a = new \stdClass();
  241. $a->strings = [];
  242. // First we get the the localizations
  243. if (file_exists("view/lang/$lang/strings.php")) {
  244. include "view/lang/$lang/strings.php";
  245. }
  246. if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
  247. include "addon/morechoice/lang/$lang/strings.php";
  248. }
  249. $localizedStrings = $a->strings;
  250. unset($a);
  251. $key = array_search($toTranslate, $localizedStrings);
  252. if ($key !== false) {
  253. break;
  254. }
  255. // defaulting to empty string
  256. $key = '';
  257. }
  258. if ($key == '') {
  259. $fail++;
  260. } else {
  261. DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
  262. Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
  263. 'was' => $data[$translateKey]]);
  264. Worker::add(PRIORITY_LOW, 'ProfileUpdate', $data['id']);
  265. Contact::updateSelfFromUserID($data['id']);
  266. GContact::updateForUser($data['id']);
  267. $success++;
  268. }
  269. }
  270. }
  271. Logger::notice($translateKey . " fix completed", ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
  272. }
  273. return Update::SUCCESS;
  274. }
  275. function update_1309()
  276. {
  277. $queue = DBA::select('queue', ['id', 'cid', 'guid']);
  278. while ($entry = DBA::fetch($queue)) {
  279. $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
  280. if (!DBA::isResult($contact)) {
  281. continue;
  282. }
  283. $item = Item::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
  284. if (!DBA::isResult($item)) {
  285. continue;
  286. }
  287. $deliver_options = ['priority' => PRIORITY_MEDIUM, 'dont_fork' => true];
  288. Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
  289. Logger::info('Added delivery worker', ['item' => $item['id'], 'contact' => $entry['cid']]);
  290. DBA::delete('queue', ['id' => $entry['id']]);
  291. }
  292. return Update::SUCCESS;
  293. }
  294. function update_1315()
  295. {
  296. DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
  297. return Update::SUCCESS;
  298. }
  299. function update_1318()
  300. {
  301. DBA::update('profile', ['marital' => "In a relation"], ['marital' => "Unavailable"]);
  302. DBA::update('profile', ['marital' => "Single"], ['marital' => "Available"]);
  303. Worker::add(PRIORITY_LOW, 'ProfileUpdate');
  304. return Update::SUCCESS;
  305. }
  306. function update_1323()
  307. {
  308. $users = DBA::select('user', ['uid']);
  309. while ($user = DBA::fetch($users)) {
  310. Contact::updateSelfFromUserID($user['uid']);
  311. }
  312. DBA::close($users);
  313. return Update::SUCCESS;
  314. }
  315. function update_1327()
  316. {
  317. $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
  318. while ($contact = DBA::fetch($contacts)) {
  319. Contact::setBlockedForUser($contact['id'], $contact['uid'], $contact['blocked']);
  320. Contact::setIgnoredForUser($contact['id'], $contact['uid'], $contact['readonly']);
  321. }
  322. DBA::close($contacts);
  323. return Update::SUCCESS;
  324. }
  325. function update_1330()
  326. {
  327. $currStorage = DI::config()->get('storage', 'class', '');
  328. // set the name of the storage instead of the classpath as config
  329. if (!empty($currStorage)) {
  330. /** @var Storage\IStorage $currStorage */
  331. if (!DI::config()->set('storage', 'name', $currStorage::getName())) {
  332. return Update::FAILED;
  333. }
  334. // try to delete the class since it isn't needed. This won't work with config files
  335. DI::config()->delete('storage', 'class');
  336. }
  337. // Update attachments and photos
  338. if (!DBA::p("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
  339. !DBA::p("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
  340. return Update::FAILED;
  341. };
  342. return Update::SUCCESS;
  343. }
  344. function update_1332()
  345. {
  346. $condition = ["`is-default` IS NOT NULL"];
  347. $profiles = DBA::select('profile', [], $condition);
  348. while ($profile = DBA::fetch($profiles)) {
  349. DI::profileField()->migrateFromLegacyProfile($profile);
  350. }
  351. DBA::close($profiles);
  352. DBA::update('contact', ['profile-id' => null], ['`profile-id` IS NOT NULL']);
  353. return Update::SUCCESS;
  354. }
  355. function update_1347()
  356. {
  357. foreach (Item::ACTIVITIES as $index => $activity) {
  358. DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], true);
  359. }
  360. return Update::SUCCESS;
  361. }
  362. function pre_update_1348()
  363. {
  364. if (!DBA::exists('contact', ['id' => 0])) {
  365. DBA::insert('contact', ['nurl' => '']);
  366. $lastid = DBA::lastInsertId();
  367. if ($lastid != 0) {
  368. DBA::update('contact', ['id' => 0], ['id' => $lastid]);
  369. }
  370. }
  371. // The tables "permissionset" and "tag" could or could not exist during the update.
  372. // This depends upon the previous version. Depending upon this situation we have to add
  373. // the "0" values before adding the foreign keys - or after would be sufficient.
  374. update_1348();
  375. return Update::SUCCESS;
  376. }
  377. function update_1348()
  378. {
  379. // Insert a permissionset with id=0
  380. // Inserting it without an ID and then changing the value to 0 tricks the auto increment
  381. if (!DBA::exists('permissionset', ['id' => 0])) {
  382. DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']);
  383. $lastid = DBA::lastInsertId();
  384. if ($lastid != 0) {
  385. DBA::update('permissionset', ['id' => 0], ['id' => $lastid]);
  386. }
  387. }
  388. if (!DBA::exists('tag', ['id' => 0])) {
  389. DBA::insert('tag', ['name' => '']);
  390. $lastid = DBA::lastInsertId();
  391. if ($lastid != 0) {
  392. DBA::update('tag', ['id' => 0], ['id' => $lastid]);
  393. }
  394. }
  395. return Update::SUCCESS;
  396. }
  397. function update_1349()
  398. {
  399. $correct = true;
  400. foreach (Item::ACTIVITIES as $index => $activity) {
  401. if (!DBA::exists('verb', ['id' => $index + 1, 'name' => $activity])) {
  402. $correct = false;
  403. }
  404. }
  405. if (!$correct) {
  406. // The update failed - but it cannot be recovered, since the data doesn't match our expectation
  407. // This means that we can't use this "shortcut" to fill the "vid" field and we have to rely upon
  408. // the postupdate. This is not fatal, but means that it will take some longer time for the system
  409. // to fill all data.
  410. return Update::SUCCESS;
  411. }
  412. if (!DBA::e("UPDATE `item` INNER JOIN `item-activity` ON `item`.`uri-id` = `item-activity`.`uri-id`
  413. SET `vid` = `item-activity`.`activity` + 1 WHERE `gravity` = ? AND (`vid` IS NULL OR `vid` = 0)", GRAVITY_ACTIVITY)) {
  414. return Update::FAILED;
  415. }
  416. return Update::SUCCESS;
  417. }
  418. function update_1351()
  419. {
  420. if (!DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id` SET `thread`.`uri-id` = `item`.`uri-id`")) {
  421. return Update::FAILED;
  422. }
  423. return Update::SUCCESS;
  424. }
  425. function pre_update_1354()
  426. {
  427. if (DBStructure::existsColumn('contact', ['ffi_keyword_blacklist'])
  428. && !DBA::e("ALTER TABLE `contact` CHANGE `ffi_keyword_blacklist` `ffi_keyword_denylist` text null")) {
  429. return Update::FAILED;
  430. }
  431. return Update::SUCCESS;
  432. }