SitemapPlugin.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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!, Google, 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('googlekey' => 'google-site-verification',
  107. '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(&$versions)
  156. {
  157. $url = 'http://status.net/wiki/Plugin:Sitemap';
  158. $versions[] = array('name' => 'Sitemap',
  159. 'version' => GNUSOCIAL_VERSION,
  160. 'author' => 'Evan Prodromou',
  161. 'homepage' => $url,
  162. 'rawdescription' =>
  163. // TRANS: Plugin description.
  164. _m('This plugin allows creation of sitemaps for Bing, Yahoo! and Google.'));
  165. return true;
  166. }
  167. }