mirror of
https://github.com/Sarjuuk/aowow.git
synced 2025-11-29 15:58:16 +08:00
UtilityPage/Random
* fix invalid type error * simplyfy query building (there are no longer tables with an index named `entry`)
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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']
|
||||
|
||||
@@ -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 = [])
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user