Repository: litespeedtech/lscache_wp
Pull request #826: Move crawler statuses to const
Author: login: timotei-litespeed
Created: 2025-04-07T18:13:16Z | Merged: 2025-04-09T14:55:00Z

In completion to PR #814 


Diff:
diff --git a/cli/crawler.cls.php b/cli/crawler.cls.php
index 8505875d7..8a12f1bba 100644
--- a/cli/crawler.cls.php
+++ b/cli/crawler.cls.php
@@ -84,14 +84,14 @@ public function list()
 
 		$list = array();
 		foreach ($crawler_list as $i => $v) {
-			$hit = !empty($summary['crawler_stats'][$i]['H']) ? $summary['crawler_stats'][$i]['H'] : 0;
-			$miss = !empty($summary['crawler_stats'][$i]['M']) ? $summary['crawler_stats'][$i]['M'] : 0;
+			$hit = !empty($summary['crawler_stats'][$i][Crawler2::STATUS_HIT]) ? $summary['crawler_stats'][$i][Crawler2::STATUS_HIT] : 0;
+			$miss = !empty($summary['crawler_stats'][$i][Crawler2::STATUS_MISS]) ? $summary['crawler_stats'][$i][Crawler2::STATUS_MISS] : 0;
 
-			$blacklisted = !empty($summary['crawler_stats'][$i]['B']) ? $summary['crawler_stats'][$i]['B'] : 0;
-			$blacklisted += !empty($summary['crawler_stats'][$i]['N']) ? $summary['crawler_stats'][$i]['N'] : 0;
+			$blacklisted = !empty($summary['crawler_stats'][$i][Crawler2::STATUS_BLACKLIST]) ? $summary['crawler_stats'][$i][Crawler2::STATUS_BLACKLIST] : 0;
+			$blacklisted += !empty($summary['crawler_stats'][$i][Crawler2::STATUS_NOCACHE]) ? $summary['crawler_stats'][$i][Crawler2::STATUS_NOCACHE] : 0;
 
-			if (isset($summary['crawler_stats'][$i]['W'])) {
-				$waiting = $summary['crawler_stats'][$i]['W'] ?: 0;
+			if (isset($summary['crawler_stats'][$i][Crawler2::STATUS_WAIT])) {
+				$waiting = $summary['crawler_stats'][$i][Crawler2::STATUS_WAIT] ?: 0;
 			} else {
 				$waiting = $summary['list_size'] - $hit - $miss - $blacklisted;
 			}
diff --git a/src/crawler-map.cls.php b/src/crawler-map.cls.php
index de3762d3e..c2a3de955 100644
--- a/src/crawler-map.cls.php
+++ b/src/crawler-map.cls.php
@@ -71,7 +71,7 @@ public function save_map_status($list, $curr_crawler)
 			$wpdb->query("UPDATE `$this->_tb` SET res = $sql_res WHERE id IN ( $id_all )");
 
 			// Add blacklist
-			if ($bit == 'B' || $bit == 'N') {
+			if ($bit == Crawler::STATUS_BLACKLIST || $bit == Crawler::STATUS_NOCACHE) {
 				$q = "SELECT a.id, a.url FROM `$this->_tb_blacklist` a LEFT JOIN `$this->_tb` b ON b.url=a.url WHERE b.id IN ( $id_all )";
 				$existing = $wpdb->get_results($q, ARRAY_A);
 				// Update current crawler status tag in existing blacklist
@@ -129,7 +129,7 @@ public function save_map_status($list, $curr_crawler)
 				self::debug("Update map reason [code] $code [pos] left $curr_crawler right -$right_pos [count] $count");
 
 				// Update blacklist reason
-				if ($bit == 'B' || $bit == 'N') {
+				if ($bit == Crawler::STATUS_BLACKLIST || $bit == Crawler::STATUS_NOCACHE) {
 					$count = $wpdb->query(
 						"UPDATE `$this->_tb_blacklist` a LEFT JOIN `$this->_tb` b ON b.url = a.url SET a.reason=CONCAT(SUBSTRING_INDEX(a.reason, ',', $curr_crawler), '$code', SUBSTRING_INDEX(a.reason, ',', -$right_pos)) WHERE b.id IN (" .
 							implode(',', $v2) .
@@ -161,7 +161,7 @@ public function blacklist_add($id)
 
 		// Build res&reason
 		$total_crawler = count(Crawler::cls()->list_crawlers());
-		$res = str_repeat('B', $total_crawler);
+		$res = str_repeat(Crawler::STATUS_BLACKLIST, $total_crawler);
 		$reason = implode(',', array_fill(0, $total_crawler, 'Man'));
 
 		$row = $wpdb->get_row("SELECT a.url, b.id FROM `$this->_tb` a LEFT JOIN `$this->_tb_blacklist` b ON b.url = a.url WHERE a.id = '$id'", ARRAY_A);
@@ -200,7 +200,15 @@ public function blacklist_del($id)
 		$id = (int) $id;
 		self::debug('blacklist delete [id] ' . $id);
 
-		$wpdb->query("UPDATE `$this->_tb` SET res=REPLACE(REPLACE(res, 'N', '-'), 'B', '-') WHERE url=(SELECT url FROM `$this->_tb_blacklist` WHERE id='$id')");
+		$sql = sprintf(
+			"UPDATE `%s` SET res=REPLACE(REPLACE(res, '%s', '-'), '%s', '-') WHERE url=(SELECT url FROM `%s` WHERE id=%d)",
+			$this->_tb,
+			Crawler::STATUS_NOCACHE,
+			Crawler::STATUS_BLACKLIST,
+			$this->_tb_blacklist,
+			$id
+		);
+		$wpdb->query($sql);
 		$wpdb->query("DELETE FROM `$this->_tb_blacklist` WHERE id='$id'");
 	}
 
@@ -219,7 +227,8 @@ public function blacklist_empty()
 		}
 
 		self::debug('Truncate blacklist');
-		$wpdb->query("UPDATE `$this->_tb` SET res=REPLACE(REPLACE(res, 'N', '-'), 'B', '-')");
+		$sql = sprintf("UPDATE `%s` SET res=REPLACE(REPLACE(res, '%s', '-'), '%s', '-')", $this->_tb, Crawler::STATUS_NOCACHE, Crawler::STATUS_BLACKLIST);
+		$wpdb->query($sql);
 		$wpdb->query("TRUNCATE `$this->_tb_blacklist`");
 	}
 
@@ -304,13 +313,13 @@ public function list_map($limit, $offset = false)
 		if (!empty($_POST['kw'])) {
 			$q = "SELECT * FROM `$this->_tb` WHERE url LIKE %s";
 			if ($type == 'hit') {
-				$q .= " AND res LIKE '%H%'";
+				$q .= " AND res LIKE '%" . Crawler::STATUS_HIT . "%'";
 			}
 			if ($type == 'miss') {
-				$q .= " AND res LIKE '%M%'";
+				$q .= " AND res LIKE '%" . Crawler::STATUS_MISS . "%'";
 			}
 			if ($type == 'blacklisted') {
-				$q .= " AND res LIKE '%B%'";
+				$q .= " AND res LIKE '%" . Crawler::STATUS_BLACKLIST . "%'";
 			}
 			$q .= ' ORDER BY id LIMIT %d, %d';
 			$where = '%' . $wpdb->esc_like($_POST['kw']) . '%';
@@ -319,13 +328,13 @@ public function list_map($limit, $offset = false)
 
 		$q = "SELECT * FROM `$this->_tb`";
 		if ($type == 'hit') {
-			$q .= " WHERE res LIKE '%H%'";
+			$q .= " WHERE res LIKE '%" . Crawler::STATUS_HIT . "%'";
 		}
 		if ($type == 'miss') {
-			$q .= " WHERE res LIKE '%M%'";
+			$q .= " WHERE res LIKE '%" . Crawler::STATUS_MISS . "%'";
 		}
 		if ($type == 'blacklisted') {
-			$q .= " WHERE res LIKE '%B%'";
+			$q .= " WHERE res LIKE '%" . Crawler::STATUS_BLACKLIST . "%'";
 		}
 		$q .= ' ORDER BY id LIMIT %d, %d';
 		// self::debug("q=$q offset=$offset, limit=$limit");
@@ -347,13 +356,13 @@ public function count_map()
 
 		$type = Router::verify_type();
 		if ($type == 'hit') {
-			$q .= " WHERE res LIKE '%H%'";
+			$q .= " WHERE res LIKE '%" . Crawler::STATUS_HIT . "%'";
 		}
 		if ($type == 'miss') {
-			$q .= " WHERE res LIKE '%M%'";
+			$q .= " WHERE res LIKE '%" . Crawler::STATUS_MISS . "%'";
 		}
 		if ($type == 'blacklisted') {
-			$q .= " WHERE res LIKE '%B%'";
+			$q .= " WHERE res LIKE '%" . Crawler::STATUS_BLACKLIST . "%'";
 		}
 
 		return $wpdb->get_var($q);
diff --git a/src/crawler.cls.php b/src/crawler.cls.php
index fa1008229..6b0a3ee88 100644
--- a/src/crawler.cls.php
+++ b/src/crawler.cls.php
@@ -26,6 +26,12 @@ class Crawler extends Root
 	const FAST_USER_AGENT = 'lscache_runner';
 	const CHUNKS = 10000;
 
+	const STATUS_WAIT = 'W';
+	const STATUS_HIT = 'H';
+	const STATUS_MISS = 'M';
+	const STATUS_BLACKLIST = 'B';
+	const STATUS_NOCACHE = 'N';
+
 	private $_sitemeta = 'meta.data';
 	private $_resetfile;
 	private $_end_reason;
@@ -909,10 +915,10 @@ private function _multi_request($rows, $options)
 		$CRAWLER_DROP_DOMAIN = defined('LITESPEED_CRAWLER_DROP_DOMAIN') ? LITESPEED_CRAWLER_DROP_DOMAIN : false;
 		$curls = array();
 		foreach ($rows as $row) {
-			if (substr($row['res'], $this->_summary['curr_crawler'], 1) == 'B') {
+			if (substr($row['res'], $this->_summary['curr_crawler'], 1) == self::STATUS_BLACKLIST) {
 				continue;
 			}
-			if (substr($row['res'], $this->_summary['curr_crawler'], 1) == 'N') {
+			if (substr($row['res'], $this->_summary['curr_crawler'], 1) == self::STATUS_NOCACHE) {
 				continue;
 			}
 
@@ -971,10 +977,10 @@ private function _multi_request($rows, $options)
 		// curl done
 		$ret = array();
 		foreach ($rows as $row) {
-			if (substr($row['res'], $this->_summary['curr_crawler'], 1) == 'B') {
+			if (substr($row['res'], $this->_summary['curr_crawler'], 1) == self::STATUS_BLACKLIST) {
 				continue;
 			}
-			if (substr($row['res'], $this->_summary['curr_crawler'], 1) == 'N') {
+			if (substr($row['res'], $this->_summary['curr_crawler'], 1) == self::STATUS_NOCACHE) {
 				continue;
 			}
 			// self::debug('-----debug3');
@@ -1005,16 +1011,16 @@ private function _multi_request($rows, $options)
 	 */
 	private function _status2title($status)
 	{
-		if ($status == 'H') {
+		if ($status == self::STATUS_HIT) {
 			return '✅ Hit';
 		}
-		if ($status == 'M') {
+		if ($status == self::STATUS_MISS) {
 			return '😊 Miss';
 		}
-		if ($status == 'B') {
+		if ($status == self::STATUS_BLACKLIST) {
 			return '😅 Blacklisted';
 		}
-		if ($status == 'N') {
+		if ($status == self::STATUS_NOCACHE) {
 			return '😅 Blacklisted';
 		}
 		return '🛸 Unknown';
@@ -1030,21 +1036,21 @@ private function _status_parse($header, $code, $url)
 	{
 		// self::debug('http status code: ' . $code . ' [headers]', $header);
 		if ($code == 201) {
-			return 'H';
+			return self::STATUS_HIT;
 		}
 
 		if (stripos($header, 'X-Litespeed-Cache-Control: no-cache') !== false) {
 			// If is from DIVI, taken as miss
 			if (defined('LITESPEED_CRAWLER_IGNORE_NONCACHEABLE') && LITESPEED_CRAWLER_IGNORE_NONCACHEABLE) {
-				return 'M';
+				return self::STATUS_MISS;
 			}
 
 			// If blacklist is disabled
 			if ((defined('LITESPEED_CRAWLER_DISABLE_BLOCKLIST') && LITESPEED_CRAWLER_DISABLE_BLOCKLIST) || apply_filters('litespeed_crawler_disable_blocklist', false, $url)) {
-				return 'M';
+				return self::STATUS_MISS;
 			}
 
-			return 'N'; // Blacklist
+			return self::STATUS_NOCACHE; // Blacklist
 		}
 
 		$_cache_headers = array('x-qc-cache', 'x-lsadc-cache', 'x-litespeed-cache');
@@ -1052,18 +1058,18 @@ private function _status_parse($header, $code, $url)
 		foreach ($_cache_headers as $_header) {
 			if (stripos($header, $_header) !== false) {
 				if (stripos($header, $_header . ': miss') !== false) {
-					return 'M'; // Miss
+					return self::STATUS_MISS; // Miss
 				}
-				return 'H'; // Hit
+				return self::STATUS_HIT; // Hit
 			}
 		}
 
 		// If blacklist is disabled
 		if ((defined('LITESPEED_CRAWLER_DISABLE_BLOCKLIST') && LITESPEED_CRAWLER_DISABLE_BLOCKLIST) || apply_filters('litespeed_crawler_disable_blocklist', false, $url)) {
-			return 'M';
+			return self::STATUS_MISS;
 		}
 
-		return 'B'; // Blacklist
+		return self::STATUS_BLACKLIST; // Blacklist
 	}
 
 	/**
@@ -1383,10 +1389,10 @@ public function display_status($status_row, $reason_set)
 
 		$_status_list = array(
 			'-' => 'default',
-			'M' => 'primary',
-			'H' => 'success',
-			'B' => 'danger',
-			'N' => 'warning',
+			self::STATUS_MISS => 'primary',
+			self::STATUS_HIT => 'success',
+			self::STATUS_BLACKLIST => 'danger',
+			self::STATUS_NOCACHE => 'warning',
 		);
 
 		$reason_set = explode(',', $reason_set);
diff --git a/tpl/crawler/summary.tpl.php b/tpl/crawler/summary.tpl.php
index dafa6af75..9cfc65213 100644
--- a/tpl/crawler/summary.tpl.php
+++ b/tpl/crawler/summary.tpl.php
@@ -141,14 +141,14 @@
 				</thead>
 				<tbody>
 					<?php foreach ($crawler_list as $i => $v) :
-						$hit = !empty($summary['crawler_stats'][$i]['H']) ? $summary['crawler_stats'][$i]['H'] : 0;
-						$miss = !empty($summary['crawler_stats'][$i]['M']) ? $summary['crawler_stats'][$i]['M'] : 0;
+						$hit = !empty($summary['crawler_stats'][$i][Crawler::STATUS_HIT]) ? $summary['crawler_stats'][$i][Crawler::STATUS_HIT] : 0;
+						$miss = !empty($summary['crawler_stats'][$i][Crawler::STATUS_MISS]) ? $summary['crawler_stats'][$i][Crawler::STATUS_MISS] : 0;
 
-						$blacklisted = !empty($summary['crawler_stats'][$i]['B']) ? $summary['crawler_stats'][$i]['B'] : 0;
-						$blacklisted += !empty($summary['crawler_stats'][$i]['N']) ? $summary['crawler_stats'][$i]['N'] : 0;
+						$blacklisted = !empty($summary['crawler_stats'][$i][Crawler::STATUS_BLACKLIST]) ? $summary['crawler_stats'][$i][Crawler::STATUS_BLACKLIST] : 0;
+						$blacklisted += !empty($summary['crawler_stats'][$i][Crawler::STATUS_NOCACHE]) ? $summary['crawler_stats'][$i][Crawler::STATUS_NOCACHE] : 0;
 
-						if (isset($summary['crawler_stats'][$i]['W'])) {
-							$waiting = $summary['crawler_stats'][$i]['W'] ?: 0;
+						if (isset($summary['crawler_stats'][$i][Crawler::STATUS_WAIT])) {
+							$waiting = $summary['crawler_stats'][$i][Crawler::STATUS_WAIT] ?: 0;
 						} else {
 							$waiting = $summary['list_size'] - $hit - $miss - $blacklisted;
 						}


Reviews:
user: login: hi-hai
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-04-08T15:17:25Z
user: login: timotei-litespeed
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-08T18:41:45Z
user: login: hi-hai
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-04-08T18:54:32Z
user: login: timotei-litespeed
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-08T20:35:41Z
user: login: timotei-litespeed
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-09T11:54:28Z

Review comments:
diff_hunk: @@ -200,7 +200,13 @@ public function blacklist_del($id)
 		$id = (int) $id;
 		self::debug('blacklist delete [id] ' . $id);
 
-		$wpdb->query("UPDATE `$this->_tb` SET res=REPLACE(REPLACE(res, 'N', '-'), 'B', '-') WHERE url=(SELECT url FROM `$this->_tb_blacklist` WHERE id='$id')");
+		$wpdb->query(
+			"UPDATE `$this->_tb` SET res=REPLACE(REPLACE(res, '" .
path: src/crawler-map.cls.php
user: login: hi-hai
body: @timotei-litespeed  this is diff to read. Better sprintf?
created_at: 2025-04-08T15:17:25Z
author_association: COLLABORATOR
diff_hunk: @@ -200,7 +200,13 @@ public function blacklist_del($id)
 		$id = (int) $id;
 		self::debug('blacklist delete [id] ' . $id);
 
-		$wpdb->query("UPDATE `$this->_tb` SET res=REPLACE(REPLACE(res, 'N', '-'), 'B', '-') WHERE url=(SELECT url FROM `$this->_tb_blacklist` WHERE id='$id')");
+		$wpdb->query(
+			"UPDATE `$this->_tb` SET res=REPLACE(REPLACE(res, '" .
path: src/crawler-map.cls.php
user: login: timotei-litespeed
body: Changed
created_at: 2025-04-08T18:41:45Z
author_association: CONTRIBUTOR
diff_hunk: @@ -200,7 +200,8 @@ public function blacklist_del($id)
 		$id = (int) $id;
 		self::debug('blacklist delete [id] ' . $id);
 
-		$wpdb->query("UPDATE `$this->_tb` SET res=REPLACE(REPLACE(res, 'N', '-'), 'B', '-') WHERE url=(SELECT url FROM `$this->_tb_blacklist` WHERE id='$id')");
+		$sql = "UPDATE `%s` SET res=REPLACE(REPLACE(res, '%s', '-'), '%s', '-') WHERE url=(SELECT url FROM `%s` WHERE id=%d)";
+		$wpdb->query(sprintf($sql, $this->_tb, Crawler::STATUS_NOCACHE, Crawler::STATUS_BLACKLIST, $this->_tb_blacklist, $id));
path: src/crawler-map.cls.php
user: login: hi-hai
body: @timotei-litespeed if we move `sprintf` up to the `$sql=` line, will it be more clear?
created_at: 2025-04-08T18:54:32Z
author_association: COLLABORATOR
diff_hunk: @@ -200,7 +200,8 @@ public function blacklist_del($id)
 		$id = (int) $id;
 		self::debug('blacklist delete [id] ' . $id);
 
-		$wpdb->query("UPDATE `$this->_tb` SET res=REPLACE(REPLACE(res, 'N', '-'), 'B', '-') WHERE url=(SELECT url FROM `$this->_tb_blacklist` WHERE id='$id')");
+		$sql = "UPDATE `%s` SET res=REPLACE(REPLACE(res, '%s', '-'), '%s', '-') WHERE url=(SELECT url FROM `%s` WHERE id=%d)";
+		$wpdb->query(sprintf($sql, $this->_tb, Crawler::STATUS_NOCACHE, Crawler::STATUS_BLACKLIST, $this->_tb_blacklist, $id));
path: src/crawler-map.cls.php
user: login: timotei-litespeed
body: ok :)
created_at: 2025-04-08T20:35:41Z
author_association: CONTRIBUTOR
diff_hunk: @@ -200,7 +200,8 @@ public function blacklist_del($id)
 		$id = (int) $id;
 		self::debug('blacklist delete [id] ' . $id);
 
-		$wpdb->query("UPDATE `$this->_tb` SET res=REPLACE(REPLACE(res, 'N', '-'), 'B', '-') WHERE url=(SELECT url FROM `$this->_tb_blacklist` WHERE id='$id')");
+		$sql = "UPDATE `%s` SET res=REPLACE(REPLACE(res, '%s', '-'), '%s', '-') WHERE url=(SELECT url FROM `%s` WHERE id=%d)";
+		$wpdb->query(sprintf($sql, $this->_tb, Crawler::STATUS_NOCACHE, Crawler::STATUS_BLACKLIST, $this->_tb_blacklist, $id));
path: src/crawler-map.cls.php
user: login: timotei-litespeed
body: Done
created_at: 2025-04-09T11:54:27Z
author_association: CONTRIBUTOR

PR comments:
user: login: szepeviktor
created_at: 2025-04-07T18:25:08Z
body: 😍 This is very nice.
All literal strings should be moved to constants.
author_association: CONTRIBUTOR
user: login: timotei-litespeed
created_at: 2025-04-07T20:10:43Z
body: @szepeviktor 
Thank you :)
You last sentence is a general comment, or you saw a place that I forgot to change?
author_association: CONTRIBUTOR
user: login: szepeviktor
created_at: 2025-04-07T20:47:16Z
body: This is just my policy.
I let you work.

author_association: CONTRIBUTOR
user: login: timotei-litespeed
created_at: 2025-04-08T09:21:36Z
body: @szepeviktor I understand :)
Thank you for ideeas! :)
author_association: CONTRIBUTOR
user: login: szepeviktor
created_at: 2025-04-08T09:51:01Z
body: >  Note that class constants are allocated once per class, and not for each class instance. 

https://www.php.net/manual/en/language.oop5.constants.php

So instead of `$object::NAME` please write `Class::NAME`
author_association: CONTRIBUTOR
user: login: timotei-litespeed
created_at: 2025-04-08T10:16:24Z
body: Yes, you are right. But having the objects there, I used what is already there.
author_association: CONTRIBUTOR
user: login: hi-hai
created_at: 2025-04-08T13:17:49Z
body: Should use `self::CONSTNAME`
author_association: COLLABORATOR
user: login: timotei-litespeed
created_at: 2025-04-08T14:45:53Z
body: @hi-hai and @szepeviktor thank you for suggestion. Change added
author_association: CONTRIBUTOR