Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
createCheckIndex.php
Go to the documentation of this file.
1<?php
12// Standard boilerplate to define $IP
13
16use MediaWiki\Languages\LanguageNameUtils;
17use MediaWiki\MediaWikiServices;
18
19if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
20 $IP = getenv( 'MW_INSTALL_PATH' );
21} else {
22 $dir = __DIR__;
23 $IP = "$dir/../../..";
24}
25require_once "$IP/maintenance/Maintenance.php";
26
27class CreateCheckIndex extends Maintenance {
28 public function __construct() {
29 parent::__construct();
30 $this->addDescription( 'Creates serialised database of messages that need ' .
31 'checking for problems.' );
32 $this->addOption(
33 'group',
34 'Comma separated list of group IDs to process (can use * as wildcard).',
35 true, /*required*/
36 true /*has arg*/
37 );
38
39 $this->addOption(
40 'verbose',
41 '(optional) Enable verbose logging. Default: off',
42 false, /*required*/
43 false /*has arg*/
44 );
45 $this->requireExtension( 'Translate' );
46 }
47
48 public function execute() {
49 $codes = MediaWikiServices::getInstance()
50 ->getLanguageNameUtils()
51 ->getLanguageNames( LanguageNameUtils::AUTONYMS, LanguageNameUtils::ALL );
52
53 // Exclude the documentation language code
54 global $wgTranslateDocumentationLanguageCode;
55 if ( $wgTranslateDocumentationLanguageCode ) {
56 unset( $codes[$wgTranslateDocumentationLanguageCode] );
57 }
58
59 $reqGroupsPattern = $this->getOption( 'group' );
60 $reqGroups = explode( ',', $reqGroupsPattern );
61 $reqGroups = array_map( 'trim', $reqGroups );
62 $reqGroups = MessageGroups::expandWildcards( $reqGroups );
63
64 $verbose = $this->hasOption( 'verbose' );
65
66 if ( !$reqGroups ) {
67 $this->fatalError( "Pattern '$reqGroupsPattern' did not match any groups" );
68 }
69
70 $groups = MessageGroups::singleton()->getGroups();
71 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
72
74 foreach ( $reqGroups as $id ) {
75 $g = MessageGroups::getGroup( $id );
76 // Aliases may have changed the id
77 $id = $g->getId();
78 $sourceLanguage = $g->getSourceLanguage();
79
80 $validator = $g->getValidator();
81 if ( !$validator ) {
82 unset( $g );
83 $this->output( "Skipping group $id due to lack of validators" );
84 continue;
85 }
86
87 // Initialise messages, using unique definitions if appropriate
88 // @phan-suppress-next-line PhanParamTooMany MessageGroupOld takes two args
89 $collection = $g->initCollection( $sourceLanguage, true );
90 if ( !count( $collection ) ) {
91 continue;
92 }
93
94 $this->output( "Processing group $id: ", $id );
95
96 // Skip source language code
97 $langCodes = $codes;
98 unset( $langCodes[$sourceLanguage] );
99
100 $langCodes = array_keys( $langCodes );
101 sort( $langCodes );
102
103 foreach ( $langCodes as $code ) {
104 $this->output( "$code ", $id );
105
106 $problematic = [];
107
108 $collection->resetForNewLanguage( $code );
109 $collection->loadTranslations();
110 $collection->filter( 'ignored' );
111 $collection->filter( 'fuzzy' );
112 $collection->filter( 'translated', false );
113
114 foreach ( $collection as $key => $message ) {
115 $result = $validator->quickValidate( $message, $code );
116 if ( $result->hasIssues() ) {
117 if ( $verbose ) {
118 // Print it
119 $nsText = $contLang->getNsText( $g->getNamespace() );
120 $this->output( "# [[$nsText:$key/$code]]\n" );
121 }
122
123 // Add it to the array
124 $problematic[] = [ $g->getNamespace(), "$key/$code" ];
125 }
126 }
127
128 self::tagFuzzy( $problematic );
129 }
130 }
131 }
132
133 public static function tagFuzzy( array $problematic ): void {
134 if ( $problematic === [] ) {
135 return;
136 }
137
138 $titleConditions = [];
139 $dbw = wfGetDB( DB_PRIMARY );
140
141 foreach ( $problematic as $p ) {
142 // Normalize page key
143 $title = Title::makeTitleSafe( $p[0], $p[1] );
144 $titleText = $title->getDBkey();
145 $titleConditions[] = $dbw->makeList(
146 [
147 'page_namespace' => $p[0],
148 'page_title' => $titleText
149 ],
150 LIST_AND
151 );
152 }
153
154 $conds = $dbw->makeList( $titleConditions, LIST_OR );
155
156 $res = $dbw->select( 'page', [ 'page_id', 'page_latest' ], $conds, __METHOD__ );
157 $inserts = [];
158 foreach ( $res as $row ) {
159 $inserts[] = [
160 'rt_page' => $row->page_id,
161 'rt_revision' => $row->page_latest,
162 'rt_type' => RevTagStore::FUZZY_TAG
163 ];
164 }
165 $dbw->replace( 'revtag', [ [ 'rt_type', 'rt_page', 'rt_revision' ] ], $inserts, __METHOD__ );
166 }
167}
168
169$maintClass = CreateCheckIndex::class;
170require_once RUN_MAINTENANCE_IF_MAIN;
Factory class for accessing message groups individually by id or all of them as a list.
Class to manage revision tags for translatable bundles.