2023-07-12 21:52:55 +00:00
< ? php
namespace Misuzu\Emoticons ;
2023-07-14 20:26:38 +00:00
use InvalidArgumentException ;
2023-07-12 21:52:55 +00:00
use RuntimeException ;
2023-07-22 14:00:51 +00:00
use Index\Data\DbStatementCache ;
2023-07-12 21:52:55 +00:00
use Index\Data\IDbConnection ;
class Emotes {
private const EMOTE_ORDER = [
'order' => 'emote_order' ,
'id' => 'emote_id' ,
'rank' => 'emote_hierarchy' ,
];
private IDbConnection $dbConn ;
2023-07-14 20:26:38 +00:00
private DbStatementCache $cache ;
2023-07-12 21:52:55 +00:00
public function __construct ( IDbConnection $dbConn ) {
$this -> dbConn = $dbConn ;
2023-07-14 20:26:38 +00:00
$this -> cache = new DbStatementCache ( $dbConn );
2023-07-12 21:52:55 +00:00
}
public function getEmoteById ( string $emoteId , bool $withStrings = false ) : EmoteInfo {
2023-07-14 20:26:38 +00:00
$stmt = $this -> cache -> get ( 'SELECT emote_id, emote_order, emote_hierarchy, emote_url FROM msz_emoticons WHERE emote_id = ?' );
$stmt -> addParameter ( 1 , $emoteId );
$stmt -> execute ();
2023-07-12 21:52:55 +00:00
2023-07-14 20:26:38 +00:00
$result = $stmt -> getResult ();
2023-07-12 21:52:55 +00:00
if ( ! $result -> next ())
2023-07-15 02:05:49 +00:00
throw new RuntimeException ( 'No emoticon with that ID exists.' );
2023-07-12 21:52:55 +00:00
$strings = $withStrings ? $this -> getEmoteStrings ( $emoteId ) : [];
return new EmoteInfo ( $result , $strings );
}
public static function emoteOrderOptions () : array {
return array_keys ( self :: EMOTE_ORDER );
}
// TODO: pagination
public function getAllEmotes (
int $minRank = - 1 ,
string $orderBy = '' ,
bool $desc = false ,
bool $withStrings = false
) : array {
if ( $minRank < 0 ) $minRank = PHP_INT_MAX ;
$orderBy = self :: EMOTE_ORDER [ $orderBy ] ? ? self :: EMOTE_ORDER [ array_key_first ( self :: EMOTE_ORDER )];
2023-07-14 20:26:38 +00:00
$stmt = $this -> cache -> get ( sprintf (
2023-07-12 21:52:55 +00:00
'SELECT emote_id, emote_order, emote_hierarchy, emote_url FROM msz_emoticons WHERE emote_hierarchy <= ? ORDER BY %s %s' ,
$orderBy , ( $desc ? 'DESC' : 'ASC' )
));
2023-07-14 20:26:38 +00:00
$stmt -> addParameter ( 1 , $minRank );
$stmt -> execute ();
2023-07-12 21:52:55 +00:00
$emotes = [];
2023-07-14 20:26:38 +00:00
$result = $stmt -> getResult ();
2023-07-12 21:52:55 +00:00
if ( $withStrings ) {
while ( $result -> next ())
$emotes [] = new EmoteInfo ( $result , $this -> getEmoteStrings (( string ) $result -> getInteger ( 0 )));
} else {
while ( $result -> next ())
$emotes [] = new EmoteInfo ( $result );
}
return $emotes ;
}
private static function checkEmoteUrlInternal ( string $url ) : string {
// more checks?
if ( empty ( $url ))
return 'empty' ;
if ( trim ( $url ) !== $url )
return 'spaces' ;
return '' ;
}
public function checkEmoteUrl ( string $url ) : string {
$check = self :: checkEmoteUrlInternal ( $url );
if ( $check !== '' )
return $check ;
2023-07-14 20:26:38 +00:00
$stmt = $this -> cache -> get ( 'SELECT COUNT(*) FROM msz_emoticons WHERE emote_url = ?' );
$stmt -> addParameter ( 1 , $url );
$stmt -> execute ();
$result = $stmt -> getResult ();
2023-07-12 21:52:55 +00:00
if ( $result -> next () && $result -> getInteger ( 0 ) > 0 )
return 'used' ;
return '' ;
}
public function createEmote ( string $url , int $minRank = 0 , ? int $order = null ) : EmoteInfo {
$check = self :: checkEmoteUrlInternal ( $url );
if ( $check !== '' )
throw new InvalidArgumentException ( '$url is not correctly formatted: ' . $check );
2023-07-14 20:26:38 +00:00
$stmt = $this -> cache -> get ( 'INSERT INTO msz_emoticons (emote_url, emote_hierarchy, emote_order) SELECT ?, ?, COALESCE(?, (SELECT FLOOR(MAX(emote_order) / 10) * 10 + 10 FROM msz_emoticons), 10)' );
$stmt -> addParameter ( 1 , $url );
$stmt -> addParameter ( 2 , $minRank );
$stmt -> addParameter ( 3 , $order );
$stmt -> execute ();
2023-07-12 21:52:55 +00:00
return $this -> getEmoteById (( string ) $this -> dbConn -> getLastInsertId ());
}
public function deleteEmote ( EmoteInfo | string $infoOrId ) : void {
if ( $infoOrId instanceof EmoteInfo )
$infoOrId = $infoOrId -> getId ();
2023-07-14 20:26:38 +00:00
$stmt = $this -> cache -> get ( 'DELETE FROM msz_emoticons WHERE emote_id = ?' );
$stmt -> addParameter ( 1 , $infoOrId );
$stmt -> execute ();
2023-07-12 21:52:55 +00:00
}
public function updateEmote (
EmoteInfo | string $infoOrId ,
? int $order = null ,
? int $minRank = null ,
? string $url = null
) : void {
if ( $url !== null ) {
$check = self :: checkEmoteUrlInternal ( $url );
if ( $check !== '' )
throw new InvalidArgumentException ( '$url is not correctly formatted: ' . $check );
}
if ( $infoOrId instanceof EmoteInfo )
$infoOrId = $infoOrId -> getId ();
2023-07-14 20:26:38 +00:00
$stmt = $this -> cache -> get ( 'UPDATE msz_emoticons SET emote_order = COALESCE(?, emote_order), emote_hierarchy = COALESCE(?, emote_hierarchy), emote_url = COALESCE(?, emote_url) WHERE emote_id = ?' );
$stmt -> addParameter ( 1 , $order );
$stmt -> addParameter ( 2 , $minRank );
$stmt -> addParameter ( 3 , $url );
$stmt -> addParameter ( 4 , $infoOrId );
$stmt -> execute ();
2023-07-12 21:52:55 +00:00
}
public function updateEmoteOrderOffset ( EmoteInfo | string $infoOrId , int $offset ) : void {
if ( $offset === 0 ) return ;
if ( $infoOrId instanceof EmoteInfo )
$infoOrId = $infoOrId -> getId ();
2023-07-14 20:26:38 +00:00
$stmt = $this -> cache -> get ( 'UPDATE msz_emoticons SET emote_order = emote_order + ? WHERE emote_id = ?' );
$stmt -> addParameter ( 1 , $offset );
$stmt -> addParameter ( 2 , $infoOrId );
$stmt -> execute ();
2023-07-12 21:52:55 +00:00
}
public function getEmoteStrings ( EmoteInfo | string $infoOrId ) : array {
if ( $infoOrId instanceof EmoteInfo )
$infoOrId = $infoOrId -> getId ();
2023-07-14 20:26:38 +00:00
$stmt = $this -> cache -> get ( 'SELECT emote_id, emote_string_order, emote_string FROM msz_emoticons_strings WHERE emote_id = ? ORDER BY emote_string_order' );
$stmt -> addParameter ( 1 , $infoOrId );
$stmt -> execute ();
2023-07-12 21:52:55 +00:00
$strings = [];
2023-07-14 20:26:38 +00:00
$result = $stmt -> getResult ();
2023-07-12 21:52:55 +00:00
while ( $result -> next ())
$strings [] = new EmoteStringInfo ( $result );
return $strings ;
}
private static function checkEmoteStringInternal ( string $string ) : string {
if ( empty ( $string ))
return 'empty' ;
if ( trim ( $string ) !== $string )
return 'spaces' ;
if ( strtolower ( $string ) !== $string )
return 'case' ;
if ( ! preg_match ( '#^[a-z][a-z0-9_-]*[a-z0-9]$#' , $string ))
return 'format' ;
return '' ;
}
public function checkEmoteString ( string $string ) : string {
$check = self :: checkEmoteStringInternal ( $string );
if ( $check !== '' )
return $check ;
2023-07-14 20:26:38 +00:00
$stmt = $this -> cache -> get ( 'SELECT COUNT(*) FROM msz_emoticons_strings WHERE emote_string = ?' );
$stmt -> addParameter ( 1 , $string );
$stmt -> execute ();
$result = $stmt -> getResult ();
2023-07-12 21:52:55 +00:00
if ( $result -> next () && $result -> getInteger ( 0 ) > 0 )
return 'used' ;
return '' ;
}
public function addEmoteString ( EmoteInfo | string $infoOrId , string $string , ? int $order = null ) : void {
$check = self :: checkEmoteStringInternal ( $string );
if ( $check !== '' )
throw new InvalidArgumentException ( '$string is not correctly formatted: ' . $check );
if ( $infoOrId instanceof EmoteInfo )
$infoOrId = $infoOrId -> getId ();
2023-07-14 20:26:38 +00:00
$stmt = $this -> cache -> get ( 'INSERT INTO msz_emoticons_strings (emote_id, emote_string, emote_string_order) SELECT ? AS target_emote_id, ?, COALESCE(?, (SELECT MAX(emote_string_order) + 1 FROM msz_emoticons_strings WHERE emote_id = target_emote_id), 1)' );
$stmt -> addParameter ( 1 , $infoOrId );
$stmt -> addParameter ( 2 , $string );
$stmt -> addParameter ( 3 , $order );
$stmt -> execute ();
2023-07-12 21:52:55 +00:00
}
public function removeEmoteString ( EmoteStringInfo | string $infoOrString ) : void {
if ( $infoOrString instanceof EmoteStringInfo )
$infoOrString = $infoOrString -> getString ();
2023-07-14 20:26:38 +00:00
$stmt = $this -> cache -> get ( 'DELETE FROM msz_emoticons_strings WHERE emote_string = ?' );
$stmt -> addParameter ( 1 , $infoOrString );
$stmt -> execute ();
2023-07-12 21:52:55 +00:00
}
}