Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
RevTagStore.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use 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 if ( !$identity->exists() ) {
66 return null;
67 }
68
69 $articleId = $identity->getId();
70
71 // ATTENTION: Cache should only be updated on POST requests.
72 if ( isset( self::$tagCache[$articleId][$tag] ) ) {
73 return self::$tagCache[$articleId][$tag];
74 }
75
76 $db = wfGetDB( DB_REPLICA );
77 $conds = [
78 'rt_page' => $articleId,
79 'rt_type' => $tag
80 ];
81
82 $options = [ 'ORDER BY' => 'rt_revision DESC' ];
83 $value = $db->selectField( 'revtag', 'rt_revision', $conds, __METHOD__, $options );
84
85 return $value === false ? null : (int)$value;
86 }
87
88 public function removeTags( PageIdentity $identity, string ...$tag ): void {
89 if ( !$identity->exists() ) {
90 return;
91 }
92
93 $articleId = $identity->getId();
94
95 $dbw = wfGetDB( DB_PRIMARY );
96 $conds = [
97 'rt_page' => $articleId,
98 'rt_type' => $tag,
99 ];
100 $dbw->delete( 'revtag', $conds, __METHOD__ );
101
102 unset( self::$tagCache[$articleId] );
103 }
104
106 public static function getTranslatableBundleIds( string ...$revTags ): array {
107 $dbr = TranslateUtils::getSafeReadDB();
108
109 $tables = [ 'revtag', 'page' ];
110 $fields = 'rt_page';
111 $conds = [
112 'rt_page = page_id',
113 'rt_revision = page_latest',
114 'rt_type' => $revTags,
115 ];
116 $options = [ 'GROUP BY' => 'rt_page' ];
117
118 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options );
119 $results = [];
120 foreach ( $res as $row ) {
121 $results[] = (int)$row->rt_page;
122 }
123
124 return $results;
125 }
126}
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.
Essentially random collection of helper functions, similar to GlobalFunctions.php.