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
29class PopulateFuzzy extends Maintenance {
30 public function __construct() {
31 parent::__construct();
32 $this->addDescription( 'A script to populate fuzzy tags to revtag table.' );
33 $this->addOption(
34 'namespace',
35 '(optional) Namepace name or id',
36 /*required*/false,
37 /*has arg*/true
38 );
39 $this->setBatchSize( 500 );
40 $this->requireExtension( 'Translate' );
41 }
42
43 public function execute() {
44 global $wgTranslateMessageNamespaces;
45
46 $namespace = $this->getOption( 'namespace', $wgTranslateMessageNamespaces );
47 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
48 if ( is_string( $namespace ) && ( !is_numeric( $namespace ) || !$nsInfo->exists( (int)$namespace ) ) ) {
49 $namespace = $nsInfo->getCanonicalIndex( $namespace );
50 if ( $namespace === null ) {
51 $this->fatalError( 'Bad namespace' );
52 }
53 }
54
55 $dbw = MediaWikiServices::getInstance()->getDBLoadBalancer()
56 ->getMaintenanceConnectionRef( DB_PRIMARY );
57 $revStore = MediaWikiServices::getInstance()->getRevisionStore();
58
59 $limit = $this->getBatchSize();
60 $offset = 0;
61 while ( true ) {
62 $this->output( '.', 0 );
63 $res = $revStore->newSelectQueryBuilder( $dbw )
64 ->joinPage()
65 ->joinComment()
66 ->where( [
67 'page_latest = rev_id',
68 'page_namespace' => $namespace,
69 ] )
70 ->limit( $limit )
71 ->offset( $offset )
72 ->caller( __METHOD__ )
73 ->fetchResultSet();
74
75 if ( !$res->numRows() ) {
76 break;
77 }
78
79 $inserts = [];
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->insert( 'revtag', $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.