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.1 KiB

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