diff --git a/includes/basetype.class.php b/includes/basetype.class.php index ab591850..39c40539 100644 --- a/includes/basetype.class.php +++ b/includes/basetype.class.php @@ -379,16 +379,15 @@ abstract class BaseType return $data; } - public function getRandomId() + public function getRandomId() : int { // 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; - $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'; + $where = User::isInGroup(U_GROUP_EMPLOYEE) ? ' WHERE (`cuFlags` & '.CUSTOM_EXCLUDE_FOR_LISTVIEW.') = 0' : ''; - $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() diff --git a/includes/types/title.class.php b/includes/types/title.class.php index 84a02f86..b1360e5b 100644 --- a/includes/types/title.class.php +++ b/includes/types/title.class.php @@ -14,7 +14,7 @@ class TitleList extends BaseType 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( 't' => [['src']], // 11: Type::TITLE 'src' => ['j' => ['?_source src ON type = 11 AND typeId = t.id', true], 's' => ', src13, moreType, moreTypeId'] diff --git a/includes/types/zone.class.php b/includes/types/zone.class.php index e8975a64..73b60458 100644 --- a/includes/types/zone.class.php +++ b/includes/types/zone.class.php @@ -12,7 +12,7 @@ class ZoneList extends BaseType public static $brickFile = 'zone'; 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 = []) { diff --git a/includes/utilities.php b/includes/utilities.php index 8c9277bf..6d14b6ba 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -1787,7 +1787,7 @@ abstract class Type /* Field Operations */ /********************/ - public static function newList(int $type, ?array $conditions = []) : ?BaseType + public static function newList(int $type, array $conditions = []) : ?BaseType { if (!self::exists($type)) return null; diff --git a/pages/utility.php b/pages/utility.php index e6a552bf..6fc4455e 100644 --- a/pages/utility.php +++ b/pages/utility.php @@ -72,7 +72,7 @@ class UtilityPage extends GenericPage { case 'random': $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); die();