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