MediaWiki master
PagePropsTable.php
Go to the documentation of this file.
1<?php
2
4
10
22 private $jobQueueGroup;
23
25 private $newProps = [];
26
28 private $existingProps;
29
38 private $linkInvalidations;
39
41
42 public function __construct(
43 ServiceOptions $options,
44 JobQueueGroup $jobQueueGroup
45 ) {
46 $this->jobQueueGroup = $jobQueueGroup;
47 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
48 $this->linkInvalidations = $options->get( MainConfigNames::PagePropLinkInvalidations );
49 }
50
51 public function setParserOutput( ParserOutput $parserOutput ) {
52 $this->newProps = $parserOutput->getPageProperties();
53 }
54
55 protected function getTableName() {
56 return 'page_props';
57 }
58
59 protected function getFromField() {
60 return 'pp_page';
61 }
62
63 protected function getExistingFields() {
64 return [ 'pp_propname', 'pp_value' ];
65 }
66
67 protected function getNewLinkIDs() {
68 foreach ( $this->newProps as $name => $value ) {
69 yield [ (string)$name, $value ];
70 }
71 }
72
78 private function getExistingProps() {
79 if ( $this->existingProps === null ) {
80 $this->existingProps = [];
81 foreach ( $this->fetchExistingRows() as $row ) {
82 $this->existingProps[$row->pp_propname] = $row->pp_value;
83 }
84 }
85 return $this->existingProps;
86 }
87
88 protected function getExistingLinkIDs() {
89 foreach ( $this->getExistingProps() as $name => $value ) {
90 yield [ (string)$name, $value ];
91 }
92 }
93
94 protected function isExisting( $linkId ) {
95 $existing = $this->getExistingProps();
96 [ $name, $value ] = $linkId;
97 return \array_key_exists( $name, $existing )
98 && $this->encodeValue( $existing[$name] ) === $this->encodeValue( $value );
99 }
100
101 protected function isInNewSet( $linkId ) {
102 [ $name, $value ] = $linkId;
103 return \array_key_exists( $name, $this->newProps )
104 && $this->encodeValue( $this->newProps[$name] ) === $this->encodeValue( $value );
105 }
106
110 private function encodeValue( $value ): string {
111 if ( is_bool( $value ) ) {
112 return (string)(int)$value;
113 } elseif ( $value === null ) {
114 return '';
115 } else {
116 return (string)$value;
117 }
118 }
119
120 protected function insertLink( $linkId ) {
121 [ $name, $value ] = $linkId;
122 $this->insertRow( [
123 'pp_propname' => $name,
124 'pp_value' => $this->encodeValue( $value ),
125 'pp_sortkey' => $this->getPropertySortKeyValue( $value )
126 ] );
127 }
128
141 private function getPropertySortKeyValue( $value ) {
142 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
143 return floatval( $value );
144 }
145
146 return null;
147 }
148
149 protected function deleteLink( $linkId ) {
150 $this->deleteRow( [
151 'pp_propname' => $linkId[0]
152 ] );
153 }
154
155 protected function finishUpdate() {
156 $changed = array_unique( array_merge(
157 array_column( $this->insertedLinks, 0 ),
158 array_column( $this->deletedLinks, 0 ) ) );
159 $this->invalidateProperties( $changed );
160 }
161
167 private function invalidateProperties( array $changed ) {
168 $jobs = [];
169 foreach ( $changed as $name ) {
170 if ( isset( $this->linkInvalidations[$name] ) ) {
171 $inv = $this->linkInvalidations[$name];
172 if ( !is_array( $inv ) ) {
173 $inv = [ $inv ];
174 }
175 foreach ( $inv as $table ) {
177 $this->getSourcePage(),
178 $table,
179 [ 'causeAction' => 'page-props' ]
180 );
181 }
182 }
183 }
184
185 if ( $jobs ) {
186 $this->jobQueueGroup->lazyPush( $jobs );
187 }
188 }
189
196 public function getAssocArray( $setType ) {
197 $props = [];
198 foreach ( $this->getLinkIDs( $setType ) as [ $name, $value ] ) {
199 $props[$name] = $value;
200 }
201 return $props;
202 }
203}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:82
A class for passing options to services.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
The base class for classes which update a single link table.
fetchExistingRows()
Do a select query to fetch the existing rows.
finishUpdate()
Subclasses can override this to do any updates associated with their link data, for example dispatchi...
getFromField()
Get the name of the field which links to page_id.
getExistingFields()
Get the fields to be used in fetchExistingRows().
insertLink( $linkId)
Insert a link identified by ID.
isExisting( $linkId)
Determine whether a link (from the new set) is in the existing set.
deleteLink( $linkId)
Delete a link identified by ID.
getExistingLinkIDs()
Get an array (or iterator) of link IDs for the existing state.
__construct(ServiceOptions $options, JobQueueGroup $jobQueueGroup)
setParserOutput(ParserOutput $parserOutput)
Subclasses should implement this to extract the data they need from the ParserOutput.
getAssocArray( $setType)
Get the properties for a given link set as an associative array.
getNewLinkIDs()
Get an array (or iterator) of link IDs for the new state.
isInNewSet( $linkId)
Determine whether a link (from the existing set) is in the new set.
Handle enqueueing of background jobs.
Job to purge the HTML/file cache for all pages that link to or use another page or file.
static newForBacklinks(PageReference $page, $table, $params=[])
A class containing constants representing the names of configuration variables.
const PagePropLinkInvalidations
Name constant for the PagePropLinkInvalidations setting, for use with Config::get()
ParserOutput is a rendering of a Content object or a message.
getPageProperties()
Return all the page properties set on this ParserOutput.