Ejemplo n.º 1
0
 public function actionIndex($mapName = '')
 {
     $config = $this->normalizeConfig($this->config);
     $mapName = basename($mapName, '.xml');
     require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'SitemapGenerator.php';
     if (!empty($this->import)) {
         SitemapGenerator::importAliases(array('import' => $this->import, 'force_include' => $this->force_include));
     }
     try {
         if (!is_array($config)) {
             throw new Exception(Yii::t('sitemapgenerator.msg', 'Sitemaps configuration must be set as an array.'));
         }
         if (!isset($config['sitemap'])) {
             throw new Exception(Yii::t('sitemapgenerator.msg', 'Main sitemap file (sitemap.xml) must have configuration.'));
         }
         if (!isset($config[$mapName])) {
             throw new CHttpException(404, Yii::t('sitemapgenerator.msg', 'Sitemap file not founded or disabled.'));
         }
         $map_config = $config[$mapName];
         if (!is_array($map_config)) {
             throw new Exception(Yii::t('sitemapgenerator.msg', 'Sitemap file configuration must be set as an array.'));
         }
         // Prepare
         $this->setHeaders();
         if ($this->disable_weblogroutes) {
             $this->disableWebLogRoutes();
         }
         $this->mergeCacheParams($this->cache, $map_config);
         // Cache
         $cachemode = false;
         if (isset($map_config['cache']) && !empty($map_config['cache'])) {
             $cache = Yii::app()->{$map_config['cache'][0]};
             if ($cache === null) {
                 throw new Exception(Yii::t('sitemapgenerator.msg', 'Specified cache component not founed. Cache ID: {value}', array('{value}' => $map_config['cache'][0])));
             }
             $output = $cache->get($this->cache_record_id);
             $cachemode = true;
             ob_start();
         }
         // If has cache
         if (!empty($output)) {
             if (isset($map_config['gzencode']) && $map_config['gzencode']) {
                 $this->checkAndDisableGz();
                 $this->outputGzData($output);
             } else {
                 echo $output;
             }
             ob_get_flush();
             Yii::app()->end();
         }
         // GZencode mode
         $gzmode = false;
         if (isset($map_config['gzencode']) && $map_config['gzencode']) {
             $this->checkAndDisableGz();
             $gzmode = true;
             ob_start();
         }
         // Pre-import
         if (isset($map_config['import'])) {
             SitemapGenerator::importAliases($map_config);
         }
         // Sitemap render
         if (isset($map_config['index']) && $map_config['index']) {
             // Index sitemap
             unset($config[$mapName]);
             $this->renderIndex($config);
         } else {
             // Basic sitemap
             $this->renderNormal($map_config);
         }
         // GZencode mode output
         if ($gzmode) {
             $output = ob_get_clean();
             $gzip_output = gzencode($output, 9);
             $this->outputGzData($gzip_output);
         }
         // Cache set
         if ($cachemode) {
             $output = ob_get_clean();
             $dur = isset($map_config['cache'][1]) ? $map_config['cache'][1] : 0;
             $dep = isset($map_config['cache'][2]) ? $map_config['cache'][2] : null;
             $cache->set($this->cache_record_id . $mapName, $output, $dur, $dep);
             echo $output;
         }
     } catch (Exception $e) {
         SitemapGenerator::logExceptionError($e);
         if (YII_DEBUG) {
             throw $e;
         }
     }
 }