/ojs/ojs-2.3.2-1/pages/sitemap/SitemapHandler.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite · PHP · 143 lines · 83 code · 20 blank · 40 comment · 5 complexity · 99cbf184f26bac1c0995f9892b1d861f MD5 · raw file

  1. <?php
  2. /**
  3. * @file SitemapHandler.inc.php
  4. *
  5. * Copyright (c) 2003-2008 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class SitemapHandler
  9. * @ingroup pages_sitemap
  10. *
  11. * @brief Produce a sitemap in XML format for submitting to search engines.
  12. */
  13. // $Id$
  14. import('xml.XMLCustomWriter');
  15. import('handler.Handler');
  16. define('SITEMAP_XSD_URL', 'http://www.sitemaps.org/schemas/sitemap/0.9');
  17. class SitemapHandler extends Handler {
  18. /**
  19. * Generate an XML sitemap for webcrawlers
  20. * Creates a sitemap index if in site context, else creates a sitemap
  21. */
  22. function index() {
  23. if (Request::getRequestedJournalPath() == 'index') {
  24. $doc = SitemapHandler::createSitemapIndex();
  25. header("Content-Type: application/xml");
  26. header("Cache-Control: private");
  27. header("Content-Disposition: inline; filename=\"sitemap_index.xml\"");
  28. XMLCustomWriter::printXML($doc);
  29. } else {
  30. $doc = SitemapHandler::createJournalSitemap();
  31. header("Content-Type: application/xml");
  32. header("Cache-Control: private");
  33. header("Content-Disposition: inline; filename=\"sitemap.xml\"");
  34. XMLCustomWriter::printXML($doc);
  35. }
  36. }
  37. /**
  38. * Construct a sitemap index listing each journal's individual sitemap
  39. * @return XMLNode
  40. */
  41. function createSitemapIndex() {
  42. $journalDao =& DAORegistry::getDAO('JournalDAO');
  43. $doc =& XMLCustomWriter::createDocument();
  44. $root =& XMLCustomWriter::createElement($doc, 'sitemapindex');
  45. XMLCustomWriter::setAttribute($root, 'xmlns', SITEMAP_XSD_URL);
  46. $journals =& $journalDao->getJournals();
  47. while ($journal =& $journals->next()) {
  48. $sitemapUrl = Request::url($journal->getPath(), 'sitemap');
  49. $sitemap =& XMLCustomWriter::createElement($doc, 'sitemap');
  50. XMLCustomWriter::createChildWithText($doc, $sitemap, 'loc', $sitemapUrl, false);
  51. XMLCustomWriter::appendChild($root, $sitemap);
  52. unset($journal);
  53. }
  54. XMLCustomWriter::appendChild($doc, $root);
  55. return $doc;
  56. }
  57. /**
  58. * Construct the sitemap
  59. * @return XMLNode
  60. */
  61. function createJournalSitemap() {
  62. $issueDao =& DAORegistry::getDAO('IssueDAO');
  63. $publishedArticleDao =& DAORegistry::getDAO('PublishedArticleDAO');
  64. $galleyDao =& DAORegistry::getDAO('ArticleGalleyDAO');
  65. $journal =& Request::getJournal();
  66. $journalId = $journal->getJournalId();
  67. $doc =& XMLCustomWriter::createDocument();
  68. $root =& XMLCustomWriter::createElement($doc, 'urlset');
  69. XMLCustomWriter::setAttribute($root, 'xmlns', SITEMAP_XSD_URL);
  70. // Journal home
  71. XMLCustomWriter::appendChild($root, SitemapHandler::createUrlTree($doc, Request::url($journal->getPath(),'index','index')));
  72. // About page
  73. XMLCustomWriter::appendChild($root, SitemapHandler::createUrlTree($doc, Request::url($journal->getPath(), 'about')));
  74. XMLCustomWriter::appendChild($root, SitemapHandler::createUrlTree($doc, Request::url($journal->getPath(), 'about', 'editorialTeam')));
  75. XMLCustomWriter::appendChild($root, SitemapHandler::createUrlTree($doc, Request::url($journal->getPath(), 'about', 'editorialPolicies')));
  76. XMLCustomWriter::appendChild($root, SitemapHandler::createUrlTree($doc, Request::url($journal->getPath(), 'about', 'submissions')));
  77. XMLCustomWriter::appendChild($root, SitemapHandler::createUrlTree($doc, Request::url($journal->getPath(), 'about', 'siteMap')));
  78. XMLCustomWriter::appendChild($root, SitemapHandler::createUrlTree($doc, Request::url($journal->getPath(), 'about', 'aboutThisPublishingSystem')));
  79. // Search
  80. XMLCustomWriter::appendChild($root, SitemapHandler::createUrlTree($doc, Request::url($journal->getPath(), 'search')));
  81. XMLCustomWriter::appendChild($root, SitemapHandler::createUrlTree($doc, Request::url($journal->getPath(), 'search', 'authors')));
  82. XMLCustomWriter::appendChild($root, SitemapHandler::createUrlTree($doc, Request::url($journal->getPath(), 'search', 'titles')));
  83. // Issues
  84. XMLCustomWriter::appendChild($root, SitemapHandler::createUrlTree($doc, Request::url($journal->getPath(), 'issue', 'current')));
  85. XMLCustomWriter::appendChild($root, SitemapHandler::createUrlTree($doc, Request::url($journal->getPath(), 'issue', 'archive')));
  86. $publishedIssues =& $issueDao->getPublishedIssues($journalId);
  87. while ($issue =& $publishedIssues->next()) {
  88. XMLCustomWriter::appendChild($root, SitemapHandler::createUrlTree($doc, Request::url($journal->getPath(), 'issue', 'view', $issue->getIssueId())));
  89. // Articles for issue
  90. $articles = $publishedArticleDao->getPublishedArticles($issue->getIssueId());
  91. foreach($articles as $article) {
  92. // Abstract
  93. XMLCustomWriter::appendChild($root, SitemapHandler::createUrlTree($doc, Request::url($journal->getPath(), 'article', 'view', array($article->getArticleId()))));
  94. // Galley files
  95. $galleys = $galleyDao->getGalleysByArticle($article->getArticleId());
  96. foreach ($galleys as $galley) {
  97. XMLCustomWriter::appendChild($root, SitemapHandler::createUrlTree($doc, Request::url($journal->getPath(), 'article', 'view', array($article->getArticleId(), $galley->getGalleyId()))));
  98. }
  99. }
  100. unset($issue);
  101. }
  102. XMLCustomWriter::appendChild($doc, $root);
  103. return $doc;
  104. }
  105. /**
  106. * Create a url entry with children
  107. * @param $doc XMLNode Reference to the XML document object
  108. * @param $loc string URL of page (required)
  109. * @param $lastmod string Last modification date of page (optional)
  110. * @param $changefreq Frequency of page modifications (optional)
  111. * @param $priority string Subjective priority assesment of page (optional)
  112. * @return XMLNode
  113. */
  114. function createUrlTree(&$doc, $loc, $lastmod = null, $changefreq = null, $priority = null) {
  115. $url =& XMLCustomWriter::createElement($doc, 'url');
  116. XMLCustomWriter::createChildWithText($doc, $url, htmlentities('loc'), $loc, false);
  117. XMLCustomWriter::createChildWithText($doc, $url, 'lastmod', $lastmod, false);
  118. XMLCustomWriter::createChildWithText($doc, $url, 'changefreq', $changefreq, false);
  119. XMLCustomWriter::createChildWithText($doc, $url, 'priority', $priority, false);
  120. return $url;
  121. }
  122. }
  123. ?>