[PHP-WEBMASTER] [web-php] master: Create phpweb\News\NewsHandler class to handle pregen-news.inc (#1288)

Author: Maurício Meneghini Fauth (MauricioFauth)
Committer: GitHub (web-flow)
Pusher: sy-records
Date: 2025-07-21T10:06:28+08:00

Commit: Create phpweb\News\NewsHandler class to handle pregen-news.inc (#1288) · php/web-php@35d6a90 · GitHub
Raw diff: https://github.com/php/web-php/commit/35d6a900acf5b110c2ad65ef200f06cc92eeb7c8.diff

Create phpweb\News\NewsHandler class to handle pregen-news.inc (#1288)

Changed paths:
  A src/News/NewsHandler.php
  A tests/Unit/News/NewsHandlerTest.php
  M archive/2013.php
  M archive/2014.php
  M archive/2015.php
  M archive/2016.php
  M archive/2017.php
  M archive/2018.php
  M archive/2019.php
  M archive/2020.php
  M archive/2021.php
  M archive/2022.php
  M archive/2023.php
  M archive/2024.php
  M archive/2025.php
  M conferences/index.php
  M include/layout.inc
  M index.php

Diff:

diff --git a/archive/2013.php b/archive/2013.php
index a6a745885b..744b8dd584 100644
--- a/archive/2013.php
+++ b/archive/2013.php
@@ -2,7 +2,6 @@

$_SERVER['BASE_PAGE'] = 'archive/2013.php';
include_once __DIR__ . '/../include/prepend.inc';
-include_once __DIR__ . '/../include/pregen-news.inc';
news_archive_sidebar();
site_header("News Archive - 2013", ["cache" => true]);
?>
diff --git a/archive/2014.php b/archive/2014.php
index e5d456aef2..7a61874b99 100644
--- a/archive/2014.php
+++ b/archive/2014.php
@@ -2,7 +2,6 @@

$_SERVER['BASE_PAGE'] = 'archive/2014.php';
include_once __DIR__ . '/../include/prepend.inc';
-include_once __DIR__ . '/../include/pregen-news.inc';
news_archive_sidebar();
site_header("News Archive - 2014", ["cache" => true]);
?>
diff --git a/archive/2015.php b/archive/2015.php
index b42a3dcef5..b71a865e4b 100644
--- a/archive/2015.php
+++ b/archive/2015.php
@@ -1,8 +1,9 @@
<?php

+use phpweb\News\NewsHandler;
+
$_SERVER['BASE_PAGE'] = 'archive/2015.php';
include_once __DIR__ . '/../include/prepend.inc';
-include_once __DIR__ . '/../include/pregen-news.inc';
news_archive_sidebar();
site_header("News Archive - 2015");
?>
@@ -17,7 +18,7 @@

<?php

-print_news($NEWS_ENTRIES, ["conferences", "cfp", "frontpage", "nofrontpage"], 500, 2015);
+print_news((new NewsHandler())->getNewsByYear(2015), ["conferences", "cfp", "frontpage", "nofrontpage"], 500);

/* %s/<a href="\(.*\)"><img src="\/images\/news\/\(.*\)" alt="\(.*\)" width.*><\/a>/<?php news_image("\1", "\2", "\3"); ?>/g */
site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]);
diff --git a/archive/2016.php b/archive/2016.php
index 10e482cec7..228f0ea9a2 100644
--- a/archive/2016.php
+++ b/archive/2016.php
@@ -1,8 +1,9 @@
<?php

+use phpweb\News\NewsHandler;
+
$_SERVER['BASE_PAGE'] = 'archive/2016.php';
include_once __DIR__ . '/../include/prepend.inc';
-include_once __DIR__ . '/../include/pregen-news.inc';
news_archive_sidebar();
site_header("News Archive - 2016");
?>
@@ -17,7 +18,7 @@

<?php

-print_news($NEWS_ENTRIES, ["conferences", "cfp", "frontpage", "nofrontpage"], 500, 2016);
+print_news((new NewsHandler())->getNewsByYear(2016), ["conferences", "cfp", "frontpage", "nofrontpage"], 500);

/* %s/<a href="\(.*\)"><img src="\/images\/news\/\(.*\)" alt="\(.*\)" width.*><\/a>/<?php news_image("\1", "\2", "\3"); ?>/g */
site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]);
diff --git a/archive/2017.php b/archive/2017.php
index f49960fc1b..26e7b5a400 100644
--- a/archive/2017.php
+++ b/archive/2017.php
@@ -1,8 +1,9 @@
<?php

