25 public function __construct() {
26 parent::__construct();
27 $this->addDescription(
'Script to show number of characters translated .' );
30 '(optional) Show given number of language codes (default: show all)',
36 '(optional) Calculate for given number of days (default: 30)',
42 '(optional) Comma separated list of namespace IDs',
46 $this->requireExtension(
'Translate' );
49 public function execute() {
50 global $wgSitename, $wgTranslateMessageNamespaces;
52 $days = (int)$this->getOption(
'days', 30 );
53 $top = (int)$this->getOption(
'top', -1 );
56 if ( $this->hasOption(
'ns' ) ) {
57 $input = explode(
',', $this->getOption(
'ns' ) );
59 foreach ( $input as $namespace ) {
60 if ( is_numeric( $namespace ) ) {
61 $namespaces[] = $namespace;
65 $namespaces = $wgTranslateMessageNamespaces;
69 $rows = $this->getRevisionsFromHistory( $days, $namespaces );
74 foreach ( $rows as $_ ) {
76 if ( $_->user_text === FuzzyBot::getName() ) {
80 $handle =
new MessageHandle( Title::newFromText( $_->title ) );
81 $code = $handle->getCode();
83 if ( !isset( $codes[$code] ) ) {
87 $codes[$code] += $_->length;
94 $this->output(
"Character edit stats for last $days days in $wgSitename\n" );
95 $this->output(
"code\tname\tedit\n" );
96 $this->output(
"-----------------------\n" );
97 foreach ( $codes as $code => $num ) {
98 if ( $i++ === $top ) {
101 $language = Language::fetchLanguageName( $code );
106 $charRatio = mb_strlen( $language,
'UTF-8' ) / strlen( $language );
107 $num = (int)( $num * $charRatio );
109 $this->output(
"$code\t$language\t$num\n" );
111 $this->output(
"-----------------------\n" );
112 $this->output(
"Total\t\t$total\n" );
115 private function getRevisionsFromHistory( $days, array $namespaces ) {
116 $dbr = wfGetDB( DB_REPLICA );
117 $cutoff = $dbr->addQuotes( $dbr->timestamp( time() - $days * 24 * 3600 ) );
119 $revQuery = MediaWikiServices::getInstance()->getRevisionStore()->getQueryInfo( [
'page' ] );
120 $revUserText = $revQuery[
'fields'][
'rev_user_text'] ??
'rev_user_text';
123 "rev_timestamp > $cutoff",
124 'page_namespace' => $namespaces,
130 'title' =>
'page_title',
131 'user_text' => $revUserText,
132 'length' =>
'rev_len',
139 return iterator_to_array( $res );