summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/scripts/fuzzy.php
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/Translate/scripts/fuzzy.php')
-rw-r--r--www/wiki/extensions/Translate/scripts/fuzzy.php129
1 files changed, 90 insertions, 39 deletions
diff --git a/www/wiki/extensions/Translate/scripts/fuzzy.php b/www/wiki/extensions/Translate/scripts/fuzzy.php
index 54b2110f..30aff9c9 100644
--- a/www/wiki/extensions/Translate/scripts/fuzzy.php
+++ b/www/wiki/extensions/Translate/scripts/fuzzy.php
@@ -6,7 +6,7 @@
* @author Niklas Laxström
* @author Siebrand Mazeland
* @copyright Copyright © 2007-2013, Niklas Laxström, Siebrand Mazeland
- * @license GPL-2.0+
+ * @license GPL-2.0-or-later
*/
// Standard boilerplate to define $IP
@@ -26,8 +26,8 @@ class Fuzzy extends Maintenance {
parent::__construct();
$this->mDescription = 'Fuzzy bot command line script.';
$this->addArg(
- 'messages',
- 'Message to fuzzy'
+ 'arg',
+ 'Title pattern or username if user option is provided.'
);
$this->addOption(
'really',
@@ -45,21 +45,34 @@ class Fuzzy extends Maintenance {
false, /*required*/
true /*has arg*/
);
+ $this->addOption(
+ 'user',
+ '(optional) Fuzzy the translations made by user given as an argument.',
+ false, /*required*/
+ false /*has arg*/
+ );
}
public function execute() {
- $bot = new FuzzyScript( $this->getArg( 0 ) );
-
+ $skipLanguages = [];
if ( $this->hasOption( 'skiplanguages' ) ) {
- $bot->skipLanguages = array_map(
+ $skipLanguages = array_map(
'trim',
explode( ',', $this->getOption( 'skiplanguages' ) )
);
}
+ if ( $this->hasOption( 'user' ) ) {
+ $user = User::newFromName( $this->getArg( 0 ) );
+ $pages = FuzzyScript::getPagesForUser( $user, $skipLanguages );
+ } else {
+ $pages = FuzzyScript::getPagesForPattern( $this->getArg( 0 ), $skipLanguages );
+ }
+
+ $bot = new FuzzyScript( $pages );
$bot->comment = $this->getOption( 'comment' );
$bot->dryrun = !$this->hasOption( 'really' );
- $bot->setProgressCallback( array( $this, 'myOutput' ) );
+ $bot->setProgressCallback( [ $this, 'myOutput' ] );
$bot->execute();
}
@@ -67,7 +80,7 @@ class Fuzzy extends Maintenance {
* Public alternative for protected Maintenance::output() as we need to get
* messages from the ChangeSyncer class to the commandline.
* @param string $text The text to show to the user
- * @param string $channel Unique identifier for the channel.
+ * @param string|null $channel Unique identifier for the channel.
* @param bool $error Whether this is an error message
*/
public function myOutput( $text, $channel = null, $error = false ) {
@@ -84,11 +97,6 @@ class Fuzzy extends Maintenance {
*/
class FuzzyScript {
/**
- /* @var string[] List of patterns to mark.
- */
- private $titles = array();
-
- /**
* @var bool Check for configuration problems.
*/
private $allclear = false;
@@ -107,15 +115,10 @@ class FuzzyScript {
public $comment;
/**
- * string[] List of language codes to skip.
+ * @param array $pages
*/
- public $skipLanguages = array();
-
- /**
- * @param string[] $titles
- */
- public function __construct( $titles ) {
- $this->titles = (array)$titles;
+ public function __construct( $pages ) {
+ $this->pages = $pages;
$this->allclear = true;
}
@@ -136,7 +139,7 @@ class FuzzyScript {
return;
}
- $msgs = $this->getPages();
+ $msgs = $this->pages;
$count = count( $msgs );
$this->reportProgress( "Found $count pages to update.", 'pagecount' );
@@ -148,51 +151,99 @@ class FuzzyScript {
}
/// Searches pages that match given patterns
- private function getPages() {
+ public static function getPagesForPattern( $pattern, $skipLanguages = [] ) {
global $wgTranslateMessageNamespaces;
- $dbr = wfGetDB( DB_SLAVE );
+ $dbr = wfGetDB( DB_REPLICA );
- $search = array();
- foreach ( $this->titles as $title ) {
+ $search = [];
+ foreach ( (array)$pattern as $title ) {
$title = Title::newFromText( $title );
$ns = $title->getNamespace();
if ( !isset( $search[$ns] ) ) {
- $search[$ns] = array();
+ $search[$ns] = [];
}
$search[$ns][] = 'page_title' . $dbr->buildLike( $title->getDBkey(), $dbr->anyString() );
}
- $title_conds = array();
+ $title_conds = [];
foreach ( $search as $ns => $names ) {
if ( $ns === NS_MAIN ) {
$ns = $wgTranslateMessageNamespaces;
}
$titles = $dbr->makeList( $names, LIST_OR );
- $title_conds[] = $dbr->makeList( array( 'page_namespace' => $ns, $titles ), LIST_AND );
+ $title_conds[] = $dbr->makeList( [ 'page_namespace' => $ns, $titles ], LIST_AND );
}
- $conds = array(
+ $conds = [
'page_latest=rev_id',
'rev_text_id=old_id',
$dbr->makeList( $title_conds, LIST_OR ),
- );
+ ];
- if ( count( $this->skipLanguages ) ) {
- $skiplist = $dbr->makeList( $this->skipLanguages );
+ if ( count( $skipLanguages ) ) {
+ $skiplist = $dbr->makeList( $skipLanguages );
$conds[] = "substring_index(page_title, '/', -1) NOT IN ($skiplist)";
}
$rows = $dbr->select(
- array( 'page', 'revision', 'text' ),
- array( 'page_title', 'page_namespace', 'old_text', 'old_flags' ),
+ [ 'page', 'revision', 'text' ],
+ [ 'page_title', 'page_namespace', 'old_text', 'old_flags' ],
$conds,
__METHOD__
);
- $messagesContents = array();
+ $messagesContents = [];
+ foreach ( $rows as $row ) {
+ $title = Title::makeTitle( $row->page_namespace, $row->page_title );
+ $messagesContents[] = [ $title, Revision::getRevisionText( $row ) ];
+ }
+
+ $rows->free();
+
+ return $messagesContents;
+ }
+
+ public static function getPagesForUser( User $user, $skipLanguages = [] ) {
+ global $wgTranslateMessageNamespaces;
+ $dbr = wfGetDB( DB_REPLICA );
+
+ if ( class_exists( ActorMigration::class ) ) {
+ $revWhere = ActorMigration::newMigration()->getWhere( $dbr, 'rev_user', $user );
+ } else {
+ $revWhere = [
+ 'tables' => [],
+ 'conds' => 'rev_user = ' . (int)$user->getId(),
+ 'joins' => [],
+ ];
+ }
+
+ $conds = [
+ $revWhere['conds'],
+ 'page_namespace' => $wgTranslateMessageNamespaces,
+ 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() ),
+ ];
+
+ if ( count( $skipLanguages ) ) {
+ $skiplist = $dbr->makeList( $skipLanguages );
+ $conds[] = "substring_index(page_title, '/', -1) NOT IN ($skiplist)";
+ }
+
+ $rows = $dbr->select(
+ [ 'page', 'revision', 'text' ] + $revWhere['tables'],
+ [ 'page_title', 'page_namespace', 'old_text', 'old_flags' ],
+ $conds,
+ __METHOD__,
+ [],
+ [
+ 'revision' => [ 'JOIN', 'page_latest=rev_id' ],
+ 'text' => [ 'JOIN', 'rev_text_id=old_id' ],
+ ] + $revWhere['joins']
+ );
+
+ $messagesContents = [];
foreach ( $rows as $row ) {
$title = Title::makeTitle( $row->page_namespace, $row->page_title );
- $messagesContents[] = array( $title, Revision::getRevisionText( $row ) );
+ $messagesContents[] = [ $title, Revision::getRevisionText( $row ) ];
}
$rows->free();
@@ -234,7 +285,7 @@ class FuzzyScript {
$content = ContentHandler::makeContent( $text, $title );
$status = $wikipage->doEditContent(
$content,
- $comment ? $comment : 'Marking as fuzzy',
+ $comment ?: 'Marking as fuzzy',
EDIT_FORCE_BOT | EDIT_UPDATE,
false, /*base revision id*/
FuzzyBot::getUser()
@@ -245,5 +296,5 @@ class FuzzyScript {
}
}
-$maintClass = 'Fuzzy';
+$maintClass = Fuzzy::class;
require_once RUN_MAINTENANCE_IF_MAIN;