summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/messagegroups/RecentMessageGroup.php
blob: 6059349404d3360b5245d0c17aa6205227b201d9 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
 * This file contains an unmanaged message group implementation.
 *
 * @file
 * @author Niklas Laxström
 * @author Siebrand Mazeland
 * @copyright Copyright © 2008-2013, Niklas Laxström, Siebrand Mazeland
 * @license GPL-2.0-or-later
 */

/**
 * @since 2011-11-28
 * @ingroup MessageGroup
 */
class RecentMessageGroup extends WikiMessageGroup {
	/*
	 * Yes this is very ugly hack and should not be removed.
	 * @see MessageCollection::getPages()
	 */
	protected $namespace = false;

	protected $language;

	/**
	 * These groups are always generated for one language. Method setLanguage
	 * must be called before calling getDefinitions.
	 */
	public function __construct() {
	}

	public function setLanguage( $code ) {
		$this->language = $code;
	}

	public function getId() {
		return '!recent';
	}

	public function getLabel( IContextSource $context = null ) {
		$msg = wfMessage( 'translate-dynagroup-recent-label' );
		$msg = self::addContext( $msg, $context );

		return $msg->plain();
	}

	public function getDescription( IContextSource $context = null ) {
		$msg = wfMessage( 'translate-dynagroup-recent-desc' );
		$msg = self::addContext( $msg, $context );

		return $msg->plain();
	}

	protected function getRCCutoff() {
		$db = wfGetDB( DB_REPLICA );
		$tables = 'recentchanges';
		$max = $db->selectField( $tables, 'MAX(rc_id)', [], __METHOD__ );

		return max( 0, $max - 50000 );
	}

	/**
	 * Allows subclasses to partially customize the query.
	 * @return array
	 */
	protected function getQueryConditions() {
		global $wgTranslateMessageNamespaces;
		$db = wfGetDB( DB_REPLICA );
		$conds = [
			'rc_title ' . $db->buildLike( $db->anyString(), '/' . $this->language ),
			'rc_namespace' => $wgTranslateMessageNamespaces,
			'rc_type != ' . RC_LOG,
			'rc_id > ' . $this->getRCCutoff(),
		];

		return $conds;
	}

	/**
	 * Allows subclasses to filter out more unwanted messages.
	 *
	 * @param MessageHandle $msg
	 * @return bool
	 */
	protected function matchingMessage( MessageHandle $msg ) {
		return true;
	}

	public function getDefinitions() {
		if ( !$this->language ) {
				throw new MWException( 'Language not set' );
		}

		$db = wfGetDB( DB_REPLICA );

		if ( is_callable( RecentChange::class, 'getQueryInfo' ) ) {
			$rcQuery = RecentChange::getQueryInfo();
			$tables = $rcQuery['tables'];
			$joins = $rcQuery['joins'];
		} else {
			$tables = 'recentchanges';
			$joins = [];
		}

		$fields = [ 'rc_namespace', 'rc_title' ];
		$conds = $this->getQueryConditions();
		$options = [
			'ORDER BY' => 'rc_id DESC',
			'LIMIT' => 5000
		];
		$res = $db->select( $tables, $fields, $conds, __METHOD__, $options, $joins );

		$groupIdsPreload = [];
		foreach ( $res as $row ) {
			$title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
			$handle = new MessageHandle( $title );
			if ( $handle->isValid() ) {
				$groupIdsPreload[] = $handle->getGroup()->getId();
			}
		}
		TranslateMetadata::preloadGroups( $groupIdsPreload );

		$defs = [];
		foreach ( $res as $row ) {
			$title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
			$handle = new MessageHandle( $title );

			if ( !$handle->isValid() || !$this->matchingMessage( $handle ) ) {
				continue;
			}

			$messageKey = $handle->getKey();
			$fullKey = $row->rc_namespace . ':' . $messageKey;

			/* Note: due to bugs, getMessage might return null even for
			 * known messages. These negatives are not cached, but that
			 * should be rare enough case to not affect performance. */
			if ( !isset( $defs[$fullKey] ) ) {
				$group = $handle->getGroup();
				$msg = $group->getMessage( $messageKey, $group->getSourceLanguage() );

				if ( $msg !== null ) {
					$defs[$fullKey] = $msg;
				}
			}
		}

		return $defs;
	}

	public function getChecker() {
		return null;
	}

	/**
	 * Subpage language code, if any in the title, is ignored.
	 * @param MessageHandle $handle
	 * @return null|string
	 * @throws MWException
	 */
	public function getMessageContent( MessageHandle $handle ) {
		$groupId = MessageIndex::getPrimaryGroupId( $handle );
		$group = MessageGroups::getGroup( $groupId );
		if ( $group ) {
			return $group->getMessage( $handle->getKey(), $group->getSourceLanguage() );
		}

		throw new MWException( 'Could not find group for ' . $handle->getKey() );
	}
}