SitemapPlugin.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 PLUGIN_VERSION = '2.0.0';
  48. const USERS_PER_MAP = 50000;
  49. const NOTICES_PER_MAP = 50000;
  50. /**
  51. * Add sitemap-related information at the end of robots.txt
  52. *
  53. * @param Action $action Action being run
  54. *
  55. * @return boolean hook value.
  56. */
  57. function onEndRobotsTxt($action)
  58. {
  59. $url = common_local_url('sitemapindex');
  60. print "\nSitemap: $url\n";
  61. return true;
  62. }
  63. /**
  64. * Map URLs to actions
  65. *
  66. * @param URLMapper $m path-to-action mapper
  67. *
  68. * @return boolean hook value; true means continue processing, false means stop.
  69. */
  70. public function onRouterInitialized(URLMapper $m)
  71. {
  72. $m->connect('sitemapindex.xml',
  73. ['action' => 'sitemapindex']);
  74. $m->connect('notice-sitemap-:year-:month-:day-:index.xml',
  75. ['action' => 'noticesitemap'],
  76. ['year' => '[0-9]{4}',
  77. 'month' => '[01][0-9]',
  78. 'day' => '[0123][0-9]',
  79. 'index' => '[1-9][0-9]*']);
  80. $m->connect('user-sitemap-:year-:month-:day-:index.xml',
  81. ['action' => 'usersitemap'),
  82. ['year' => '[0-9]{4}',
  83. 'month' => '[01][0-9]',
  84. 'day' => '[0123][0-9]',
  85. 'index' => '[1-9][0-9]*']);
  86. $m->connect('panel/sitemap',
  87. ['action' => 'sitemapadminpanel']);
  88. return true;
  89. }
  90. /**
  91. * Meta tags for "claiming" a site
  92. *
  93. * We add extra meta tags that search engines like Yahoo! and Bing
  94. * require to let you claim your site.
  95. *
  96. * @param Action $action Action being executed
  97. *
  98. * @return boolean hook value.
  99. */
  100. function onStartShowHeadElements($action)
  101. {
  102. $actionName = $action->trimmed('action');
  103. $singleUser = common_config('singleuser', 'enabled');
  104. // Different "top" pages if it's single user or not
  105. if (($singleUser && $actionName == 'showstream') ||
  106. (!$singleUser && $actionName == 'public')) {
  107. $keys = array('yahookey' => 'y_key',
  108. 'bingkey' => 'msvalidate.01'); // XXX: is this the same for all sites?
  109. foreach ($keys as $config => $metaname) {
  110. $content = common_config('sitemap', $config);
  111. if (!empty($content)) {
  112. $action->element('meta', array('name' => $metaname,
  113. 'content' => $content));
  114. }
  115. }
  116. }
  117. return true;
  118. }
  119. /**
  120. * Database schema setup
  121. *
  122. * We cache some data persistently to avoid overlong queries.
  123. *
  124. * @see Sitemap_user_count
  125. * @see Sitemap_notice_count
  126. *
  127. * @return boolean hook value; true means continue processing, false means stop.
  128. */
  129. function onCheckSchema()
  130. {
  131. $schema = Schema::get();
  132. $schema->ensureTable('sitemap_user_count', Sitemap_user_count::schemaDef());
  133. $schema->ensureTable('sitemap_notice_count', Sitemap_notice_count::schemaDef());
  134. return true;
  135. }
  136. function onEndAdminPanelNav($menu) {
  137. if (AdminPanelAction::canAdmin('sitemap')) {
  138. // TRANS: Menu item title/tooltip
  139. $menu_title = _m('Sitemap configuration');
  140. // TRANS: Menu item for site administration
  141. $menu->out->menuItem(common_local_url('sitemapadminpanel'), _m('MENU','Sitemap'),
  142. $menu_title, $action_name == 'sitemapadminpanel', 'nav_sitemap_admin_panel');
  143. }
  144. return true;
  145. }
  146. /**
  147. * Provide plugin version information.
  148. *
  149. * This data is used when showing the version page.
  150. *
  151. * @param array &$versions array of version data arrays; see EVENTS.txt
  152. *
  153. * @return boolean hook value
  154. */
  155. function onPluginVersion(array &$versions)
  156. {
  157. $url = 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Sitemap';
  158. $versions[] = array('name' => 'Sitemap',
  159. 'version' => self::PLUGIN_VERSION,
  160. 'author' => 'Evan Prodromou',
  161. 'homepage' => $url,
  162. 'rawdescription' =>
  163. // TRANS: Plugin description.
  164. _m('This plugin allows creation of sitemaps for Bing and Yahoo!.'));
  165. return true;
  166. }
  167. }