ReferenceLinkDB.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Populates a reference datastore to test LinkDB
  4. */
  5. class ReferenceLinkDB
  6. {
  7. public static $NB_LINKS_TOTAL = 8;
  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. 8,
  38. 'Free as in Freedom 2.0 @website',
  39. 'https://static.fsf.org/nosvn/faif-2.0.pdf',
  40. 'Richard Stallman and the Free Software Revolution. Read this. #hashtag',
  41. 0,
  42. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'),
  43. 'free gnu software stallman -exclude stuff hashtag',
  44. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160803_093033')
  45. );
  46. $this->addLink(
  47. 7,
  48. 'MediaGoblin',
  49. 'http://mediagoblin.org/',
  50. 'A free software media publishing platform #hashtagOther',
  51. 0,
  52. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
  53. 'gnu media web .hidden hashtag',
  54. null,
  55. 'IuWvgA'
  56. );
  57. $this->addLink(
  58. 6,
  59. 'w3c-markup-validator',
  60. 'https://dvcs.w3.org/hg/markup-validator/summary',
  61. 'Mercurial repository for the W3C Validator #private',
  62. 1,
  63. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20141125_084734'),
  64. 'css html w3c web Mercurial'
  65. );
  66. $this->addLink(
  67. 4,
  68. 'UserFriendly - Web Designer',
  69. 'http://ars.userfriendly.org/cartoons/?id=20121206',
  70. 'Naming conventions... #private',
  71. 0,
  72. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'),
  73. 'dev cartoon web'
  74. );
  75. $this->addLink(
  76. 1,
  77. 'UserFriendly - Samba',
  78. 'http://ars.userfriendly.org/cartoons/?id=20010306',
  79. 'Tropical printing',
  80. 0,
  81. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'),
  82. 'samba cartoon web'
  83. );
  84. $this->addLink(
  85. 0,
  86. 'Geek and Poke',
  87. 'http://geek-and-poke.com/',
  88. '',
  89. 1,
  90. DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'),
  91. 'dev cartoon tag1 tag2 tag3 tag4 '
  92. );
  93. }
  94. /**
  95. * Adds a new link
  96. */
  97. protected function addLink($id, $title, $url, $description, $private, $date, $tags, $updated = '', $shorturl = '')
  98. {
  99. $link = array(
  100. 'id' => $id,
  101. 'title' => $title,
  102. 'url' => $url,
  103. 'description' => $description,
  104. 'private' => $private,
  105. 'tags' => $tags,
  106. 'created' => $date,
  107. 'updated' => $updated,
  108. 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id),
  109. );
  110. $this->_links[$id] = $link;
  111. if ($private) {
  112. $this->_privateCount++;
  113. return;
  114. }
  115. $this->_publicCount++;
  116. }
  117. /**
  118. * Writes data to the datastore
  119. */
  120. public function write($filename)
  121. {
  122. file_put_contents(
  123. $filename,
  124. '<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>'
  125. );
  126. }
  127. /**
  128. * Returns the number of links in the reference data
  129. */
  130. public function countLinks()
  131. {
  132. return $this->_publicCount + $this->_privateCount;
  133. }
  134. /**
  135. * Returns the number of public links in the reference data
  136. */
  137. public function countPublicLinks()
  138. {
  139. return $this->_publicCount;
  140. }
  141. /**
  142. * Returns the number of private links in the reference data
  143. */
  144. public function countPrivateLinks()
  145. {
  146. return $this->_privateCount;
  147. }
  148. public function getLinks()
  149. {
  150. return $this->_links;
  151. }
  152. /**
  153. * Setter to override link creation.
  154. *
  155. * @param array $links List of links.
  156. */
  157. public function setLinks($links)
  158. {
  159. $this->_links = $links;
  160. }
  161. }