+use phpweb\News\NewsHandler;
+
$_SERVER['BASE_PAGE'] = 'archive/2017.php';
include_once __DIR__ . '/../include/prepend.inc';
-include_once __DIR__ . '/../include/pregen-news.inc';
news_archive_sidebar();
site_header("News Archive - 2017");
?>
@@ -17,7 +18,7 @@

<?php

-print_news($NEWS_ENTRIES, null, 500, 2017);
+print_news((new NewsHandler())->getNewsByYear(2017), null, 500);

/* %s/<a href="\(.*\)"><img src="\/images\/news\/\(.*\)" alt="\(.*\)" width.*><\/a>/<?php news_image("\1", "\2", "\3"); ?>/g */
site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]);
diff --git a/archive/2018.php b/archive/2018.php
index 7a9a74989c..1db8020b7b 100644
--- a/archive/2018.php
+++ b/archive/2018.php
@@ -1,8 +1,9 @@
<?php

+use phpweb\News\NewsHandler;
+
$_SERVER['BASE_PAGE'] = 'archive/2018.php';
include_once __DIR__ . '/../include/prepend.inc';
-include_once __DIR__ . '/../include/pregen-news.inc';
news_archive_sidebar();
site_header("News Archive - 2018");
?>
@@ -17,7 +18,7 @@

<?php

-print_news($NEWS_ENTRIES, null, 500, 2018);
+print_news((new NewsHandler())->getNewsByYear(2018), null, 500);

/* %s/<a href="\(.*\)"><img src="\/images\/news\/\(.*\)" alt="\(.*\)" width.*><\/a>/<?php news_image("\1", "\2", "\3"); ?>/g */
site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]);
diff --git a/archive/2019.php b/archive/2019.php
index 9e6d3ad3fb..3538aa2daa 100644
--- a/archive/2019.php
+++ b/archive/2019.php
@@ -1,8 +1,9 @@
<?php

+use phpweb\News\NewsHandler;
+
$_SERVER['BASE_PAGE'] = 'archive/2019.php';
include_once __DIR__ . '/../include/prepend.inc';
-include_once __DIR__ . '/../include/pregen-news.inc';
news_archive_sidebar();
site_header("News Archive - 2019");
?>
@@ -17,7 +18,7 @@

<?php

-print_news($NEWS_ENTRIES, null, 500, 2019);
+print_news((new NewsHandler())->getNewsByYear(2019), null, 500);

/* %s/<a href="\(.*\)"><img src="\/images\/news\/\(.*\)" alt="\(.*\)" width.*><\/a>/<?php news_image("\1", "\2", "\3"); ?>/g */
site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]);
diff --git a/archive/2020.php b/archive/2020.php
index ab66d3c0ea..5a469f6cc9 100644
--- a/archive/2020.php
+++ b/archive/2020.php
@@ -1,8 +1,9 @@
<?php

+use phpweb\News\NewsHandler;
+
$_SERVER['BASE_PAGE'] = 'archive/2020.php';
include_once __DIR__ . '/../include/prepend.inc';
-include_once __DIR__ . '/../include/pregen-news.inc';
news_archive_sidebar();
site_header("News Archive - 2020");
?>
@@ -17,7 +18,7 @@

<?php

-print_news($NEWS_ENTRIES, null, 500, 2020);
+print_news((new NewsHandler())->getNewsByYear(2020), null, 500);

/* %s/<a href="\(.*\)"><img src="\/images\/news\/\(.*\)" alt="\(.*\)" width.*><\/a>/<?php news_image("\1", "\2", "\3"); ?>/g */
site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]);
diff --git a/archive/2021.php b/archive/2021.php
index 6a57771c18..1191c7585b 100644
--- a/archive/2021.php
+++ b/archive/2021.php
@@ -1,8 +1,9 @@
<?php

+use phpweb\News\NewsHandler;
+
$_SERVER['BASE_PAGE'] = 'archive/2021.php';
include_once __DIR__ . '/../include/prepend.inc';
-include_once __DIR__ . '/../include/pregen-news.inc';
news_archive_sidebar();
site_header("News Archive - 2021");
?>
@@ -17,7 +18,7 @@

<?php

