Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
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.

update.php 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. use Friendica\Core\Addon;
  3. use Friendica\Core\Config;
  4. use Friendica\Core\L10n;
  5. use Friendica\Core\Logger;
  6. use Friendica\Core\PConfig;
  7. use Friendica\Core\Update;
  8. use Friendica\Core\Worker;
  9. use Friendica\Database\DBA;
  10. use Friendica\Model\Contact;
  11. use Friendica\Model\GContact;
  12. use Friendica\Model\Item;
  13. use Friendica\Model\User;
  14. use Friendica\Util\DateTimeFormat;
  15. /**
  16. *
  17. * update.php - automatic system update
  18. *
  19. * This function is responsible for doing post update changes to the data
  20. * (not the structure) in the database.
  21. *
  22. * Database structure changes are done in config/dbstructure.config.php
  23. *
  24. * If there is a need for a post process to a structure change, update this file
  25. * by adding a new function at the end with the number of the new DB_UPDATE_VERSION.
  26. *
  27. * The numbered script in this file has to be exactly like the DB_UPDATE_VERSION
  28. *
  29. * Example:
  30. * You are currently on version 4711 and you are preparing changes that demand an update script.
  31. *
  32. * 1. Create a function "update_4712()" here in the update.php
  33. * 2. Apply the needed structural changes in config/dbStructure.php
  34. * 3. Set DB_UPDATE_VERSION in config/dbstructure.config.php to 4712.
  35. *
  36. * If you need to run a script before the database update, name the function "pre_update_4712()"
  37. */
  38. function update_1178()
  39. {
  40. require_once 'mod/profiles.php';
  41. $profiles = q("SELECT `uid`, `about`, `locality`, `pub_keywords`, `gender` FROM `profile` WHERE `is-default`");
  42. foreach ($profiles as $profile) {
  43. if ($profile["about"].$profile["locality"].$profile["pub_keywords"].$profile["gender"] == "") {
  44. continue;
  45. }
  46. $profile["pub_keywords"] = profile_clean_keywords($profile["pub_keywords"]);
  47. $r = q("UPDATE `contact` SET `about` = '%s', `location` = '%s', `keywords` = '%s', `gender` = '%s' WHERE `self` AND `uid` = %d",
  48. DBA::escape($profile["about"]),
  49. DBA::escape($profile["locality"]),
  50. DBA::escape($profile["pub_keywords"]),
  51. DBA::escape($profile["gender"]),
  52. intval($profile["uid"])
  53. );
  54. }
  55. }
  56. function update_1179()
  57. {
  58. if (Config::get('system', 'no_community_page')) {
  59. 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. Worker::add(PRIORITY_LOW, "TagUpdate");
  69. return Update::SUCCESS;
  70. }
  71. function update_1189()
  72. {
  73. if (strlen(Config::get('system', 'directory_submit_url')) &&
  74. !strlen(Config::get('system', 'directory'))) {
  75. Config::set('system', 'directory', dirname(Config::get('system', 'directory_submit_url')));
  76. Config::delete('system', 'directory_submit_url');
  77. }
  78. return Update::SUCCESS;
  79. }
  80. function update_1191()
  81. {
  82. Config::set('system', 'maintenance', 1);
  83. if (Addon::isEnabled('forumlist')) {
  84. $addon = 'forumlist';
  85. $addons = Config::get('system', 'addon');
  86. $addons_arr = [];
  87. if ($addons) {
  88. $addons_arr = explode(",", str_replace(" ", "", $addons));
  89. $idx = array_search($addon, $addons_arr);
  90. if ($idx !== false) {
  91. unset($addons_arr[$idx]);
  92. //delete forumlist manually from addon and hook table
  93. // since Addon::uninstall() don't work here
  94. q("DELETE FROM `addon` WHERE `name` = 'forumlist' ");
  95. q("DELETE FROM `hook` WHERE `file` = 'addon/forumlist/forumlist.php' ");
  96. Config::set('system', 'addon', implode(", ", $addons_arr));
  97. }
  98. }
  99. }
  100. // select old formlist addon entries
  101. $r = q("SELECT `uid`, `cat`, `k`, `v` FROM `pconfig` WHERE `cat` = '%s' ",
  102. DBA::escape('forumlist')
  103. );
  104. // convert old forumlist addon entries in new config entries
  105. if (DBA::isResult($r)) {
  106. foreach ($r as $rr) {
  107. $uid = $rr['uid'];
  108. $family = $rr['cat'];
  109. $key = $rr['k'];
  110. $value = $rr['v'];
  111. if ($key === 'randomise') {
  112. PConfig::delete($uid, $family, $key);
  113. }
  114. if ($key === 'show_on_profile') {
  115. if ($value) {
  116. PConfig::set($uid, feature, forumlist_profile, $value);
  117. }
  118. PConfig::delete($uid, $family, $key);
  119. }
  120. if ($key === 'show_on_network') {
  121. if ($value) {
  122. PConfig::set($uid, feature, forumlist_widget, $value);
  123. }
  124. PConfig::delete($uid, $family, $key);
  125. }
  126. }
  127. }
  128. Config::set('system', 'maintenance', 0);
  129. return Update::SUCCESS;
  130. }
  131. function update_1203()
  132. {
  133. $r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
  134. DBA::escape(User::ACCOUNT_TYPE_COMMUNITY),
  135. DBA::escape(User::PAGE_FLAGS_COMMUNITY),
  136. DBA::escape(User::PAGE_FLAGS_PRVGROUP)
  137. );
  138. }
  139. function update_1244()
  140. {
  141. // Sets legacy_password for all legacy hashes
  142. DBA::update('user', ['legacy_password' => true], ['SUBSTR(password, 1, 4) != "$2y$"']);
  143. // All legacy hashes are re-hashed using the new secure hashing function
  144. $stmt = DBA::select('user', ['uid', 'password'], ['legacy_password' => true]);
  145. while ($user = DBA::fetch($stmt)) {
  146. DBA::update('user', ['password' => User::hashPassword($user['password'])], ['uid' => $user['uid']]);
  147. }
  148. // Logged in users are forcibly logged out
  149. DBA::delete('session', ['1 = 1']);
  150. return Update::SUCCESS;
  151. }
  152. function update_1245()
  153. {
  154. $rino = Config::get('system', 'rino_encrypt');
  155. if (!$rino) {
  156. return Update::SUCCESS;
  157. }
  158. Config::set('system', 'rino_encrypt', 1);
  159. return Update::SUCCESS;
  160. }
  161. function update_1247()
  162. {
  163. // Removing hooks with the old name
  164. DBA::e("DELETE FROM `hook`
  165. WHERE `hook` LIKE 'plugin_%'");
  166. // Make sure we install the new renamed ones
  167. Addon::reload();
  168. }
  169. function update_1260()
  170. {
  171. Config::set('system', 'maintenance', 1);
  172. Config::set(
  173. 'system',
  174. 'maintenance_reason',
  175. L10n::t(
  176. '%s: Updating author-id and owner-id in item and thread table. ',
  177. DateTimeFormat::utcNow().' '.date('e')
  178. )
  179. );
  180. $items = DBA::p("SELECT `id`, `owner-link`, `owner-name`, `owner-avatar`, `network` FROM `item`
  181. WHERE `owner-id` = 0 AND `owner-link` != ''");
  182. while ($item = DBA::fetch($items)) {
  183. $contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
  184. 'photo' => $item['owner-avatar'], 'network' => $item['network']];
  185. $cid = Contact::getIdForURL($item['owner-link'], 0, false, $contact);
  186. if (empty($cid)) {
  187. continue;
  188. }
  189. Item::update(['owner-id' => $cid], ['id' => $item['id']]);
  190. }
  191. DBA::close($items);
  192. DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
  193. SET `thread`.`owner-id` = `item`.`owner-id` WHERE `thread`.`owner-id` = 0");
  194. $items = DBA::p("SELECT `id`, `author-link`, `author-name`, `author-avatar`, `network` FROM `item`
  195. WHERE `author-id` = 0 AND `author-link` != ''");
  196. while ($item = DBA::fetch($items)) {
  197. $contact = ['url' => $item['author-link'], 'name' => $item['author-name'],
  198. 'photo' => $item['author-avatar'], 'network' => $item['network']];
  199. $cid = Contact::getIdForURL($item['author-link'], 0, false, $contact);
  200. if (empty($cid)) {
  201. continue;
  202. }
  203. Item::update(['author-id' => $cid], ['id' => $item['id']]);
  204. }
  205. DBA::close($items);
  206. DBA::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
  207. SET `thread`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0");
  208. Config::set('system', 'maintenance', 0);
  209. return Update::SUCCESS;
  210. }
  211. function update_1261()
  212. {
  213. // This fixes the results of an issue in the develop branch of 2018-05.
  214. DBA::update('contact', ['blocked' => false, 'pending' => false], ['uid' => 0, 'blocked' => true, 'pending' => true]);
  215. return Update::SUCCESS;
  216. }
  217. function update_1278()
  218. {
  219. Config::set('system', 'maintenance', 1);
  220. Config::set(
  221. 'system',
  222. 'maintenance_reason',
  223. L10n::t(
  224. '%s: Updating post-type.',
  225. DateTimeFormat::utcNow().' '.date('e')
  226. )
  227. );
  228. Item::update(['post-type' => Item::PT_PAGE], ['bookmark' => true]);
  229. Item::update(['post-type' => Item::PT_PERSONAL_NOTE], ['type' => 'note']);
  230. Config::set('system', 'maintenance', 0);
  231. return Update::SUCCESS;
  232. }
  233. function update_1288()
  234. {
  235. // Updates missing `uri-id` values
  236. 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");
  237. 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");
  238. return Update::SUCCESS;
  239. }
  240. // Post-update script of PR 5751
  241. function update_1298()
  242. {
  243. $keys = ['gender', 'marital', 'sexual'];
  244. foreach ($keys as $translateKey) {
  245. $allData = DBA::select('profile', ['id', $translateKey]);
  246. $allLangs = L10n::getAvailableLanguages();
  247. $success = 0;
  248. $fail = 0;
  249. foreach ($allData as $key => $data) {
  250. $toTranslate = $data[$translateKey];
  251. if ($toTranslate != '') {
  252. foreach ($allLangs as $key => $lang) {
  253. $a = new \stdClass();
  254. $a->strings = [];
  255. // First we get the the localizations
  256. if (file_exists("view/lang/$lang/strings.php")) {
  257. include "view/lang/$lang/strings.php";
  258. }
  259. if (file_exists("addon/morechoice/lang/$lang/strings.php")) {
  260. include "addon/morechoice/lang/$lang/strings.php";
  261. }
  262. $localizedStrings = $a->strings;
  263. unset($a);
  264. $key = array_search($toTranslate, $localizedStrings);
  265. if ($key !== false) {
  266. break;
  267. }
  268. // defaulting to empty string
  269. $key = '';
  270. }
  271. if ($key == '') {
  272. $fail++;
  273. } else {
  274. DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
  275. Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
  276. 'was' => $data[$translateKey]]);
  277. Worker::add(PRIORITY_LOW, 'ProfileUpdate', $data['id']);
  278. Contact::updateSelfFromUserID($data['id']);
  279. GContact::updateForUser($data['id']);
  280. $success++;
  281. }
  282. }
  283. }
  284. Logger::notice($translateKey . " fix completed", ['action' => 'update', 'translateKey' => $translateKey, 'Success' => $success, 'Fail' => $fail ]);
  285. }
  286. return Update::SUCCESS;
  287. }