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.

425 lines
12KB

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