Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
RevTagStore.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
7use MediaWiki\Page\PageIdentity;
8use Wikimedia\Rdbms\IConnectionProvider;
9
19 public const FUZZY_TAG = 'fuzzy';
23 public const TRANSVER_PROP = 'tp:transver';
25 public const TP_MARK_TAG = 'tp:mark';
27 public const TP_READY_TAG = 'tp:tag';
29 public const MB_VALID_TAG = 'mb:valid';
30
31 private IConnectionProvider $connectionProvider;
33 private $tagCache = [];
34
35 public function __construct( IConnectionProvider $connectionProvider ) {
36 $this->connectionProvider = $connectionProvider;
37 }
38
40 public function replaceTag(
41 PageIdentity $identity,
42 string $tag,
43 int $revisionId,
44 ?array $value = null
45 ): void {
46 if ( !$identity->exists() ) {
47 return;
48 }
49
50 $articleId = $identity->getId();
51
52 $dbw = $this->connectionProvider->getPrimaryDatabase();
53 $conds = [
54 'rt_page' => $articleId,
55 'rt_type' => $tag
56 ];
57 $dbw->delete( 'revtag', $conds, __METHOD__ );
58
59 if ( $value !== null ) {
60 $conds['rt_value'] = serialize( implode( '|', $value ) );
61 }
62
63 $conds['rt_revision'] = $revisionId;
64 $dbw->insert( 'revtag', $conds, __METHOD__ );
65
66 $this->tagCache[$articleId][$tag] = $revisionId;
67 }
68
69 public function getLatestRevisionWithTag( PageIdentity $identity, string $tag ): ?int {
70 $response = $this->getLatestRevisionsForTags( $identity, $tag );
71 return $response[$tag] ?? null;
72 }
73
75 public function getLatestRevisionsForTags( PageIdentity $identity, string ...$tags ): ?array {
76 if ( !$identity->exists() ) {
77 return null;
78 }
79
80 $articleId = $identity->getId();
81
82 $response = [];
83 $remainingTags = [];
84
85 // ATTENTION: Cache should only be updated on POST requests.
86 foreach ( $tags as $tag ) {
87 if ( isset( $this->tagCache[$articleId][$tag] ) ) {
88 $response[$tag] = $this->tagCache[$articleId][$tag];
89 } else {
90 $remainingTags[] = $tag;
91 }
92 }
93
94 if ( !$remainingTags ) {
95 // All tags were available in the cache, no need to run any queries.
96 return $response;
97 }
98
99 $dbr = Utilities::getSafeReadDB();
100 $results = $dbr->newSelectQueryBuilder()
101 ->select( [ 'MAX(rt_revision) AS rt_revision', 'rt_type' ] )
102 ->from( 'revtag' )
103 ->where( [
104 'rt_page' => $articleId,
105 'rt_type' => $remainingTags
106 ] )
107 ->groupBy( 'rt_type' )
108 ->caller( __METHOD__ )
109 ->fetchResultSet();
110
111 foreach ( $results as $row ) {
112 $response[$row->rt_type] = (int)$row->rt_revision;
113 }
114
115 return $response;
116 }
117
118 public function removeTags( PageIdentity $identity, string ...$tag ): void {
119 if ( !$identity->exists() ) {
120 return;
121 }
122
123 $articleId = $identity->getId();
124
125 $dbw = $this->connectionProvider->getPrimaryDatabase();
126 $conds = [
127 'rt_page' => $articleId,
128 'rt_type' => $tag,
129 ];
130 $dbw->delete( 'revtag', $conds, __METHOD__ );
131
132 unset( $this->tagCache[$articleId] );
133 }
134
136 public static function getTranslatableBundleIds( string ...$revTags ): array {
137 $dbr = Utilities::getSafeReadDB();
138
139 $tables = [ 'revtag', 'page' ];
140 $fields = 'rt_page';
141 $conds = [
142 'rt_page = page_id',
143 'rt_revision = page_latest',
144 'rt_type' => $revTags,
145 ];
146 $options = [ 'GROUP BY' => 'rt_page' ];
147
148 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options );
149 $results = [];
150 foreach ( $res as $row ) {
151 $results[$row->rt_page] = true;
152 }
153
154 return $results;
155 }
156}
Class to manage revision tags for translatable bundles.
const TRANSVER_PROP
Stores the revision id of the source text which was translated.
const MB_VALID_TAG
Indicates a revision of a page that is a valid message bundle.
const FUZZY_TAG
Indicates that a translation is fuzzy (outdated or not passing validation).
static getTranslatableBundleIds(string ... $revTags)
Get a list of page ids where the latest revision is either tagged or marked.
replaceTag(PageIdentity $identity, string $tag, int $revisionId, ?array $value=null)
Add tag for the given revisionId, while deleting it from others.
const TP_READY_TAG
Indicates a revision of a translatable page that is marked for translation.
const TP_MARK_TAG
Indicates a revision of a page that can be marked for translation.
getLatestRevisionsForTags(PageIdentity $identity, string ... $tags)
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:31