2021-10-28 09:26:16 +09:00
< ? php
2021-12-26 18:48:16 +09:00
declare ( strict_types = 1 );
2021-10-28 09:26:16 +09:00
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
2021-12-26 05:27:10 +09:00
namespace Component\Language\Entity ;
2021-10-28 09:26:16 +09:00
2021-11-02 20:11:51 +09:00
use App\Core\Cache ;
use App\Core\DB\DB ;
2021-10-28 09:26:16 +09:00
use App\Core\Entity ;
2021-12-26 18:48:16 +09:00
use function App\Core\I18n\_m ;
2021-12-26 05:27:10 +09:00
use App\Entity\Actor ;
use App\Entity\Note ;
2021-11-16 02:00:58 +09:00
use App\Util\Common ;
2021-10-29 01:34:01 +09:00
use DateTimeInterface ;
2021-11-02 20:11:51 +09:00
use Functional as F ;
2021-10-28 09:26:16 +09:00
/**
* Entity for languages
*
* @ category DB
* @ package GNUsocial
*
* @ author Hugo Sales < hugo @ hsal . es >
* @ copyright 2021 Free Software Foundation , Inc http :// www . fsf . org
* @ license https :// www . gnu . org / licenses / agpl . html GNU AGPL v3 or later
*/
class Language extends Entity
{
// {{{ Autocode
// @codeCoverageIgnoreStart
private int $id ;
2021-12-27 06:32:09 +09:00
private ? string $locale = null ;
private ? string $long_display = null ;
private ? string $short_display = null ;
2021-10-29 01:34:01 +09:00
private DateTimeInterface $created ;
2021-10-28 09:26:16 +09:00
public function setId ( int $id ) : self
{
$this -> id = $id ;
return $this ;
}
public function getId () : int
{
return $this -> id ;
}
2021-12-27 00:12:06 +09:00
public function setLocale ( ? string $locale ) : self
2021-10-28 09:26:16 +09:00
{
2021-12-27 06:32:09 +09:00
$this -> locale = \is_null ( $locale ) ? null : mb_substr ( $locale , 0 , 64 );
2021-10-28 09:26:16 +09:00
return $this ;
}
2021-12-27 00:12:06 +09:00
public function getLocale () : ? string
2021-10-28 09:26:16 +09:00
{
2021-11-02 19:42:21 +09:00
return $this -> locale ;
}
2021-12-27 00:12:06 +09:00
public function setLongDisplay ( ? string $long_display ) : self
2021-11-02 19:42:21 +09:00
{
2021-12-27 06:32:09 +09:00
$this -> long_display = \is_null ( $long_display ) ? null : mb_substr ( $long_display , 0 , 64 );
2021-11-02 19:42:21 +09:00
return $this ;
}
2021-12-27 00:12:06 +09:00
public function getLongDisplay () : ? string
2021-11-02 19:42:21 +09:00
{
return $this -> long_display ;
}
2021-12-27 00:12:06 +09:00
public function setShortDisplay ( ? string $short_display ) : self
2021-11-02 19:42:21 +09:00
{
2021-12-27 06:32:09 +09:00
$this -> short_display = \is_null ( $short_display ) ? null : mb_substr ( $short_display , 0 , 12 );
2021-11-02 19:42:21 +09:00
return $this ;
}
2021-12-27 00:12:06 +09:00
public function getShortDisplay () : ? string
2021-11-02 19:42:21 +09:00
{
return $this -> short_display ;
2021-10-28 09:26:16 +09:00
}
public function setCreated ( DateTimeInterface $created ) : self
{
$this -> created = $created ;
return $this ;
}
public function getCreated () : DateTimeInterface
{
return $this -> created ;
}
2021-12-27 00:12:06 +09:00
2021-10-28 09:26:16 +09:00
// @codeCoverageIgnoreEnd
// }}} Autocode
2021-12-05 04:58:00 +09:00
public static function getById ( int $id ) : self
2021-11-25 00:51:01 +09:00
{
return Cache :: getHashMapKey (
2021-11-28 20:14:58 +09:00
map_key : 'languages-id' ,
2021-12-26 18:48:16 +09:00
key : ( string ) $id ,
2022-03-20 07:16:50 +09:00
calculate_map : fn () => F\reindex ( DB :: dql ( 'SELECT l FROM \Component\Language\Entity\Language AS l' ), fn ( self $l ) => ( string ) $l -> getId ()),
2021-11-25 00:51:01 +09:00
);
}
2021-12-05 04:58:00 +09:00
public static function getByLocale ( string $locale ) : self
2021-11-25 00:51:01 +09:00
{
return Cache :: getHashMapKey (
'languages' ,
$locale ,
2022-03-20 07:16:50 +09:00
calculate_map : fn () => F\reindex ( DB :: dql ( 'SELECT l FROM \Component\Language\Entity\Language AS l' ), fn ( self $l ) => $l -> getLocale ()),
2021-11-25 00:51:01 +09:00
);
}
2021-12-05 04:58:00 +09:00
public static function getByNote ( Note $note ) : self
2021-12-04 23:07:15 +09:00
{
2021-12-05 04:58:00 +09:00
return self :: getById ( $note -> getLanguageId ());
2021-12-04 23:07:15 +09:00
}
2021-11-02 20:11:51 +09:00
public static function getLanguageChoices () : array
2021-10-29 01:34:01 +09:00
{
2021-11-09 00:03:39 +09:00
$langs = Cache :: getHashMap (
'languages' ,
2022-03-20 07:16:50 +09:00
fn () => F\reindex ( DB :: dql ( 'SELECT l FROM \Component\Language\Entity\Language AS l' ), fn ( self $l ) => $l -> getLocale ()),
2021-11-02 20:11:51 +09:00
);
2021-11-09 00:03:39 +09:00
2021-12-26 18:48:16 +09:00
return array_merge ( ... F\map ( array_values ( $langs ), fn ( $l ) => $l -> toChoiceFormat ()));
2021-11-02 20:11:51 +09:00
}
public function toChoiceFormat () : array
{
2021-11-09 00:03:39 +09:00
return [ _m ( $this -> getLongDisplay ()) => $this -> getLocale ()];
2021-10-29 01:34:01 +09:00
}
2021-11-16 02:00:58 +09:00
/**
* Get all the available languages as well as the languages $actor
* prefers and are appropriate for posting in / to $context_actor
*/
2021-12-12 07:18:31 +09:00
public static function getSortedLanguageChoices ( ? Actor $actor , ? Actor $context_actor , ? bool $use_short_display ) : array
2021-11-16 02:00:58 +09:00
{
2021-12-12 07:18:31 +09:00
$language_choices = self :: getLanguageChoices ();
2021-12-26 18:48:16 +09:00
if ( \is_null ( $actor )) {
2021-12-12 07:18:31 +09:00
return [ $language_choices , []];
}
2021-11-16 02:00:58 +09:00
$preferred_language_choices = $actor -> getPreferredLanguageChoices ( $context_actor );
ksort ( $language_choices );
if ( $use_short_display ? ? Common :: config ( 'posting' , 'use_short_language_display' )) {
2021-12-26 18:48:16 +09:00
$key = array_key_first ( $preferred_language_choices );
2021-12-26 05:27:10 +09:00
$language = $preferred_language_choices [ $key ];
2021-11-16 02:00:58 +09:00
unset ( $preferred_language_choices [ $key ], $language_choices [ $key ]);
2021-12-26 18:48:16 +09:00
$short_display = $language -> getShortDisplay ();
2021-12-26 05:27:10 +09:00
$preferred_language_choices [ $short_display ] = ( $locale = $language -> getLocale ());
2021-12-26 18:48:16 +09:00
$language_choices [ $short_display ] = $locale ;
2021-11-16 02:00:58 +09:00
}
return [ $language_choices , $preferred_language_choices ];
}
2021-10-28 09:26:16 +09:00
public static function schemaDef () : array
{
return [
2021-12-26 18:48:16 +09:00
'name' => 'language' ,
2021-10-28 09:26:16 +09:00
'description' => 'all known languages' ,
2021-12-26 18:48:16 +09:00
'fields' => [
'id' => [ 'type' => 'serial' , 'not null' => true , 'description' => 'unique identifier' ],
'locale' => [ 'type' => 'varchar' , 'length' => 64 , 'description' => 'The locale identifier for the language of a note. 2-leter-iso-language-code_4-leter-script-code_2-leter-iso-country-code, but kept longer in case we get a different format' ],
'long_display' => [ 'type' => 'varchar' , 'length' => 64 , 'description' => 'The long display string for the language, in english (translated later)' ],
2021-12-26 05:27:10 +09:00
'short_display' => [ 'type' => 'varchar' , 'length' => 12 , 'description' => 'The short display string for the language (used for the first option)' ],
2021-12-26 18:48:16 +09:00
'created' => [ 'type' => 'datetime' , 'not null' => true , 'default' => 'CURRENT_TIMESTAMP' , 'description' => 'date this record was created' ],
2021-10-28 09:26:16 +09:00
],
'primary key' => [ 'id' ],
2021-10-29 01:34:01 +09:00
'unique keys' => [
2021-11-02 19:42:21 +09:00
'language_locale_uniq' => [ 'locale' ],
2021-10-29 01:34:01 +09:00
],
'indexes' => [
2021-11-02 19:42:21 +09:00
'locale_idx' => [ 'locale' ],
2021-10-28 09:26:16 +09:00
],
];
}
}