PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Cache/NewswireCache.php

https://gitlab.com/indybay/indybay-active
PHP | 327 lines | 188 code | 53 blank | 86 comment | 60 complexity | 3e53b0cd3e1a13d51e8a8eb51d91997c MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. namespace Indybay\Cache;
  3. use Indybay\Renderer\NewswireRenderer;
  4. use Indybay\DB\NewsItemDB;
  5. use Indybay\DB\NewswireDB;
  6. use Indybay\DB\FeaturePageDB;
  7. use Indybay\MediaAndFileUtil\FileUtil;
  8. /**
  9. * Generates the newswires included on the feature pages.
  10. *
  11. * Most of the code relates to only updating the needed newswires when events
  12. * are classified or change category.
  13. *
  14. * Written January 2006.
  15. * Modification Log:
  16. * 12/2005 - 1/2006 zogren/Zachary Ogren/zogren@yahoo.com
  17. * initial development.
  18. */
  19. class NewswireCache extends Cache {
  20. /**
  21. * Regenerate newswires for a newsitem that just came in.
  22. */
  23. public function regenerateNewswiresForNewNewsitem($news_item_id) {
  24. $old_cats = [];
  25. $this->regenerateNewswiresForNewsitemHelper($news_item_id, $old_cats, NEWS_ITEM_STATUS_ID_NEW, NEWS_ITEM_STATUS_ID_NEW);
  26. }
  27. /**
  28. * Regenerate newswires for a changed newsitem.
  29. */
  30. public function regenerateNewswiresForNewsitem($news_item_id, $old_cats, $old_display_status, $new_display_status) {
  31. $this->regenerateNewswiresForNewsitemHelper($news_item_id, $old_cats, $old_display_status, $new_display_status);
  32. }
  33. /**
  34. * Regenerate newswires for a changed newsitem or new item.
  35. */
  36. public function regenerateNewswiresForNewsitemHelper($news_item_id, $old_cats, $old_display_status, $new_display_status) {
  37. $news_item_db_class = new NewsItemDB();
  38. $page_db_class = new FeaturePageDB();
  39. $new_categories = $news_item_db_class->getNewsItemCategoryIds($news_item_id);
  40. $page_id_list = $page_db_class->getAssociatedPageIds(array_merge($new_categories, $old_cats));
  41. $page_id_list = array_merge($page_id_list, $page_db_class->getPagesWithAllCategoryNewswires());
  42. foreach ($page_id_list as $page_id) {
  43. $this->regenerateNewswireForPageHelper($page_id, $old_display_status, $new_display_status);
  44. }
  45. }
  46. /**
  47. * Force full regeneration of all the newswire sections on a specific page.
  48. */
  49. public function forceRegenerateNewswireForPage($page_id) {
  50. $this->regenerateNewswireForPageHelper($page_id, 0, 0);
  51. }
  52. /**
  53. * Helper method used in regeneration of newswire sections on a page.
  54. */
  55. public function regenerateNewswireForPageHelper($page_id, $old_display_status, $new_display_status) {
  56. $page_db_class = new FeaturePageDB();
  57. $page_info = $page_db_class->getFeaturePageInfo($page_id);
  58. $page_name = 'page' . $page_info['page_id'];
  59. $newswire_type_id = $page_info['newswire_type_id'];
  60. $items_per_newswire_section = $page_info['items_per_newswire_section'];
  61. $associated_category_array = [];
  62. if ($old_display_status == 0 && $new_display_status == 0) {
  63. $force_regenerate = 1;
  64. }
  65. if ($newswire_type_id != NEWSWIRE_TYPE_LOCAL_NONLOCAL_OTHER_NOCAT) {
  66. $associated_category_array = $page_db_class->getCategoryIdsForPage($page_id);
  67. }
  68. if ($newswire_type_id == NEWSWIRE_TYPE_ALL_CAT) {
  69. $this->regenerateAllNewswireForCategories($page_name, $associated_category_array, $items_per_newswire_section);
  70. }
  71. elseif ($newswire_type_id == NEWSWIRE_TYPE_CLASSIFIED_CAT) {
  72. if (!empty($force_regenerate) || !($old_display_status == NEWS_ITEM_STATUS_ID_HIDDEN || $old_display_status == NEWS_ITEM_STATUS_ID_QUESTIONABLE_HIDDEN
  73. || $new_display_status == NEWS_ITEM_STATUS_ID_HIDDEN || $new_display_status == NEWS_ITEM_STATUS_ID_QUESTIONABLE_HIDDEN)) {
  74. $this->regenerateClassifiedNewswire($page_name, $associated_category_array, $items_per_newswire_section);
  75. }
  76. }
  77. else {
  78. if (isset($force_regenerate) || $old_display_status == NEWS_ITEM_STATUS_ID_LOCAL_HIGHLIGHTED ||
  79. $new_display_status == NEWS_ITEM_STATUS_ID_LOCAL_HIGHLIGHTED) {
  80. $this->regenerateLocalNewswire($page_name, $associated_category_array, $items_per_newswire_section);
  81. }
  82. if (isset($force_regenerate) ||$old_display_status == NEWS_ITEM_STATUS_ID_NONLOCAL_HIGHLIGHTED ||
  83. $new_display_status == NEWS_ITEM_STATUS_ID_NONLOCAL_HIGHLIGHTED) {
  84. $this->regenerateNonlocalNewswire($page_name, $associated_category_array, $items_per_newswire_section);
  85. }
  86. $this->regenerateOtherNewswire($page_name, $associated_category_array, $items_per_newswire_section);
  87. }
  88. }
  89. /**
  90. * Recache the local newswire on a page.
  91. */
  92. public function regenerateLocalNewswire($page_name, $category_list, $items_per_newswire_section) {
  93. $newswire_db_class = new NewswireDB();
  94. $newswire_cache_file_name = "newswire_{$page_name}_local";
  95. if (isset($GLOBALS['part_of_bulk_operation']) && $GLOBALS['part_of_bulk_operation'] == 1 && count($category_list) != 0) {
  96. $this->markNewswireFileForRegenerateOnView($newswire_cache_file_name);
  97. }
  98. else {
  99. $newswire_list = $newswire_db_class->getLocalListForCategories($category_list, $items_per_newswire_section);
  100. $this->regenerateNewswireGivenResultData($newswire_cache_file_name, $newswire_list);
  101. }
  102. }
  103. /**
  104. * Recache the nonlocal newswire on a page.
  105. */
  106. public function regenerateNonlocalNewswire($page_name, $category_list, $items_per_newswire_section) {
  107. $newswire_db_class = new NewswireDB();
  108. $newswire_cache_file_name = "newswire_{$page_name}_nonlocal";
  109. if (array_key_exists('part_of_bulk_operation', $GLOBALS) && $GLOBALS['part_of_bulk_operation'] == 1 && count($category_list) != 0) {
  110. $this->markNewswireFileForRegenerateOnView($newswire_cache_file_name);
  111. }
  112. else {
  113. $newswire_list = $newswire_db_class->getNonlocalListForCategories($category_list, $items_per_newswire_section);
  114. $this->regenerateNewswireGivenResultData($newswire_cache_file_name, $newswire_list);
  115. }
  116. }
  117. /**
  118. * Recache the other newswire on a page.
  119. */
  120. public function regenerateOtherNewswire($page_name, $category_list, $items_per_newswire_section) {
  121. $newswire_db_class = new NewswireDB();
  122. $newswire_cache_file_name = "newswire_{$page_name}_other";
  123. if (array_key_exists('part_of_bulk_operation', $GLOBALS) && $GLOBALS['part_of_bulk_operation'] == 1 && count($category_list) != 0) {
  124. $this->markNewswireFileForRegenerateOnView($newswire_cache_file_name);
  125. }
  126. else {
  127. $newswire_list = $newswire_db_class->getOtherListForCategories($category_list, $items_per_newswire_section);
  128. $this->regenerateNewswireGivenResultData($newswire_cache_file_name, $newswire_list);
  129. }
  130. }
  131. /**
  132. * Recache the classified newswire on a page.
  133. */
  134. public function regenerateClassifiedNewswire($page_name, $category_list, $items_per_newswire_section) {
  135. $newswire_db_class = new NewswireDB();
  136. $newswire_cache_file_name = "newswire_{$page_name}_classified";
  137. if (isset($GLOBALS['part_of_bulk_operation']) && $GLOBALS['part_of_bulk_operation'] == 1 && count($category_list) != 0) {
  138. $this->markNewswireFileForRegenerateOnView($newswire_cache_file_name);
  139. }
  140. else {
  141. $newswire_list = $newswire_db_class->getClassifiedListForCategories($category_list, $items_per_newswire_section);
  142. $this->regenerateNewswireGivenResultData($newswire_cache_file_name, $newswire_list);
  143. }
  144. }
  145. /**
  146. * Recache the all newswire on a page.
  147. */
  148. public function regenerateAllNewswireForCategories($page_name, $category_list, $items_per_newswire_section) {
  149. $newswire_db_class = new NewswireDB();
  150. $newswire_cache_file_name = "newswire_{$page_name}_all";
  151. if (!empty($GLOBALS['part_of_bulk_operation']) && !count($category_list)) {
  152. $this->markNewswireFileForRegenerateOnView($newswire_cache_file_name);
  153. }
  154. else {
  155. $newswire_list = $newswire_db_class->getAllListForCategories($category_list, $items_per_newswire_section);
  156. $this->regenerateNewswireGivenResultData($newswire_cache_file_name, $newswire_list);
  157. }
  158. }
  159. /**
  160. * Helper method used in saving of any newswire.
  161. */
  162. public function regenerateNewswireGivenResultData($newswire_cache_file_name, $result_data) {
  163. $file_path = CACHE_PATH . '/newswires/' . $newswire_cache_file_name;
  164. $newswire_renderer_class = new NewswireRenderer();
  165. $html = $newswire_renderer_class->getNewswireRowsHtml($result_data);
  166. $cache_class = new Cache();
  167. if ($html != '') {
  168. $cache_class->cacheFile($file_path, $html);
  169. }
  170. }
  171. /**
  172. * Adds hidden field to newswire section forcing it to be regenerated.
  173. *
  174. * This is mainly needed so updates to posts that are on a lot of pages
  175. * dont take forever to save. Instead of regenerating the newswires when an
  176. * item in them changes this just sets the newswire itself to have a tag in it
  177. * telling the cache code that loads it to rerun the newswire section on next
  178. * viewing.
  179. */
  180. public function markNewswireFileForRegenerateOnView($cachefile_name) {
  181. $file_util = new FileUtil();
  182. $str = $file_util->loadFileAsString(CACHE_PATH . '/newswires/' . $cachefile_name);
  183. $str = ' <!--regen-->' . $str;
  184. $file_util->saveStringAsFile(CACHE_PATH . '/newswires/' . $cachefile_name, $str);
  185. }
  186. /**
  187. * Loads the highlighted local section of the newswire for a given page.
  188. *
  189. * The code first tries to load from cache but if it is marked dirty it will
  190. * rerun the newswire.
  191. */
  192. public function loadLocalNewswireForPage($page_id) {
  193. $cachefile_name = "newswire_page{$page_id}_local";
  194. $file_path = CACHE_PATH . '/newswires/' . $cachefile_name;
  195. $file_util = new FileUtil();
  196. $str = $file_util->loadFileAsString($file_path);
  197. if (strpos($str, '<!--regen-->') > 0) {
  198. $this->regenerateNewswireForPageHelper($page_id, NEWS_ITEM_STATUS_ID_LOCAL_HIGHLIGHTED, NEWS_ITEM_STATUS_ID_LOCAL_HIGHLIGHTED);
  199. $str = $file_util->loadFileAsString($file_path);
  200. }
  201. return $str;
  202. }
  203. /**
  204. * Loads the highlighted non-local (ie global) section of the newswire.
  205. *
  206. * The code first tries to load from cache but if it is marked dirty it will
  207. * rerun the newswire.
  208. */
  209. public function loadNonlocalNewswireForPage($page_id) {
  210. $cachefile_name = "newswire_page{$page_id}_nonlocal";
  211. $file_path = CACHE_PATH . '/newswires/' . $cachefile_name;
  212. $file_util = new FileUtil();
  213. $str = $file_util->loadFileAsString($file_path);
  214. if (strpos($str, '<!--regen-->') > 0) {
  215. $this->regenerateNewswireForPageHelper($page_id, NEWS_ITEM_STATUS_ID_LOCAL_HIGHLIGHTED, NEWS_ITEM_STATUS_ID_LOCAL_HIGHLIGHTED);
  216. $str = $file_util->loadFileAsString($file_path);
  217. }
  218. return $str;
  219. }
  220. /**
  221. * Loads the "other/breaking" section of the newswire for a given page.
  222. *
  223. * The code first tries to load from cache but if it is marked dirty it will
  224. * rerun the newswire.
  225. */
  226. public function loadOtherNewswireForPage($page_id) {
  227. $cachefile_name = "newswire_page{$page_id}_other";
  228. $file_path = CACHE_PATH . '/newswires/' . $cachefile_name;
  229. $file_util = new FileUtil();
  230. $str = $file_util->loadFileAsString($file_path);
  231. if (strpos($str, '<!--regen-->') > 0) {
  232. $this->regenerateNewswireForPageHelper($page_id, NEWS_ITEM_STATUS_ID_LOCAL_HIGHLIGHTED, NEWS_ITEM_STATUS_ID_LOCAL_HIGHLIGHTED);
  233. $str = $file_util->loadFileAsString($file_path);
  234. }
  235. return $str;
  236. }
  237. /**
  238. * Loads the classified newswire for a given page.
  239. *
  240. * This newswire is mainly used by international pages on Indybay
  241. * it has no sections and displays anything that has been classified as
  242. * editors to new anything but OTHER, NEW, HIDDEN or QUESTIONABLE (hidden or
  243. * unhidden).
  244. */
  245. public function loadClassifiedNewswireForPage($page_id) {
  246. $cachefile_name = "newswire_page{$page_id}_classified";
  247. $file_path = CACHE_PATH . '/newswires/' . $cachefile_name;
  248. $file_util = new FileUtil();
  249. $str = $file_util->loadFileAsString($file_path);
  250. if (strpos($str, '<!--regen-->') > 0) {
  251. $this->regenerateNewswireForPageHelper($page_id, NEWS_ITEM_STATUS_ID_LOCAL_HIGHLIGHTED, NEWS_ITEM_STATUS_ID_LOCAL_HIGHLIGHTED);
  252. $str = $file_util->loadFileAsString($file_path);
  253. }
  254. return $str;
  255. }
  256. /**
  257. * Loads the full newswire for a given page.
  258. *
  259. * This newswire is not a part of Indybay it has no sections and displays
  260. * anything that has not been classified as editors as HIDDEN or QUESTIONABLE
  261. * HIDDEN.
  262. */
  263. public function loadAllNewswireForPage($page_id) {
  264. $cachefile_name = "newswire_page{$page_id}_all";
  265. $file_path = CACHE_PATH . '/newswires/' . $cachefile_name;
  266. $file_util = new FileUtil();
  267. $str = $file_util->loadFileAsString($file_path);
  268. if (strpos($str, '<!--regen-->') > 0) {
  269. $this->regenerateNewswireForPageHelper($page_id, NEWS_ITEM_STATUS_ID_OTHER, NEWS_ITEM_STATUS_ID_OTHER);
  270. $str = $file_util->loadFileAsString($file_path);
  271. }
  272. return $str;
  273. }
  274. }