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;
8
18 public const FUZZY_TAG = 'fuzzy';
22 public const TRANSVER_PROP = 'tp:transver';
24 public const TP_MARK_TAG = 'tp:mark';
26 public const TP_READY_TAG = 'tp:tag';
28 public const MB_VALID_TAG = 'mb:valid';
29
30 // TODO: Convert to a normal member variable once RevTagStore is a service.
32 private static $tagCache = [];
33
35 public function replaceTag(
36 PageIdentity $identity,
37 string $tag,
38 int $revisionId,
39 ?array $value = null
40 ): void {
41 if ( !$identity->exists() ) {
42 return;
43 }
44
45 $articleId = $identity->getId();
46
47 $dbw = wfGetDB( DB_PRIMARY );
48 $conds = [
49 'rt_page' => $articleId,
50 'rt_type' => $tag
51 ];
52 $dbw->delete( 'revtag', $conds, __METHOD__ );
53
54 if ( $value !== null ) {
55 $conds['rt_value'] = serialize( implode( '|', $value ) );
56 }
57
58 $conds['rt_revision'] = $revisionId;
59 $dbw->insert( 'revtag', $conds, __METHOD__ );
60
61 self::$tagCache[$articleId][$tag] = $revisionId;
62 }
63
64 public function getLatestRevisionWithTag( PageIdentity $identity, string $tag ): ?int {
65 $response = $this->getLatestRevisionsForTags( $identity, $tag );
66 return $response[$tag] ?? null;
67 }
68
70 public function getLatestRevisionsForTags( PageIdentity $identity, string ...$tags ): ?array {
71 if ( !$identity->exists() ) {
72 return null;
73 }
74
75 $articleId = $identity->getId();
76
77 $response = [];
78 $remainingTags = [];
79
80 // ATTENTION: Cache should only be updated on POST requests.
81 foreach ( $tags as $tag ) {
82 if ( isset( self::$tagCache[$articleId][$tag] ) ) {
83 $response[$tag] = self::$tagCache[$articleId][$tag];
84 } else {
85 $remainingTags[] = $tag;
86 }
87 }
88
89 if ( !$remainingTags ) {
90 // All tags were available in the cache, no need to run any queries.
91 return $response;
92 }
93
94 $dbr = Utilities::getSafeReadDB();
95 $vars = [ 'MAX(rt_revision) AS rt_revision', 'rt_type' ];
96 $conds = [
97 'rt_page' => $articleId,
98 'rt_type' => $remainingTags
99 ];
100
101 $options = [ 'GROUP BY' => [ 'rt_type' ] ];
102 $results = $dbr->select( 'revtag', $vars, $conds, __METHOD__, $options );
103
104 foreach ( $results as $row ) {
105 $response[$row->rt_type] = (int)$row->rt_revision;
106 }
107
108 return $response;
109 }
110
111 public function removeTags( PageIdentity $identity, string ...$tag ): void {
112 if ( !$identity->exists() ) {
113 return;
114 }
115
116 $articleId = $identity->getId();
117
118 $dbw = wfGetDB( DB_PRIMARY );
119 $conds = [
120 'rt_page' => $articleId,
121 'rt_type' => $tag,
122 ];
123 $dbw->delete( 'revtag', $conds, __METHOD__ );
124
125 unset( self::$tagCache[$articleId] );
126 }
127
129 public static function getTranslatableBundleIds( string ...$revTags ): array {
130 $dbr = Utilities::getSafeReadDB();
131
132 $tables = [ 'revtag', 'page' ];
133 $fields = 'rt_page';
134 $conds = [
135 'rt_page = page_id',
136 'rt_revision = page_latest',
137 'rt_type' => $revTags,
138 ];
139 $options = [ 'GROUP BY' => 'rt_page' ];
140
141 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options );
142 $results = [];
143 foreach ( $res as $row ) {
144 $results[] = (int)$row->rt_page;
145 }
146
147 return $results;
148 }
149}
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:30