-print_news($NEWS_ENTRIES, null, 500, 2021);
+print_news((new NewsHandler())->getNewsByYear(2021), null, 500);

/* %s/<a href="\(.*\)"><img src="\/images\/news\/\(.*\)" alt="\(.*\)" width.*><\/a>/<?php news_image("\1", "\2", "\3"); ?>/g */
site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]);
diff --git a/archive/2022.php b/archive/2022.php
index 34a16adefc..abfcee9354 100644
--- a/archive/2022.php
+++ b/archive/2022.php
@@ -1,8 +1,9 @@
<?php

+use phpweb\News\NewsHandler;
+
$_SERVER['BASE_PAGE'] = 'archive/2022.php';
include_once __DIR__ . '/../include/prepend.inc';
-include_once __DIR__ . '/../include/pregen-news.inc';
news_archive_sidebar();
site_header("News Archive - 2022");
?>
@@ -17,5 +18,5 @@

<?php

-print_news($NEWS_ENTRIES, null, 500, 2022);
+print_news((new NewsHandler())->getNewsByYear(2022), null, 500);
site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]);
diff --git a/archive/2023.php b/archive/2023.php
index 706530a7ce..b6ef8abde9 100644
--- a/archive/2023.php
+++ b/archive/2023.php
@@ -1,8 +1,9 @@
<?php

+use phpweb\News\NewsHandler;
+
$_SERVER['BASE_PAGE'] = 'archive/2023.php';
include_once __DIR__ . '/../include/prepend.inc';
-include_once __DIR__ . '/../include/pregen-news.inc';
news_archive_sidebar();
site_header("News Archive - 2023");
?>
@@ -17,5 +18,5 @@

<?php

-print_news($NEWS_ENTRIES, null, 500, 2023);
+print_news((new NewsHandler())->getNewsByYear(2023), null, 500);
site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]);
diff --git a/archive/2024.php b/archive/2024.php
index 43a145ae57..9834117665 100644
--- a/archive/2024.php
+++ b/archive/2024.php
@@ -1,8 +1,9 @@
<?php

+use phpweb\News\NewsHandler;
+
$_SERVER['BASE_PAGE'] = 'archive/2024.php';
include_once __DIR__ . '/../include/prepend.inc';
-include_once __DIR__ . '/../include/pregen-news.inc';
news_archive_sidebar();
site_header("News Archive - 2024");
?>
@@ -17,5 +18,5 @@

<?php

-print_news($NEWS_ENTRIES, null, 500, 2024);
+print_news((new NewsHandler())->getNewsByYear(2024), null, 500);
site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]);
diff --git a/archive/2025.php b/archive/2025.php
index abbb2f1e0a..2787ee7d73 100644
--- a/archive/2025.php
+++ b/archive/2025.php
@@ -1,8 +1,9 @@
<?php

+use phpweb\News\NewsHandler;
+
$_SERVER['BASE_PAGE'] = 'archive/2025.php';
include_once __DIR__ . '/../include/prepend.inc';
-include_once __DIR__ . '/../include/pregen-news.inc';
news_archive_sidebar();
site_header("News Archive - 2025");
?>
@@ -17,5 +18,5 @@

<?php

-print_news($NEWS_ENTRIES, null, 500, 2025);
+print_news((new NewsHandler())->getNewsByYear(2025), null, 500);
site_footer(['elephpants' => true, 'sidebar' => $SIDEBAR_DATA]);
diff --git a/conferences/index.php b/conferences/index.php
index 4ed6dd0627..3718ba5baf 100644
--- a/conferences/index.php
+++ b/conferences/index.php
@@ -1,8 +1,9 @@
<?php

+use phpweb\News\NewsHandler;
+
$_SERVER['BASE_PAGE'] = 'conferences/index.php';
include_once __DIR__ . '/../include/prepend.inc';
-include_once __DIR__ . '/../include/pregen-news.inc';

mirror_setcookie("LAST_NEWS", $_SERVER["REQUEST_TIME"], 60 * 60 * 24 * 365);
site_header("PHP Conferences around the world", [
@@ -12,22 +13,9 @@
]);

$content = "<div class='home-content'>";
-$frontpage = ;
-foreach ($NEWS_ENTRIES as $entry) {
- foreach ($entry["category"] as $category) {
- if ($category["term"] == "cfp") {
- $frontpage = $entry;
- break;
- }
- if ($category["term"] == "conferences") {
- $frontpage = $entry;
- break;
- }
- }
-}
$panels = '<p class="prepend"><a href="PHP: conferences to see your conference appear here?</a></p>';

