From 4f1eb3d22794002b582fbbd21e5e32aa581626a1 Mon Sep 17 00:00:00 2001 From: Florian Thoma Date: Wed, 3 May 2017 11:44:37 +1000 Subject: [PATCH 1/5] add meta links for previous and next pages in pagination --- code/model/Blog.php | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/code/model/Blog.php b/code/model/Blog.php index 0ce1b08..43e8c23 100644 --- a/code/model/Blog.php +++ b/code/model/Blog.php @@ -758,7 +758,6 @@ class Blog_Controller extends Page_Controller if ($year) { $this->blogPosts = $dataRecord->getArchivedBlogPosts($year, $month, $day); - return $this->render(); } @@ -838,7 +837,7 @@ class Blog_Controller extends Page_Controller if($this->isRSS()) { return $this->rssFeed($this->blogPosts, $tag->getLink()); } else { - return $this->render(); + return $this->render(); } } @@ -882,7 +881,7 @@ class Blog_Controller extends Page_Controller if($this->isRSS()) { return $this->rssFeed($this->blogPosts, $category->getLink()); } else { - return $this->render(); + return $this->render(); } } @@ -1043,7 +1042,37 @@ class Blog_Controller extends Page_Controller return $posts; } + + /** + * Returns the absolute link to the next page for use in the page meta tags. This helps search engines + * find the pagination and index all pages properly. + * + * @example "<% if $PaginationAbsoluteNextLink %><% end_if %>" + * + * @return string + */ + public function PaginationAbsoluteNextLink() { + $posts = $this->PaginatedList(); + if ($posts->NotLastPage()) { + return Director::absoluteURL($posts->NextLink()); + } + } + /** + * Returns the absolute link to the previous page for use in the page meta tags. This helps search engines + * find the pagination and index all pages properly. + * + * @example "<% if $PaginationAbsolutePrevLink %><% end_if %>" + * + * @return string + */ + public function PaginationAbsolutePrevLink() { + $posts = $this->PaginatedList(); + if ($posts->NotFirstPage()) { + return Director::absoluteURL($posts->PrevLink()); + } + } + /** * Displays an RSS feed of blog posts. * @@ -1128,4 +1157,5 @@ class Blog_Controller extends Page_Controller return false; } } + } From 27044d86a52b8113a0f447e658feafad3d7b6435 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Fri, 22 Sep 2017 14:54:44 +1200 Subject: [PATCH 2/5] FIX Display individual years in blog archive widget when set to "Yearly" --- code/widgets/BlogArchiveWidget.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/code/widgets/BlogArchiveWidget.php b/code/widgets/BlogArchiveWidget.php index e6b7b8f..6fc4695 100644 --- a/code/widgets/BlogArchiveWidget.php +++ b/code/widgets/BlogArchiveWidget.php @@ -105,14 +105,15 @@ class BlogArchiveWidget extends Widget $posts = $query->execute(); $result = new ArrayList(); while ($next = $posts->next()) { - $date = Date::create(); - $date->setValue(strtotime($next['PublishDate'])); - $year = $date->Format('Y'); - if ($this->ArchiveType == 'Yearly') { + $year = $next['PublishDate']; $month = null; $title = $year; } else { + $date = Date::create(); + $date->setValue(strtotime($next['PublishDate'])); + + $year = $date->Format('Y'); $month = $date->Format('m'); $title = $date->FormatI18N('%B %Y'); } From a1e4f3efce46f342031c003746a5a0d49bb7dbd4 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Tue, 26 Sep 2017 16:15:57 +1300 Subject: [PATCH 3/5] Add tests for BlogArchiveWidget --- code/widgets/BlogArchiveWidget.php | 3 +- tests/Widgets/BlogArchiveWidgetTest.php | 71 +++++++++++++++++++++++++ tests/Widgets/BlogArchiveWidgetTest.yml | 27 ++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tests/Widgets/BlogArchiveWidgetTest.php create mode 100644 tests/Widgets/BlogArchiveWidgetTest.yml diff --git a/code/widgets/BlogArchiveWidget.php b/code/widgets/BlogArchiveWidget.php index 6fc4695..a971fca 100644 --- a/code/widgets/BlogArchiveWidget.php +++ b/code/widgets/BlogArchiveWidget.php @@ -96,7 +96,7 @@ class BlogArchiveWidget extends Widget ); $stage = Versioned::current_stage(); - $suffix = ($stage == 'Stage') ? '' : "_{$stage}"; + $suffix = ($stage === 'Live') ? '_Live' : ''; $query = SQLSelect::create($fields, "BlogPost{$suffix}") ->addGroupBy($publishDate) ->addOrderBy('PublishDate Desc') @@ -125,6 +125,7 @@ class BlogArchiveWidget extends Widget } $this->extend('updateGetArchive', $result); + return $result; } } diff --git a/tests/Widgets/BlogArchiveWidgetTest.php b/tests/Widgets/BlogArchiveWidgetTest.php new file mode 100644 index 0000000..7f93de4 --- /dev/null +++ b/tests/Widgets/BlogArchiveWidgetTest.php @@ -0,0 +1,71 @@ +markTestSkipped('Test requires silverstripe/widgets to be installed.'); + } + + SS_Datetime::set_mock_now('2017-09-20 00:00:00'); + + parent::setUp(); + } + + public function tearDown() + { + parent::tearDown(); + + SS_Datetime::clear_mock_now(); + } + + public function testArchiveMonthlyFromStage() + { + $widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly'); + $archive = $widget->getArchive(); + + $this->assertInstanceOf('SS_List', $archive); + $this->assertCount(3, $archive); + $this->assertDOSContains(array( + array('Title' => 'August 2017'), + array('Title' => 'September 2017'), + array('Title' => 'May 2015'), + ), $archive); + } + + public function testArchiveMonthlyFromLive() + { + $original = Versioned::current_stage(); + + $this->objFromFixture('BlogPost', 'post-b')->doPublish(); + Versioned::reading_stage('Live'); + + $widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly'); + $archive = $widget->getArchive(); + + $this->assertCount(1, $archive); + $this->assertDOSContains(array( + array('Title' => 'August 2017'), + ), $archive); + + Versioned::reading_stage($original); + } + + public function testArchiveYearly() + { + $widget = $this->objFromFixture('BlogArchiveWidget', 'archive-yearly'); + $archive = $widget->getArchive(); + + $this->assertInstanceOf('SS_List', $archive); + $this->assertCount(2, $archive); + $this->assertDOSContains(array( + array('Title' => '2017'), + array('Title' => '2015'), + ), $archive); + } +} diff --git a/tests/Widgets/BlogArchiveWidgetTest.yml b/tests/Widgets/BlogArchiveWidgetTest.yml new file mode 100644 index 0000000..4126956 --- /dev/null +++ b/tests/Widgets/BlogArchiveWidgetTest.yml @@ -0,0 +1,27 @@ +Blog: + my-blog: + Title: My Blog + +BlogPost: + post-a: + Title: September Digest + PublishDate: 2017-09-01 00:00:00 + ParentID: =>Blog.my-blog + post-b: + Title: August is Awesome + PublishDate: 2017-08-01 00:00:00 + ParentID: =>Blog.my-blog + post-c: + Title: 2015 is so two years ago + PublishDate: 2015-05-02 00:01:02 + ParentID: =>Blog.my-blog + +BlogArchiveWidget: + archive-monthly: + NumberToDisplay: 5 + ArchiveType: Monthly + BlogID: =>Blog.my-blog + archive-yearly: + NumberToDisplay: 5 + ArchiveType: Yearly + BlogID: =>Blog.my-blog From 92f61928701012723cace4189c131bd84114cdb4 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Tue, 26 Sep 2017 17:05:04 +1300 Subject: [PATCH 4/5] Increase mimumum versioned of SilverStripe to 3.2 and update versions in Travis configuration --- .travis.yml | 12 ++++++------ composer.json | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 651cf6c..a10d844 100755 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ before_install: env: global: - - DB=MYSQL CORE_RELEASE=3.1 + - DB=MYSQL CORE_RELEASE=3.6 # Turn coverage off by default, as it's expensive time wise - COVERAGE=0 @@ -27,13 +27,13 @@ matrix: - php: 5.6 env: DB=MYSQL COVERAGE=1 - php: 5.5 - env: DB=MYSQL - - php: 5.6 - env: DB=PGSQL - - php: 5.6 env: DB=MYSQL CORE_RELEASE=3.2 - php: 5.6 - env: DB=PGSQL CORE_RELEASE=3.2 + env: DB=PGSQL CORE_RELEASE=3.3 + - php: 5.6 + env: DB=MYSQL CORE_RELEASE=3.4 + - php: 5.6 + env: DB=PGSQL CORE_RELEASE=3.5 - php: 5.4 env: DB=SQLITE - php: 5.3 diff --git a/composer.json b/composer.json index d333288..8073483 100755 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ ], "type": "silverstripe-module", "require": { - "silverstripe/cms": "^3.1.0", + "silverstripe/cms": "^3.2.0", "silverstripe/lumberjack": "~1.1", "silverstripe/tagfield": "^1.0" }, @@ -32,4 +32,4 @@ }, "minimum-stability": "dev", "prefer-stable": true -} \ No newline at end of file +} From 8e684dea507a5bf54c812c40d900b31dc44aa856 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Tue, 26 Sep 2017 17:17:53 +1300 Subject: [PATCH 5/5] FIX BlogArchiveWidget for PostgreSQL compatibility --- code/widgets/BlogArchiveWidget.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/code/widgets/BlogArchiveWidget.php b/code/widgets/BlogArchiveWidget.php index a971fca..711c939 100644 --- a/code/widgets/BlogArchiveWidget.php +++ b/code/widgets/BlogArchiveWidget.php @@ -92,15 +92,15 @@ class BlogArchiveWidget extends Widget $publishDate = DB::get_conn()->formattedDatetimeClause('"PublishDate"', $format); $fields = array( 'PublishDate' => $publishDate, - 'Total' => "Count('PublishDate')" + 'Total' => "COUNT('\"PublishDate\"')" ); $stage = Versioned::current_stage(); $suffix = ($stage === 'Live') ? '_Live' : ''; - $query = SQLSelect::create($fields, "BlogPost{$suffix}") + $query = SQLSelect::create($fields, '"BlogPost' . $suffix . '"') ->addGroupBy($publishDate) - ->addOrderBy('PublishDate Desc') - ->addWhere(array('PublishDate < ?' => SS_Datetime::now()->Format('Y-m-d'))); + ->addOrderBy('"PublishDate" DESC') + ->addWhere(array('"PublishDate" < ?' => SS_Datetime::now()->Format('Y-m-d'))); $posts = $query->execute(); $result = new ArrayList();