Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
<ruleset>
<rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki">
<exclude name="MediaWiki.Usage.ExtendClassUsage.FunctionVarUsage" />
<exclude name="Generic.Files.OneObjectStructurePerFile.MultipleFound" />
</rule>
<file>.</file>
<exclude-pattern>/(vendor|conf)/</exclude-pattern>
<exclude-pattern type="relative">extensions/*</exclude-pattern>
<arg name="extensions" value="php"/>
<arg name="encoding" value="UTF-8"/>
</ruleset>
</ruleset>
3 changes: 3 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"ExtensionMessagesFiles": {
"KnowledgeGraphMagic": "KnowledgeGraph.i18n.magic.php"
},
"AutoloadNamespaces": {
"MediaWiki\\Extension\\KnowledgeGraph\\Aliases\\": "includes/aliases/"
},
"AutoloadClasses": {
"KnowledgeGraph":"includes/KnowledgeGraph.php",
"SpecialKnowledgeGraphDesigner":"includes/specials/SpecialKnowledgeGraphDesigner.php",
Expand Down
53 changes: 36 additions & 17 deletions includes/KnowledgeGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @author thomas-topway-it for KM-A
*/

// use MediaWiki\Extension\KnowledgeGraph\Aliases\Category as CategoryClass;
use MediaWiki\Extension\KnowledgeGraph\Aliases\Title as TitleClass;
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\SlotRecord;
use SMW\MediaWiki\Specials\SearchByProperty\PageRequestOptions;
Expand Down Expand Up @@ -142,7 +144,7 @@
public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater = null ) {
$text = file_get_contents( __DIR__ . '/../data/KnowledgeGraphOptions.js' );
$user = RequestContext::getMain()->getUser();
$title = Title::makeTitleSafe( NS_MEDIAWIKI, 'KnowledgeGraphOptions' );
$title = TitleClass::makeTitleSafe( NS_MEDIAWIKI, 'KnowledgeGraphOptions' );

Check warning on line 147 in includes/KnowledgeGraph.php

View check run for this annotation

Codecov / codecov/patch

includes/KnowledgeGraph.php#L147

Added line #L147 was not covered by tests

$wikiPage = self::getWikiPage( $title );
$pageUpdater = $wikiPage->newPageUpdater( $user );
Expand Down Expand Up @@ -226,15 +228,15 @@
// property-related options
foreach ( $values as $val ) {
if ( preg_match( '/^property-options(\?(.+))?=(.+)/', $val, $match ) ) {
$title_ = Title::makeTitleSafe( \SMW_NS_PROPERTY, $match[2] );
$title_ = TitleClass::makeTitleSafe( \SMW_NS_PROPERTY, $match[2] );

Check warning on line 231 in includes/KnowledgeGraph.php

View check run for this annotation

Codecov / codecov/patch

includes/KnowledgeGraph.php#L231

Added line #L231 was not covered by tests
if ( $title_ ) {
$propertyOptions[$title_->getText()] = $match[3];
}
}
}

foreach ( $params['nodes'] as $titleText ) {
$title_ = Title::newFromText( $titleText );
$title_ = TitleClass::newFromText( $titleText );

Check warning on line 239 in includes/KnowledgeGraph.php

View check run for this annotation

Codecov / codecov/patch

includes/KnowledgeGraph.php#L239

Added line #L239 was not covered by tests
if ( $title_ && $title_->isKnown() ) {
if ( !isset( self::$data[$title_->getFullText()] ) ) {
self::setSemanticData( $title_, $params['properties'], 0, $params['depth'] );
Expand All @@ -245,7 +247,7 @@
$graphOptions = [];
if ( !empty( $params['graph-options'] ) ) {
// , NS_KNOWLEDGEGRAPH
$title_ = Title::newFromText( $params['graph-options'], NS_MEDIAWIKI );
$title_ = TitleClass::newFromText( $params['graph-options'], NS_MEDIAWIKI );

Check warning on line 250 in includes/KnowledgeGraph.php

View check run for this annotation

Codecov / codecov/patch

includes/KnowledgeGraph.php#L250

Added line #L250 was not covered by tests

if ( $title_ && $title_->isKnown() ) {
// $graphOptions = json_decode( self::getWikipageContent( $title_ ), true );
Expand All @@ -254,7 +256,7 @@
}

foreach ( $propertyOptions as $property => $titleText ) {
$title_ = Title::newFromText( $titleText, NS_MEDIAWIKI );
$title_ = TitleClass::newFromText( $titleText, NS_MEDIAWIKI );

Check warning on line 259 in includes/KnowledgeGraph.php

View check run for this annotation

Codecov / codecov/patch

includes/KnowledgeGraph.php#L259

Added line #L259 was not covered by tests
if ( $title_ && $title_->isKnown() ) {
// $propertyOptions[$property] = json_decode( self::getWikipageContent( $title_ ), true );
$propertyOptions[$property] = self::getWikipageContent( $title_ );
Expand Down Expand Up @@ -320,7 +322,7 @@
}

/**
* @param Title $title
* @param Title|MediaWiki\Title\Title $title $title
* @return string|null
*/
public static function getWikipageContent( $title ) {
Expand All @@ -337,7 +339,7 @@
}

/**
* @param Title $title
* @param Title|MediaWiki\Title\Title $title
* @return WikiPage|null
*/
public static function getWikiPage( $title ) {
Expand Down Expand Up @@ -443,30 +445,46 @@
}

/**
*
* @param string $category
* @param int $limit
* @param int $offset
* @return array
*/
public static function articlesInCategories( $category ) {
$dbr = wfGetDB( DB_REPLICA );
$res = $dbr->select( 'categorylinks',
public static function articlesInCategories( $category, $limit, $offset ) {
$options = [
'LIMIT' => $limit,
'OFFSET' => $offset
];
$dbr = wfGetDB( DB_REPLICA );
$res = $dbr->select( 'categorylinks',

Check warning on line 459 in includes/KnowledgeGraph.php

View check run for this annotation

Codecov / codecov/patch

includes/KnowledgeGraph.php#L453-L459

Added lines #L453 - L459 were not covered by tests
[ 'pageid' => 'cl_from' ],
[ 'cl_to' => str_replace( ' ', '_', $category ) ],
__METHOD__
);
$ret = [];
__METHOD__,
$options
);
$ret = [];

Check warning on line 465 in includes/KnowledgeGraph.php

View check run for this annotation

Codecov / codecov/patch

includes/KnowledgeGraph.php#L462-L465

Added lines #L462 - L465 were not covered by tests
foreach ( $res as $row ) {
$title_ = Title::newFromID( $row->pageid );
$title_ = TitleClass::newFromID( $row->pageid );

Check warning on line 467 in includes/KnowledgeGraph.php

View check run for this annotation

Codecov / codecov/patch

includes/KnowledgeGraph.php#L467

Added line #L467 was not covered by tests
if ( $title_ ) {
$ret[] = $title_;
}
}
return $ret;

// *** this does not work with numerical offset
// $cat = CategoryClass::newFromName( str_replace( ' ', '_', $category ) );
// $iterator_ = $cat->getMembers( $limit, $offset );
// $ret = [];
// while ( $iterator_->valid() ) {
// $ret[] = $iterator_->current();
// $iterator_->next();
// }
// return $ret;
}

/**
* @see https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/PageProperties/+/refs/heads/1.0.3/includes/PageProperties.php
* @param Title $title
* @param Title|MediaWiki\Title\Title $title
* @param array $onlyProperties
* @param int $depth
* @param int $maxDepth
Expand Down Expand Up @@ -532,7 +550,8 @@

if ( count( $onlyProperties )
&& !in_array( $canonicalLabel, $onlyProperties )
&& !in_array( $preferredLabel, $onlyProperties ) ) {
&& !in_array( $preferredLabel, $onlyProperties )

Check warning on line 553 in includes/KnowledgeGraph.php

View check run for this annotation

Codecov / codecov/patch

includes/KnowledgeGraph.php#L553

Added line #L553 was not covered by tests
) {
continue;
}

Expand Down
19 changes: 19 additions & 0 deletions includes/aliases/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* KnowledgeGraph
*
* @license GPL-2.0-or-later
* @author thomas-topway-it for KM-A
*/

namespace MediaWiki\Extension\KnowledgeGraph\Aliases;

if ( class_exists( 'Category' ) ) {
class Category extends \Category {
}
} else {
// phpcs:ignore Generic.Classes.DuplicateClassName.Found
class Category extends \Mediawiki\Title\Category {
}
}
19 changes: 19 additions & 0 deletions includes/aliases/Title.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* KnowledgeGraph
*
* @license GPL-2.0-or-later
* @author thomas-topway-it for KM-A
*/

namespace MediaWiki\Extension\KnowledgeGraph\Aliases;

if ( class_exists( 'Title' ) ) {

Check warning on line 12 in includes/aliases/Title.php

View check run for this annotation

Codecov / codecov/patch

includes/aliases/Title.php#L12

Added line #L12 was not covered by tests
class Title extends \Title {
}
} else {
// phpcs:ignore Generic.Classes.DuplicateClassName.Found
class Title extends \Mediawiki\Title\Title {
}
}
10 changes: 8 additions & 2 deletions includes/api/KnowledgeGraphApiLoadCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @author thomas-topway-it for KM-A
*/

use MediaWiki\Extension\KnowledgeGraph\Aliases\Title as TitleClass;

class KnowledgeGraphApiLoadCategories extends ApiBase {

/**
Expand Down Expand Up @@ -38,10 +40,14 @@

$titles = [];
foreach ( $categories as $categoryText ) {
$category_ = Title::makeTitleSafe( NS_CATEGORY, $categoryText );
$category_ = TitleClass::makeTitleSafe( NS_CATEGORY, $categoryText );

Check warning on line 43 in includes/api/KnowledgeGraphApiLoadCategories.php

View check run for this annotation

Codecov / codecov/patch

includes/api/KnowledgeGraphApiLoadCategories.php#L43

Added line #L43 was not covered by tests
// && $category_->isKnown()
if ( $category_ ) {
$titles_ = \KnowledgeGraph::articlesInCategories( $categoryText );
$titles_ = \KnowledgeGraph::articlesInCategories(
$categoryText,
$params['limit'],
$params['offset']
);

Check warning on line 50 in includes/api/KnowledgeGraphApiLoadCategories.php

View check run for this annotation

Codecov / codecov/patch

includes/api/KnowledgeGraphApiLoadCategories.php#L46-L50

Added lines #L46 - L50 were not covered by tests

foreach ( $titles_ as $title_ ) {
$titles[$title_->getFullText()] = $title_;
Expand Down
4 changes: 3 additions & 1 deletion includes/api/KnowledgeGraphApiLoadNodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @author thomas-topway-it for KM-A
*/

use MediaWiki\Extension\KnowledgeGraph\Aliases\Title as TitleClass;

class KnowledgeGraphApiLoadNodes extends ApiBase {

/**
Expand Down Expand Up @@ -39,7 +41,7 @@

$titles = explode( '|', $params['titles'] );
foreach ( $titles as $titleText ) {
$title_ = Title::newFromText( $titleText );
$title_ = TitleClass::newFromText( $titleText );

Check warning on line 44 in includes/api/KnowledgeGraphApiLoadNodes.php

View check run for this annotation

Codecov / codecov/patch

includes/api/KnowledgeGraphApiLoadNodes.php#L44

Added line #L44 was not covered by tests
if ( $title_ && $title_->isKnown() ) {
if ( !isset( self::$data[$title_->getFullText()] ) ) {
\KnowledgeGraph::setSemanticData( $title_, $params['properties'], 0, $params['depth'] );
Expand Down
11 changes: 7 additions & 4 deletions includes/api/KnowledgeGraphApiLoadProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @author thomas-topway-it for KM-A
*/

use MediaWiki\Extension\KnowledgeGraph\Aliases\Title as TitleClass;

class KnowledgeGraphApiLoadProperties extends ApiBase {

/**
Expand Down Expand Up @@ -37,12 +39,13 @@
$titles_ = explode( '|', $params['properties'] );
$titles = [];
foreach ( $titles_ as $titleText ) {
$title_ = Title::makeTitleSafe( SMW_NS_PROPERTY, $titleText );
$title_ = TitleClass::makeTitleSafe( SMW_NS_PROPERTY, $titleText );

Check warning on line 42 in includes/api/KnowledgeGraphApiLoadProperties.php

View check run for this annotation

Codecov / codecov/patch

includes/api/KnowledgeGraphApiLoadProperties.php#L42

Added line #L42 was not covered by tests
if ( $title_ && $title_->isKnown() ) {
$subjects = \KnowledgeGraph::getSubjectsByProperty(
$title_->getText(),
$params['limit'],
$params['offset'] );
$title_->getText(),
$params['limit'],
$params['offset']
);

Check warning on line 48 in includes/api/KnowledgeGraphApiLoadProperties.php

View check run for this annotation

Codecov / codecov/patch

includes/api/KnowledgeGraphApiLoadProperties.php#L45-L48

Added lines #L45 - L48 were not covered by tests
foreach ( $subjects as $title__ ) {
$titles[$title__->getFullText()] = $title__;
}
Expand Down
4 changes: 3 additions & 1 deletion includes/specials/SpecialKnowledgeGraphDesigner.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use MediaWiki\Extension\KnowledgeGraph\Aliases\Title as TitleClass;

/**
* @ingroup SpecialPage
*/
Expand Down Expand Up @@ -43,7 +45,7 @@ public function execute( $par ) {
$params = \KnowledgeGraph::applyDefaultParams( $defaultParameters, $params );

\KnowledgeGraph::initSMW();
$title_ = Title::makeTitleSafe( NS_MEDIAWIKI, 'KnowledgeGraphOptions' );
$title_ = TitleClass::makeTitleSafe( NS_MEDIAWIKI, 'KnowledgeGraphOptions' );

$graphOptions = [];
if ( $title_ && $title_->isKnown() ) {
Expand Down