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;
|
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()
|
||||||
|
|||||||
@@ -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']
|
||||||
|
|||||||
@@ -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 = [])
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user