File: PagePropsTable.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (200 lines) | stat: -rw-r--r-- 4,930 bytes parent folder | download
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php

namespace MediaWiki\Deferred\LinksUpdate;

use HTMLCacheUpdateJob;
use JobQueueGroup;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\MainConfigNames;
use MediaWiki\Parser\ParserOutput;

/**
 * page_props
 *
 * Link ID format: string[]
 *   0: Property name (pp_propname)
 *   1: Property value (pp_value)
 *
 * @since 1.38
 */
class PagePropsTable extends LinksTable {
	/** @var JobQueueGroup */
	private $jobQueueGroup;

	/** @var array */
	private $newProps = [];

	/** @var array|null */
	private $existingProps;

	/**
	 * The configured PagePropLinkInvalidations. An associative array where the
	 * key is the property name and the value is a string or array of strings
	 * giving the link table names which will be used for backlink cache
	 * invalidation.
	 *
	 * @var array
	 */
	private $linkInvalidations;

	public const CONSTRUCTOR_OPTIONS = [ MainConfigNames::PagePropLinkInvalidations ];

	public function __construct(
		ServiceOptions $options,
		JobQueueGroup $jobQueueGroup
	) {
		$this->jobQueueGroup = $jobQueueGroup;
		$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
		$this->linkInvalidations = $options->get( MainConfigNames::PagePropLinkInvalidations );
	}

	public function setParserOutput( ParserOutput $parserOutput ) {
		$this->newProps = $parserOutput->getPageProperties();
	}

	protected function getTableName() {
		return 'page_props';
	}

	protected function getFromField() {
		return 'pp_page';
	}

	protected function getExistingFields() {
		return [ 'pp_propname', 'pp_value' ];
	}

	protected function getNewLinkIDs() {
		foreach ( $this->newProps as $name => $value ) {
			yield [ (string)$name, $value ];
		}
	}

	/**
	 * Get the existing page_props as an associative array
	 *
	 * @return array
	 */
	private function getExistingProps() {
		if ( $this->existingProps === null ) {
			$this->existingProps = [];
			foreach ( $this->fetchExistingRows() as $row ) {
				$this->existingProps[$row->pp_propname] = $row->pp_value;
			}
		}
		return $this->existingProps;
	}

	protected function getExistingLinkIDs() {
		foreach ( $this->getExistingProps() as $name => $value ) {
			yield [ (string)$name, $value ];
		}
	}

	protected function isExisting( $linkId ) {
		$existing = $this->getExistingProps();
		[ $name, $value ] = $linkId;
		return \array_key_exists( $name, $existing )
			&& $this->encodeValue( $existing[$name] ) === $this->encodeValue( $value );
	}

	protected function isInNewSet( $linkId ) {
		[ $name, $value ] = $linkId;
		return \array_key_exists( $name, $this->newProps )
			&& $this->encodeValue( $this->newProps[$name] ) === $this->encodeValue( $value );
	}

	private function encodeValue( $value ) {
		if ( is_bool( $value ) ) {
			return (string)(int)$value;
		} elseif ( $value === null ) {
			return '';
		} else {
			return (string)$value;
		}
	}

	protected function insertLink( $linkId ) {
		[ $name, $value ] = $linkId;
		$this->insertRow( [
			'pp_propname' => $name,
			'pp_value' => $this->encodeValue( $value ),
			'pp_sortkey' => $this->getPropertySortKeyValue( $value )
		] );
	}

	/**
	 * Determines the sort key for the given property value.
	 * This will return $value if it is a float or int,
	 * 1 or resp. 0 if it is a bool, and null otherwise.
	 *
	 * @note In the future, we may allow the sortkey to be specified explicitly
	 *       in ParserOutput::setPageProperty (T357783).
	 *
	 * @param mixed $value
	 *
	 * @return float|null
	 */
	private function getPropertySortKeyValue( $value ) {
		if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
			return floatval( $value );
		}

		return null;
	}

	protected function deleteLink( $linkId ) {
		$this->deleteRow( [
			'pp_propname' => $linkId[0]
		] );
	}

	protected function finishUpdate() {
		$changed = array_unique( array_merge(
			array_column( $this->insertedLinks, 0 ),
			array_column( $this->deletedLinks, 0 ) ) );
		$this->invalidateProperties( $changed );
	}

	/**
	 * Invalidate the properties given the list of changed property names
	 *
	 * @param string[] $changed
	 */
	private function invalidateProperties( array $changed ) {
		$jobs = [];
		foreach ( $changed as $name ) {
			if ( isset( $this->linkInvalidations[$name] ) ) {
				$inv = $this->linkInvalidations[$name];
				if ( !is_array( $inv ) ) {
					$inv = [ $inv ];
				}
				foreach ( $inv as $table ) {
					$jobs[] = HTMLCacheUpdateJob::newForBacklinks(
						$this->getSourcePage(),
						$table,
						[ 'causeAction' => 'page-props' ]
					);
				}
			}
		}

		if ( $jobs ) {
			$this->jobQueueGroup->lazyPush( $jobs );
		}
	}

	/**
	 * Get the properties for a given link set as an associative array
	 *
	 * @param int $setType The set type as in LinksTable::getLinkIDs()
	 * @return array
	 */
	public function getAssocArray( $setType ) {
		$props = [];
		foreach ( $this->getLinkIDs( $setType ) as [ $name, $value ] ) {
			$props[$name] = $value;
		}
		return $props;
	}
}