SitemapPlugin.php 5.7 KB

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