-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add LinksTOPropertyAnnotator (#216)
- Loading branch information
Showing
6 changed files
with
258 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace SESP\PropertyAnnotators; | ||
|
||
use MediaWiki\MediaWikiServices; | ||
use SESP\AppFactory; | ||
use SESP\PropertyAnnotator; | ||
use SMW\DIProperty; | ||
use SMW\DIWikiPage; | ||
use SMW\SemanticData; | ||
use Title; | ||
|
||
/** | ||
* @private | ||
* @ingroup SESP | ||
* | ||
* @license GNU GPL v2+ | ||
* @since 3.0.4 | ||
*/ | ||
class LinksToPropertyAnnotator implements PropertyAnnotator { | ||
|
||
/** | ||
* Predefined property ID | ||
*/ | ||
const PROP_ID = '___LINKSTO'; | ||
|
||
/** | ||
* @var AppFactory | ||
*/ | ||
private $appFactory; | ||
|
||
private $enabledNamespaces; | ||
|
||
/** | ||
* @param AppFactory $appFactory | ||
*/ | ||
public function __construct( AppFactory $appFactory ) { | ||
$this->appFactory = $appFactory; | ||
|
||
$this->enabledNamespaces = MediaWikiServices::getInstance() | ||
->getConfigFactory() | ||
->makeConfig( 'sespg' ) | ||
->get( 'LinksToEnabledNamespaces' ); | ||
} | ||
|
||
public function setEnabledNamespaces( array $namespaces ) { | ||
$this->enabledNamespaces = $namespaces; | ||
} | ||
|
||
/** | ||
* @since 2.0 | ||
* | ||
* {@inheritDoc} | ||
*/ | ||
public function isAnnotatorFor( DIProperty $property ) { | ||
return $property->getKey() === self::PROP_ID ; | ||
} | ||
|
||
/** | ||
* @since 2.0 | ||
* | ||
* {@inheritDoc} | ||
*/ | ||
public function addAnnotation( DIProperty $property, SemanticData $semanticData ) { | ||
$page = $semanticData->getSubject()->getTitle(); | ||
|
||
if ( $page === null || ( !empty( $this->enabledNamespaces ) && !$page->inNamespaces( $this->enabledNamespaces ) ) ) { | ||
return; | ||
} | ||
|
||
$con = $this->appFactory->getConnection(); | ||
|
||
if ( $con === null ) { | ||
return; | ||
} | ||
|
||
$where = sprintf( | ||
'pl.pl_from = %s AND pl.pl_title != %s', | ||
$page->getArticleID(), | ||
$con->addQuotes( $page->getDBkey() ) | ||
); | ||
|
||
$res = $con->select( | ||
[ 'pl' => 'pagelinks', 'page' ], | ||
[ 'sel_title' => 'pl.pl_title', 'sel_ns' => 'pl.pl_namespace' ], | ||
[ $where ], | ||
__METHOD__, | ||
[ 'DISTINCT' ], | ||
[ 'page' => [ 'JOIN', 'page_id=pl_from' ] ] | ||
); | ||
|
||
foreach( $res as $row ) { | ||
$title = Title::newFromText( $row->sel_title, $row->sel_ns ); | ||
if ( $title !== null && $title->exists() ) { | ||
$semanticData->addPropertyObjectValue( $property, DIWikiPage::newFromTitle( $title ) ); | ||
} | ||
} | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
tests/phpunit/Unit/PropertyAnnotators/LinksToPropertyAnnotatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
namespace SESP\Tests\PropertyAnnotators; | ||
|
||
use PHPUnit_Framework_TestCase; | ||
use SESP\AppFactory; | ||
use SESP\PropertyAnnotators\LinksToPropertyAnnotator; | ||
use SMW\DIProperty; | ||
use SMW\DIWikiPage; | ||
use SMW\SemanticData; | ||
use Title; | ||
|
||
/** | ||
* @covers \SESP\PropertyAnnotators\LinksToPropertyAnnotator | ||
* @group semantic-extra-special-properties | ||
* | ||
* @license GNU GPL v2+ | ||
* @since 2.0 | ||
*/ | ||
class LinksToPropertyAnnotatorTest extends PHPUnit_Framework_TestCase { | ||
|
||
private $property; | ||
private $appFactory; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->appFactory = $this->getMockBuilder( '\SESP\AppFactory' ) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->property = new DIProperty( '___LINKSTO' ); | ||
} | ||
|
||
public function testCanConstruct() { | ||
|
||
$this->assertInstanceOf( | ||
LinksToPropertyAnnotator::class, | ||
new LinksToPropertyAnnotator( $this->appFactory ) | ||
); | ||
} | ||
|
||
public function testIsAnnotatorFor() { | ||
|
||
$instance = new LinksToPropertyAnnotator( | ||
$this->appFactory | ||
); | ||
|
||
$this->assertTrue( | ||
$instance->isAnnotatorFor( $this->property ) | ||
); | ||
} | ||
|
||
public function testAddAnnotation() { | ||
$factory = $this->getMockBuilder( AppFactory::class ) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$factory->expects( $this->once() )->method('getConnection'); | ||
|
||
$subject = $this->getMockBuilder( DIWikiPage::class ) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$subject->expects( $this->once() ) | ||
->method( 'getTitle' ) | ||
->willReturn( Title::newFromText( 'Foo' ) ); | ||
|
||
$semanticData = $this->getMockBuilder( SemanticData::class ) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$semanticData->expects( $this->once() ) | ||
->method( 'getSubject' ) | ||
->willReturn( $subject ); | ||
|
||
$semanticData->expects( $this->never() ) | ||
->method( 'addPropertyObjectValue' ); | ||
|
||
$annotator = new LinksToPropertyAnnotator( | ||
$factory | ||
); | ||
|
||
$annotator->addAnnotation( $this->property, $semanticData ); | ||
} | ||
|
||
public function testNotAddAnnotation() { | ||
$factory = $this->getMockBuilder( AppFactory::class ) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$factory->expects( $this->never() ) | ||
->method('getConnection'); | ||
|
||
$subject = $this->getMockBuilder( DIWikiPage::class ) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$subject->expects( $this->once() ) | ||
->method( 'getTitle' ) | ||
->willReturn( Title::newFromText( 'Foo' ) ); | ||
|
||
$semanticData = $this->getMockBuilder( SemanticData::class ) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$semanticData->expects( $this->once() ) | ||
->method( 'getSubject' ) | ||
->willReturn( $subject ); | ||
|
||
$semanticData->expects( $this->never() ) | ||
->method( 'addPropertyObjectValue' ); | ||
|
||
$annotator = new LinksToPropertyAnnotator( | ||
$factory | ||
); | ||
|
||
$annotator->setEnabledNamespaces( [ 2 ] ); | ||
|
||
$annotator->addAnnotation( $this->property, $semanticData ); | ||
} | ||
} |
63bf7dd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@octfx can you please fix the syntax that now causes broken tests? See: https://github.com/SemanticMediaWiki/SemanticExtraSpecialProperties/actions/runs/11517717288/job/32062975809