GlisWeb framework
_980.sitemap.php
Vai alla documentazione di questo file.
1 <?php
2 
18  // sitemap file
19  $sitemapFile = DIRECTORY_BASE . 'etc/sitemap/sitemap.' . $cf['site']['fqdn'] . '.xml';
20 
21  // controllo il percorso
22  checkFolder( dirname( $sitemapFile ) );
23 
24  // timestamp di modifica della sitemap
25  if( file_exists( $sitemapFile ) ) {
26  $cf['sitemap']['updated'] = @filemtime( $sitemapFile );
27  } else {
28  $cf['sitemap']['updated'] = 0;
29  }
30 
31  // verifico se la sitemap va aggiornata
32  if( $cf['sitemap']['updated'] < $cf['contents']['updated'] || defined( 'MEMCACHE_REFRESH' ) ) {
33 // if( true ) {
34 
35  // log
36  logWrite( 'sitemap scritta in quanto ' . $cf['contents']['updated'] . ' > ' . $cf['sitemap']['updated'], 'sitemap', LOG_DEBUG );
37 
38  // inizializzo l'array degli URL
39  $url = array();
40 
41  // inizializzo l'oggetto XML
42  $xml = new XMLWriter();
43 
44  // specifico il file di destinazione
45  $xml->openURI( $sitemapFile );
46 
47  // inizio il documento
48  $xml->startDocument( '1.0', 'UTF-8' );
49 
50  // attivo l'indentazione
51  $xml->setIndent( true );
52  $xml->setIndentString( ' ' );
53 
54  // root element
55  $xml->startElement( 'urlset' );
56  $xml->writeAttribute( 'xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9' );
57  $xml->writeAttribute( 'xmlns:xhtml', 'http://www.w3.org/1999/xhtml' );
58 // $xml->writeAttributeNs( 'xhtml', 'xmlns', 'http://www.w3.org/1999/xhtml', 'http://www.sitemaps.org/schemas/sitemap/0.9' );
59 
60 /*
61  // array della sitemap
62  $map = array();
63 
64  // urlset
65  $map['urlset']['@'] = array( 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9', 'xmlns|xhtml' => 'http://www.w3.org/1999/xhtml' );
66 */
67 
68  // scorro l'elenco delle pagine
69  foreach( $cf['localization']['languages'] as $lang ) {
70 
71  // scorro l'elenco delle pagine
72  foreach( $cf['contents']['pages'] as $id => $page ) {
73 
74  // verifico se la pagina va nella sitemap
75  if( isset( $page['sitemap'] ) && $page['sitemap'] == true ) {
76 
77  // verifico se la pagina corrente appartiene al sito corrente
78  if( true ) {
79 
80  // inizio l'elemento <url>
81  $xml->startElement( 'url' );
82 
83  // scrivo in un solo passaggio l'elemento <loc>
84  $xml->writeElement( 'loc', $page['url'][ $lang['ietf'] ] );
85 
86  // aggiungo l'URL
87  $url[] = $page['url'][ $lang['ietf'] ];
88 
89  // ciclo sugli altri linguaggi se il sito è multilingua
90  if( count( $cf['localization']['languages'] ) > 1 ) {
91  foreach( $cf['localization']['languages'] as $linklang ) {
92 
93  // inizio l'elemento <xhtml:link>
94  $xml->startElementNs( 'xhtml', 'link', NULL );
95 
96  // attributi di <xhtml:link>
97  $xml->writeAttribute( 'rel', 'alternate' );
98  $xml->writeAttribute( 'hreflang', $linklang['ietf'] );
99  $xml->writeAttribute( 'href', $page['url'][ $linklang['ietf'] ] );
100 
101 # $current = array( 'loc' => $page['url'][ $lang['ietf'] ] );
102 # foreach( $cf['localization']['languages'] as $linklang ) {
103 # $current['xhtml|link'][] = array(
104 # '@' => array(
105 # 'rel' => 'alternate',
106 # 'hreflang' => $linklang['ietf'],
107 # 'href' => $page['url'][ $linklang['ietf'] ]
108 # )
109 # );
110 # }
111 # $map['urlset']['url'][] = $current;
112 
113  // chiudo l'elemento <xhtml:link>
114  $xml->endElement();
115 
116  }
117 
118  }
119 
120  // chiudo l'elemento <url>
121  $xml->endElement();
122 
123  } else {
124 
125  // debug
126 
127  }
128 
129  } else {
130 
131  // debug
132  // echo 'la pagina #' . $id . ' non è candidata per la sitemap' . PHP_EOL;
133  // var_dump( $page['sitemap'] );
134 
135  }
136 
137  }
138 
139  }
140 
141  // debug
142  // die( print_r( $map, true ) );
143 /*
144  // generazione sitemap
145  array2xml( $map, 'sitemap.xml' );
146 */
147  // fine del root element
148  $xml->endElement();
149 
150  // fine del document
151  $xml->endDocument();
152 
153  // scrittura su file
154  $xml->flush();
155 
156  // prelevo le immagini dal database
157  // TODO prelevare solo le immagini relative al sito corrente
158  $img = mysqlSelectColumn( 'path', $cf['mysql']['connection'], 'SELECT path FROM immagini' );
159 
160  // aggiungo le immagini alla mappa CSV
161  foreach( $img as $i ) {
162  $url[] = $cf['site']['url'] . $i;
163  }
164 
165  // prelevo i file dal database
166  // TODO prelevare solo le immagini relative al sito corrente
167  $fls = mysqlSelectColumn( 'path', $cf['mysql']['connection'], 'SELECT path FROM file' );
168 
169  // aggiungo le immagini alla mappa CSV
170  foreach( $fls as $f ) {
171  $url[] = $cf['site']['url'] . dirname( $f ) . '/' . rawurlencode( basename( $f ) );
172  }
173 
174  // pulisco e riordino l'array degli URL
175  $url = array_unique( $url );
176  sort( $url );
177 
178  // apro la mappa CSV
179  $csv = fopen( DIRECTORY_BASE . 'etc/sitemap/sitemap.' . $cf['site']['fqdn'] . '.csv', 'w+' );
180 
181  // scrivo la sitemap CSV
182  foreach( $url as $u ) {
183  fwrite( $csv, $u . PHP_EOL );
184  }
185 
186  // chiudo la mappa CSV
187  fclose( $csv );
188 
189  } else {
190 
191  // log
192  logWrite( 'sitemap non scritta in quanto ' . $cf['contents']['updated'] . ' < ' . $cf['sitemap']['updated'], 'sitemap', LOG_DEBUG );
193 
194  }
195 
196  // debug
197  // echo file_get_contents( DIRECTORY_BASE . 'sitemap.xml' );
198  // print_r( xml2array( file_get_contents( DIRECTORY_BASE . 'sitemap.xml' ) ) );
199  // print_r( $map );
200  // echo array2xml( xml2array( file_get_contents( DIRECTORY_BASE . 'sitemap.xml' ) ) );
201 
202 ?>
logWrite( $m, $f='site', $l=LOG_NOTICE, $d=DIRECTORY_LOG, $t=CURRENT_LOG_LEVEL, $s=SITE_STATUS)
scrive un messaggio nei log del sito
Definition: _log.utils.php:48
$f
Definition: _filesystem.php:21
$cf['ricerca']['template']
Definition: _030.common.php:7
checkFolder( $p, &$e=NULL)
verifica l&#39;esistenza di un path di directory creando quelle mancanti
$page
const DIRECTORY_BASE
Definition: _osm.php:3
mysqlSelectColumn( $f, $c, $q, $p=false, &$e=array())
$sitemapFile