Open Journal Systems  3.3.0
PKPSitemapHandler.inc.php
1 <?php
2 
16 import('classes.handler.Handler');
17 
18 define('SITEMAP_XSD_URL', 'https://www.sitemaps.org/schemas/sitemap/0.9');
19 
20 class PKPSitemapHandler extends Handler {
27  function index($args, $request) {
28  $context = $request->getContext();
29  if (!$context) {
30  $doc = $this->_createSitemapIndex($request);
31  header("Content-Type: application/xml");
32  header("Cache-Control: private");
33  header("Content-Disposition: inline; filename=\"sitemap_index.xml\"");
34  echo $doc->saveXml();
35  } else {
36  $doc = $this->_createContextSitemap($request);
37  header("Content-Type: application/xml");
38  header("Cache-Control: private");
39  header("Content-Disposition: inline; filename=\"sitemap.xml\"");
40  echo $doc->saveXml();
41  }
42  }
43 
49  function _createSitemapIndex($request) {
50  $contextDao = Application::getContextDAO();
51 
52  $doc = new DOMDocument('1.0', 'utf-8');
53  $root = $doc->createElement('sitemapindex');
54  $root->setAttribute('xmlns', SITEMAP_XSD_URL);
55 
56  $contexts = $contextDao->getAll(true);
57  while ($context = $contexts->next()) {
58  $sitemapUrl = $request->url($context->getPath(), 'sitemap');
59  $sitemap = $doc->createElement('sitemap');
60  $sitemap->appendChild($doc->createElement('loc', htmlspecialchars($sitemapUrl, ENT_COMPAT, 'UTF-8')));
61  $root->appendChild($sitemap);
62  }
63 
64  $doc->appendChild($root);
65  return $doc;
66  }
67 
73  function _createContextSitemap($request) {
74  $context = $request->getContext();
75  $contextId = $context->getId();
76 
77  $doc = new DOMDocument('1.0', 'utf-8');
78 
79  $root = $doc->createElement('urlset');
80  $root->setAttribute('xmlns', SITEMAP_XSD_URL);
81 
82  // Context home
83  $root->appendChild($this->_createUrlTree($doc, $request->url($context->getPath())));
84  // User register
85  if ($context->getData('disableUserReg') != 1) {
86  $root->appendChild($this->_createUrlTree($doc, $request->url($context->getPath(), 'user', 'register')));
87  }
88  // User login
89  $root->appendChild($this->_createUrlTree($doc, $request->url($context->getPath(), 'login')));
90  // Announcements
91  if ($context->getData('enableAnnouncements') == 1) {
92  $root->appendChild($this->_createUrlTree($doc, $request->url($context->getPath(), 'announcement')));
93  $announcementDao = DAORegistry::getDAO('AnnouncementDAO'); /* @var $announcementDao AnnouncementDAO */
94  $contextAssocType = Application::getContextAssocType();
95  $announcementsResult = $announcementDao->getByAssocId($contextAssocType, $contextId);
96  while ($announcement = $announcementsResult->next()) {
97  $root->appendChild($this->_createUrlTree($doc, $request->url($context->getPath(), 'announcement', 'view', $announcement->getId())));
98  }
99  }
100  // About: context
101  if (!empty($context->getData('about'))) {
102  $root->appendChild($this->_createUrlTree($doc, $request->url($context->getPath(), 'about')));
103  }
104  // About: submissions
105  $root->appendChild($this->_createUrlTree($doc, $request->url($context->getPath(), 'about', 'submissions')));
106  // About: editorial team
107  if (!empty($context->getData('editorialTeam'))) {
108  $root->appendChild($this->_createUrlTree($doc, $request->url($context->getPath(), 'about', 'editorialTeam')));
109  }
110  // About: contact
111  if (!empty($context->getData('mailingAddress')) || !empty($context->getData('contactName'))) {
112  $root->appendChild($this->_createUrlTree($doc, $request->url($context->getPath(), 'about', 'contact')));
113  }
114  // Custom pages (navigation menu items)
115  $navigationMenuItemDao = DAORegistry::getDAO('NavigationMenuItemDAO'); /* @var $navigationMenuItemDao NavigationMenuItemDAO */
116  $menuItemsResult = $navigationMenuItemDao->getByType(NMI_TYPE_CUSTOM, $contextId);
117  while ($menuItem = $menuItemsResult->next()) {
118  $root->appendChild($this->_createUrlTree($doc, $request->url($context->getPath(), $menuItem->getPath())));
119  }
120 
121  $doc->appendChild($root);
122 
123  return $doc;
124  }
125 
135  protected function _createUrlTree($doc, $loc, $lastmod = null, $changefreq = null, $priority = null) {
136  $url = $doc->createElement('url');
137  $url->appendChild($doc->createElement('loc', htmlspecialchars($loc, ENT_COMPAT, 'UTF-8')));
138  if ($lastmod) {
139  $url->appendChild($doc->createElement('lastmod', $lastmod));
140  }
141  if ($changefreq) {
142  $url->appendChild($doc->createElement('changefreq', $changefreq));
143  }
144  if ($priority) {
145  $url->appendChild($doc->createElement('priority', $priority));
146  }
147  return $url;
148  }
149 
150 }
151 
152 
Application\getContextDAO
static getContextDAO()
Definition: Application.inc.php:137
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
PKPSitemapHandler\_createUrlTree
_createUrlTree($doc, $loc, $lastmod=null, $changefreq=null, $priority=null)
Definition: PKPSitemapHandler.inc.php:135
PKPSitemapHandler\index
index($args, $request)
Definition: PKPSitemapHandler.inc.php:27
PKPSitemapHandler\_createContextSitemap
_createContextSitemap($request)
Definition: PKPSitemapHandler.inc.php:73
Application\getContextAssocType
static getContextAssocType()
Definition: Application.inc.php:199
PKPSitemapHandler
Produce a sitemap in XML format for submitting to search engines.
Definition: PKPSitemapHandler.inc.php:20
Handler
Base request handler application class.
Definition: Handler.inc.php:18
PKPSitemapHandler\_createSitemapIndex
_createSitemapIndex($request)
Definition: PKPSitemapHandler.inc.php:49