Skip to content

Commit

Permalink
phpcs fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gesinn-it-evl committed Dec 28, 2023
1 parent b4426d7 commit f5436c3
Showing 1 changed file with 52 additions and 75 deletions.
127 changes: 52 additions & 75 deletions src/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,72 @@

use DeferredUpdates;
use JobQueueGroup;
use SMW\Options;
use SMW\Services\ServicesFactory as ApplicationFactory;
use SMW\MediaWiki\Jobs\UpdateJob;
use SMW\Services\ServicesFactory as ApplicationFactory;
use SMWDIBlob;
use SMWQueryProcessor;
use SMWSemanticData;
use SMWStore;
use WikiPage;

class Hooks
{
class Hooks {

public static function setup()
{
if (!defined('MEDIAWIKI')) {
public static function setup() {
if ( !defined( 'MEDIAWIKI' ) ) {
die();
}

if (!defined('SMW_VERSION')) {
die("ERROR: Semantic MediaWiki must be installed for Semantic Dependency Updater to run!");
if ( !defined( 'SMW_VERSION' ) ) {
die( "ERROR: Semantic MediaWiki must be installed for Semantic Dependency Updater to run!" );
}
}

public static function onAfterDataUpdateComplete(
SMWStore $store, SMWSemanticData $newData,
$compositePropertyTableDiffIterator
)
{
) {
global $wgSDUProperty;
global $wgSDUTraversed;

if (!isset($wgSDUTraversed)) {
if ( !isset( $wgSDUTraversed ) ) {
$wgSDUTraversed = [];
}

$wgSDUProperty = str_replace(' ', '_', $wgSDUProperty);
$wgSDUProperty = str_replace( ' ', '_', $wgSDUProperty );
$subject = $newData->getSubject();
$title = $subject->getTitle();
if ($title == null) {
if ( $title == null ) {
return true;
}

$id = $title->getPrefixedDBKey();

wfDebugLog('SemanticDependencyUpdater', "[SDU] --> " . $title);
wfDebugLog( 'SemanticDependencyUpdater', "[SDU] --> " . $title );

// FIRST CHECK: Does the page data contain a $wgSDUProperty semantic property ?
$properties = $newData->getProperties();
if (!isset($properties[$wgSDUProperty])) {
wfDebugLog('SemanticDependencyUpdater', "[SDU] <-- No SDU property found");
if ( !isset( $properties[$wgSDUProperty] ) ) {
wfDebugLog( 'SemanticDependencyUpdater', "[SDU] <-- No SDU property found" );
return true;
}

$diffTable = $compositePropertyTableDiffIterator->getOrderedDiffByTable();
$smwSID = $compositePropertyTableDiffIterator->getSubject()->getId();
// SECOND CHECK: Have there been actual changes in the data? (Ignore internal SMW data!)
// TODO: Introduce an explicit list of Semantic Properties to watch ?
unset($diffTable['smw_fpt_mdat']); // Ignore SMW's internal properties "smw_fpt_mdat"
unset( $diffTable['smw_fpt_mdat'] ); // Ignore SMW's internal properties "smw_fpt_mdat"

// lets try first to check the data tables: https://www.semantic-mediawiki.org/wiki/Help:Database_schema
// if change, on pageID from Issue, that is not REvision ID, then trigger all changes
$triggerSemanticDependencies = false;

if ( count( $diffTable ) > 0 ) {
wfDebugLog( 'SemanticDependencyUpdater', "[SDU] -----> Data changes detected" );

if (count($diffTable) > 0) {
wfDebugLog('SemanticDependencyUpdater', "[SDU] -----> Data changes detected");

foreach ($diffTable as $key => $value) {
if (strpos($key, 'smw_di') === 0 && is_array($value)) {
foreach ($value["insert"] as $insert) {
if ($insert["s_id"] == $smwSID) {
if ($insert["p_id"] != 506) {
foreach ( $diffTable as $key => $value ) {
if ( strpos( $key, 'smw_di' ) === 0 && is_array( $value ) ) {
foreach ( $value["insert"] as $insert ) {
if ( $insert["s_id"] == $smwSID ) {
if ( $insert["p_id"] != 506 ) {
$triggerSemanticDependencies = true;
break 2;
} // revision ID change is good, but must not trigger UpdateJob for semantic dependencies
Expand All @@ -85,20 +79,20 @@ public static function onAfterDataUpdateComplete(
}
}
} else {
wfDebugLog('SemanticDependencyUpdater', "[SDU] <-- No semantic data changes detected");
wfDebugLog( 'SemanticDependencyUpdater', "[SDU] <-- No semantic data changes detected" );
return true;
}

// THIRD CHECK: Has this page been already traversed more than twice?
// This should only be the case when SMW errors occur.
// In that case, the diffTable contains everything and SDU can't know if changes happened
if (array_key_exists($id, $wgSDUTraversed)) {
if ( array_key_exists( $id, $wgSDUTraversed ) ) {
$wgSDUTraversed[$id] = $wgSDUTraversed[$id] + 1;
} else {
$wgSDUTraversed[$id] = 1;
}
if ($wgSDUTraversed[$id] > 2) {
wfDebugLog('SemanticDependencyUpdater', "[SDU] <-- Already traversed");
if ( $wgSDUTraversed[$id] > 2 ) {
wfDebugLog( 'SemanticDependencyUpdater', "[SDU] <-- Already traversed" );
return true;
}

Expand All @@ -107,86 +101,70 @@ public static function onAfterDataUpdateComplete(
// SMW\SemanticData $newData
// SMWDataItem[] $dataItem
$wikiPageValues = [];
if ($triggerSemanticDependencies) {
$dataItem = $newData->getPropertyValues($properties[$wgSDUProperty]);
if ($dataItem != null) {
foreach ($dataItem as $valueItem) {
if ($valueItem instanceof SMWDIBlob && $valueItem->getString() != $id) {
$wikiPageValues = array_merge($wikiPageValues, self::updatePagesMatchingQuery($valueItem->getSerialization()));
if ( $triggerSemanticDependencies ) {
$dataItem = $newData->getPropertyValues( $properties[$wgSDUProperty] );
if ( $dataItem != null ) {
foreach ( $dataItem as $valueItem ) {
if ( $valueItem instanceof SMWDIBlob && $valueItem->getString() != $id ) {
$wikiPageValues = array_merge( $wikiPageValues, self::updatePagesMatchingQuery( $valueItem->getSerialization() ) );
}
}
}
} else {
$wikiPageValues = [$subject];
$wikiPageValues = [ $subject ];
}


self::rebuildData($triggerSemanticDependencies, $wikiPageValues, $subject);
self::rebuildData( $triggerSemanticDependencies, $wikiPageValues, $subject );

return true;
}

/**
* @param string $queryString Query string, excluding [[ and ]] brackets
*/
private static function updatePagesMatchingQuery($queryString)
{
private static function updatePagesMatchingQuery( $queryString ) {
global $sfgListSeparator;

$queryString = str_replace('AND', ']] [[', $queryString);
$queryString = str_replace('OR', ']] OR [[', $queryString);
$queryString = str_replace( 'AND', ']] [[', $queryString );
$queryString = str_replace( 'OR', ']] OR [[', $queryString );

// If SF is installed, get the separator character and change it into ||
// Otherwise SDU won't work with multi-value properties
if (isset($sfgListSeparator)) {
$queryString = rtrim($queryString, $sfgListSeparator);
$queryString = str_replace($sfgListSeparator, ' || ', $queryString);
if ( isset( $sfgListSeparator ) ) {
$queryString = rtrim( $queryString, $sfgListSeparator );
$queryString = str_replace( $sfgListSeparator, ' || ', $queryString );
}

wfDebugLog('SemanticDependencyUpdater', "[SDU] --------> [[$queryString]]");
wfDebugLog( 'SemanticDependencyUpdater', "[SDU] --------> [[$queryString]]" );

$store = smwfGetStore();

$params = [
'limit' => 10000,
];
$processedParams = SMWQueryProcessor::getProcessedParams($params);
$processedParams = SMWQueryProcessor::getProcessedParams( $params );
$query =
SMWQueryProcessor::createQuery("[[$queryString]]", $processedParams, SMWQueryProcessor::SPECIAL_PAGE);
$result = $store->getQueryResult($query); // SMWQueryResult
$wikiPageValues = $result->getResults(); // array of SMWWikiPageValues
SMWQueryProcessor::createQuery( "[[$queryString]]", $processedParams, SMWQueryProcessor::SPECIAL_PAGE );
$result = $store->getQueryResult( $query );
$wikiPageValues = $result->getResults();

return $wikiPageValues;
}

/**
* Rebuilds data of the given wikipages to regenerate semantic attrubutes and re-run queries
*
* @param SMWWikiPageValues[] $wikiPageValues
* @param SMWStore $store
*/
public static function rebuildData($triggerSemanticDependencies, $wikiPageValues, $subject)
{
public static function rebuildData( $triggerSemanticDependencies, $wikiPageValues, $subject ) {
global $wgSDUUseJobQueue;

if ($wgSDUUseJobQueue) {
if ( $wgSDUUseJobQueue ) {
$jobFactory = ApplicationFactory::getInstance()->newJobFactory();

if ($triggerSemanticDependencies) {
// DeferredUpdates::addCallableUpdate(static function () use ($jobFactory, $subject) {
// $job = $jobFactory->newUpdateJob(
// $subject->getTitle(),
// [
// UpdateJob::FORCED_UPDATE => true,
// 'shallowUpdate' => false
// ]
// );
// $job->run();
// });
if ( $triggerSemanticDependencies ) {

$jobs = [];

foreach ($wikiPageValues as $wikiPageValue) {
foreach ( $wikiPageValues as $wikiPageValue ) {
$jobs[] = $jobFactory->newUpdateJob(
$wikiPageValue->getTitle(),
[
Expand All @@ -195,11 +173,11 @@ public static function rebuildData($triggerSemanticDependencies, $wikiPageValues
]
);
}
if ($jobs) {
JobQueueGroup::singleton()->lazyPush($jobs);
if ( $jobs ) {
JobQueueGroup::singleton()->lazyPush( $jobs );
}
} else {
DeferredUpdates::addCallableUpdate(static function () use ($jobFactory, $wikiPageValues) {
DeferredUpdates::addCallableUpdate( static function () use ( $jobFactory, $wikiPageValues ) {
$job = $jobFactory->newUpdateJob(
$wikiPageValues[0]->getTitle(),
[
Expand All @@ -208,11 +186,10 @@ public static function rebuildData($triggerSemanticDependencies, $wikiPageValues
]
);
$job->run();
});
} );
}

}

}

}

0 comments on commit f5436c3

Please sign in to comment.