diff --git a/src/ChangeSet.php b/src/ChangeSet.php index 24f424f..850c647 100644 --- a/src/ChangeSet.php +++ b/src/ChangeSet.php @@ -15,10 +15,10 @@ use Hooks; use MediaWiki\MediaWikiServices; -use SMWDIProperty; -use SMWDIWikiPage; +use SMW\DIProperty; +use SMW\DIWikiPage; use SMWDataItem; -use SMWSemanticData; +use SMW\SemanticData; use Title; class ChangeSet { @@ -26,21 +26,21 @@ class ChangeSet { /** * The subject the changes apply to. * - * @var SMWDIWikiPage + * @var DIWikiPage */ private $subject; /** * Object holding semantic data that got inserted. * - * @var SMWSemanticData + * @var SemanticData */ private $insertions; /** * Object holding semantic data that got deleted. * - * @var SMWSemanticData + * @var SemanticData */ private $deletions; @@ -87,7 +87,7 @@ class ChangeSet { */ public static function newFromDBResult( $set ) { $changeSet = new ChangeSet( - SMWDIWikiPage::newFromTitle( Title::newFromID( $set->edit_page_id ) ), + DIWikiPage::newFromTitle( Title::newFromID( $set->edit_page_id ) ), null, null, null, @@ -100,7 +100,9 @@ public static function newFromDBResult( $set ) { ) ); - $dbr = wfGetDb( DB_REPLICA ); + $dbr = MediaWikiServices::getInstance() + ->getDBLoadBalancer() + ->getConnection( DB_REPLICA ); $changes = $dbr->select( 'swl_changes', @@ -112,11 +114,12 @@ public static function newFromDBResult( $set ) { ), array( 'change_set_id' => $set->spe_set_id - ) + ), + __METHOD__ ); foreach ( $changes as $change ) { - $property = SMWDIProperty::doUnserialize( $change->change_property, '__pro' ); + $property = DIProperty::doUnserialize( $change->change_property, '__pro' ); $changeSet->addChange( $property, @@ -139,7 +142,7 @@ public static function newFromDBResult( $set ) { */ public static function newFromArray( array $changeSetArray ) { $changeSet = new ChangeSet( - SMWDIWikiPage::newFromTitle( Title::newFromID( $changeSetArray['page_id'] ) ), + DIWikiPage::newFromTitle( Title::newFromID( $changeSetArray['page_id'] ) ), null, null, null, @@ -153,7 +156,7 @@ public static function newFromArray( array $changeSetArray ) { ); foreach ( $changeSetArray['changes'] as $propName => $changes ) { - $property = SMWDIProperty::doUnserialize( $propName, '__pro' ); + $property = DIProperty::doUnserialize( $propName, '__pro' ); foreach ( $changes as $change ) { $changeSet->addChange( @@ -171,15 +174,15 @@ public static function newFromArray( array $changeSetArray ) { } /** - * Creates and returns a new SMWChangeSet from 2 SMWSemanticData objects. + * Creates and returns a new SMWChangeSet from 2 SemanticData objects. * - * @param SMWSemanticData $old - * @param SMWSemanticData $new + * @param SemanticData $old + * @param SemanticData $new * @param array $filterProperties Optional list of properties (string serializations) to filter on. Null for no filtering. * * @return SMWChangeSet */ - public static function newFromSemanticData( SMWSemanticData $old, SMWSemanticData $new, array $filterProperties = null ) { + public static function newFromSemanticData( SemanticData $old, SemanticData $new, array $filterProperties = null ) { $subject = $old->getSubject(); if ( $subject != $new->getSubject() ) { @@ -187,8 +190,8 @@ public static function newFromSemanticData( SMWSemanticData $old, SMWSemanticDat } $changes = new PropertyChanges(); - $insertions = new SMWSemanticData( $subject ); - $deletions = new SMWSemanticData( $subject ); + $insertions = new SemanticData( $subject ); + $deletions = new SemanticData( $subject ); $oldProperties = array(); $newProperties = array(); @@ -211,7 +214,7 @@ public static function newFromSemanticData( SMWSemanticData $old, SMWSemanticDat // Find the insertions. self::findSingleDirectionChanges( $insertions, $newProperties, $new, $oldProperties, $filterProperties ); - foreach ( $oldProperties as $propertyKey => /* SMWDIProperty */ $diProperty ) { + foreach ( $oldProperties as $propertyKey => /* DIProperty */ $diProperty ) { $oldDataItems = array(); $newDataItems = array(); @@ -264,21 +267,21 @@ public static function newFromSemanticData( SMWSemanticData $old, SMWSemanticDat } /** - * Finds the inserts or deletions and adds them to the passed SMWSemanticData object. + * Finds the inserts or deletions and adds them to the passed SemanticData object. * These values will also be removed from the first list of properties and their values, * so it can be used for one-to-one change finding later on. * - * @param SMWSemanticData $changeSet + * @param SemanticData $changeSet * @param array $oldProperties - * @param SMWSemanticData $oldData + * @param SemanticData $oldData * @param array $newProperties */ - private static function findSingleDirectionChanges( SMWSemanticData &$changeSet, - array &$oldProperties, SMWSemanticData $oldData, array $newProperties ) { + private static function findSingleDirectionChanges( SemanticData &$changeSet, + array &$oldProperties, SemanticData $oldData, array $newProperties ) { $deletionKeys = array(); - foreach ( $oldProperties as $propertyKey => /* SMWDIProperty */ $diProperty ) { + foreach ( $oldProperties as $propertyKey => /* DIProperty */ $diProperty ) { if ( !array_key_exists( $propertyKey, $newProperties ) ) { foreach ( $oldData->getPropertyValues( $diProperty ) as /* SMWDataItem */ $dataItem ) { $changeSet->addPropertyObjectValue( $diProperty, $dataItem ); @@ -295,21 +298,21 @@ private static function findSingleDirectionChanges( SMWSemanticData &$changeSet, /** * Create a new instance of a change set. * - * @param SMWDIWikiPage $subject + * @param DIWikiPage $subject * @param PropertyChanges $changes Can be null - * @param SMWSemanticData $insertions Can be null - * @param SMWSemanticData $deletions Can be null + * @param SemanticData $insertions Can be null + * @param SemanticData $deletions Can be null * @param integer $id Can be null * @param Edit $edit Can be null */ - public function __construct( SMWDIWikiPage $subject, /* PropertyChanges */ $changes = null, - /* SMWSemanticData */ $insertions = null, /* SMWSemanticData */ $deletions = null, + public function __construct( DIWikiPage $subject, /* PropertyChanges */ $changes = null, + /* SemanticData */ $insertions = null, /* SemanticData */ $deletions = null, $id = null, /* Edit */ $edit = null ) { $this->subject = $subject; $this->changes = is_null( $changes ) ? new PropertyChanges() : $changes; - $this->insertions = is_null( $insertions ) ? new SMWSemanticData( $subject ): $insertions; - $this->deletions = is_null( $deletions ) ? new SMWSemanticData( $subject ): $deletions; + $this->insertions = is_null( $insertions ) ? new SemanticData( $subject ): $insertions; + $this->deletions = is_null( $deletions ) ? new SemanticData( $subject ): $deletions; $this->id = $id; $this->edit = $edit; @@ -335,7 +338,7 @@ public function hasUserDefinedProperties() { 'count' => count( $allProps ), ] ); - foreach ( $allProps as /* SMWDIProperty */ $property ) { + foreach ( $allProps as /* DIProperty */ $property ) { $userDefined = $property->isUserDefined(); wfDebugLog( @@ -369,18 +372,18 @@ public function hasChanges() { } /** - * Returns a SMWSemanticData object holding all inserted SMWDataItem objects. + * Returns a SemanticData object holding all inserted SMWDataItem objects. * - * @return SMWSemanticData + * @return SemanticData */ public function getInsertions() { return $this->insertions; } /** - * Returns a SMWSemanticData object holding all deleted SMWDataItem objects. + * Returns a SemanticData object holding all deleted SMWDataItem objects. * - * @return SMWSemanticData + * @return SemanticData */ public function getDeletions() { return $this->deletions; @@ -398,21 +401,21 @@ public function getChanges() { /** * Returns the subject these changes apply to. * - * @return SMWDIWikiPage + * @return DIWikiPage */ public function getSubject() { return $this->subject; } /** - * Adds a PropertyChange to the set for the specified SMWDIProperty. + * Adds a PropertyChange to the set for the specified DIProperty. * * @since 0.1 * - * @param SMWDIProperty $property + * @param DIProperty $property * @param PropertyChange $change */ - public function addChange( SMWDIProperty $property, PropertyChange $change ) { + public function addChange( DIProperty $property, PropertyChange $change ) { switch ( $change->getType() ) { case PropertyChange::TYPE_UPDATE: $this->changes->addPropertyObjectChange( $property, $change ); @@ -427,33 +430,33 @@ public function addChange( SMWDIProperty $property, PropertyChange $change ) { } /** - * Adds a SMWDataItem representing an insertion to the set for the specified SMWDIProperty. + * Adds a SMWDataItem representing an insertion to the set for the specified DIProperty. * * @since 0.1 * - * @param SMWDIProperty $property + * @param DIProperty $property * @param SMWDataItem $dataItem */ - public function addInsertion( SMWDIProperty $property, SMWDataItem $dataItem ) { + public function addInsertion( DIProperty $property, SMWDataItem $dataItem ) { $this->insertions->addPropertyObjectValue( $property, $dataItem ); } /** - * Adds a SMWDataItem representing a deletion to the set for the specified SMWDIProperty. + * Adds a SMWDataItem representing a deletion to the set for the specified DIProperty. * * @since 0.1 * - * @param SMWDIProperty $property + * @param DIProperty $property * @param SMWDataItem $dataItem */ - public function addDeletion( SMWDIProperty $property, SMWDataItem $dataItem ) { + public function addDeletion( DIProperty $property, SMWDataItem $dataItem ) { $this->deletions->addPropertyObjectValue( $property, $dataItem ); } /** * Returns a list of all properties. * - * @return array of SMWDIProperty + * @return array of DIProperty */ public function getAllProperties() { return array_merge( @@ -466,9 +469,9 @@ public function getAllProperties() { /** * Removes all changes for a certian property. * - * @param SMWDIProperty $property + * @param DIProperty $property */ - public function removeChangesForProperty( SMWDIProperty $property ) { + public function removeChangesForProperty( DIProperty $property ) { $this->getChanges()->removeChangesForProperty( $property ); $this->getInsertions()->removeDataForProperty( $property ); $this->getDeletions()->removeDataForProperty( $property ); @@ -477,11 +480,11 @@ public function removeChangesForProperty( SMWDIProperty $property ) { /** * Returns a list of ALL changes, including isertions and deletions. * - * @param SMWDIProperty $property + * @param DIProperty $property * * @return array of PropertyChange */ - public function getAllPropertyChanges( SMWDIProperty $property ) { + public function getAllPropertyChanges( DIProperty $property ) { $changes = array(); foreach ( $this->changes->getPropertyChanges( $property ) as /* PropertyChange */ $change ) { @@ -517,7 +520,7 @@ public function toArray() { 'changes' => array() ); - foreach ( $this->getAllProperties() as /* SMWDIProperty */ $property ) { + foreach ( $this->getAllProperties() as /* DIProperty */ $property ) { $propChanges = array(); foreach ( $this->getAllPropertyChanges( $property ) as /* PropertyChange */ $change ) { @@ -558,11 +561,14 @@ public function writeToStore( array $groupsToAssociate, $editId ) { $hookContainer = MediaWikiServices::getInstance()->getHookContainer(); $hookContainer->run( 'SWLBeforeChangeSetInsert', array( &$this, &$groupsToAssociate, &$editId ) ); - $dbw = wfGetDB( DB_PRIMARY ); + $dbw = MediaWikiServices::getInstance() + ->getDBLoadBalancer() + ->getConnection( DB_PRIMARY ); $dbw->insert( 'swl_sets', - array( 'set_id' => null ) + array( 'set_id' => null ), + __METHOD__ ); $id = $dbw->insertId(); @@ -572,12 +578,13 @@ public function writeToStore( array $groupsToAssociate, $editId ) { array( 'spe_set_id' => $id, 'spe_edit_id' => $editId - ) + ), + __METHOD__ ); $changes = array(); - foreach ( $this->getAllProperties() as /* SMWDIProperty */ $property ) { + foreach ( $this->getAllProperties() as /* DIProperty */ $property ) { if ( $property->isUserDefined() ) { $propSerialization = $property->getSerialization(); @@ -624,7 +631,8 @@ public function writeToStore( array $groupsToAssociate, $editId ) { 'change_property' => $change['property'], 'change_old_value' => $change['old'], 'change_new_value' => $change['new'] - ) + ), + __METHOD__ ); } @@ -634,7 +642,8 @@ public function writeToStore( array $groupsToAssociate, $editId ) { array( 'spg_group_id' => $group->getId(), 'spg_set_id' => $id - ) + ), + __METHOD__ ); } @@ -687,12 +696,12 @@ public function setEdit( Edit $edit ) { * * @since 0.1 * - * @param SMWDIProperty $property + * @param DIProperty $property * @param string $value * * @return boolean */ - public function hasInsertion( SMWDIProperty $property, $value ) { + public function hasInsertion( DIProperty $property, $value ) { $has = false; foreach ( $this->insertions->getPropertyValues( $property ) as /* SMWDataItem */ $insertion ) { @@ -710,12 +719,12 @@ public function hasInsertion( SMWDIProperty $property, $value ) { * * @since 0.1 * - * @param SMWDIProperty $property + * @param DIProperty $property * @param string $value * * @return boolean */ - public function hasDeletion( SMWDIProperty $property, $value ) { + public function hasDeletion( DIProperty $property, $value ) { $has = false; foreach ( $this->deletions->getPropertyValues( $property ) as /* SMWDataItem */ $deletion ) { @@ -733,12 +742,12 @@ public function hasDeletion( SMWDIProperty $property, $value ) { * * @since 0.1 * - * @param SMWDIProperty $property + * @param DIProperty $property * @param PropertyChange $change * * @return boolean */ - public function hasChange( SMWDIProperty $property, PropertyChange $change ) { + public function hasChange( DIProperty $property, PropertyChange $change ) { $has = false; foreach ( $this->changes->getPropertyChanges( $property ) as /* PropertyChange */ $propChange ) { diff --git a/src/CustomTexts.php b/src/CustomTexts.php index fb7e007..a930164 100644 --- a/src/CustomTexts.php +++ b/src/CustomTexts.php @@ -12,7 +12,8 @@ */ namespace SWL; -use SMWDIProperty; +use MediaWiki\MediaWikiServices; +use SMW\DIProperty; class CustomTexts { @@ -49,7 +50,9 @@ private function initCustomTexts() { return; } $this->customTexts = array(); - $dbr = wfGetDB( DB_REPLICA ); + $dbr = MediaWikiServices::getInstance() + ->getDBLoadBalancer() + ->getConnection( DB_REPLICA ); $row = $dbr->selectRow( 'swl_groups', 'group_custom_texts', @@ -79,12 +82,12 @@ private function initCustomTexts() { * * @since 0.2 * - * @param SMWDIProperty $property + * @param DIProperty $property * @param String $newValue * * @return String or false */ - public function getPropertyCustomText( SMWDIProperty $property, $newValue ) { + public function getPropertyCustomText( DIProperty $property, $newValue ) { $this->initCustomTexts(); if( array_key_exists( $property->getLabel(), $this->customTexts ) && array_key_exists( $newValue, $this->customTexts[$property->getLabel()] ) ) { return $this->customTexts[$property->getLabel()][$newValue]; diff --git a/src/Edit.php b/src/Edit.php index 594b5a5..99029d5 100644 --- a/src/Edit.php +++ b/src/Edit.php @@ -65,7 +65,9 @@ class Edit { * @return Edit */ public static function newFromId( $id ) { - $dbr = wfGetDB( DB_REPLICA ); + $dbr = MediaWikiServices::getInstance() + ->getDBLoadBalancer() + ->getConnection( DB_REPLICA ); return self::newFromDBResult( $dbr->select( 'swl_edits', @@ -75,7 +77,8 @@ public static function newFromId( $id ) { 'edit_page_id', 'edit_time' ), - array( 'edit_id' => $id ) + array( 'edit_id' => $id ), + __METHOD__ ) ); } @@ -134,16 +137,19 @@ public function writeToDB() { * @return boolean Success indicator */ private function updateInDB() { - $dbr = wfGetDB( DB_PRIMARY ); + $dbw = MediaWikiServices::getInstance() + ->getDBLoadBalancer() + ->getConnection( DB_PRIMARY ); - return $dbr->update( + return $dbw->update( 'swl_edits', array( 'edit_user_name' => $this->userName, 'edit_page_id' => $this->pageId, 'edit_time' => $this->time ), - array( 'edit_id' => $this->id ) + array( 'edit_id' => $this->id ), + __METHOD__ ); } @@ -158,18 +164,21 @@ private function insertIntoDB() { $hookContainer = MediaWikiServices::getInstance()->getHookContainer(); $hookContainer->run( 'SWLBeforeEditInsert', array( &$this ) ); - $dbr = wfGetDB( DB_PRIMARY ); + $dbw = MediaWikiServices::getInstance() + ->getDBLoadBalancer() + ->getConnection( DB_PRIMARY ); - $result = $dbr->insert( + $result = $dbw->insert( 'swl_edits', array( 'edit_user_name' => $this->userName, 'edit_page_id' => $this->pageId, 'edit_time' => $this->time - ) + ), + __METHOD__ ); - $this->id = $dbr->insertId(); + $this->id = $dbw->insertId(); $hookContainer->run( 'SWLAfterEditInsert', array( &$this ) ); diff --git a/src/Emailer.php b/src/Emailer.php index 1b9df0b..ca4e532 100644 --- a/src/Emailer.php +++ b/src/Emailer.php @@ -20,7 +20,7 @@ use User; use UserMailer; use MailAddress; -use SMWDataValueFactory; +use SMW\DataValueFactory; use SMW\DIProperty; final class Emailer { @@ -118,10 +118,10 @@ private static function getPropertyHTML( DIProperty $property, array $changes, C } if( !$justCustomMessage ) { if ( !is_null( $change->getOldValue() ) ) { - $deletions[] = SMWDataValueFactory::newDataItemValue( $change->getOldValue(), $property )->getShortHTMLText(); + $deletions[] = DataValueFactory::newDataItemValue( $change->getOldValue(), $property )->getShortHTMLText(); } if ( !is_null( $change->getNewValue() ) ) { - $insertions[] = SMWDataValueFactory::newDataItemValue( $change->getNewValue(), $property )->getShortHTMLText(); + $insertions[] = DataValueFactory::newDataItemValue( $change->getNewValue(), $property )->getShortHTMLText(); } } } diff --git a/src/Group.php b/src/Group.php index 278fcbd..63c58ba 100644 --- a/src/Group.php +++ b/src/Group.php @@ -20,9 +20,8 @@ use SMW\Query\Language\ConceptDescription; use SMW\Query\Language\Conjunction; use SMW\Query\Language\ValueDescription; -use SMWDIProperty; +use SMW\DIProperty; use SMWQuery; -use SMWValueDescription; use Title; use User; @@ -186,8 +185,11 @@ public function writeToDB() { * @return boolean Success indicator */ private function updateInDB() { - $dbr = wfGetDB( DB_PRIMARY ); - return $dbr->update( + $dbw = MediaWikiServices::getInstance() + ->getDBLoadBalancer() + ->getConnection( DB_PRIMARY ); + + return $dbw->update( 'swl_groups', array( 'group_name' => $this->name, @@ -197,7 +199,8 @@ private function updateInDB() { 'group_concepts' => implode( '|', $this->concepts ), 'group_custom_texts' => implode( '|', $this->getSerializedCustomTexts() ), ), - array( 'group_id' => $this->id ) + array( 'group_id' => $this->id ), + __METHOD__ ); } @@ -209,9 +212,11 @@ private function updateInDB() { * @return boolean Success indicator */ private function insertIntoDB() { - $dbr = wfGetDB( DB_PRIMARY ); + $dbw = MediaWikiServices::getInstance() + ->getDBLoadBalancer() + ->getConnection( DB_PRIMARY ); - $result = $dbr->insert( + $result = $dbw->insert( 'swl_groups', array( 'group_name' => $this->name, @@ -220,10 +225,11 @@ private function insertIntoDB() { 'group_namespaces' => implode( '|', $this->namespaces ), 'group_concepts' => implode( '|', $this->concepts ), 'group_custom_texts' => implode( '|', $this->getSerializedCustomTexts() ), - ) + ), + __METHOD__ ); - $this->id = $dbr->insertId(); + $this->id = $dbw->insertId(); return $result; } @@ -251,7 +257,7 @@ public function getNamespaces() { } /** - * Returns the properties specified by the group as strings (serializations of SMWDIProperty). + * Returns the properties specified by the group as strings (serializations of DIProperty). * * @since 0.1 * @@ -262,17 +268,17 @@ public function getProperties() { } /** - * Returns the properties specified by the group as SMWDIProperty objects. + * Returns the properties specified by the group as DIProperty objects. * * @since 0.1 * - * @return array[SMWDIProperty] + * @return array[DIProperty] */ public function getPropertyObjects() { $properties = array(); foreach ( $this->properties as $property ) { - $properties[] = SMWDIProperty::newFromSerialization( $property ); + $properties[] = DIProperty::newFromSerialization( $property ); } return $properties; @@ -481,7 +487,9 @@ public function conceptsCoverPage( Title $title ) { */ public function getWatchingUsers() { if ( $this->watchingUsers == false ) { - $dbr = wfGetDB( DB_REPLICA ); + $dbr = MediaWikiServices::getInstance() + ->getDBLoadBalancer() + ->getConnection( DB_REPLICA ); $users = $dbr->select( 'swl_users_per_group', @@ -490,7 +498,8 @@ public function getWatchingUsers() { ), array( 'upg_group_id' => $this->getId() - ) + ), + __METHOD__ ); $userIds = array(); diff --git a/src/Groups.php b/src/Groups.php index f345b3f..2441b7c 100644 --- a/src/Groups.php +++ b/src/Groups.php @@ -13,6 +13,7 @@ */ namespace SWL; +use MediaWiki\MediaWikiServices; use Title; use User; @@ -36,17 +37,24 @@ public static function getAll() { if ( self::$groups === false ) { self::$groups = array(); - $dbr = wfGetDB( DB_REPLICA ); - - $groups = $dbr->select( 'swl_groups', array( - 'group_id', - 'group_name', - 'group_categories', - 'group_namespaces', - 'group_properties', - 'group_concepts', - 'group_custom_texts' - ) ); + $dbr = MediaWikiServices::getInstance() + ->getDBLoadBalancer() + ->getConnection( DB_REPLICA ); + + $groups = $dbr->select( + 'swl_groups', + array( + 'group_id', + 'group_name', + 'group_categories', + 'group_namespaces', + 'group_properties', + 'group_concepts', + 'group_custom_texts', + ), + [], + __METHOD__ + ); foreach ( $groups as $group ) { self::$groups[] = Group::newFromDBResult( $group ); diff --git a/src/HookRegistry.php b/src/HookRegistry.php index a061cb5..86f1b98 100644 --- a/src/HookRegistry.php +++ b/src/HookRegistry.php @@ -7,6 +7,7 @@ use SWL\MediaWiki\Hooks\GetPreferences; use SWL\MediaWiki\Hooks\ExtensionSchemaUpdater; use SWL\TableUpdater; +use MediaWiki\HookContainer\HookContainer; use MediaWiki\MediaWikiServices; use MediaWiki\User\UserIdentity; use User; @@ -37,10 +38,8 @@ public function __construct( array $configuration ) { /** * @since 1.0 - * - * @param array &$wgHooks */ - public function register( &$wgHooks ) { + public function register( HookContainer $hookContainer ) { $configuration = $this->configuration; @@ -51,80 +50,91 @@ public function register( &$wgHooks ) { /** * @see https://www.mediawiki.org/wiki/Manual:Hooks/SkinTemplateNavigation::Universal */ - $wgHooks['SkinTemplateNavigation::Universal'][] = - function( $skinTemplate, &$links ) use ( $configuration ) { + $hookContainer->register( + 'SkinTemplateNavigation::Universal', + function ( $skinTemplate, &$links ) use ( $configuration ) { - $linkHandler = new SkinTemplateNavigationUniversal( - $links['user-menu'], - $skinTemplate->getTitle(), - $skinTemplate->getUser(), - MediaWikiServices::getInstance()->getUserOptionsManager() - ); + $linkHandler = new SkinTemplateNavigationUniversal( + $links['user-menu'], + $skinTemplate->getTitle(), + $skinTemplate->getUser(), + MediaWikiServices::getInstance()->getUserOptionsManager() + ); - $linkHandler->setConfiguration( $configuration ); + $linkHandler->setConfiguration( $configuration ); - return $linkHandler->execute(); - }; + return $linkHandler->execute(); + } + ); /** * @see https://www.mediawiki.org/wiki/Manual:Hooks/SaveUserOptions */ - $wgHooks['SaveUserOptions'][] = function( - UserIdentity $user, - array &$modifications, - array $originalOptions - ) use ( $configuration, $tableUpdater ) { - - $saveUserOptions = new SaveUserOptions( - $tableUpdater, - $user, - $modifications, - $originalOptions - ); - - return $saveUserOptions->execute(); - }; + $hookContainer->register( + 'SaveUserOptions', + function ( + UserIdentity $user, + array &$modifications, + array $originalOptions + ) use ( $configuration, $tableUpdater ) { + + $saveUserOptions = new SaveUserOptions( + $tableUpdater, + $user, + $modifications, + $originalOptions + ); + + return $saveUserOptions->execute(); + } + ); /** * @see https://www.mediawiki.org/wiki/Manual:Hooks/LoadExtensionSchemaUpdates */ - $wgHooks['LoadExtensionSchemaUpdates'][] = function( \DatabaseUpdater $databaseUpdater ) use ( $configuration ) { + $hookContainer->register( + 'LoadExtensionSchemaUpdates', + function ( \DatabaseUpdater $databaseUpdater ) use ( $configuration ) { - $extensionSchemaUpdater = new ExtensionSchemaUpdater( - $databaseUpdater - ); + $extensionSchemaUpdater = new ExtensionSchemaUpdater( + $databaseUpdater + ); - $extensionSchemaUpdater->setConfiguration( $configuration ); + $extensionSchemaUpdater->setConfiguration( $configuration ); - return $extensionSchemaUpdater->execute(); - }; + return $extensionSchemaUpdater->execute(); + } + ); /** * @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences */ - $wgHooks['GetPreferences'][] = function( User $user, array &$preferences ) use ( $configuration ) { + $hookContainer->register( + 'GetPreferences', + function ( User $user, array &$preferences ) use ( $configuration ) { - $userLanguage = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( - $GLOBALS['wgLang']->getCode() - ); + $userLanguage = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( + $GLOBALS['wgLang']->getCode() + ); - $getPreferences = new GetPreferences( - $user, - $userLanguage, - $preferences, - MediaWikiServices::getInstance()->getNamespaceInfo() - ); + $getPreferences = new GetPreferences( + $user, + $userLanguage, + $preferences, + MediaWikiServices::getInstance()->getNamespaceInfo() + ); - $getPreferences->setConfiguration( $configuration ); + $getPreferences->setConfiguration( $configuration ); - return $getPreferences->execute(); - }; + return $getPreferences->execute(); + } + ); - $wgHooks['AdminLinks'][] = 'SWL\\Hooks::addToAdminLinks'; - $wgHooks['SMWStore::updateDataBefore'][] = 'SWL\\Hooks::onDataUpdate'; + $hookContainer->register( 'AdminLinks', 'SWL\\Hooks::addToAdminLinks' ); + $hookContainer->register( 'SMWStore::updateDataBefore', 'SWL\\Hooks::onDataUpdate' ); if ( $configuration['egSWLEnableEmailNotify'] ) { - $wgHooks['SWLGroupNotify'][] = 'SWL\\Hooks::onGroupNotify'; + $hookContainer->register( 'SWLGroupNotify', 'SWL\\Hooks::onGroupNotify' ); } } diff --git a/src/MediaWiki/Hooks/SkinTemplateNavigationUniversal.php b/src/MediaWiki/Hooks/SkinTemplateNavigationUniversal.php index ddcdc90..67c8a48 100644 --- a/src/MediaWiki/Hooks/SkinTemplateNavigationUniversal.php +++ b/src/MediaWiki/Hooks/SkinTemplateNavigationUniversal.php @@ -20,6 +20,8 @@ */ class SkinTemplateNavigationUniversal { + private array $configuration = []; + protected $personalUrls; protected $title; protected $user; diff --git a/src/PropertyChange.php b/src/PropertyChange.php index 8c70f00..cddfa27 100644 --- a/src/PropertyChange.php +++ b/src/PropertyChange.php @@ -15,7 +15,7 @@ use SMWDataItem; use SMW\DataTypeRegistry; -use SMWDIProperty; +use SMW\DIProperty; class PropertyChange { @@ -45,7 +45,7 @@ class PropertyChange { * * @return PropertyChange */ - public static function newFromSerialization( SMWDIProperty $property, $oldValue, $newValue ) { + public static function newFromSerialization( DIProperty $property, $oldValue, $newValue ) { $typeId = $property->findPropertyTypeID(); $diType = DataTypeRegistry::getInstance()->getDataItemId( $typeId ); diff --git a/src/PropertyChanges.php b/src/PropertyChanges.php index caee6c5..ab9b474 100644 --- a/src/PropertyChanges.php +++ b/src/PropertyChanges.php @@ -17,8 +17,8 @@ namespace SWL; use Iterator; -use SMWDIProperty; -use SMWPropertyValue; +use SMW\DIProperty; +use SMW\DataValues\PropertyValue; class PropertyChanges implements Iterator { @@ -40,9 +40,9 @@ class PropertyChanges implements Iterator { private $changes = array(); /** - * Array mapping property keys (string) to SMWDIProperty objects. + * Array mapping property keys (string) to DIProperty objects. * - * @var array of SMWDIProperty + * @var array of DIProperty */ private $properties = array(); @@ -56,7 +56,7 @@ class PropertyChanges implements Iterator { /** * Get the array of all properties that have changes. * - * @return array of SMWDIProperty + * @return array of DIProperty */ public function getProperties() { return $this->properties; @@ -75,11 +75,11 @@ public function hasChanges() { /** * Get the array of all stored values for some property. * - * @param $property SMWDIProperty + * @param $property DIProperty * * @return array of PropertyChange */ - public function getPropertyChanges( SMWDIProperty $property ) { + public function getPropertyChanges( DIProperty $property ) { if ( array_key_exists( $property->getKey(), $this->changes ) ) { return $this->changes[$property->getKey()]; } else { @@ -95,10 +95,10 @@ public function getPropertyChanges( SMWDIProperty $property ) { * change, all parts of SMW are prepared to handle mismatched data item * types anyway. * - * @param SMWDIProperty $property + * @param DIProperty $property * @param PropertyChange $change */ - public function addPropertyObjectChange( SMWDIProperty $property, PropertyChange $change ) { + public function addPropertyObjectChange( DIProperty $property, PropertyChange $change ) { if ( $property->isInverse() ) { // inverse properties cannot be used for annotation return; } @@ -131,7 +131,7 @@ public function addPropertyChange( $propertyName, PropertyChange $change ) { self::$propertyPrefix = $wgContLang->getNsText( SMW_NS_PROPERTY ) . ':'; } // explicitly use prefix to cope with things like [[Property:User:Stupid::somevalue]] - $propertyDV = SMWPropertyValue::makeUserProperty( self::$propertyPrefix . $propertyName ); + $propertyDV = PropertyValue::makeUserProperty( self::$propertyPrefix . $propertyName ); if ( !$propertyDV->isValid() ) { // error, maybe illegal title text return; @@ -146,38 +146,40 @@ public function addPropertyChange( $propertyName, PropertyChange $change ) { /** * Removes all changes for a certian property. * - * @param SMWDIProperty $property + * @param DIProperty $property */ - public function removeChangesForProperty( SMWDIProperty $property ) { + public function removeChangesForProperty( DIProperty $property ) { if ( array_key_exists( $property->getKey(), $this->changes ) ) { unset( $this->changes[$property->getKey()] ); unset( $this->properties[$property->getKey()] ); } } - function rewind() { + function rewind(): void { $this->pos = 0; $this->currentRow = null; } - function current() { + function current(): mixed { if ( is_null( $this->currentRow ) ) { $this->next(); } return $this->currentRow; } - function key() { + function key(): mixed { return $this->pos; } + #[\ReturnTypeWillChange] function next() { $this->pos++; $this->currentRow = array_key_exists( $this->pos, $this->changes ) ? $this->changes[$this->pos] : false; + // TODO this doesn't match the iterator interface return $this->currentRow; } - function valid() { + function valid(): bool { return $this->current() !== false; } diff --git a/src/SemanticWatchlist.php b/src/SemanticWatchlist.php index d8d3d8c..97ffba7 100644 --- a/src/SemanticWatchlist.php +++ b/src/SemanticWatchlist.php @@ -37,7 +37,9 @@ public static function onExtensionFunction() { $configuration ); - $hookRegistry->register( $GLOBALS['wgHooks'] ); + $hookRegistry->register( + MediaWikiServices::getInstance()->getHookContainer() + ); } private static function checkRequirements() { diff --git a/src/Special/Watchlist.php b/src/Special/Watchlist.php index a3c4e06..19ac828 100644 --- a/src/Special/Watchlist.php +++ b/src/Special/Watchlist.php @@ -21,7 +21,7 @@ use Profiler; use SpecialPage; use SMWOutputs; -use SMWDataValueFactory; +use SMW\DataValueFactory; use SMW\DIProperty; use User; @@ -411,10 +411,10 @@ protected function getPropertyHTML( DIProperty $property, array $changes ) { // Convert the changes into a list of insertions and a list of deletions. foreach ( $changes as /* SWLPropertyChange */ $change ) { if ( !is_null( $change->getOldValue() ) ) { - $deletions[] = SMWDataValueFactory::newDataItemValue( $change->getOldValue(), $property )->getLongHTMLText(); + $deletions[] = DataValueFactory::newDataItemValue( $change->getOldValue(), $property )->getLongHTMLText(); } if ( !is_null( $change->getNewValue() ) ) { - $insertions[] = SMWDataValueFactory::newDataItemValue( $change->getNewValue(), $property )->getLongHTMLText(); + $insertions[] = DataValueFactory::newDataItemValue( $change->getNewValue(), $property )->getLongHTMLText(); } } @@ -453,12 +453,15 @@ protected function userHasWatchlistGroups( User $user ) { return false; } - $dbr = wfGetDB( DB_REPLICA ); + $dbr = MediaWikiServices::getInstance() + ->getDBLoadBalancer() + ->getConnection( DB_REPLICA ); $group = $dbr->selectRow( 'swl_users_per_group', array( 'upg_group_id' ), - array( 'upg_user_id' => $user->getId() ) + array( 'upg_user_id' => $user->getId() ), + __METHOD__ ); return $group !== false; diff --git a/tests/phpunit/Unit/HookRegistryTest.php b/tests/phpunit/Integration/HookRegistryTest.php similarity index 70% rename from tests/phpunit/Unit/HookRegistryTest.php rename to tests/phpunit/Integration/HookRegistryTest.php index 2345baa..21a071c 100644 --- a/tests/phpunit/Unit/HookRegistryTest.php +++ b/tests/phpunit/Integration/HookRegistryTest.php @@ -2,6 +2,8 @@ namespace SWL\Tests; +use MediaWiki\HookContainer\HookContainer; +use MediaWikiIntegrationTestCase; use SWL\HookRegistry; use SMW\DIWikiPage; use Title; @@ -9,13 +11,14 @@ /** * @covers \SWL\HookRegistry * @group semantic-watchlist + * @group Database * * @license GNU GPL v2+ * @since 1.0 * * @author mwjames */ -class HookRegistryTest extends \PHPUnit\Framework\TestCase { +class HookRegistryTest extends MediaWikiIntegrationTestCase { public function testCanConstruct() { @@ -46,23 +49,25 @@ public function testRegister() { 'wgLang' => $language ]; - $wgHooks = []; - $instance = new HookRegistry( $configuration ); - $instance->register( $wgHooks ); - - $this->assertNotEmpty( - $wgHooks - ); - $this->doTestSkinTemplateNavigationUniversal( $wgHooks, $user ); - $this->doTestSaveUserOptions( $wgHooks, $user ); - $this->doTestLoadExtensionSchemaUpdates( $wgHooks ); - $this->doTestGetPreferences( $wgHooks, $user ); - $this->doTestStoreUpdate( $wgHooks ); + $hooks = $this->getServiceContainer()->getHookContainer(); + // Clear existing handlers so that only the ones from SWL are there + $hooks->clear( 'SkinTemplateNavigation::Universal' ); + $hooks->clear( 'SaveUserOptions' ); + $hooks->clear( 'LoadExtensionSchemaUpdates' ); + $hooks->clear( 'GetPreferences' ); + $hooks->clear( 'SMWStore::updateDataBefore' ); + $instance->register( $hooks ); + + $this->doTestSkinTemplateNavigationUniversal( $hooks, $user ); + $this->doTestSaveUserOptions( $hooks, $user ); + $this->doTestLoadExtensionSchemaUpdates( $hooks ); + $this->doTestGetPreferences( $hooks, $user ); + $this->doTestStoreUpdate( $hooks ); } - private function doTestSkinTemplateNavigationUniversal( $wgHooks, $user ) { + private function doTestSkinTemplateNavigationUniversal( $hooks, $user ) { $title = $this->getMockBuilder( '\Title' ) ->disableOriginalConstructor() @@ -83,25 +88,25 @@ private function doTestSkinTemplateNavigationUniversal( $wgHooks, $user ) { $personal_urls = []; $this->assertThatHookIsExcutable( - $wgHooks, + $hooks, 'SkinTemplateNavigation::Universal', [ $skinTemplate, &$personal_urls ] ); } - private function doTestSaveUserOptions( $wgHooks, $user ) { + private function doTestSaveUserOptions( $hooks, $user ) { $options = []; $modifications = []; $this->assertThatHookIsExcutable( - $wgHooks, + $hooks, 'SaveUserOptions', [ $user, &$modifications, &$options ] ); } - private function doTestLoadExtensionSchemaUpdates( $wgHooks ) { + private function doTestLoadExtensionSchemaUpdates( $hooks ) { $database = $this->getMockBuilder( '\Wikimedia\Rdbms\Database' ) ->disableOriginalConstructor() @@ -117,24 +122,24 @@ private function doTestLoadExtensionSchemaUpdates( $wgHooks ) { ->will( $this->returnValue( $database ) ); $this->assertThatHookIsExcutable( - $wgHooks, + $hooks, 'LoadExtensionSchemaUpdates', [ $databaseUpdater ] ); } - private function doTestGetPreferences( $wgHooks, $user ) { + private function doTestGetPreferences( $hooks, $user ) { $preferences = []; $this->assertThatHookIsExcutable( - $wgHooks, + $hooks, 'GetPreferences', [ $user, &$preferences ] ); } - public function doTestStoreUpdate( $wgHooks ) { + public function doTestStoreUpdate( $hooks ) { $subject = DIWikiPage::newFromTitle( Title::newFromText( __METHOD__ ) ); @@ -163,18 +168,16 @@ public function doTestStoreUpdate( $wgHooks ) { ->will( $this->returnValue( $semanticData ) ); $this->assertThatHookIsExcutable( - $wgHooks, + $hooks, 'SMWStore::updateDataBefore', [ $store, $semanticData ] ); } - private function assertThatHookIsExcutable( $wgHooks, $hookName, $arguments ) { - foreach ( $wgHooks[ $hookName ] as $hook ) { - $this->assertIsBool( - call_user_func_array( $hook, $arguments ) - ); - } + private function assertThatHookIsExcutable( $hooks, $hookName, $arguments ) { + $this->assertIsBool( + $hooks->run( $hookName, $arguments ) + ); } } diff --git a/tests/phpunit/Unit/MediaWiki/Hooks/ExtensionSchemaUpdaterTest.php b/tests/phpunit/Unit/MediaWiki/Hooks/ExtensionSchemaUpdaterTest.php index 5609f2f..b0590bd 100644 --- a/tests/phpunit/Unit/MediaWiki/Hooks/ExtensionSchemaUpdaterTest.php +++ b/tests/phpunit/Unit/MediaWiki/Hooks/ExtensionSchemaUpdaterTest.php @@ -85,9 +85,8 @@ public function testExtensionUpdateForValidDBType( $dbType ) { ->method( 'getDB' ) ->will( $this->returnValue( $dbConnection ) ); - $databaseUpdater->expects( $this->at( 6 ) ) - ->method( 'addExtensionUpdate' ) - ->will( $this->returnValue( true ) ); + $databaseUpdater->expects( $this->exactly( 6 ) ) + ->method( 'addExtensionUpdate' ); $configuration = array( 'egSWLSqlDatabaseSchemaPath' => 'foo' );