SitemapPlugin.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * Creates a dynamic sitemap for a StatusNet site
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Sample
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2010 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Sitemap plugin
  37. *
  38. * @category Sample
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2010 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class SitemapPlugin extends Plugin
  46. {
  47. const USERS_PER_MAP = 50000;
  48. const NOTICES_PER_MAP = 50000;
  49. /**
  50. * Add sitemap-related information at the end of robots.txt
  51. *
  52. * @param Action $action Action being run
  53. *
  54. * @return boolean hook value.
  55. */
  56. function onEndRobotsTxt($action)
  57. {
  58. $url = common_local_url('sitemapindex');
  59. print "\nSitemap: $url\n";
  60. return true;
  61. }
  62. /**
  63. * Map URLs to actions
  64. *
  65. * @param URLMapper $m path-to-action mapper
  66. *
  67. * @return boolean hook value; true means continue processing, false means stop.
  68. */
  69. public function onRouterInitialized(URLMapper $m)
  70. {
  71. $m->connect('sitemapindex.xml',
  72. array('action' => 'sitemapindex'));
  73. $m->connect('notice-sitemap-:year-:month-:day-:index.xml',
  74. array('action' => 'noticesitemap'),
  75. array('year' => '[0-9]{4}',
  76. 'month' => '[01][0-9]',
  77. 'day' => '[0123][0-9]',
  78. 'index' => '[1-9][0-9]*'));
  79. $m->connect('user-sitemap-:year-:month-:day-:index.xml',
  80. array('action' => 'usersitemap'),
  81. array('year' => '[0-9]{4}',
  82. 'month' => '[01][0-9]',
  83. 'day' => '[0123][0-9]',
  84. 'index' => '[1-9][0-9]*'));
  85. $m->connect('panel/sitemap',
  86. array('action' => 'sitemapadminpanel'));
  87. return true;
  88. }
  89. /**
  90. * Meta tags for "claiming" a site
  91. *
  92. * We add extra meta tags that search engines like Yahoo! and Bing
  93. * require to let you claim your site.
  94. *
  95. * @param Action $action Action being executed
  96. *
  97. * @return boolean hook value.
  98. */
  99. function onStartShowHeadElements($action)
  100. {
  101. $actionName = $action->trimmed('action');
  102. $singleUser = common_config('singleuser', 'enabled');
  103. // Different "top" pages if it's single user or not
  104. if (($singleUser && $actionName == 'showstream') ||
  105. (!$singleUser && $actionName == 'public')) {
  106. $keys = array('yahookey' => 'y_key',
  107. 'bingkey' => 'msvalidate.01'); // XXX: is this the same for all sites?
  108. foreach ($keys as $config => $metaname) {
  109. $content = common_config('sitemap', $config);
  110. if (!empty($content)) {
  111. $action->element('meta', array('name' => $metaname,
  112. 'content' => $content));
  113. }
  114. }
  115. }
  116. return true;
  117. }
  118. /**
  119. * Database schema setup
  120. *
  121. * We cache some data persistently to avoid overlong queries.
  122. *
  123. * @see Sitemap_user_count
  124. * @see Sitemap_notice_count
  125. *
  126. * @return boolean hook value; true means continue processing, false means stop.
  127. */
  128. function onCheckSchema()
  129. {
  130. $schema = Schema::get();
  131. $schema->ensureTable('sitemap_user_count', Sitemap_user_count::schemaDef());
  132. $schema->ensureTable('sitemap_notice_count', Sitemap_notice_count::schemaDef());
  133. return true;
  134. }
  135. function onEndAdminPanelNav($menu) {
  136. if (AdminPanelAction::canAdmin('sitemap')) {
  137. // TRANS: Menu item title/tooltip
  138. $menu_title = _m('Sitemap configuration');
  139. // TRANS: Menu item for site administration
  140. $menu->out->menuItem(common_local_url('sitemapadminpanel'), _m('MENU','Sitemap'),
  141. $menu_title, $action_name == 'sitemapadminpanel', 'nav_sitemap_admin_panel');
  142. }
  143. return true;
  144. }
  145. /**
  146. * Provide plugin version information.
  147. *
  148. * This data is used when showing the version page.
  149. *
  150. * @param array &$versions array of version data arrays; see EVENTS.txt
  151. *
  152. * @return boolean hook value
  153. */
  154. function onPluginVersion(array &$versions)
  155. {
  156. $url = 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Sitemap';
  157. $versions[] = array('name' => 'Sitemap',
  158. 'version' => GNUSOCIAL_VERSION,
  159. 'author' => 'Evan Prodromou',
  160. 'homepage' => $url,
  161. 'rawdescription' =>
  162. // TRANS: Plugin description.
  163. _m('This plugin allows creation of sitemaps for Bing and Yahoo!.'));
  164. return true;
  165. }
  166. }