From 86d953045220dbcc0a8bd1e4235e8063dea4e005 Mon Sep 17 00:00:00 2001 From: Oreolek Date: Tue, 14 Jan 2014 14:02:56 +0700 Subject: [PATCH] Site sitemap --- .gitmodules | 3 +++ README.md | 1 + application/bootstrap.php | 8 ++++++ application/classes/Controller/Sitemap.php | 29 ++++++++++++++++++++++ application/classes/Model/Page.php | 10 ++++++++ application/classes/Model/Post.php | 10 ++++++++ application/config/debug_toolbar.php | 4 ++- modules/sitemap | 1 + robots.txt | 3 +++ 9 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 application/classes/Controller/Sitemap.php create mode 160000 modules/sitemap create mode 100644 robots.txt diff --git a/.gitmodules b/.gitmodules index 3034451..0617e2f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -31,3 +31,6 @@ [submodule "modules/cache"] path = modules/cache url = git@github.com:Oreolek/cache.git +[submodule "modules/sitemap"] + path = modules/sitemap + url = git@github.com:flody/kohana-sitemap.git diff --git a/README.md b/README.md index 7375b49..fcf0617 100644 --- a/README.md +++ b/README.md @@ -20,4 +20,5 @@ Oreolek. * Copy `application/config/auth.php.example` to `application/config/auth.php` and edit it. * Import SQL schema from schema.sql (autoinstall currently not working) * Open in browser `SERVER_ADDR/install` +* Edit robots.txt with your domain * Set server variable `KOHANA_ENV` to `production` when you're done hacking diff --git a/application/bootstrap.php b/application/bootstrap.php index f75de73..9d70036 100644 --- a/application/bootstrap.php +++ b/application/bootstrap.php @@ -117,12 +117,20 @@ Kohana::modules(array( 'unittest' => MODPATH.'unittest', // Unit testing 'cache' => MODPATH.'cache', // Caching with multiple backends // 'codebench' => MODPATH.'codebench', // Benchmarking tool + 'sitemap' => MODPATH.'sitemap', // Sitemap generator )); /** * Set the routes. Each route must have a minimum of a name, a URI and a set of * defaults for the URI. */ +Route::set('sitemap_index', 'sitemap.xml()', array('gzip' => '\.gz')) + ->defaults(array( + 'controller' => 'Sitemap', + 'action' => 'index' + )); + + Route::set('error', 'error/(/)', array('action' => '[0-9]++','message' => '.+')) ->defaults(array( 'controller' => 'Error', diff --git a/application/classes/Controller/Sitemap.php b/application/classes/Controller/Sitemap.php new file mode 100644 index 0000000..bdfd36f --- /dev/null +++ b/application/classes/Controller/Sitemap.php @@ -0,0 +1,29 @@ +request->param('gzip')) + { + $sitemap->gzip = TRUE; + } + $posts = Model_Post::get_dates(); + foreach ($posts as $id => $posted_at) + { + $url = new Sitemap_URL; + $url->set_loc(Route::url('default', array('controller' => 'Post', 'action' => 'view', 'id' => $id), TRUE)); + $url->set_last_mod(strtotime($posted_at)); + $url->set_change_frequency('weekly'); // new comments + $sitemap->add($url); + } + $pages = Model_Page::get_ids(); + foreach ($pages as $id) + { + $url = new Sitemap_URL; + $url->set_loc(Route::url('default', array('controller' => 'Page', 'action' => 'view', 'id' => $id), TRUE)); + $url->set_change_frequency('never'); + $sitemap->add($url); + } + $this->response->body($sitemap->render()); + } +} diff --git a/application/classes/Model/Page.php b/application/classes/Model/Page.php index e62ab26..167d3d6 100644 --- a/application/classes/Model/Page.php +++ b/application/classes/Model/Page.php @@ -29,4 +29,14 @@ class Model_Page extends ORM { 'content' => 'Текст страницы', 'is_draft' => 'Черновик' ); + + /** + * Returns array of ids and posted_at timestamps. + * Used in sitemap generation. + **/ + public static function get_ids() + { + $query = DB::select('id')->from('pages'); + return $query->execute()->as_array(NULL, 'id'); + } } diff --git a/application/classes/Model/Post.php b/application/classes/Model/Post.php index 26e0c5e..91c9436 100644 --- a/application/classes/Model/Post.php +++ b/application/classes/Model/Post.php @@ -62,4 +62,14 @@ class Model_Post extends ORM { $query = DB::select(array(DB::expr('MAX(`posted_at`)'), 'max_date'))->from('posts'); return $query->execute()->get('max_date'); } + + /** + * Returns array of ids and posted_at timestamps. + * Used in sitemap generation. + **/ + public static function get_dates() + { + $query = DB::select('id', 'posted_at')->from('posts'); + return $query->execute()->as_array('id', 'posted_at'); + } } diff --git a/application/config/debug_toolbar.php b/application/config/debug_toolbar.php index 117f579..6cad69e 100644 --- a/application/config/debug_toolbar.php +++ b/application/config/debug_toolbar.php @@ -14,7 +14,9 @@ $config['skip_configs'] = array('database', 'encrypt'); * Disabled routes */ $config['excluded_routes'] = array( - 'docs/media' // Userguide media route + 'docs/media', // Userguide media route + 'sitemap', // Sitemaps + 'sitemap_index' // Main sitemap ); return $config; diff --git a/modules/sitemap b/modules/sitemap new file mode 160000 index 0000000..7773a61 --- /dev/null +++ b/modules/sitemap @@ -0,0 +1 @@ +Subproject commit 7773a61dc857c0bf904003c8cdcfc3b4642aaf31 diff --git a/robots.txt b/robots.txt new file mode 100644 index 0000000..ae5d1a3 --- /dev/null +++ b/robots.txt @@ -0,0 +1,3 @@ +User-agent: * +Disallow: +Sitemap: http://oreolek.ru/sitemap.xml.gz