2
2
3
3
namespace SDU ;
4
4
5
- use ContentHandler ;
6
- use MediaWiki \Revision \RevisionRecord ;
5
+ use DeferredUpdates ;
6
+ use JobQueueGroup ;
7
+ use SMW \Options ;
8
+ use SMW \Services \ServicesFactory as ApplicationFactory ;
7
9
use SMWDIBlob ;
8
10
use SMWQueryProcessor ;
9
11
use SMWSemanticData ;
10
12
use SMWStore ;
11
- use Title ;
12
13
use WikiPage ;
13
14
14
15
class Hooks {
@@ -23,8 +24,10 @@ public static function setup() {
23
24
}
24
25
}
25
26
26
- public static function onAfterDataUpdateComplete ( SMWStore $ store , SMWSemanticData $ newData ,
27
- $ compositePropertyTableDiffIterator ) {
27
+ public static function onAfterDataUpdateComplete (
28
+ SMWStore $ store , SMWSemanticData $ newData ,
29
+ $ compositePropertyTableDiffIterator
30
+ ) {
28
31
global $ wgSDUProperty ;
29
32
global $ wgSDUTraversed ;
30
33
@@ -83,14 +86,16 @@ public static function onAfterDataUpdateComplete( SMWStore $store, SMWSemanticDa
83
86
// SMWDataItem[] $dataItem
84
87
$ dataItem = $ newData ->getPropertyValues ( $ properties [$ wgSDUProperty ] );
85
88
89
+ $ wikiPageValues = [];
86
90
if ( $ dataItem != null ) {
87
91
foreach ( $ dataItem as $ valueItem ) {
88
92
if ( $ valueItem instanceof SMWDIBlob ) {
89
- self ::updatePagesMatchingQuery ( $ valueItem ->getSerialization () );
93
+ $ wikiPageValues = array_merge ( $ wikiPageValues , self ::updatePagesMatchingQuery ( $ valueItem ->getSerialization () ) );
90
94
}
91
95
}
92
96
}
93
97
98
+ self ::rebuildData ( $ wikiPageValues , $ store );
94
99
return true ;
95
100
}
96
101
@@ -123,42 +128,49 @@ private static function updatePagesMatchingQuery( $queryString ) {
123
128
$ result = $ store ->getQueryResult ( $ query ); // SMWQueryResult
124
129
$ wikiPageValues = $ result ->getResults (); // array of SMWWikiPageValues
125
130
126
- // TODO: This can be optimized by collecting a list of all pages first, make them unique
127
- // and do the dummy edit afterwards
128
- // TODO: A threshold when to switch to Queue Jobs might be smarter
129
- foreach ( $ wikiPageValues as $ page ) {
130
- self ::dummyEdit ( $ page ->getTitle () );
131
- }
131
+ return $ wikiPageValues ;
132
132
}
133
133
134
134
/**
135
- * Save a null revision in the page's history to propagate the update
135
+ * Rebuilds data of the given wikipages to regenerate semantic attrubutes and re-run queries
136
136
*
137
- * @param Title $title
137
+ * @param SMWWikiPageValues[] $wikiPageValues
138
+ * @param SMWStore $store
138
139
*/
139
- public static function dummyEdit ( $ title ) {
140
+ public static function rebuildData ( $ wikiPageValues , $ store ) {
140
141
global $ wgSDUUseJobQueue ;
141
142
143
+ $ pageArray = [];
144
+ foreach ( $ wikiPageValues as $ wikiPageValue ) {
145
+ $ page = WikiPage::newFromID ( $ wikiPageValue ->getTitle ()->getArticleId () );
146
+ if ( $ page ) {
147
+ $ pageArray [] = $ page ->getTitle ()->prefixedText ;
148
+ }
149
+ }
150
+ $ pageString = implode ( $ pageArray , "| " );
151
+
152
+ // TODO: A threshold when to switch to Queue Jobs might be smarter
142
153
if ( $ wgSDUUseJobQueue ) {
143
- wfDebugLog ( 'SemanticDependencyUpdater ' , "[SDU] --------> [Edit Job] $ title " );
144
- $ job = new DummyEditJob ( $ title );
145
- $ job ->insert ();
146
- } else {
147
- wfDebugLog ( 'SemanticDependencyUpdater ' , "[SDU] --------> [Edit] $ title " );
148
- $ page = WikiPage::newFromID ( $ title ->getArticleId () );
149
- if ( $ page ) { // prevent NPE when page not found
150
- $ content = $ page ->getContent ( RevisionRecord::RAW );
151
-
152
- if ( $ content ) {
153
- $ text = ContentHandler::getContentText ( $ content );
154
- $ page ->doEditContent ( ContentHandler::makeContent ( $ text , $ page ->getTitle () ),
155
- "[SemanticDependencyUpdater] Null edit. " ); // since this is a null edit, the edit summary will be ignored.
156
- $ page ->doPurge (); // required since SMW 2.5.1
157
-
158
- # Consider calling doSecondaryDataUpdates() for MW 1.32+
159
- # https://doc.wikimedia.org/mediawiki-core/master/php/classWikiPage.html#ac761e927ec2e7d95c9bb48aac60ff7c8
160
- }
154
+ $ jobs [] = new RebuildDataJob ( [
155
+ 'pageString ' => $ pageString ,
156
+ ] );
157
+ foreach ( $ wikiPageValues as $ page ) {
158
+ $ jobs [] = new PageUpdaterJob ( [
159
+ 'page ' => $ page
160
+ ] );
161
161
}
162
+ JobQueueGroup::singleton ()->lazyPush ( $ jobs );
163
+ } else {
164
+ DeferredUpdates::addCallableUpdate ( static function () use ( $ store , $ pageString ) {
165
+ wfDebugLog ( 'SemanticDependencyUpdater ' , "[SDU] --------> [rebuildData] $ pageString " );
166
+ $ maintenanceFactory = ApplicationFactory::getInstance ()->newMaintenanceFactory ();
167
+
168
+ $ dataRebuilder = $ maintenanceFactory ->newDataRebuilder ( $ store );
169
+ $ dataRebuilder ->setOptions (
170
+ new Options ( [ 'page ' => $ pageString ] )
171
+ );
172
+ $ dataRebuilder ->rebuild ();
173
+ } );
162
174
}
163
175
}
164
176
0 commit comments