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.

441 lines
13KB

  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\DI;
  48. use Friendica\Model\Contact;
  49. use Friendica\Model\GContact;
  50. use Friendica\Model\Item;
  51. use Friendica\Model\User;
  52. use Friendica\Model\Storage;
  53. use Friendica\Util\DateTimeFormat;
  54. use Friendica\Worker\Delivery;
  55. function update_1179()
  56. {
  57. if (DI::config()->get('system', 'no_community_page')) {
  58. DI::config()->set('system', 'community_page_style', CP_NO_COMMUNITY_PAGE);
  59. }
  60. // Update the central item storage with uid=0
  61. Worker::add(PRIORITY_LOW, "threadupdate");
  62. return Update::SUCCESS;
  63. }
  64. function update_1181()
  65. {
  66. // Fill the new fields in the term table.
  67. Worker::add(PRIORITY_LOW, "TagUpdate");
  68. return Update::SUCCESS;
  69. }
  70. function update_1189()
  71. {
  72. if (strlen(DI::config()->get('system', 'directory_submit_url')) &&
  73. !strlen(DI::config()->get('system', 'directory'))) {
  74. DI::config()->set('system', 'directory', dirname(DI::config()->get('system', 'directory_submit_url')));
  75. DI::config()->delete('system', 'directory_submit_url');
  76. }
  77. return Update::SUCCESS;
  78. }
  79. function update_1191()
  80. {
  81. DI::config()->set('system', 'maintenance', 1);
  82. if (Addon::isEnabled('forumlist')) {
  83. $addon = 'forumlist';
  84. $addons = DI::config()->get('system', 'addon');
  85. $addons_arr = [];
  86. if ($addons) {
  87. $addons_arr = explode(",", str_replace(" ", "", $addons));
  88. $idx = array_search($addon, $addons_arr);
  89. if ($idx !== false) {
  90. unset($addons_arr[$idx]);
  91. //delete forumlist manually from addon and hook table
  92. // since Addon::uninstall() don't work here
  93. q("DELETE FROM `addon` WHERE `name` = 'forumlist' ");
  94. q("DELETE FROM `hook` WHERE `file` = 'addon/forumlist/forumlist.php' ");
  95. DI::config()->set('system', 'addon', implode(", ", $addons_arr));
  96. }
  97. }
  98. }
  99. // select old formlist addon entries
  100. $r = q("SELECT `uid`, `cat`, `k`, `v` FROM `pconfig` WHERE `cat` = '%s' ",
  101. DBA::escape('forumlist')
  102. );
  103. // convert old forumlist addon entries in new config entries
  104. if (DBA::isResult($r)) {
  105. foreach ($r as $rr) {
  106. $uid = $rr['uid'];
  107. $family = $rr['cat'];
  108. $key = $rr['k'];
  109. $value = $rr['v'];
  110. if ($key === 'randomise') {
  111. DI::pConfig()->delete($uid, $family, $key);
  112. }
  113. if ($key === 'show_on_profile') {
  114. if ($value) {
  115. DI::pConfig()->set($uid, 'feature', 'forumlist_profile', $value);
  116. }
  117. DI::pConfig()->delete($uid, $family, $key);
  118. }
  119. if ($key === 'show_on_network') {
  120. if ($value) {
  121. DI::pConfig()->set($uid, 'feature', 'forumlist_widget', $value);
  122. }
  123. DI::pConfig()->delete($uid, $family, $key);
  124. }
  125. }
  126. }
  127. DI::config()->set('system', 'maintenance', 0);
  128. return Update::SUCCESS;
  129. }
  130. function update_1203()
  131. {
  132. $r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
  133. DBA::escape(User::ACCOUNT_TYPE_COMMUNITY),
  134. DBA::escape(User::PAGE_FLAGS_COMMUNITY),
  135. DBA::escape(User::PAGE_FLAGS_PRVGROUP)
  136. );
  137. }
  138. function update_1244()
  139. {
  140. // Sets legacy_password for all legacy hashes
  141. DBA::update('user', ['legacy_password' => true], ['SUBSTR(password, 1, 4) != "$2y$"']);
  142. // All legacy hashes are re-hashed using the new secure hashing function
  143. $stmt = DBA::select('user', ['uid', 'password'], ['legacy_password' => true]);
  144. while ($user = DBA::fetch($stmt)) {
  145. DBA::update('user', ['password' => User::hashPassword($user['password'])], ['uid' => $user['uid']]);
  146. }
  147. // Logged in users are forcibly logged out
  148. DBA::delete('session', ['1 = 1']);
  149. return Update::SUCCESS;
  150. }
  151. function update_1245()
  152. {
  153. $rino = DI::config()->get('system', 'rino_encrypt');
  154. if (!$rino) {
  155. return Update::SUCCESS;
  156. }
  157. DI::config()->set('system', 'rino_encrypt', 1);
  158. return Update::SUCCESS;
  159. }
  160. function update_1247()
  161. {
  162. // Removing hooks with the old name
  163. DBA::e("DELETE FROM `hook`
  164. WHERE `hook` LIKE 'plugin_%'");
  165. // Make sure we install the new renamed ones
  166. Addon::reload();
  167. }
  168. function update_1260()
  169. {
  170. DI::config()->set('system', 'maintenance', 1);
  171. DI::config()->set(
  172. 'system',
  173. 'maintenance_reason',
  174. DI::l10n()->t(
  175. '%s: Updating author-id and owner-id in item and thread table. ',
  176. DateTimeFormat::utcNow().' '.date('e')
  177. )
  178. );
  179. $items = DBA::p("SELECT `id`, `owner-link`, `owner-name`, `owner-avatar`, `network` FROM `item`
  180. WHERE `owner-id` = 0 AND `owner-link` != ''");
  181. while ($item = DBA::fetch($items)) {
  182. $contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
  183. 'photo' => $item['owner-avatar'], 'network' => $item['network']];
  184. $cid = Contact::getIdForURL($item['owner-link'], 0, false, $contact);
  185. if (empty($cid)) {
  186. continue;
  187. }
  188. Item::update(['owner-id' => $cid], ['id' => $item['id']]);
  189. }
  190. DBA::close($items);
  191. DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
  192. SET `thread`.`owner-id` = `item`.`owner-id` WHERE `thread`.`owner-id` = 0");
  193. $items = DBA::p("SELECT `id`, `author-link`, `author-name`, `author-avatar`, `network` FROM `item`
  194. WHERE `author-id` = 0 AND `author-link` != ''");
  195. while ($item = DBA::fetch($items)) {
  196. $contact = ['url' => $item['author-link'], 'name' => $item['author-name'],
  197. 'photo' => $item['author-avatar'], 'network' => $item['network']];
  198. $cid = Contact::getIdForURL($item['author-link'], 0, false, $contact);
  199. if (empty($cid)) {
  200. continue;
  201. }
  202. Item::update(['author-id' => $cid], ['id' => $item['id']]);
  203. }
  204. DBA::close($items);
  205. DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
  206. SET `thread`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0");
  207. DI::config()->set('system', 'maintenance', 0);
  208. return Update::SUCCESS;
  209. }
  210. function update_1261()
  211. {
  212. // This fixes the results of an issue in the develop branch of 2018-05.
  213. DBA::update('contact', ['blocked' => false, 'pending' => false], ['uid' => 0, 'blocked' => true, 'pending' => true]);
  214. return Update::SUCCESS;
  215. }
  216. function update_1278()
  217. {
  218. DI::config()->set('system', 'maintenance', 1);
  219. DI::config()->set(
  220. 'system',
  221. 'maintenance_reason',
  222. DI::l10n()->t(
  223. '%s: Updating post-type.',
  224. DateTimeFormat::utcNow().' '.date('e')
  225. )
  226. );
  227. Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
  228. Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
  229. DI::config()->set('system', 'maintenance', 0);
  230. return Update::SUCCESS;
  231. }
  232. function update_1288()
  233. {
  234. // Updates missing `uri-id` values
  235. 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");
  236. 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");
  237. return Update::SUCCESS;
  238. }
  239. // Post-update script of PR 5751
  240. function update_1298()
  241. {
  242. $keys = ['gender', 'marital', 'sexual'];
  243. foreach ($keys as $translateKey) {
  244. $allData = DBA::select('profile', ['id', $translateKey]);
  245. $allLangs = DI::l10n()->getAvailableLanguages();
  246. $success = 0;
  247. $fail = 0;
  248. foreach ($allData as $key => $data) {
  249. $toTranslate = $data[$translateKey];
  250. if ($toTranslate != '') {
  251. foreach ($allLangs as $key => $lang) {
  252. $a = new \stdClass();
  253. $a->strings = [];
  254. // First we get the the localizations
  255. if (file_exists("view/lang/$lang/strings.php")) {
  256. include "view/lang/$lang/strings.php";
  257. }
  258. if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
  259. include "addon/morechoice/lang/$lang/strings.php";
  260. }
  261. $localizedStrings = $a->strings;
  262. unset($a);
  263. $key = array_search($toTranslate, $localizedStrings);
  264. if ($key !== false) {
  265. break;
  266. }
  267. // defaulting to empty string
  268. $key = '';
  269. }
  270. if ($key == '') {
  271. $fail++;
  272. } else {
  273. DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
  274. Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
  275. 'was' => $data[$translateKey]]);
  276. Worker::add(PRIORITY_LOW, 'ProfileUpdate', $data['id']);
  277. Contact::updateSelfFromUserID($data['id']);
  278. GContact::updateForUser($data['id']);
  279. $success++;
  280. }
  281. }
  282. }
  283. Logger::notice($translateKey . " fix completed", ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
  284. }
  285. return Update::SUCCESS;
  286. }
  287. function update_1309()
  288. {
  289. $queue = DBA::select('queue', ['id', 'cid', 'guid']);
  290. while ($entry = DBA::fetch($queue)) {
  291. $contact = DBA::selectFirst('contact', ['uid'], ['id' => $entry['cid']]);
  292. if (!DBA::isResult($contact)) {
  293. continue;
  294. }
  295. $item = Item::selectFirst(['id', 'gravity'], ['uid' => $contact['uid'], 'guid' => $entry['guid']]);
  296. if (!DBA::isResult($item)) {
  297. continue;
  298. }
  299. $deliver_options = ['priority' => PRIORITY_MEDIUM, 'dont_fork' => true];
  300. Worker::add($deliver_options, 'Delivery', Delivery::POST, $item['id'], $entry['cid']);
  301. Logger::info('Added delivery worker', ['item' => $item['id'], 'contact' => $entry['cid']]);
  302. DBA::delete('queue', ['id' => $entry['id']]);
  303. }
  304. return Update::SUCCESS;
  305. }
  306. function update_1315()
  307. {
  308. DBA::delete('item-delivery-data', ['postopts' => '', 'inform' => '', 'queue_count' => 0, 'queue_done' => 0]);
  309. return Update::SUCCESS;
  310. }
  311. function update_1318()
  312. {
  313. DBA::update('profile', ['marital' => "In a relation"], ['marital' => "Unavailable"]);
  314. DBA::update('profile', ['marital' => "Single"], ['marital' => "Available"]);
  315. Worker::add(PRIORITY_LOW, 'ProfileUpdate');
  316. return Update::SUCCESS;
  317. }
  318. function update_1323()
  319. {
  320. $users = DBA::select('user', ['uid']);
  321. while ($user = DBA::fetch($users)) {
  322. Contact::updateSelfFromUserID($user['uid']);
  323. }
  324. DBA::close($users);
  325. return Update::SUCCESS;
  326. }
  327. function update_1327()
  328. {
  329. $contacts = DBA::select('contact', ['uid', 'id', 'blocked', 'readonly'], ["`uid` != ? AND (`blocked` OR `readonly`) AND NOT `pending`", 0]);
  330. while ($contact = DBA::fetch($contacts)) {
  331. Contact::setBlockedForUser($contact['id'], $contact['uid'], $contact['blocked']);
  332. Contact::setIgnoredForUser($contact['id'], $contact['uid'], $contact['readonly']);
  333. }
  334. DBA::close($contacts);
  335. return Update::SUCCESS;
  336. }
  337. function update_1330()
  338. {
  339. $currStorage = DI::config()->get('storage', 'class', '');
  340. // set the name of the storage instead of the classpath as config
  341. if (!empty($currStorage)) {
  342. /** @var Storage\IStorage $currStorage */
  343. if (!DI::config()->set('storage', 'name', $currStorage::getName())) {
  344. return Update::FAILED;
  345. }
  346. // try to delete the class since it isn't needed. This won't work with config files
  347. DI::config()->delete('storage', 'class');
  348. }
  349. // Update attachments and photos
  350. if (!DBA::p("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
  351. !DBA::p("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
  352. return Update::FAILED;
  353. };
  354. return Update::SUCCESS;
  355. }
  356. function update_1332()
  357. {
  358. $condition = ["`is-default` IS NOT NULL"];
  359. $profiles = DBA::select('profile', [], $condition);
  360. while ($profile = DBA::fetch($profiles)) {
  361. DI::profileField()->migrateFromLegacyProfile($profile);
  362. }
  363. DBA::close($profiles);
  364. DBA::update('contact', ['profile-id' => null], ['`profile-id` IS NOT NULL']);
  365. return Update::SUCCESS;
  366. }