summaryrefslogtreecommitdiff
blob: 75451d27db09d60d519ab36ff53469b4eb03488e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
 * A script to populate fuzzy tags to revtag table.
 *
 * @author Niklas Laxström
 * @copyright Copyright © 2009-2013, Niklas Laxström
 * @license GPL-2.0-or-later
 * @file
 */

// Standard boilerplate to define $IP
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\SlotRecord;

if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
	$IP = getenv( 'MW_INSTALL_PATH' );
} else {
	$dir = __DIR__;
	$IP = "$dir/../../..";
}
require_once "$IP/maintenance/Maintenance.php";

/// A script to populate fuzzy tags to revtag table.
class PopulateFuzzy extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->addDescription( 'A script to populate fuzzy tags to revtag table.' );
		$this->addOption(
			'namespace',
			'(optional) Namepace name or id',
			/*required*/false,
			/*has arg*/true
		);
		$this->setBatchSize( 5000 );
		$this->requireExtension( 'Translate' );
	}

	public function execute() {
		global $wgTranslateMessageNamespaces;

		$namespace = $this->getOption( 'namespace', $wgTranslateMessageNamespaces );
		$nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
		if ( is_string( $namespace ) && !$nsInfo->exists( $namespace ) ) {
			$namespace = $nsInfo->getCanonicalIndex( $namespace );
			if ( $namespace === null ) {
				$this->fatalError( 'Bad namespace' );
			}
		}

		$dbw = MediaWikiServices::getInstance()->getDBLoadBalancer()
			->getMaintenanceConnectionRef( DB_MASTER );
		$revStore = MediaWikiServices::getInstance()->getRevisionStore();
		$queryInfo = $revStore->getQueryInfo( [ 'page' ] );

		$limit = $this->getBatchSize();
		$offset = 0;
		while ( true ) {
			$inserts = [];
			$this->output( '.', 0 );
			$options = [ 'LIMIT' => $limit, 'OFFSET' => $offset ];
			$res = $dbw->select(
				$queryInfo['tables'],
				$queryInfo['fields'],
				[
					'page_latest = rev_id',
					'page_namespace' => $namespace,
				],
				__METHOD__,
				$options,
				$queryInfo['joins']
			);

			if ( !$res->numRows() ) {
				break;
			}

			$slots = $revStore->getContentBlobsForBatch( $res, [ SlotRecord::MAIN ] )->getValue();
			foreach ( $res as $r ) {
				if ( isset( $slots[$r->rev_id] ) ) {
					$text = $slots[$r->rev_id][SlotRecord::MAIN]->blob_data;
				} else {
					$text = $revStore->newRevisionFromRow( $r )
						->getContent( SlotRecord::MAIN )
						->getNativeData();
				}
				if ( strpos( $text, TRANSLATE_FUZZY ) !== false ) {
					$inserts[] = [
						'rt_page' => $r->page_id,
						'rt_revision' => $r->rev_id,
						'rt_type' => RevTag::getType( 'fuzzy' ),
					];
				}
			}

			$offset += $limit;

			if ( $inserts ) {
				$dbw->replace( 'revtag', 'rt_type_page_revision', $inserts, __METHOD__ );
			}
		}
	}
}

$maintClass = PopulateFuzzy::class;
require_once RUN_MAINTENANCE_IF_MAIN;