Ver código fonte

Evolution #828: Robot.txt

Bastien Sevajol 11 anos atrás
pai
commit
c40cce4785

+ 179 - 0
src/Muzich/CoreBundle/Command/GenerateRobotFilesCommand.php Ver arquivo

@@ -0,0 +1,179 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Command;
4
+
5
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
+use Symfony\Component\Console\Input\InputInterface;
7
+use Symfony\Component\Console\Output\OutputInterface;
8
+
9
+class GenerateRobotFilesCommand extends ContainerAwareCommand
10
+{
11
+  
12
+  protected $sitemap_content;
13
+  protected $sitemap_urls;
14
+  protected $router;
15
+  protected $locales;
16
+  protected $siteurl_prefix;
17
+  protected $em;
18
+  
19
+  protected function configure()
20
+  {
21
+    $this
22
+      ->setName('generate:robot:files')
23
+      ->setDescription('Generate files for bots')
24
+    ;
25
+  }
26
+
27
+  protected function execute(InputInterface $input, OutputInterface $output)
28
+  {
29
+    $this->init();
30
+    $this->generateRobotsTxt();
31
+    $this->generateSitemap();
32
+    $output->writeln('<info>Terminé !</info>');
33
+  }
34
+  
35
+  protected function init()
36
+  {
37
+    $this->sitemap_content = new \DOMDocument('1.0', 'utf-8');
38
+    $this->sitemap_urls = $this->sitemap_content->createElement('urlset');
39
+    $this->router = $this->getContainer()->get('router');
40
+    $this->locales = $this->getContainer()->getParameter('supported_langs');
41
+    $this->siteurl_prefix = $this->getContainer()->getParameter('siteurl');
42
+    $this->em = $this->getContainer()->get('doctrine')->getEntityManager();
43
+  }
44
+  
45
+  protected function generateRobotsTxt()
46
+  {
47
+    $robotstxt_content = "User-agent: *\n".
48
+      "Sitemap: http://muzi.ch/sitemap.xml";
49
+    file_put_contents('web/robots.txt', $robotstxt_content);
50
+  }
51
+  
52
+  protected function generateSitemap()
53
+  {
54
+    $this->addStaticsUrlsToUrlNode();
55
+    $this->addUserUrlsToUrlNode();
56
+    $this->addGroupUrlsToUrlNode();
57
+    $this->addPlaylistUrlsToUrlNode();
58
+    $this->addElementPermalinkUrlsToUrlNode();
59
+    
60
+    $this->sitemap_content->appendChild($this->sitemap_urls);
61
+    $this->sitemap_content->save('web/sitemap.xml');
62
+  }
63
+  
64
+  protected function addStaticsUrlsToUrlNode()
65
+  {
66
+    $this->addUrlsToNode($this->sitemap_urls, $this->generateUrls('home'), 'always');
67
+    $this->addUrlsToNode($this->sitemap_urls, $this->generateUrls('element_show_need_tags'), 'hourly');
68
+  }
69
+  
70
+  protected function addUserUrlsToUrlNode()
71
+  {
72
+    $users = $this->em->createQueryBuilder()
73
+      ->from('MuzichCoreBundle:User', 'user')
74
+      ->select('user.id, user.slug')
75
+      ->getQuery()->getScalarResult();
76
+    
77
+    foreach ($users as $user)
78
+    {
79
+      $this->addUrlsToNode($this->sitemap_urls, $this->generateUrls('show_user', array(
80
+        'slug' => $user['slug']
81
+      )), 'weekly');
82
+      
83
+      $this->addUrlsToNode($this->sitemap_urls, $this->generateUrls('favorite_user_list', array(
84
+        'slug' => $user['slug']
85
+      )), 'weekly');
86
+      
87
+      $this->addUrlsToNode($this->sitemap_urls, $this->generateUrls('playlists_user', array(
88
+        'user_slug' => $user['slug']
89
+      )), 'weekly');
90
+    }
91
+  }
92
+  
93
+  protected function addGroupUrlsToUrlNode()
94
+  {
95
+    $groups = $this->em->createQueryBuilder()
96
+      ->from('MuzichCoreBundle:Group', 'g')
97
+      ->select('g.id, g.slug')
98
+      ->getQuery()->getScalarResult();
99
+    
100
+    foreach ($groups as $group)
101
+    {
102
+      $this->addUrlsToNode($this->sitemap_urls, $this->generateUrls('show_group', array(
103
+        'slug' => $group['slug']
104
+      )), 'weekly');
105
+    }
106
+  }
107
+  
108
+  protected function addPlaylistUrlsToUrlNode()
109
+  {
110
+    $playlists = $this->em->createQueryBuilder()
111
+      ->from('MuzichCoreBundle:Playlist', 'playlist')
112
+      ->leftJoin('playlist.owner', 'owner')
113
+      ->select('playlist.id, owner.slug')
114
+      ->getQuery()->getScalarResult();
115
+    
116
+    foreach ($playlists as $playlist)
117
+    {
118
+      $this->addUrlsToNode($this->sitemap_urls, $this->generateUrls('playlist', array(
119
+        'playlist_id' => $playlist['id'],
120
+        'user_slug' => $playlist['slug']
121
+      )), 'monthly');
122
+    }
123
+  }
124
+  
125
+  protected function addElementPermalinkUrlsToUrlNode()
126
+  {
127
+    $elements = $this->em->createQueryBuilder()
128
+      ->from('MuzichCoreBundle:Element', 'element')
129
+      ->select('element.id, element.slug')
130
+      ->where('element.private = 0')
131
+      ->getQuery()->getScalarResult();
132
+    
133
+    foreach ($elements as $element)
134
+    {
135
+      $this->addUrlsToNode($this->sitemap_urls, $this->generateUrls('element_show_one', array(
136
+        'element_id' => $element['id'],
137
+        'element_slug' => $element['slug']
138
+      )), 'yearly');
139
+    }
140
+  }
141
+  
142
+  /** @return array */
143
+  protected function generateUrls($route, $parameters = array())
144
+  {
145
+    $urls = array();
146
+    foreach ($this->locales as $locale)
147
+    {
148
+      $urls[] = $this->siteurl_prefix . $this->router->generate($route, array_merge($parameters, array(
149
+        '_locale' => $locale
150
+      )));
151
+    }
152
+    
153
+    return $urls;
154
+  }
155
+  
156
+  protected function addUrlsToNode(\DOMNode $node, $urls, $changefreq = null)
157
+  {
158
+    foreach ($urls as $url)
159
+    {
160
+      $url_loc_content = $this->sitemap_content->createTextNode($url);
161
+      $url_node = $this->sitemap_content->createElement('url');
162
+      
163
+      $loc_node = $this->sitemap_content->createElement('loc');
164
+      $loc_node->appendChild($url_loc_content);
165
+      
166
+      $url_node->appendChild($loc_node);
167
+      
168
+      if ($changefreq)
169
+      {
170
+        $changefreq_node = $this->sitemap_content->createElement('changefreq');
171
+        $changefreq_node->appendChild($this->sitemap_content->createTextNode($changefreq));
172
+        $url_node->appendChild($changefreq_node);
173
+      }
174
+      
175
+      $node->appendChild($url_node);
176
+    }
177
+  }
178
+  
179
+}

+ 1 - 3
web/robots.txt Ver arquivo

@@ -1,4 +1,2 @@
1
-# www.robotstxt.org/
2
-# www.google.com/support/webmasters/bin/answer.py?hl=en&answer=156449
3
-
4 1
 User-agent: *
2
+Sitemap: http://muzi.ch/sitemap.xml

Diferenças do arquivo suprimidas por serem muito extensas
+ 2 - 0
web/sitemap.xml