diff options
Diffstat (limited to 'MLEB/Translate/scripts/characterEditStats.php')
-rw-r--r-- | MLEB/Translate/scripts/characterEditStats.php | 83 |
1 files changed, 52 insertions, 31 deletions
diff --git a/MLEB/Translate/scripts/characterEditStats.php b/MLEB/Translate/scripts/characterEditStats.php index 782d0086..69db5a27 100644 --- a/MLEB/Translate/scripts/characterEditStats.php +++ b/MLEB/Translate/scripts/characterEditStats.php @@ -3,8 +3,8 @@ * Show number of characters translated over a given period of time. * * @author Santhosh Thottingal - * @copyright Copyright 2013 Santhosh Thottingal - * @license GPL-2.0+ + * @copyright Copyright © 2013 Santhosh Thottingal + * @license GPL-2.0-or-later * @file * @ingroup Script Stats */ @@ -30,20 +30,11 @@ class CharacterEditStats extends Maintenance { ); $this->addOption( 'days', - '(optional) Calculate for given number of days (default: 30) ' . - '(capped by the max age of recent changes on the wiki)', + '(optional) Calculate for given number of days (default: 30)', false, /*required*/ true /*has arg*/ ); $this->addOption( - 'bots', - '(optional) Include bot edits' - ); - $this->addOption( - 'diff', - '(optional) Count the edit diffs alone' - ); - $this->addOption( 'ns', '(optional) Comma separated list of namespace IDs', false, /*required*/ @@ -52,16 +43,12 @@ class CharacterEditStats extends Maintenance { } public function execute() { - global $wgTranslateFuzzyBotName, $wgSitename; + global $wgTranslateFuzzyBotName, $wgSitename, $wgTranslateMessageNamespaces; $days = (int)$this->getOption( 'days', 30 ); - $hours = $days * 24; - $top = (int)$this->getOption( 'top', -1 ); - $bots = $this->hasOption( 'bots' ); - - $namespaces = array(); + $namespaces = []; if ( $this->hasOption( 'ns' ) ) { $input = explode( ',', $this->getOption( 'ns' ) ); @@ -70,35 +57,30 @@ class CharacterEditStats extends Maintenance { $namespaces[] = $namespace; } } + } else { + $namespaces = $wgTranslateMessageNamespaces; } // Select set of edits to report on + $rows = self::getRevisionsFromHistory( $days, $namespaces ); - // Fetch some extrac fields that normally TranslateUtils::translationChanges wont - $extraFields = array( 'rc_old_len', 'rc_new_len' ); - $rows = TranslateUtils::translationChanges( $hours, $bots, $namespaces, $extraFields ); // Get counts for edits per language code after filtering out edits by FuzzyBot - $codes = array(); + $codes = []; foreach ( $rows as $_ ) { // Filter out edits by $wgTranslateFuzzyBotName - if ( $_->rc_user_text === $wgTranslateFuzzyBotName ) { + if ( $_->user_text === $wgTranslateFuzzyBotName ) { continue; } - $handle = new MessageHandle( Title::newFromText( $_->rc_title ) ); + $handle = new MessageHandle( Title::newFromText( $_->title ) ); $code = $handle->getCode(); if ( !isset( $codes[$code] ) ) { $codes[$code] = 0; } - if ( $this->hasOption( 'diff' ) ) { - $diff = abs( $_->rc_new_len - $_->rc_old_len ); - } else { - $diff = $_->rc_new_len; - } - $codes[$code] += $diff; + $codes[$code] += $_->length; } // Sort counts and report descending up to $top rows. @@ -118,13 +100,52 @@ class CharacterEditStats extends Maintenance { continue; } $charRatio = mb_strlen( $language, 'UTF-8' ) / strlen( $language ); - $num = intval( $num * $charRatio ); + $num = (int)( $num * $charRatio ); $total += $num; $this->output( "$code\t$language\t$num\n" ); } $this->output( "-----------------------\n" ); $this->output( "Total\t\t$total\n" ); } + + private function getRevisionsFromHistory( $days, array $namespaces ) { + $dbr = wfGetDB( DB_REPLICA ); + $cutoff = $dbr->addQuotes( $dbr->timestamp( time() - $days * 24 * 3600 ) ); + + // The field renames are to be compatible with recentchanges table query + if ( is_callable( Revision::class, 'getQueryInfo' ) ) { + $revQuery = Revision::getQueryInfo( [ 'page' ] ); + $revUserText = isset( $revQuery['fields']['rev_user_text'] ) + ? $revQuery['fields']['rev_user_text'] + : 'rev_user_text'; + } else { + $revQuery = [ + 'tables' => [ 'revision', 'page' ], + 'joins' => [ + 'page' => [ 'JOIN', 'rev_page = page_id' ], + ] + ]; + $revUserText = 'rev_user_text'; + } + $conds = [ + "rev_timestamp > $cutoff", + 'page_namespace' => $namespaces, + ]; + + $res = $dbr->select( + $revQuery['tables'], + [ + 'title' => 'page_title', + 'user_text' => $revUserText, + 'length' => 'rev_len', + ], + $conds, + __METHOD__, + [], + $revQuery['joins'] + ); + return iterator_to_array( $res ); + } } $maintClass = 'CharacterEditStats'; |