ReferenceLinkDB.php 6.2 KB

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