Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 343 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
343 jpm 1
<?php
2
// Encodage : UTF-8
3
// +-------------------------------------------------------------------------------------------------------------------+
4
/**
5
* Sitemap
6
*
7
* Description : classe permettant de réaliser un fichier Sitemap pour eFlore
8
*
9
//Auteur original :
10
* @author       Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
* @copyright	Tela-Botanica 1999-2008
12
* @link			http://www.tela-botanica.org/wikini/eflore
13
* @licence		GPL v3 & CeCILL v2
14
* @version		$Id: Sitemap.class.php 1873 2009-03-31 10:07:24Z Jean-Pascal MILCENT $
15
*/
16
// +-------------------------------------------------------------------------------------------------------------------+
17
 
18
/**
19
* Classe crééant un Sitemap
20
*/
21
class Robot extends ScriptCommande {
22
 
23
	private $lastmod = '2008-04-28';
24
	private $changefreq = 'monthly';
25
 
26
    public function executer() {
27
    	$cmd = $this->getParam('a');
28
    	switch ($cmd) {
29
			case 'creer' :
30
				$this->creerSiteMap();
31
				break;
32
			default :
33
				trigger_error('Erreur : la commande "'.$cmd.'" n\'existe pas!'."\n", E_USER_ERROR);
34
		}
35
    }
36
 
37
    private function creerSiteMap() {
38
    	// +-----------------------------------------------------------------------------------------------------------+
39
		// Initialisation des paramêtres variables
40
		$url_site = 'http://www.tela-botanica.org/';
41
		$url_eflore = $url_site.'eflore/%s/nt/%s/%s';
42
		$projets = array(	array('id' => 25, 'code' => 'BDNFF', 'url' => $url_eflore, 'taxon_max' => 50000, 'onglets' => '*'),
43
							array('id' => 29, 'code' => 'BDNFM', 'url' => $url_eflore, 'taxon_max' => 50000, 'onglets' => 'synthese,synonymie,vernaculaire,chorologie,biblio,information,illustration,wiki'),
44
							array('id' => 38, 'code' => 'BDNBE', 'url' => $url_eflore, 'taxon_max' => 50000, 'onglets' => 'synthese,synonymie,chorologie,biblio,information,illustration,wiki'),
45
							array('id' => 45, 'code' => 'BDAFN', 'url' => $url_eflore, 'taxon_max' => 500000, 'onglets' => 'synthese,synonymie,chorologie,biblio,information,illustration,wiki')
46
						);
47
		$onglets = array(	'synthese' => '0.9',
48
							'synonymie' => '0.6',
49
							'vernaculaire' => '0.8',
50
							'chorologie' => '0.7',
51
							'biblio' => '0.8',
52
							'information' => '0.2',
53
							'illustration' => '0.9',
54
							'wiki' => '0.3',
55
							'cel' => '0.5');
56
 
57
		$xmlstr_sitemap = 	'<?xml version="1.0" encoding="UTF-8"?>'."\n".
58
							'<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '.
59
							'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 '.
60
							'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" '.
61
							'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n".
62
							'</urlset>'."\n";
63
		$xmlstr_sitemapindex = 	'<?xml version="1.0" encoding="UTF-8"?>'."\n".
64
								'<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '.
65
	         					'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 '.
66
	         					'http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" '.
67
	         					'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n".
68
								'</sitemapindex>'."\n";
69
    	// +-----------------------------------------------------------------------------------------------------------+
70
		// Initialisation des variables
71
		$UrlSet = null;
72
		$SiteMapIndex = null;
73
		$cpt_url = 1;
74
		$cpt_fichier = 1;
75
		$Taxon = new EfloreTaxon(true);
76
 
77
		// +-----------------------------------------------------------------------------------------------------------+
78
		// Lancement du traitement
79
		foreach ($projets as $projet) {
80
			// Gestion des onglets affichables pour le projet courrant
81
			if ($projet['onglets'] != '*') {
82
				$projet['onglets'] = array_flip(explode(',', $projet['onglets']));
83
			}
84
 
85
			// +-------------------------------------------------------------------------------------------------------+
86
			echo "Création des URLs des taxons pour le projet {$projet['code']} : ";
87
			$i = 1;
88
			$taxons = $Taxon->consulterTaxon($projet['id']);
89
			foreach ($taxons as $taxon) {
90
				// Seul les taxons du projet sont indexés, on exclue les taxons virtuels
91
				if ($taxon['et']['id']['taxon'] < $projet['taxon_max']) {
92
					foreach ($onglets as $onglet => $priorite) {
93
						// Vérification que l'onglet est autorisé pour ce projet
94
						if ($projet['onglets'] == '*' || isset($projet['onglets'][$onglet])) {
95
							// Affichage en console et en cas de test...
96
			 				echo str_repeat(chr(8), ( strlen( $i ) + 1 ))."\t".$i++;
97
 
98
							// Création du fichier XML si nécessaire
99
							if (is_null($UrlSet)) {
100
								$UrlSet = new SimpleXMLElement($xmlstr_sitemap);
101
							}
102
 
103
							// Ajout de l'url
104
							$Url = $UrlSet->addChild('url');
105
							$Url->addChild('loc', sprintf($projet['url'], $projet['code'], $taxon['et']['id']['taxon'], $onglet));
106
							$Url->addChild('lastmod', $this->lastmod);
107
							$Url->addChild('changefreq', $this->changefreq);
108
							$Url->addChild('priority', $priorite);
109
 
110
							// Vérification écriture du fichier ou pas
111
							if ($cpt_url == 1) {
112
								$estimation = strlen($UrlSet->asXml());
113
							}
114
							if (49999 == $cpt_url++ || ($estimation * $cpt_url) > 20000000 || $i == (int)$this->getParam('t')) {
115
								$contenu = $UrlSet->asXml();
116
								$cpt_url = 1;
117
								$UrlSet = null;
118
 
119
								// Création du fichier Sitemap compressé
120
								$fichier_nom = 'sitemap'.$cpt_fichier++.'.xml';
121
								$compression = false;
122
								if (!is_numeric($this->getParam('t'))) {
123
									$compression = true;
124
									$fichier_nom .= '.gz';
125
								}
126
								$fichier = $this->getIni('log_chemin').$fichier_nom;
127
								$this->creerFichier($fichier, $contenu, $compression);
128
 
129
								// Création du XML d'index des Sitemap si nécessaire
130
								if (is_null($SiteMapIndex)) {
131
									$SiteMapIndex = new SimpleXMLElement($xmlstr_sitemapindex);
132
								}
133
 
134
								// Ajout du fichier Sitemap à l'index
135
								$SiteMap = $SiteMapIndex->addChild('sitemap');
136
								$SiteMap->addChild('loc', $url_site.$fichier_nom);
137
								$SiteMap->addChild('lastmod', date('c', time()));
138
							}
139
 
140
			 				if ($i == (int)$this->getParam('t')) {break;}
141
						}
142
					}
143
				}
144
				// En cas de test...
145
				if ($i == (int)$this->getParam('t')) {break;}
146
			}
147
			echo "\n";
148
 
149
			// Création du fichier d'index des Sitemap
150
			if (is_object($SiteMapIndex)) {
151
				$index_contenu = $SiteMapIndex->asXml();
152
				$index_fichier_nom = 'sitemap_index.xml';
153
				$index_fichier = $this->getIni('log_chemin').$index_fichier_nom;
154
				$this->creerFichier($index_fichier, $index_contenu);
155
			}
156
		}
157
    }
158
 
159
}
160
?>