Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
populateFuzzy.php
Go to the documentation of this file.
1<?php
11// Standard boilerplate to define $IP
12
14use MediaWiki\MediaWikiServices;
15use MediaWiki\Revision\SlotRecord;
16
17if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
18 $IP = getenv( 'MW_INSTALL_PATH' );
19} else {
20 $dir = __DIR__;
21 $IP = "$dir/../../..";
22}
23require_once "$IP/maintenance/Maintenance.php";
24
26class PopulateFuzzy extends Maintenance {
27 public function __construct() {
28 parent::__construct();
29 $this->addDescription( 'A script to populate fuzzy tags to revtag table.' );
30 $this->addOption(
31 'namespace',
32 '(optional) Namepace name or id',
33 /*required*/false,
34 /*has arg*/true
35 );
36 $this->setBatchSize( 5000 );
37 $this->requireExtension( 'Translate' );
38 }
39
40 public function execute() {
41 global $wgTranslateMessageNamespaces;
42
43 $namespace = $this->getOption( 'namespace', $wgTranslateMessageNamespaces );
44 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
45 if ( is_string( $namespace ) && !$nsInfo->exists( $namespace ) ) {
46 $namespace = $nsInfo->getCanonicalIndex( $namespace );
47 if ( $namespace === null ) {
48 $this->fatalError( 'Bad namespace' );
49 }
50 }
51
52 $dbw = MediaWikiServices::getInstance()->getDBLoadBalancer()
53 ->getMaintenanceConnectionRef( DB_PRIMARY );
54 $revStore = MediaWikiServices::getInstance()->getRevisionStore();
55 $queryInfo = $revStore->getQueryInfo( [ 'page' ] );
56
57 $limit = $this->getBatchSize();
58 $offset = 0;
59 while ( true ) {
60 $inserts = [];
61 $this->output( '.', 0 );
62 $options = [ 'LIMIT' => $limit, 'OFFSET' => $offset ];
63 $res = $dbw->select(
64 $queryInfo['tables'],
65 $queryInfo['fields'],
66 [
67 'page_latest = rev_id',
68 'page_namespace' => $namespace,
69 ],
70 __METHOD__,
71 $options,
72 $queryInfo['joins']
73 );
74
75 if ( !$res->numRows() ) {
76 break;
77 }
78
79 $slots = $revStore->getContentBlobsForBatch( $res, [ SlotRecord::MAIN ] )->getValue();
80 foreach ( $res as $r ) {
81 if ( isset( $slots[$r->rev_id] ) ) {
82 $text = $slots[$r->rev_id][SlotRecord::MAIN]->blob_data;
83 } else {
84 $content = $revStore->newRevisionFromRow( $r )
85 ->getContent( SlotRecord::MAIN );
86 $text = TranslateUtils::getTextFromTextContent( $content );
87 }
88 if ( strpos( $text, TRANSLATE_FUZZY ) !== false ) {
89 $inserts[] = [
90 'rt_page' => $r->page_id,
91 'rt_revision' => $r->rev_id,
92 'rt_type' => RevTagStore::FUZZY_TAG
93 ];
94 }
95 }
96
97 $offset += $limit;
98
99 if ( $inserts ) {
100 $dbw->replace( 'revtag', [ [ 'rt_type', 'rt_page', 'rt_revision' ] ], $inserts, __METHOD__ );
101 }
102 }
103 }
104}
105
106$maintClass = PopulateFuzzy::class;
107require_once RUN_MAINTENANCE_IF_MAIN;
Class to manage revision tags for translatable bundles.
A script to populate fuzzy tags to revtag table.