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\Maintenance\Maintenance;
16use MediaWiki\MediaWikiServices;
17use MediaWiki\Revision\SlotRecord;
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
30class PopulateFuzzy extends Maintenance {
31 public function __construct() {
32 parent::__construct();
33 $this->addDescription( 'A script to populate fuzzy tags to revtag table.' );
34 $this->addOption(
35 'namespace',
36 '(optional) Namepace name or id',
37 /*required*/false,
38 /*has arg*/true
39 );
40 $this->setBatchSize( 500 );
41 $this->requireExtension( 'Translate' );
42 }
43
44 public function execute() {
45 global $wgTranslateMessageNamespaces;
46
47 $namespace = $this->getOption( 'namespace', $wgTranslateMessageNamespaces );
48 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
49 if ( is_string( $namespace ) && ( !is_numeric( $namespace ) || !$nsInfo->exists( (int)$namespace ) ) ) {
50 $namespace = $nsInfo->getCanonicalIndex( $namespace );
51 if ( $namespace === null ) {
52 $this->fatalError( 'Bad namespace' );
53 }
54 }
55
56 $dbw = MediaWikiServices::getInstance()->getDBLoadBalancer()
57 ->getMaintenanceConnectionRef( DB_PRIMARY );
58 $revStore = MediaWikiServices::getInstance()->getRevisionStore();
59
60 $limit = $this->getBatchSize();
61 $offset = 0;
62 while ( true ) {
63 $this->output( '.', 0 );
64 $res = $revStore->newSelectQueryBuilder( $dbw )
65 ->joinPage()
66 ->joinComment()
67 ->where( [
68 'page_latest = rev_id',
69 'page_namespace' => $namespace,
70 ] )
71 ->limit( $limit )
72 ->offset( $offset )
73 ->caller( __METHOD__ )
74 ->fetchResultSet();
75
76 if ( !$res->numRows() ) {
77 break;
78 }
79
80 $inserts = [];
81 $slots = $revStore->getContentBlobsForBatch( $res, [ SlotRecord::MAIN ] )->getValue();
82 foreach ( $res as $r ) {
83 if ( isset( $slots[$r->rev_id] ) ) {
84 $text = $slots[$r->rev_id][SlotRecord::MAIN]->blob_data;
85 } else {
86 $content = $revStore->newRevisionFromRow( $r )
87 ->getContent( SlotRecord::MAIN );
88 $text = Utilities::getTextFromTextContent( $content );
89 }
90 if ( str_contains( $text, TRANSLATE_FUZZY ) ) {
91 $inserts[] = [
92 'rt_page' => $r->page_id,
93 'rt_revision' => $r->rev_id,
94 'rt_type' => RevTagStore::FUZZY_TAG,
95 ];
96 }
97 }
98
99 $offset += $limit;
100
101 if ( $inserts ) {
102 $dbw->newInsertQueryBuilder()
103 ->insertInto( 'revtag' )
104 ->rows( $inserts )
105 ->caller( __METHOD__ )
106 ->execute();
107 }
108 }
109 }
110}
111
112$maintClass = PopulateFuzzy::class;
113require_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.