ReferenceLinkDB.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Populates a reference datastore to test LinkDB
  4. */
  5. class ReferenceLinkDB
  6. {
  7. public static $NB_LINKS_TOTAL = 11;
  8. private $_links = array();
  9. private $_publicCount = 0;
  10. private $_privateCount = 0;
  11. /**
  12. * Populates the test DB with reference data
  13. */
  14. public function __construct()
  15. {
  16. $this->addLink(
  17. 11,
  18. 'Pined older',
  19. '?PCRizQ',
  20. 'This is an older pinned link',
  21. 0,
  22. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100309_101010'),
  23. '',
  24. null,
  25. 'PCRizQ',
  26. true
  27. );
  28. $this->addLink(
  29. 10,
  30. 'Pined',
  31. '?0gCTjQ',
  32. 'This is a pinned link',
  33. 0,
  34. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121207_152312'),
  35. '',
  36. null,
  37. '0gCTjQ',
  38. true
  39. );
  40. $this->addLink(
  41. 41,
  42. 'Link title: @website',
  43. '?WDWyig',
  44. 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
  45. 0,
  46. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'),
  47. 'sTuff',
  48. null,
  49. 'WDWyig'
  50. );
  51. $this->addLink(
  52. 42,
  53. 'Note: I have a big ID but an old date',
  54. '?WDWyig',
  55. 'Used to test links reordering.',
  56. 0,
  57. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100310_101010'),
  58. 'ut'
  59. );
  60. $this->addLink(
  61. 9,
  62. 'PSR-2: Coding Style Guide',
  63. 'http://www.php-fig.org/psr/psr-2/',
  64. 'This guide extends and expands on PSR-1, the basic coding standard.',
  65. 0,
  66. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_152312'),
  67. ''
  68. );
  69. $this->addLink(
  70. 8,
  71. 'Free as in Freedom 2.0 @website',
  72. 'https://static.fsf.org/nosvn/faif-2.0.pdf',
  73. 'Richard Stallman and the Free Software Revolution. Read this. #hashtag',
  74. 0,
  75. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'),
  76. 'free gnu software stallman -exclude stuff hashtag',
  77. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160803_093033')
  78. );
  79. $this->addLink(
  80. 7,
  81. 'MediaGoblin',
  82. 'http://mediagoblin.org/',
  83. 'A free software media publishing platform #hashtagOther',
  84. 0,
  85. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
  86. 'gnu media web .hidden hashtag',
  87. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130615_184230'),
  88. 'IuWvgA'
  89. );
  90. $this->addLink(
  91. 6,
  92. 'w3c-markup-validator',
  93. 'https://dvcs.w3.org/hg/markup-validator/summary',
  94. 'Mercurial repository for the W3C Validator #private',
  95. 1,
  96. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20141125_084734'),
  97. 'css html w3c web Mercurial'
  98. );
  99. $this->addLink(
  100. 4,
  101. 'UserFriendly - Web Designer',
  102. 'http://ars.userfriendly.org/cartoons/?id=20121206',
  103. 'Naming conventions... #private',
  104. 0,
  105. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'),
  106. 'dev cartoon web'
  107. );
  108. $this->addLink(
  109. 1,
  110. 'UserFriendly - Samba',
  111. 'http://ars.userfriendly.org/cartoons/?id=20010306',
  112. 'Tropical printing',
  113. 0,
  114. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'),
  115. 'samba cartoon web'
  116. );
  117. $this->addLink(
  118. 0,
  119. 'Geek and Poke',
  120. 'http://geek-and-poke.com/',
  121. '',
  122. 1,
  123. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'),
  124. 'dev cartoon tag1 tag2 tag3 tag4 '
  125. );
  126. }
  127. /**
  128. * Adds a new link
  129. */
  130. protected function addLink(
  131. $id,
  132. $title,
  133. $url,
  134. $description,
  135. $private,
  136. $date,
  137. $tags,
  138. $updated = '',
  139. $shorturl = '',
  140. $pinned = false)
  141. {
  142. $link = array(
  143. 'id' => $id,
  144. 'title' => $title,
  145. 'url' => $url,
  146. 'description' => $description,
  147. 'private' => $private,
  148. 'tags' => $tags,
  149. 'created' => $date,
  150. 'updated' => $updated,
  151. 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id),
  152. 'sticky' => $pinned
  153. );
  154. $this->_links[$id] = $link;
  155. if ($private) {
  156. $this->_privateCount++;
  157. return;
  158. }
  159. $this->_publicCount++;
  160. }
  161. /**
  162. * Writes data to the datastore
  163. */
  164. public function write($filename)
  165. {
  166. $this->reorder();
  167. file_put_contents(
  168. $filename,
  169. '<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>'
  170. );
  171. }
  172. /**
  173. * Reorder links by creation date (newest first).
  174. *
  175. * Also update the urls and ids mapping arrays.
  176. *
  177. * @param string $order ASC|DESC
  178. */
  179. public function reorder($order = 'DESC')
  180. {
  181. // backward compatibility: ignore reorder if the the `created` field doesn't exist
  182. if (! isset(array_values($this->_links)[0]['created'])) {
  183. return;
  184. }
  185. $order = $order === 'ASC' ? -1 : 1;
  186. // Reorder array by dates.
  187. usort($this->_links, function($a, $b) use ($order) {
  188. if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) {
  189. return $a['sticky'] ? -1 : 1;
  190. }
  191. return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
  192. });
  193. }
  194. /**
  195. * Returns the number of links in the reference data
  196. */
  197. public function countLinks()
  198. {
  199. return $this->_publicCount + $this->_privateCount;
  200. }
  201. /**
  202. * Returns the number of public links in the reference data
  203. */
  204. public function countPublicLinks()
  205. {
  206. return $this->_publicCount;
  207. }
  208. /**
  209. * Returns the number of private links in the reference data
  210. */
  211. public function countPrivateLinks()
  212. {
  213. return $this->_privateCount;
  214. }
  215. /**
  216. * Returns the number of links without tag
  217. */
  218. public function countUntaggedLinks()
  219. {
  220. $cpt = 0;
  221. foreach ($this->_links as $link) {
  222. if (empty($link['tags'])) {
  223. ++$cpt;
  224. }
  225. }
  226. return $cpt;
  227. }
  228. public function getLinks()
  229. {
  230. $this->reorder();
  231. return $this->_links;
  232. }
  233. /**
  234. * Setter to override link creation.
  235. *
  236. * @param array $links List of links.
  237. */
  238. public function setLinks($links)
  239. {
  240. $this->_links = $links;
  241. }
  242. }