Create Regex Service

This commit is contained in:
DariusIII
2025-12-29 12:35:25 +01:00
parent 4cabd00e5d
commit c8b0c4b744
8 changed files with 47 additions and 52 deletions
@@ -5,7 +5,7 @@ namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BasePageController;
use App\Models\UsenetGroup;
use App\Services\BlacklistService;
use Blacklight\Regexes;
use App\Services\RegexService;
use Illuminate\Http\Request;
class AdminAjaxController extends BasePageController
@@ -29,19 +29,19 @@ class AdminAjaxController extends BasePageController
case 'category_regex_delete':
$id = (int) $request->input('row_id');
(new Regexes(['Settings' => $this->settings, 'Table_Name' => 'category_regexes']))->deleteRegex($id);
(new RegexService('category_regexes'))->deleteRegex($id);
echo "Regex $id deleted.";
break;
case 'collection_regex_delete':
$id = (int) $request->input('row_id');
(new Regexes(['Settings' => $this->settings, 'Table_Name' => 'collection_regexes']))->deleteRegex($id);
(new RegexService('collection_regexes'))->deleteRegex($id);
echo "Regex $id deleted.";
break;
case 'release_naming_regex_delete':
$id = (int) $request->input('row_id');
(new Regexes(['Settings' => $this->settings, 'Table_Name' => 'release_naming_regexes']))->deleteRegex($id);
(new RegexService('release_naming_regexes'))->deleteRegex($id);
echo "Regex $id deleted.";
break;
@@ -4,7 +4,7 @@ namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BasePageController;
use App\Models\Category;
use Blacklight\Regexes;
use App\Services\RegexService;
use Illuminate\Http\Request;
class AdminCategoryRegexesController extends BasePageController
@@ -15,7 +15,7 @@ class AdminCategoryRegexesController extends BasePageController
public function index(Request $request)
{
$this->setAdminPrefs();
$regexes = new Regexes(['Settings' => null, 'Table_Name' => 'category_regexes']);
$regexes = new RegexService('category_regexes');
$meta_title = $title = 'Category Regex List';
@@ -40,7 +40,7 @@ class AdminCategoryRegexesController extends BasePageController
public function edit(Request $request)
{
$this->setAdminPrefs();
$regexes = new Regexes(['Settings' => null, 'Table_Name' => 'category_regexes']);
$regexes = new RegexService('category_regexes');
// Set the current action.
$action = $request->input('action') ?? 'view';
@@ -4,7 +4,7 @@ namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BasePageController;
use App\Models\Category;
use Blacklight\Regexes;
use App\Services\RegexService;
use Illuminate\Http\Request;
class AdminCollectionRegexesController extends BasePageController
@@ -40,7 +40,7 @@ class AdminCollectionRegexesController extends BasePageController
public function edit(Request $request)
{
$this->setAdminPrefs();
$regexes = new Regexes(['Settings' => null, 'Table_Name' => 'collection_regexes']);
$regexes = new RegexService('collection_regexes');
$error = '';
$regex = ['id' => '', 'regex' => '', 'description' => '', 'group_regex' => '', 'ordinal' => '', 'status' => 1];
$meta_title = $title = 'Collections Regex';
@@ -112,7 +112,7 @@ class AdminCollectionRegexesController extends BasePageController
$data = null;
if ($group && $regex) {
$data = (new Regexes(['Settings' => null, 'Table_Name' => 'collection_regexes']))->testCollectionRegex($group, $regex, $limit);
$data = (new RegexService('collection_regexes'))->testCollectionRegex($group, $regex, $limit);
}
$this->viewData = array_merge($this->viewData, [
@@ -4,7 +4,7 @@ namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BasePageController;
use App\Models\Category;
use Blacklight\Regexes;
use App\Services\RegexService;
use Illuminate\Http\Request;
class AdminReleaseNamingRegexesController extends BasePageController
@@ -43,7 +43,7 @@ class AdminReleaseNamingRegexesController extends BasePageController
public function edit(Request $request)
{
$this->setAdminPrefs();
$regexes = new Regexes(['Settings' => null, 'Table_Name' => 'release_naming_regexes']);
$regexes = new RegexService('release_naming_regexes');
// Set the current action.
$action = $request->input('action') ?? 'view';
@@ -120,7 +120,7 @@ class AdminReleaseNamingRegexesController extends BasePageController
$data = null;
if ($group && $regex) {
$data = (new Regexes(['Settings' => null, 'Table_Name' => 'release_naming_regexes']))->testReleaseNamingRegex($group, $regex, $showLimit, $queryLimit);
$data = (new RegexService('release_naming_regexes'))->testReleaseNamingRegex($group, $regex, $showLimit, $queryLimit);
}
$this->viewData = array_merge($this->viewData, [
+2 -3
View File
@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace App\Services;
use Blacklight\Regexes;
/**
* Cleans names for collections/imports/namefixer.
@@ -63,7 +62,7 @@ class CollectionsCleaningService
/**
* Regex handler for database-stored patterns
*/
protected Regexes $_regexes;
protected RegexService $_regexes;
/**
* CollectionsCleaningService constructor.
@@ -79,7 +78,7 @@ class CollectionsCleaningService
$this->e1 = self::REGEX_FILE_EXTENSIONS.self::REGEX_END;
$this->e2 = self::REGEX_FILE_EXTENSIONS.self::REGEX_SUBJECT_SIZE.self::REGEX_END;
$this->_regexes = new Regexes(['Table_Name' => 'collection_regexes']);
$this->_regexes = new RegexService('collection_regexes');
}
/**
+30 -32
View File
@@ -1,6 +1,8 @@
<?php
namespace Blacklight;
declare(strict_types=1);
namespace App\Services;
use App\Models\Category;
use App\Models\CategoryRegex;
@@ -13,42 +15,38 @@ use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
/**
* Class Regexes.
* Service for managing regex patterns for collections, categories, and release naming.
*/
class Regexes
class RegexService
{
/**
* @var mixed The ID of the Regex input string matched or the generic name
* The ID of the Regex input string matched or the generic name
*/
public $matchedRegex;
public mixed $matchedRegex;
/**
* @var string Name of the current table we are working on.
* Name of the current table we are working on.
*/
public $tableName;
public string $tableName;
/**
* @var array Cache of regex and their TTL.
* Cache of regex and their TTL.
*/
protected $_regexCache;
protected array $_regexCache = [];
/**
* @var int
* Default category ID
*/
protected $_categoriesID = Category::OTHER_MISC;
protected int $_categoriesID = Category::OTHER_MISC;
/**
* @throws \Exception
* RegexService constructor.
*
* @param string $tableName The table name to work with (collection_regexes, category_regexes, release_naming_regexes)
*/
public function __construct(array $options = [])
public function __construct(string $tableName = '')
{
$defaults = [
'Settings' => null,
'Table_Name' => '',
];
$options += $defaults;
$this->tableName = $options['Table_Name'];
$this->tableName = $tableName;
}
/**
@@ -79,8 +77,8 @@ class Regexes
return (bool) DB::update(
sprintf(
'UPDATE %s
SET group_regex = %s, regex = %s, status = %d, description = %s, ordinal = %d %s
WHERE id = %d',
SET group_regex = %s, regex = %s, status = %d, description = %s, ordinal = %d %s
WHERE id = %d',
$this->tableName,
trim(escapeString($data['group_regex'])),
trim(escapeString($data['regex'])),
@@ -102,6 +100,8 @@ class Regexes
}
/**
* Get paginated regex results.
*
* @return mixed
*/
public function getRegex(string $group_regex = '')
@@ -144,7 +144,6 @@ class Regexes
/**
* Delete a regex using its id.
*
*
* @throws \Throwable
*/
public function deleteRegex(int $id): void
@@ -159,7 +158,6 @@ class Regexes
*
* Requires table per group to be on.
*
*
* @throws \Exception
*/
public function testCollectionRegex(string $groupName, string $regex, int $limit): array
@@ -172,11 +170,10 @@ class Regexes
$rows = DB::select(
'SELECT
b.name, b.totalparts, b.currentparts, HEX(b.binaryhash) AS binaryhash,
c.fromname, c.collectionhash
FROM binaries b
INNER JOIN collections c ON c.id = b.collections_id'
b.name, b.totalparts, b.currentparts, HEX(b.binaryhash) AS binaryhash,
c.fromname, c.collectionhash
FROM binaries b
INNER JOIN collections c ON c.id = b.collections_id'
);
$data = [];
@@ -221,6 +218,8 @@ class Regexes
}
/**
* Test release naming regex against releases.
*
* @throws \Exception
*/
public function testReleaseNamingRegex($groupName, $regex, $displayLimit, $queryLimit): array
@@ -262,7 +261,6 @@ class Regexes
/**
* This will try to find regex in the DB for a group and a usenet subject, attempt to match them and return the matches.
*
*
* @throws \Exception
*/
public function tryRegex(string $subject, string $groupName): string
@@ -319,7 +317,6 @@ class Regexes
*
* Requires at least 1 named captured group.
*
*
* @throws \Exception
*/
protected function _matchRegex(string $regex, string $subject): string
@@ -339,7 +336,7 @@ class Regexes
$returnString .= $value; // Concatenate the string to return.
break;
case 'category_regexes':
$returnString = $this->_categoriesID; // Regex matched, so return the category ID.
$returnString = (string) $this->_categoriesID; // Regex matched, so return the category ID.
break 2;
}
}
@@ -356,3 +353,4 @@ class Regexes
return $group_regex ? ('WHERE group_regex LIKE '.escapeString('%'.$group_regex.'%')) : '';
}
}
+2 -3
View File
@@ -5,7 +5,6 @@ declare(strict_types=1);
namespace App\Services;
use App\Models\Predb;
use Blacklight\Regexes;
/**
* Cleans names for releases/imports/namefixer.
@@ -44,7 +43,7 @@ class ReleaseCleaningService
public string $subject = '';
protected Regexes $_regexes;
protected RegexService $_regexes;
/**
* ReleaseCleaningService constructor.
@@ -57,7 +56,7 @@ class ReleaseCleaningService
$this->e0 = CollectionsCleaningService::REGEX_FILE_EXTENSIONS;
$this->e1 = CollectionsCleaningService::REGEX_FILE_EXTENSIONS.CollectionsCleaningService::REGEX_END;
$this->e2 = CollectionsCleaningService::REGEX_FILE_EXTENSIONS.CollectionsCleaningService::REGEX_SUBJECT_SIZE.CollectionsCleaningService::REGEX_END;
$this->_regexes = new Regexes(['Settings' => null, 'Table_Name' => 'release_naming_regexes']);
$this->_regexes = new RegexService('release_naming_regexes');
}
/**
-1
View File
@@ -141,7 +141,6 @@
"autoload": {
"psr-4": {
"App\\": "app/",
"Blacklight\\": "Blacklight/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},