-foreach ($frontpage as $entry) {
+foreach ((new NewsHandler())->getConferences() as $entry) {
     $link = preg_replace('~^(http://php.net/|https://www.php.net/)~', '', $entry["id"]);
     $id = parse_url($entry["id"], PHP_URL_FRAGMENT);
     $date = date_format(date_create($entry["updated"]), 'Y-m-d');
diff --git a/include/layout.inc b/include/layout.inc
index b3dc6dbfa9..c69d390b32 100644
--- a/include/layout.inc
+++ b/include/layout.inc
@@ -2,6 +2,7 @@

use phpweb\I18n\Languages;
use phpweb\Navigation\NavItem;
+use phpweb\News\NewsHandler;

$_SERVER['STATIC_ROOT'] = $MYSITE;
$_SERVER['MYSITE'] = $MYSITE;
@@ -346,7 +347,7 @@ function news_archive_sidebar(): void
}

// Print news
-function print_news($news, $dog, $max = 5, $onlyyear = null, $return = false) {
+function print_news($news, $dog, $max = 5, $return = false) {
     $retval = ;
     $count = 0;
     $news = $news ?: ; // default to empty array (if no news)
@@ -391,10 +392,6 @@ function print_news($news, $dog, $max = 5, $onlyyear = null, $return = false) {
         $published = substr($item["published"], 0, 10);
         $nixtimestamp = strtotime($published);
         $newsdate = date("d M Y", $nixtimestamp);
- if ($onlyyear && date("Y", $nixtimestamp) != $onlyyear) {
- $count--;
- continue;
- }

         if ($return) {
             $retval = [
@@ -518,8 +515,12 @@ function get_nav_items(): array {

function get_news_changes()
{
- include __DIR__ . "/pregen-news.inc";
- $date = date_create($NEWS_ENTRIES[0]["updated"]);
+ $lastNews = (new NewsHandler())->getLastestNews();
+ if ($lastNews === null) {
+ return false;
+ }
+
+ $date = date_create($lastNews["updated"]);
     if (isset($_COOKIE["LAST_NEWS"]) && $_COOKIE["LAST_NEWS"] >= $date->getTimestamp()) {
         return false;
     }
@@ -534,8 +535,8 @@ function get_news_changes()

     $date->modify("+1 week");
     if ($date->getTimestamp() > $_SERVER["REQUEST_TIME"]) {
- $link = preg_replace('~^(http://php.net/|https://www.php.net/)~‘, '/', $NEWS_ENTRIES[0]["link"][0]["href"]);
- $title = $NEWS_ENTRIES[0]["title"];
+ $link = preg_replace('~^(http://php.net/|https://www.php.net/)~’, '/', $lastNews["link"][0]["href"]);
+ $title = $lastNews["title"];
         return "<a href='{$link}'>{$title}</a>";
     }
     return false;
diff --git a/index.php b/index.php
index 42c293ae5b..5e52b8a57c 100644
--- a/index.php
+++ b/index.php
@@ -1,5 +1,7 @@
<?php

+use phpweb\News\NewsHandler;
+
(function ($uri): void {
     // Special redirect cases not able to be captured in error.php
     $shortcuts = [
@@ -51,24 +53,12 @@
include_once 'include/prepend.inc';
include_once 'include/branches.inc';
include_once 'include/pregen-confs.inc';
-include_once 'include/pregen-news.inc';
include_once 'include/version.inc';

mirror_setcookie("LAST_NEWS", $_SERVER["REQUEST_TIME"], 60 * 60 * 24 * 365);

$content = "<div class='home-content'>";
-$frontpage = ;
-foreach ($NEWS_ENTRIES as $entry) {
- foreach ($entry["category"] as $category) {
- if ($category["term"] == "frontpage") {
- $frontpage = $entry;
- if (count($frontpage) >= 25) {
- break 2;
- }
- }
- }
-}
-foreach ($frontpage as $entry) {
+foreach ((new NewsHandler())->getFrontPageNews() as $entry) {
     $link = preg_replace('~^(http://php.net/|https://www.php.net/)~', '', $entry["id"]);
     $id = parse_url($entry["id"], PHP_URL_FRAGMENT);
     $date = date_create($entry['updated']);
diff --git a/src/News/NewsHandler.php b/src/News/NewsHandler.php
new file mode 100644
index 0000000000..a7e97f766e
--- /dev/null
+++ b/src/News/NewsHandler.php
@@ -0,0 +1,81 @@
+<?php
+
+declare(strict_types=1);
+
+namespace phpweb\News;
+
+use DateTimeImmutable;
+
+use function array_filter;
+use function array_values;
+use function is_array;
+
+final class NewsHandler
+{
+ private const MAX_FRONT_PAGE_NEWS = 25;
+
+ public function getLastestNews(): array|null
+ {
+ $news = $this->getPregeneratedNews();
+ if (!isset($news[0])) {
+ return null;
+ }
+
+ return $news[0];
+ }
+
+ /** @return list<array> */
+ public function getFrontPageNews(): array
+ {
+ $frontPage = ;
+ foreach ($this->getPregeneratedNews() as $entry) {
+ foreach ($entry['category'] as $category) {
+ if ($category['term'] !== 'frontpage') {
+ continue;
+ }
+
+ $frontPage = $entry;
+ if (count($frontPage) >= self::MAX_FRONT_PAGE_NEWS) {
+ break 2;
+ }
+ }
+ }
+
+ return $frontPage;
+ }
+
+ /** @return list<array> */
+ public function getConferences(): array
+ {
+ $conferences = ;
+ foreach ($this->getPregeneratedNews() as $entry) {
+ foreach ($entry['category'] as $category) {
+ if ($category['term'] !== 'cfp' && $category['term'] !== 'conferences') {
+ continue;
+ }
+
+ $conferences = $entry;
+ break;
+ }
+ }
+
+ return $conferences;
+ }
+
+ /** @return list<array> */
+ public function getNewsByYear(int $year): array
+ {
+ return array_values(array_filter(
+ $this->getPregeneratedNews(),
+ static fn (array $entry): bool => (int) (new DateTimeImmutable($entry['published']))->format('Y') === $year,
+ ));
+ }
+
+ public function getPregeneratedNews(): array
+ {
+ $NEWS_ENTRIES = null;
+ include __DIR__ . '/../../include/pregen-news.inc';
+
+ return is_array($NEWS_ENTRIES) ? $NEWS_ENTRIES : ;
+ }
+}
diff --git a/tests/Unit/News/NewsHandlerTest.php b/tests/Unit/News/NewsHandlerTest.php
new file mode 100644
index 0000000000..25df5f7e77
--- /dev/null
+++ b/tests/Unit/News/NewsHandlerTest.php
@@ -0,0 +1,57 @@
+<?php
+
+declare(strict_types=1);
+
+namespace News;
+
+use DateTimeImmutable;
+use PHPUnit\Framework\Attributes\CoversClass;
+use PHPUnit\Framework\TestCase;
+use phpweb\News\NewsHandler;
+
+#[CoversClass(NewsHandler::class)]
+final class NewsHandlerTest extends TestCase
+{
+ public function testGetLastestNews(): void
+ {
+ $newsHandler = new NewsHandler();
+ $news = $newsHandler->getPregeneratedNews();
+ self::assertArrayHasKey(0, $news);
+ self::assertSame($news[0], $newsHandler->getLastestNews());
+ }
+
+ public function testGetConferences(): void
+ {
+ $conferences = (new NewsHandler())->getConferences();
+ self::assertNotEmpty($conferences);
+ foreach ($conferences as $conference) {
+ $isConference = false;
+ foreach ($conference['category'] as $category) {
+ if ($category['term'] === 'cfp' || $category['term'] === 'conferences') {
+ $isConference = true;
+ break;
+ }
+ }
+
+ self::assertTrue($isConference);
+ }
+ }
+
+ public function testGetNewsByYear(): void
+ {
+ $news = (new NewsHandler())->getNewsByYear(2018);
+ self::assertNotEmpty($news);
+ foreach ($news as $entry) {
+ self::assertSame('2018', (new DateTimeImmutable($entry['published']))->format('Y'));
+ }
+ }
+
+ public function testGetFrontPageNews(): void
+ {
+ $frontPage = (new NewsHandler())->getFrontPageNews();
+ self::assertCount(25, $frontPage);
+ foreach ($frontPage as $news) {
+ self::assertContains(['term' => 'frontpage', 'label' => 'PHP.net frontpage news'], $news['category']);
+ }
+ }
+}