UtilityPage/Random

* fix invalid type error
 * simplyfy query building (there are no longer tables with an index named `entry`)
This commit is contained in:
Sarjuuk
2024-08-27 14:50:55 +02:00
parent 3aede18926
commit 03cf3a5918
5 changed files with 9 additions and 10 deletions

View File

@@ -379,16 +379,15 @@ abstract class BaseType
return $data; return $data;
} }
public function getRandomId() public function getRandomId() : int
{ {
// ORDER BY RAND() is not optimal, so if anyone has an alternative idea.. // ORDER BY RAND() is not optimal, so if anyone has an alternative idea..
$where = User::isInGroup(U_GROUP_EMPLOYEE) ? 'WHERE (cuFlags & '.CUSTOM_EXCLUDE_FOR_LISTVIEW.') = 0' : null; $where = User::isInGroup(U_GROUP_EMPLOYEE) ? ' WHERE (`cuFlags` & '.CUSTOM_EXCLUDE_FOR_LISTVIEW.') = 0' : '';
$pattern = '/SELECT .* (-?`?[\w_]*\`?.?`?(id|entry)`?) AS ARRAY_KEY,?.* FROM (\?[\w_-]+) (`?\w*`?)/i';
$replace = 'SELECT $1 FROM $3 $4 '.$where.' ORDER BY RAND() ASC LIMIT 1';
$query = preg_replace($pattern, $replace, $this->queryBase); if (preg_match('/SELECT .*? FROM (\?\_[\w_-]+) /i', $this->queryBase, $m))
return DB::Aowow()->selectCell(sprintf('SELECT `id` FROM %s%s ORDER BY RAND() ASC LIMIT 1', $m[1], $where));
return DB::Aowow()->selectCell($query); return 0;
} }
public function getFoundIDs() public function getFoundIDs()

View File

@@ -14,7 +14,7 @@ class TitleList extends BaseType
public $sources = []; public $sources = [];
protected $queryBase = 'SELECT t.*, id AS ARRAY_KEY FROM ?_titles t'; protected $queryBase = 'SELECT t.*, t.id AS ARRAY_KEY FROM ?_titles t';
protected $queryOpts = array( protected $queryOpts = array(
't' => [['src']], // 11: Type::TITLE 't' => [['src']], // 11: Type::TITLE
'src' => ['j' => ['?_source src ON type = 11 AND typeId = t.id', true], 's' => ', src13, moreType, moreTypeId'] 'src' => ['j' => ['?_source src ON type = 11 AND typeId = t.id', true], 's' => ', src13, moreType, moreTypeId']

View File

@@ -12,7 +12,7 @@ class ZoneList extends BaseType
public static $brickFile = 'zone'; public static $brickFile = 'zone';
public static $dataTable = '?_zones'; public static $dataTable = '?_zones';
protected $queryBase = 'SELECT z.*, id AS ARRAY_KEY FROM ?_zones z'; protected $queryBase = 'SELECT z.*, z.id AS ARRAY_KEY FROM ?_zones z';
public function __construct(array $conditions = [], array $miscData = []) public function __construct(array $conditions = [], array $miscData = [])
{ {

View File

@@ -1787,7 +1787,7 @@ abstract class Type
/* Field Operations */ /* Field Operations */
/********************/ /********************/
public static function newList(int $type, ?array $conditions = []) : ?BaseType public static function newList(int $type, array $conditions = []) : ?BaseType
{ {
if (!self::exists($type)) if (!self::exists($type))
return null; return null;

View File

@@ -72,7 +72,7 @@ class UtilityPage extends GenericPage
{ {
case 'random': case 'random':
$type = array_rand(Type::getClassesFor(Type::FLAG_RANDOM_SEARCHABLE)); $type = array_rand(Type::getClassesFor(Type::FLAG_RANDOM_SEARCHABLE));
$typeId = (Type::newList($type, null))?->getRandomId(); $typeId = (Type::newList($type))?->getRandomId();
header('Location: ?'.Type::getFileString($type).'='.$typeId, true, 302); header('Location: ?'.Type::getFileString($type).'='.$typeId, true, 302);
die(); die();