File modules/cms/generator/filter/SitemapFilter.class.php
Last commit: Tue Feb 14 00:23:13 2023 +0100 Jan Dankert New filters: Robots (for robots.txt) and Sitemap.
1 <?php 2 namespace cms\generator\filter; 3 4 use cms\generator\PageContext; 5 use cms\model\BaseObject; 6 use cms\model\Folder; 7 use cms\model\Page; 8 use util\YAML; 9 10 /** 11 * Filter for creating a sitemap. 12 * 13 * @author Jan Dankert 14 */ 15 class SitemapFilter extends AbstractFilter 16 { 17 18 public function filter($value) 19 { 20 $config = YAML::parse( $value ); 21 $output = ''; 22 $all = $this->getAll(); 23 24 $thispage = new BaseObject( $this->context->getObjectId() ); 25 $thispage->load(); // Seite laden 26 $project = $thispage->getProject(); 27 $lids = $project->getLanguageISOCodes(); 28 29 if ( @$config['format'] == 'text' ) { 30 foreach ( $all as $page ) 31 $output .= $this->context->getLinkScheme()->linkToObject( new BaseObject((new BaseObject( $this->context->getObjectId() ))->load()->getProject()->getRootObjectId()),(new BaseObject($page))->load() ); 32 } else { 33 // Fallback: XML 34 $output .= '<?xml version="1.0" encoding="UTF-8"?>'."\n"; 35 $output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">'."\n"; 36 foreach ( $all as $page ) { 37 38 $targetPage = (new BaseObject($page))->load(); 39 $output .= '<url>'."\n"; 40 $output .= '<loc>'; 41 42 $output .= $this->context->getLinkScheme()->linkToObject( new BaseObject((new BaseObject( $this->context->getObjectId() ))->load()->getProject()->getRootObjectId()),$targetPage ); 43 $output .= '</loc>'."\n"; 44 foreach( $lids as $lid => $isoCode ) 45 { 46 $pageContext = new PageContext( $this->context->getObjectId(),$this->context->scheme ); 47 $pageContext->languageId = $lid; 48 $output .= '<xhtml:link'; 49 $output .= ' rel="alternative"'; 50 $output .= ' hreflang="'.$isoCode.'"'; 51 $output .= ' href="'.$pageContext->getLinkScheme()->linkToObject( new BaseObject((new BaseObject( $this->context->getObjectId() ))->load()->getProject()->getRootObjectId()),$targetPage ).'"'; 52 $output .= '/>'."\n"; 53 } 54 $output .= '</url>'."\n"; 55 } 56 $output .= '</urlset>'; 57 58 } 59 return $output; 60 } 61 62 63 protected function getAll() 64 { 65 // Erstellen eines Untermenues 66 67 // Ermitteln der aktuellen Seite 68 $thispage = new BaseObject( $this->context->getObjectId() ); 69 $thispage->load(); // Seite laden 70 71 // uebergeordneter Ordner dieser Seite 72 $rootFolder = new Folder( $thispage->getProject()->getRootObjectId() ); 73 $allPages = $rootFolder->getObjectIds(); 74 return $allPages; 75 } 76 77 protected function showFolder( $oid ) 78 { 79 // uebergeordneter Ordner dieser Seite 80 $f = new Folder($oid); 81 82 // Schleife ueber alle Objekte im aktuellen Ordner 83 foreach ($f->getObjectIds() as $id) { 84 $o = new BaseObject($id); 85 $o->load(); 86 87 // Ordner 88 if ($o->isFolder) { 89 $this->output('<li><strong>' . $o->getNameForLanguage($this->pageContext->languageId)->name . '</strong><br/>'); 90 $this->output('<ul>'); 91 $this->showFolder($id); // Rekursiver Aufruf dieser Methode 92 $this->output('</ul></li>'); 93 } 94 95 // Seiten und Verkn?fpungen 96 if ($o->isPage || $o->isLink) { 97 // Wenn aktuelle Seite, dann markieren, sonst Link 98 if ($this->getObjectId() == $id) { 99 // aktuelle Seite 100 $this->output('<li><strong>' . $o->getNameForLanguage($this->pageContext->languageId)->name . '</strong></li>'); 101 } else { 102 // Link erzeugen 103 $this->output('<li><a href="' . $this->pathToObject($id) . '">' . $o->getNameForLanguage($this->pageContext->languageId)->name . '</a></li>'); 104 } 105 } 106 } 107 } 108 }
Downloadmodules/cms/generator/filter/SitemapFilter.class.php
History Tue, 14 Feb 2023 00:23:13 +0100 Jan Dankert New filters: Robots (for robots.txt) and Sitemap.