00001 <?php
00002
00003
00004
00005
00006
00007
00008 class GsitemapController extends ControllerBase {
00009 const ITEMS_PER_FILE = 300;
00010 const USE_TIMESTAMP = 1;
00011 const NO_TIMESTAMP = 0;
00012
00013
00014
00015
00016 public function get_routes() {
00017 $ret = array(
00018 new ExactMatchRoute('sitemap.xml', $this, 'gsitemap_site', new SimpleCacheManager()),
00019 new ExactMatchRoute('sitemap.xml', $this, 'gsitemap_index', new SimpleCacheManager()),
00020 new ExactMatchRoute('sitemapindex.xml', $this, '', new RedirectRenderDecorator('sitemap.xml')),
00021 );
00022 if (Config::has_feature(ConfigGSitemap::GSITEMAP_HTML_SITEMAP)) {
00023 $ret[] = new ExactMatchRoute('sitemap.html', $this, 'gsitemap_site_html', new SimpleCacheManager());
00024 $ret[] = new ExactMatchRoute('sitemap.html', $this, 'gsitemap_index_html', new SimpleCacheManager());
00025 $ret[] = new ExactMatchRoute('sitemapindex.html', $this, '', new RedirectRenderDecorator('sitemap.html'));
00026 }
00027 return $ret;
00028 }
00029
00030
00031
00032
00033 public function before_action() {
00034 Load::components('gsitemapmodel', 'gsitemapitemformatter');
00035 }
00036
00037
00038
00039
00040
00041
00042 public function action_gsitemap_index($page_data) {
00043 $view = ViewFactory::create_view(IViewFactory::XML, 'core::gsitemap/index', $page_data);
00044 $this->gsitemap_index($page_data, $view);
00045 $view->render();
00046 }
00047
00048
00049
00050
00051
00052
00053
00054 public function action_gsitemap_site($page_data) {
00055
00056 if ($page_data->get_get()->count() == 0) {
00057 return $this->action_gsitemap_index($page_data);
00058 }
00059 $view = ViewFactory::create_view(IViewFactory::XML, 'core::gsitemap/site', $page_data);
00060 $ret = $this->gsitemap_site($page_data, $view);
00061 if ($ret === self::OK) {
00062 $view->render();
00063 }
00064 return $ret;
00065 }
00066
00067
00068
00069
00070
00071
00072 public function action_gsitemap_index_html($page_data) {
00073 $view = ViewFactory::create_view(IViewFactory::CONTENT, 'core::gsitemap/index_html', $page_data);
00074 $ret = $this->gsitemap_index($page_data, $view);
00075 $view->render();
00076 return $ret;
00077 }
00078
00079
00080
00081
00082
00083
00084 public function action_gsitemap_site_html($page_data) {
00085
00086 if ($page_data->get_get()->count() == 0) {
00087 return $this->action_gsitemap_index_html($page_data);
00088 }
00089 $view = ViewFactory::create_view(IViewFactory::CONTENT, 'core::gsitemap/site_html', $page_data);
00090 $ret = $this->gsitemap_site($page_data, $view);
00091 if ($ret === self::OK) {
00092 $view->render();
00093 }
00094 return $ret;
00095 }
00096
00097 protected function collect_models() {
00098 $ret = array();
00099 $models = array();
00100 EventSource::Instance()->invoke_event('gsitemap_models', null, $models);
00101 foreach($models as $model) {
00102 if ($model instanceof GSiteMapModel) {
00103 $ret[] = $model;
00104 }
00105 else {
00106 $ret[] = new GSiteMapModel($model);
00107 }
00108 }
00109 return $ret;
00110 }
00111
00112
00113
00114
00115 protected function gsitemap_index(PageData $page_data, IView $view) {
00116 $arrret = array('main');
00117 $models = $this->collect_models();
00118 foreach($models as $model) {
00119 $arrret = array_merge($arrret, self::build_sitemap_index_for_model($model));
00120 }
00121 EventSource::Instance()->invoke_event('gsitemap_index', null, $arrret);
00122 $view->assign('files', $arrret);
00123 }
00124
00125
00126
00127
00128
00129
00130
00131 protected function gsitemap_site(PageData $page_data, IView $view) {
00132 $p = $page_data->get_get()->get_item('i');
00133 if (empty($p)) {
00134 return self::NOT_FOUND;
00135 }
00136 $arrret = array();
00137 if ($p == 'main') {
00138 $arrret[] = new GSiteMapItemFormatter(Config::get_url(Config::URL_BASEURL), array());
00139 }
00140 else {
00141 $models = $this->collect_models();
00142 foreach($models as $model) {
00143 $arrret = array_merge($arrret, self::build_sitemap_for_model($model, $p));
00144 }
00145 }
00146 EventSource::Instance()->invoke_event('gsitemap_site', $p, $arrret);
00147 if (count($arrret) == 0) {
00148 return self::NOT_FOUND;
00149 }
00150 $formatters = array();
00151 foreach($arrret as $item) {
00152 if ($item instanceof GSiteMapItemFormatter) {
00153 $formatters[] = $item;
00154 } else if (is_array($item)) {
00155 $formatters[] = new GSiteMapItemFormatter($item['url'], $item);
00156 } else {
00157 $formatters[] = new GSiteMapItemFormatter($item, array());
00158 }
00159 }
00160 $view->assign('items', $formatters);
00161 return self::OK;
00162 }
00163
00164
00165
00166
00167
00168
00169
00170 private static function extract_adapter($params) {
00171 if ($params instanceof ISearchAdapter) {
00172 return $params;
00173 }
00174 return DB::create(Cast::string($params));
00175 }
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 public static function build_sitemap_index($params, $itemsperfile = self::ITEMS_PER_FILE) {
00188 if ($itemsperfile <= 0) {
00189 $itemsperfile = self::ITEMS_PER_FILE;
00190 }
00191 $model = new GSiteMapModel($params);
00192 $model->items_per_file = $itemsperfile;
00193 return self::build_sitemap_index_for_model($model);
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203 public static function build_sitemap_index_for_model(GSiteMapModel $model) {
00204 $ret = array();
00205 $c = $model->get_number_of_chunks();
00206 for($i = 0; $i < $c; $i++) {
00207 $ret[] = $model->build_index_name($i);
00208 }
00209 return $ret;
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 public static function build_sitemap($params, $index, $itemsperfile = self::ITEMS_PER_FILE, $policy = self::USE_TIMESTAMP) {
00224 if ($itemsperfile <= 0) {
00225 $itemsperfile = self::ITEMS_PER_FILE;
00226 }
00227 $model = new GSiteMapModel($params, 'view', $policy);
00228 $model->items_per_file = $itemsperfile;
00229 return self::build_sitemap_for_model($model, $index);
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 public static function build_sitemap_for_model(GSiteMapModel $model, $index) {
00241 $ret = array();
00242 $chunk = $model->extract_chunk($index);
00243 if ($chunk !== false) {
00244 $dao = $model->create_adapter();
00245 $model->select_chunk($dao, $chunk);
00246
00247 $dao->find();
00248 while($dao->fetch()) {
00249 $ret[] = $model->create_formatter($dao);
00250 }
00251 }
00252 return $ret;
00253 }
00254
00255 }