Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
DeleteEqualTranslationsMaintenanceScript.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Diagnostics;
5
8use MediaWiki\MediaWikiServices;
11use SplObjectStorage;
12use Title;
13use TitleValue;
14use TMessage;
15use const SORT_NUMERIC;
16
23 public function __construct() {
24 parent::__construct();
25 $this->addDescription( 'Delete translations that are equal to the definition' );
26
27 $this->addOption(
28 'group',
29 'Which group to scan',
30 self::REQUIRED,
31 self::HAS_ARG
32 );
33 $this->addOption(
34 'language',
35 'Which language to scan',
36 self::REQUIRED,
37 self::HAS_ARG
38 );
39 $this->addOption(
40 'really',
41 'Delete the listed pages instead of just listing them'
42 );
43 $this->addOption(
44 'comment',
45 'Comment for the deletions'
46 );
47
48 $this->requireExtension( 'Translate' );
49 }
50
52 public function execute() {
53 $groupId = $this->getOption( 'group' );
54 $language = $this->getOption( 'language' );
55 $group = MessageGroups::getGroup( $groupId );
56 if ( !$group ) {
57 $this->fatalError( "Message group '$groupId' does not exist" );
58 }
59
60 $collection = $group->initCollection( $language );
61 $equalMessages = $this->getEqualMessages( $collection );
62 $equalMessageCount = count( $equalMessages );
63 if ( $equalMessageCount === 0 ) {
64 $this->output( "No translations equal to definition found\n" );
65 return;
66 }
67
68 $stats = $this->getUserStats( $equalMessages );
69 $this->printUserStats( $stats, $equalMessageCount );
70 $this->output( "\n" );
71 $this->printMessages( $equalMessages );
72
73 if ( $this->hasOption( 'really' ) ) {
74 $comment = $this->getOption( 'comment' ) ?? '';
75 $this->deleteMessages( $equalMessages, $comment );
76 } else {
77 $this->output( "This is a dry-run. Run again with --really to delete these messages\n" );
78 }
79 }
80
81 private function getEqualMessages( MessageCollection $collection ): SplObjectStorage {
82 $collection->filter( 'translated', false );
83 $collection->loadTranslations();
84
85 $messages = new SplObjectStorage();
86 foreach ( $collection->keys() as $key => $titleValue ) {
88 $message = $collection[$key];
89
90 if ( $message->definition() === $message->translation() ) {
91 $messages->attach( $titleValue, $message );
92 }
93 }
94
95 return $messages;
96 }
97
98 private function getUserStats( SplObjectStorage $messages ): array {
99 $stats = [];
100 foreach ( $messages as $key ) {
102 $message = $messages[$key];
103 $index = $message->getProperty( 'last-translator-text' );
104 $stats[$index] = ( $stats[$index] ?? 0 ) + 1;
105 }
106
107 return $stats;
108 }
109
110 private function printUserStats( array $stats, int $equalMessageCount ): void {
111 $this->output( "Found $equalMessageCount message(s) created by these user(s):\n" );
112 arsort( $stats, SORT_NUMERIC );
113 foreach ( $stats as $userName => $count ) {
114 $this->output( sprintf( "%6d | %s\n", $count, $userName ) );
115 }
116 }
117
118 private function printMessages( SplObjectStorage $messages ): void {
120 foreach ( $messages as $key ) {
122 $message = $messages[$key];
123 $title = Title::newFromLinkTarget( $key );
124 $this->output(
125 sprintf( "== %s ==\n%s\n\n", $title->getPrefixedText(), $message->translation() )
126 );
127 }
128 }
129
130 private function deleteMessages( SplObjectStorage $messages, string $reason ): void {
131 $user = FuzzyBot::getUser();
132 $wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory();
133
135 foreach ( $messages as $key ) {
136 $title = Title::newFromLinkTarget( $key );
137 $page = $wikiPageFactory->newFromTitle( $title );
138 $status = $page->doDeleteArticleReal(
139 $reason,
140 $user
141 );
142 if ( $status->isOK() ) {
143 $this->output( ".", 'deletions' );
144 } else {
145 $pageName = $title->getPrefixedText();
146 $this->output( "FAILED to delete page $pageName\n" );
147 }
148 }
149 }
150}
FuzzyBot - the misunderstood workhorse.
Definition FuzzyBot.php:15
Constants for making code for maintenance scripts more readable.
Core message collection class.
loadTranslations()
Loads all message data.
keys()
Returns list of available message keys.
Factory class for accessing message groups individually by id or all of them as an list.
Interface for message objects used by MessageCollection.
Definition Message.php:14