summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/scripts/populateFuzzy.php
blob: 32ff07ceb504c7cedfa9f9d8a20c9bf9df36a519 (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
<?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
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->mDescription = 'A script to populate fuzzy tags to revtag table.';
		$this->addOption(
			'namespace',
			'(optional) Namepace name or id',
			/*required*/false,
			/*has arg*/true
		);
	}

	public function execute() {
		global $wgTranslateMessageNamespaces;

		$namespace = $this->getOption( 'namespace', $wgTranslateMessageNamespaces );
		if ( is_string( $namespace ) &&
			!MWNamespace::exists( $namespace )
		) {
			$namespace = MWNamespace::getCanonicalIndex( $namespace );

			if ( $namespace === null ) {
				$this->error( 'Bad namespace', true );
			}
		}

		$dbw = wfGetDB( DB_MASTER );
		$tables = [ 'page', 'text', 'revision' ];
		$fields = [ 'page_id', 'page_title', 'page_namespace', 'rev_id', 'old_text', 'old_flags' ];
		$conds = [
			'page_latest = rev_id',
			'old_id = rev_text_id',
			'page_namespace' => $namespace,
		];

		$limit = 100;
		$offset = 0;
		while ( true ) {
			$inserts = [];
			$this->output( '.', 0 );
			$options = [ 'LIMIT' => $limit, 'OFFSET' => $offset ];
			$res = $dbw->select( $tables, $fields, $conds, __METHOD__, $options );

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

			foreach ( $res as $r ) {
				$text = Revision::getRevisionText( $r );
				if ( strpos( $text, TRANSLATE_FUZZY ) !== false ) {
					$inserts[] = [
						'rt_page' => $r->page_id,
						'rt_revision' => $r->rev_id,
						'rt_type' => RevTag::getType( 'fuzzy' ),
					];
				}
			}

			$offset += $limit;

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

$maintClass = PopulateFuzzy::class;
require_once RUN_MAINTENANCE_IF_MAIN;