4
4
5
5
use DeferredUpdates ;
6
6
use JobQueueGroup ;
7
- use SMW \Options ;
8
- use SMW \Services \ServicesFactory as ApplicationFactory ;
9
7
use SMW \MediaWiki \Jobs \UpdateJob ;
8
+ use SMW \Services \ServicesFactory as ApplicationFactory ;
10
9
use SMWDIBlob ;
11
10
use SMWQueryProcessor ;
12
11
use SMWSemanticData ;
13
12
use SMWStore ;
14
- use WikiPage ;
15
13
16
- class Hooks
17
- {
14
+ class Hooks {
18
15
19
- public static function setup ()
20
- {
21
- if (!defined ('MEDIAWIKI ' )) {
16
+ public static function setup () {
17
+ if ( !defined ( 'MEDIAWIKI ' ) ) {
22
18
die ();
23
19
}
24
20
25
- if (!defined ('SMW_VERSION ' ) ) {
26
- die ("ERROR: Semantic MediaWiki must be installed for Semantic Dependency Updater to run! " );
21
+ if ( !defined ( 'SMW_VERSION ' ) ) {
22
+ die ( "ERROR: Semantic MediaWiki must be installed for Semantic Dependency Updater to run! " );
27
23
}
28
24
}
29
25
30
26
public static function onAfterDataUpdateComplete (
31
27
SMWStore $ store , SMWSemanticData $ newData ,
32
28
$ compositePropertyTableDiffIterator
33
- )
34
- {
29
+ ) {
35
30
global $ wgSDUProperty ;
36
31
global $ wgSDUTraversed ;
37
32
38
- if (!isset ($ wgSDUTraversed) ) {
33
+ if ( !isset ( $ wgSDUTraversed ) ) {
39
34
$ wgSDUTraversed = [];
40
35
}
41
36
42
- $ wgSDUProperty = str_replace (' ' , '_ ' , $ wgSDUProperty );
37
+ $ wgSDUProperty = str_replace ( ' ' , '_ ' , $ wgSDUProperty );
43
38
$ subject = $ newData ->getSubject ();
44
39
$ title = $ subject ->getTitle ();
45
- if ($ title == null ) {
40
+ if ( $ title == null ) {
46
41
return true ;
47
42
}
48
43
49
44
$ id = $ title ->getPrefixedDBKey ();
50
45
51
- wfDebugLog ('SemanticDependencyUpdater ' , "[SDU] --> " . $ title );
46
+ wfDebugLog ( 'SemanticDependencyUpdater ' , "[SDU] --> " . $ title );
52
47
53
48
// FIRST CHECK: Does the page data contain a $wgSDUProperty semantic property ?
54
49
$ properties = $ newData ->getProperties ();
55
- if (!isset ($ properties [$ wgSDUProperty ]) ) {
56
- wfDebugLog ('SemanticDependencyUpdater ' , "[SDU] <-- No SDU property found " );
50
+ if ( !isset ( $ properties [$ wgSDUProperty ] ) ) {
51
+ wfDebugLog ( 'SemanticDependencyUpdater ' , "[SDU] <-- No SDU property found " );
57
52
return true ;
58
53
}
59
54
60
55
$ diffTable = $ compositePropertyTableDiffIterator ->getOrderedDiffByTable ();
61
56
$ smwSID = $ compositePropertyTableDiffIterator ->getSubject ()->getId ();
62
57
// SECOND CHECK: Have there been actual changes in the data? (Ignore internal SMW data!)
63
58
// TODO: Introduce an explicit list of Semantic Properties to watch ?
64
- unset($ diffTable ['smw_fpt_mdat ' ]); // Ignore SMW's internal properties "smw_fpt_mdat"
59
+ unset( $ diffTable ['smw_fpt_mdat ' ] ); // Ignore SMW's internal properties "smw_fpt_mdat"
65
60
66
61
// lets try first to check the data tables: https://www.semantic-mediawiki.org/wiki/Help:Database_schema
67
62
// if change, on pageID from Issue, that is not REvision ID, then trigger all changes
68
63
$ triggerSemanticDependencies = false ;
69
64
65
+ if ( count ( $ diffTable ) > 0 ) {
66
+ wfDebugLog ( 'SemanticDependencyUpdater ' , "[SDU] -----> Data changes detected " );
70
67
71
- if (count ($ diffTable ) > 0 ) {
72
- wfDebugLog ('SemanticDependencyUpdater ' , "[SDU] -----> Data changes detected " );
73
-
74
- foreach ($ diffTable as $ key => $ value ) {
75
- if (strpos ($ key , 'smw_di ' ) === 0 && is_array ($ value )) {
76
- foreach ($ value ["insert " ] as $ insert ) {
77
- if ($ insert ["s_id " ] == $ smwSID ) {
78
- if ($ insert ["p_id " ] != 506 ) {
68
+ foreach ( $ diffTable as $ key => $ value ) {
69
+ if ( strpos ( $ key , 'smw_di ' ) === 0 && is_array ( $ value ) ) {
70
+ foreach ( $ value ["insert " ] as $ insert ) {
71
+ if ( $ insert ["s_id " ] == $ smwSID ) {
72
+ if ( $ insert ["p_id " ] != 506 ) {
79
73
$ triggerSemanticDependencies = true ;
80
74
break 2 ;
81
75
} // revision ID change is good, but must not trigger UpdateJob for semantic dependencies
@@ -85,20 +79,20 @@ public static function onAfterDataUpdateComplete(
85
79
}
86
80
}
87
81
} else {
88
- wfDebugLog ('SemanticDependencyUpdater ' , "[SDU] <-- No semantic data changes detected " );
82
+ wfDebugLog ( 'SemanticDependencyUpdater ' , "[SDU] <-- No semantic data changes detected " );
89
83
return true ;
90
84
}
91
85
92
86
// THIRD CHECK: Has this page been already traversed more than twice?
93
87
// This should only be the case when SMW errors occur.
94
88
// In that case, the diffTable contains everything and SDU can't know if changes happened
95
- if (array_key_exists ($ id , $ wgSDUTraversed) ) {
89
+ if ( array_key_exists ( $ id , $ wgSDUTraversed ) ) {
96
90
$ wgSDUTraversed [$ id ] = $ wgSDUTraversed [$ id ] + 1 ;
97
91
} else {
98
92
$ wgSDUTraversed [$ id ] = 1 ;
99
93
}
100
- if ($ wgSDUTraversed [$ id ] > 2 ) {
101
- wfDebugLog ('SemanticDependencyUpdater ' , "[SDU] <-- Already traversed " );
94
+ if ( $ wgSDUTraversed [$ id ] > 2 ) {
95
+ wfDebugLog ( 'SemanticDependencyUpdater ' , "[SDU] <-- Already traversed " );
102
96
return true ;
103
97
}
104
98
@@ -107,86 +101,70 @@ public static function onAfterDataUpdateComplete(
107
101
// SMW\SemanticData $newData
108
102
// SMWDataItem[] $dataItem
109
103
$ wikiPageValues = [];
110
- if ($ triggerSemanticDependencies ) {
111
- $ dataItem = $ newData ->getPropertyValues ($ properties [$ wgSDUProperty ]);
112
- if ($ dataItem != null ) {
113
- foreach ($ dataItem as $ valueItem ) {
114
- if ($ valueItem instanceof SMWDIBlob && $ valueItem ->getString () != $ id ) {
115
- $ wikiPageValues = array_merge ($ wikiPageValues , self ::updatePagesMatchingQuery ($ valueItem ->getSerialization ()) );
104
+ if ( $ triggerSemanticDependencies ) {
105
+ $ dataItem = $ newData ->getPropertyValues ( $ properties [$ wgSDUProperty ] );
106
+ if ( $ dataItem != null ) {
107
+ foreach ( $ dataItem as $ valueItem ) {
108
+ if ( $ valueItem instanceof SMWDIBlob && $ valueItem ->getString () != $ id ) {
109
+ $ wikiPageValues = array_merge ( $ wikiPageValues , self ::updatePagesMatchingQuery ( $ valueItem ->getSerialization () ) );
116
110
}
117
111
}
118
112
}
119
113
} else {
120
- $ wikiPageValues = [$ subject ];
114
+ $ wikiPageValues = [ $ subject ];
121
115
}
122
116
123
-
124
- self ::rebuildData ($ triggerSemanticDependencies , $ wikiPageValues , $ subject );
117
+ self ::rebuildData ( $ triggerSemanticDependencies , $ wikiPageValues , $ subject );
125
118
126
119
return true ;
127
120
}
128
121
129
122
/**
130
123
* @param string $queryString Query string, excluding [[ and ]] brackets
131
124
*/
132
- private static function updatePagesMatchingQuery ($ queryString )
133
- {
125
+ private static function updatePagesMatchingQuery ( $ queryString ) {
134
126
global $ sfgListSeparator ;
135
127
136
- $ queryString = str_replace ('AND ' , ']] [[ ' , $ queryString );
137
- $ queryString = str_replace ('OR ' , ']] OR [[ ' , $ queryString );
128
+ $ queryString = str_replace ( 'AND ' , ']] [[ ' , $ queryString );
129
+ $ queryString = str_replace ( 'OR ' , ']] OR [[ ' , $ queryString );
138
130
139
131
// If SF is installed, get the separator character and change it into ||
140
132
// Otherwise SDU won't work with multi-value properties
141
- if (isset ($ sfgListSeparator) ) {
142
- $ queryString = rtrim ($ queryString , $ sfgListSeparator );
143
- $ queryString = str_replace ($ sfgListSeparator , ' || ' , $ queryString );
133
+ if ( isset ( $ sfgListSeparator ) ) {
134
+ $ queryString = rtrim ( $ queryString , $ sfgListSeparator );
135
+ $ queryString = str_replace ( $ sfgListSeparator , ' || ' , $ queryString );
144
136
}
145
137
146
- wfDebugLog ('SemanticDependencyUpdater ' , "[SDU] --------> [[ $ queryString]] " );
138
+ wfDebugLog ( 'SemanticDependencyUpdater ' , "[SDU] --------> [[ $ queryString]] " );
147
139
148
140
$ store = smwfGetStore ();
149
141
150
142
$ params = [
151
143
'limit ' => 10000 ,
152
144
];
153
- $ processedParams = SMWQueryProcessor::getProcessedParams ($ params );
145
+ $ processedParams = SMWQueryProcessor::getProcessedParams ( $ params );
154
146
$ query =
155
- SMWQueryProcessor::createQuery ("[[ $ queryString]] " , $ processedParams , SMWQueryProcessor::SPECIAL_PAGE );
156
- $ result = $ store ->getQueryResult ($ query); // SMWQueryResult
157
- $ wikiPageValues = $ result ->getResults (); // array of SMWWikiPageValues
147
+ SMWQueryProcessor::createQuery ( "[[ $ queryString]] " , $ processedParams , SMWQueryProcessor::SPECIAL_PAGE );
148
+ $ result = $ store ->getQueryResult ( $ query );
149
+ $ wikiPageValues = $ result ->getResults ();
158
150
159
151
return $ wikiPageValues ;
160
152
}
161
153
162
154
/**
163
155
* Rebuilds data of the given wikipages to regenerate semantic attrubutes and re-run queries
164
- *
165
- * @param SMWWikiPageValues[] $wikiPageValues
166
- * @param SMWStore $store
167
156
*/
168
- public static function rebuildData ($ triggerSemanticDependencies , $ wikiPageValues , $ subject )
169
- {
157
+ public static function rebuildData ( $ triggerSemanticDependencies , $ wikiPageValues , $ subject ) {
170
158
global $ wgSDUUseJobQueue ;
171
159
172
- if ($ wgSDUUseJobQueue ) {
160
+ if ( $ wgSDUUseJobQueue ) {
173
161
$ jobFactory = ApplicationFactory::getInstance ()->newJobFactory ();
174
162
175
- if ($ triggerSemanticDependencies ) {
176
- // DeferredUpdates::addCallableUpdate(static function () use ($jobFactory, $subject) {
177
- // $job = $jobFactory->newUpdateJob(
178
- // $subject->getTitle(),
179
- // [
180
- // UpdateJob::FORCED_UPDATE => true,
181
- // 'shallowUpdate' => false
182
- // ]
183
- // );
184
- // $job->run();
185
- // });
163
+ if ( $ triggerSemanticDependencies ) {
186
164
187
165
$ jobs = [];
188
166
189
- foreach ($ wikiPageValues as $ wikiPageValue ) {
167
+ foreach ( $ wikiPageValues as $ wikiPageValue ) {
190
168
$ jobs [] = $ jobFactory ->newUpdateJob (
191
169
$ wikiPageValue ->getTitle (),
192
170
[
@@ -195,11 +173,11 @@ public static function rebuildData($triggerSemanticDependencies, $wikiPageValues
195
173
]
196
174
);
197
175
}
198
- if ($ jobs ) {
199
- JobQueueGroup::singleton ()->lazyPush ($ jobs );
176
+ if ( $ jobs ) {
177
+ JobQueueGroup::singleton ()->lazyPush ( $ jobs );
200
178
}
201
179
} else {
202
- DeferredUpdates::addCallableUpdate (static function () use ($ jobFactory , $ wikiPageValues ) {
180
+ DeferredUpdates::addCallableUpdate ( static function () use ( $ jobFactory , $ wikiPageValues ) {
203
181
$ job = $ jobFactory ->newUpdateJob (
204
182
$ wikiPageValues [0 ]->getTitle (),
205
183
[
@@ -208,11 +186,10 @@ public static function rebuildData($triggerSemanticDependencies, $wikiPageValues
208
186
]
209
187
);
210
188
$ job ->run ();
211
- });
189
+ } );
212
190
}
213
191
214
192
}
215
-
216
193
}
217
194
218
195
}
0 commit comments