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.

261 lines
8.1KB

  1. <?php
  2. /**
  3. * @file include/post_update.php
  4. */
  5. /**
  6. * @brief Calls the post update functions
  7. */
  8. function post_update() {
  9. if (!post_update_1192()) {
  10. return;
  11. }
  12. if (!post_update_1194()) {
  13. return;
  14. }
  15. if (!post_update_1198()) {
  16. return;
  17. }
  18. if (!post_update_1206()) {
  19. return;
  20. }
  21. }
  22. /**
  23. * @brief set the gcontact-id in all item entries
  24. *
  25. * This job has to be started multiple times until all entries are set.
  26. * It isn't started in the update function since it would consume too much time and can be done in the background.
  27. *
  28. * @return bool "true" when the job is done
  29. */
  30. function post_update_1192() {
  31. // Was the script completed?
  32. if (get_config("system", "post_update_version") >= 1192)
  33. return true;
  34. // Check if the first step is done (Setting "gcontact-id" in the item table)
  35. $r = q("SELECT `author-link`, `author-name`, `author-avatar`, `uid`, `network` FROM `item` WHERE `gcontact-id` = 0 LIMIT 1000");
  36. if (!$r) {
  37. // Are there unfinished entries in the thread table?
  38. $r = q("SELECT COUNT(*) AS `total` FROM `thread`
  39. INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
  40. WHERE `thread`.`gcontact-id` = 0 AND
  41. (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
  42. if ($r AND ($r[0]["total"] == 0)) {
  43. set_config("system", "post_update_version", 1192);
  44. return true;
  45. }
  46. // Update the thread table from the item table
  47. q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
  48. SET `thread`.`gcontact-id` = `item`.`gcontact-id`
  49. WHERE `thread`.`gcontact-id` = 0 AND
  50. (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
  51. return false;
  52. }
  53. $item_arr = array();
  54. foreach ($r AS $item) {
  55. $index = $item["author-link"]."-".$item["uid"];
  56. $item_arr[$index] = array("author-link" => $item["author-link"],
  57. "uid" => $item["uid"],
  58. "network" => $item["network"]);
  59. }
  60. // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
  61. foreach($item_arr AS $item) {
  62. $gcontact_id = get_gcontact_id(array("url" => $item['author-link'], "network" => $item['network'],
  63. "photo" => $item['author-avatar'], "name" => $item['author-name']));
  64. q("UPDATE `item` SET `gcontact-id` = %d WHERE `uid` = %d AND `author-link` = '%s' AND `gcontact-id` = 0",
  65. intval($gcontact_id), intval($item["uid"]), dbesc($item["author-link"]));
  66. }
  67. return false;
  68. }
  69. /**
  70. * @brief Updates the "global" field in the item table
  71. *
  72. * @return bool "true" when the job is done
  73. */
  74. function post_update_1194() {
  75. // Was the script completed?
  76. if (get_config("system", "post_update_version") >= 1194)
  77. return true;
  78. logger("Start", LOGGER_DEBUG);
  79. $end_id = get_config("system", "post_update_1194_end");
  80. if (!$end_id) {
  81. $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
  82. if ($r) {
  83. set_config("system", "post_update_1194_end", $r[0]["id"]);
  84. $end_id = get_config("system", "post_update_1194_end");
  85. }
  86. }
  87. logger("End ID: ".$end_id, LOGGER_DEBUG);
  88. $start_id = get_config("system", "post_update_1194_start");
  89. $query1 = "SELECT `item`.`id` FROM `item` ";
  90. $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
  91. $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
  92. AND `item`.`visible` AND NOT `item`.`private`
  93. AND NOT `item`.`deleted` AND NOT `item`.`moderated`
  94. AND `item`.`network` IN ('%s', '%s', '%s', '')
  95. AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
  96. AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
  97. AND NOT `item`.`global`";
  98. $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
  99. intval($start_id), intval($end_id),
  100. dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
  101. if (!$r) {
  102. set_config("system", "post_update_version", 1194);
  103. logger("Update is done", LOGGER_DEBUG);
  104. return true;
  105. } else {
  106. set_config("system", "post_update_1194_start", $r[0]["id"]);
  107. $start_id = get_config("system", "post_update_1194_start");
  108. }
  109. logger("Start ID: ".$start_id, LOGGER_DEBUG);
  110. $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
  111. intval($start_id), intval($end_id),
  112. dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
  113. if ($r)
  114. $pos_id = $r[0]["id"];
  115. else
  116. $pos_id = $end_id;
  117. logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
  118. $r = q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
  119. intval($start_id), intval($pos_id),
  120. dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
  121. logger("Done", LOGGER_DEBUG);
  122. }
  123. /**
  124. * @brief set the author-id and owner-id in all item entries
  125. *
  126. * This job has to be started multiple times until all entries are set.
  127. * It isn't started in the update function since it would consume too much time and can be done in the background.
  128. *
  129. * @return bool "true" when the job is done
  130. */
  131. function post_update_1198() {
  132. // Was the script completed?
  133. if (get_config("system", "post_update_version") >= 1198)
  134. return true;
  135. logger("Start", LOGGER_DEBUG);
  136. // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
  137. $r = q("SELECT `author-link`, `owner-link`, `uid` FROM `item` WHERE `author-id` = 0 AND `owner-id` = 0 LIMIT 100");
  138. if (!$r) {
  139. // Are there unfinished entries in the thread table?
  140. $r = q("SELECT COUNT(*) AS `total` FROM `thread`
  141. INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
  142. WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
  143. (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
  144. if ($r AND ($r[0]["total"] == 0)) {
  145. set_config("system", "post_update_version", 1198);
  146. logger("Done", LOGGER_DEBUG);
  147. return true;
  148. }
  149. // Update the thread table from the item table
  150. $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
  151. SET `thread`.`author-id` = `item`.`author-id`,
  152. `thread`.`owner-id` = `item`.`owner-id`
  153. WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
  154. (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
  155. logger("Updated threads", LOGGER_DEBUG);
  156. if (dbm::is_result($r)) {
  157. set_config("system", "post_update_version", 1198);
  158. logger("Done", LOGGER_DEBUG);
  159. return true;
  160. }
  161. return false;
  162. }
  163. logger("Query done", LOGGER_DEBUG);
  164. $item_arr = array();
  165. foreach ($r AS $item) {
  166. $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
  167. $item_arr[$index] = array("author-link" => $item["author-link"],
  168. "owner-link" => $item["owner-link"],
  169. "uid" => $item["uid"]);
  170. }
  171. // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
  172. foreach($item_arr AS $item) {
  173. $author_id = get_contact($item["author-link"], 0);
  174. $owner_id = get_contact($item["owner-link"], 0);
  175. if ($author_id == 0)
  176. $author_id = -1;
  177. if ($owner_id == 0)
  178. $owner_id = -1;
  179. q("UPDATE `item` SET `author-id` = %d, `owner-id` = %d
  180. WHERE `uid` = %d AND `author-link` = '%s' AND `owner-link` = '%s'
  181. AND `author-id` = 0 AND `owner-id` = 0",
  182. intval($author_id), intval($owner_id), intval($item["uid"]),
  183. dbesc($item["author-link"]), dbesc($item["owner-link"]));
  184. }
  185. logger("Updated items", LOGGER_DEBUG);
  186. return false;
  187. }
  188. /**
  189. * @brief update the "last-item" field in the "self" contact
  190. *
  191. * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
  192. *
  193. * @return bool "true" when the job is done
  194. */
  195. function post_update_1206() {
  196. // Was the script completed?
  197. if (get_config("system", "post_update_version") >= 1206)
  198. return true;
  199. logger("Start", LOGGER_DEBUG);
  200. $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
  201. (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
  202. FROM `user`
  203. INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
  204. if (!dbm::is_result($r)) {
  205. return false;
  206. }
  207. foreach ($r AS $user) {
  208. if (!empty($user["lastitem_date"]) AND ($user["lastitem_date"] > $user["last-item"])) {
  209. q("UPDATE `contact` SET `last-item` = '%s' WHERE `id` = %d",
  210. dbesc($user["lastitem_date"]),
  211. intval($user["id"]));
  212. }
  213. }
  214. set_config("system", "post_update_version", 1206);
  215. logger("Done", LOGGER_DEBUG);
  216. return true;
  217. }