Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F5959518
MessagePrefixStats.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
MessagePrefixStats.php
View Options
<?php
declare
(
strict_types
=
1
);
namespace
MediaWiki\Extension\Translate\Statistics
;
use
InvalidArgumentException
;
use
MediaWiki\Extension\Translate\MessageLoading\MessageCollection
;
use
MediaWiki\Extension\Translate\MessageLoading\MessageDefinitions
;
use
MediaWiki\Title\TitleParser
;
/**
* This class abstracts MessagePrefix statistics calculation and storing.
* @author Abijeet Patro
* @since 2023.02
* @license GPL-2.0-or-later
*/
class
MessagePrefixStats
{
private
TitleParser
$titleParser
;
private
?
array
$allStats
;
public
function
__construct
(
TitleParser
$titleParser
)
{
$this
->
titleParser
=
$titleParser
;
}
/**
* Returns statistics for the message keys provided. Assumes that the message keys
* belong to the same namespace.
* @param string ...$prefixedMessagesKeys
* @return array
*/
public
function
forAll
(
string
...
$prefixedMessagesKeys
):
array
{
$languages
=
MessageGroupStats
::
getLanguages
();
$stats
=
[];
if
(
!
$prefixedMessagesKeys
)
{
throw
new
InvalidArgumentException
(
'Empty prefixed message keys passed as argument'
);
}
$messagesForDefinition
=
[];
foreach
(
$prefixedMessagesKeys
as
$key
)
{
$messageTitle
=
$this
->
titleParser
->
parseTitle
(
$key
);
$messageNamespace
=
$messageTitle
->
getNamespace
();
$messagesForDefinition
[
"$messageNamespace:{$messageTitle->getDBkey()}"
]
=
null
;
}
$messageDefinitions
=
new
MessageDefinitions
(
$messagesForDefinition
,
false
);
foreach
(
$languages
as
$code
)
{
if
(
$this
->
isLanguageUnused
(
$code
)
)
{
$collection
=
MessageCollection
::
newFromDefinitions
(
$messageDefinitions
,
$code
);
$stats
[
$code
]
=
MessageGroupStats
::
getStatsForCollection
(
$collection
);
}
else
{
$stats
[
$code
]
=
MessageGroupStats
::
getEmptyStats
();
$stats
[
$code
][
MessageGroupStats
::
TOTAL
]
=
count
(
$prefixedMessagesKeys
);
}
}
return
$stats
;
}
/** Check if there are any stats for the language */
private
function
isLanguageUnused
(
string
$languageCode
):
bool
{
$allStats
=
$this
->
getAllLanguageStats
();
$languageStats
=
$allStats
[
$languageCode
]
??
[];
$translatedStats
=
$languageStats
[
MessageGroupStats
::
TRANSLATED
];
$fuzzyStats
=
$languageStats
[
MessageGroupStats
::
FUZZY
];
return
$translatedStats
!==
0
||
$fuzzyStats
!==
0
;
}
private
function
getAllLanguageStats
():
array
{
$this
->
allStats
??=
MessageGroupStats
::
getApproximateLanguageStats
();
return
$this
->
allStats
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Oct 3, 06:34 (2 w, 3 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
a5/94/71c39aff61f0a3056c787be11d77
Default Alt Text
MessagePrefixStats.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment