diff --git a/.phpcs.xml b/.phpcs.xml index 08d1d1d..7d7d22e 100644 --- a/.phpcs.xml +++ b/.phpcs.xml @@ -2,10 +2,11 @@ + . /(vendor|conf)/ extensions/* - \ No newline at end of file + diff --git a/extension.json b/extension.json index 91f0a80..d8ee268 100644 --- a/extension.json +++ b/extension.json @@ -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", diff --git a/includes/KnowledgeGraph.php b/includes/KnowledgeGraph.php index fc6bfc2..0a7c67d 100644 --- a/includes/KnowledgeGraph.php +++ b/includes/KnowledgeGraph.php @@ -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; @@ -142,7 +144,7 @@ public static function onParserFirstCallInit( Parser $parser ) { 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' ); $wikiPage = self::getWikiPage( $title ); $pageUpdater = $wikiPage->newPageUpdater( $user ); @@ -226,7 +228,7 @@ public static function parserFunctionKnowledgeGraph( Parser $parser, ...$argv ) // 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] ); if ( $title_ ) { $propertyOptions[$title_->getText()] = $match[3]; } @@ -234,7 +236,7 @@ public static function parserFunctionKnowledgeGraph( Parser $parser, ...$argv ) } foreach ( $params['nodes'] as $titleText ) { - $title_ = Title::newFromText( $titleText ); + $title_ = TitleClass::newFromText( $titleText ); if ( $title_ && $title_->isKnown() ) { if ( !isset( self::$data[$title_->getFullText()] ) ) { self::setSemanticData( $title_, $params['properties'], 0, $params['depth'] ); @@ -245,7 +247,7 @@ public static function parserFunctionKnowledgeGraph( Parser $parser, ...$argv ) $graphOptions = []; if ( !empty( $params['graph-options'] ) ) { // , NS_KNOWLEDGEGRAPH - $title_ = Title::newFromText( $params['graph-options'], NS_MEDIAWIKI ); + $title_ = TitleClass::newFromText( $params['graph-options'], NS_MEDIAWIKI ); if ( $title_ && $title_->isKnown() ) { // $graphOptions = json_decode( self::getWikipageContent( $title_ ), true ); @@ -254,7 +256,7 @@ public static function parserFunctionKnowledgeGraph( Parser $parser, ...$argv ) } foreach ( $propertyOptions as $property => $titleText ) { - $title_ = Title::newFromText( $titleText, NS_MEDIAWIKI ); + $title_ = TitleClass::newFromText( $titleText, NS_MEDIAWIKI ); if ( $title_ && $title_->isKnown() ) { // $propertyOptions[$property] = json_decode( self::getWikipageContent( $title_ ), true ); $propertyOptions[$property] = self::getWikipageContent( $title_ ); @@ -320,7 +322,7 @@ public static function getSubjectsByProperty( $propertyText, $limit = 100, $offs } /** - * @param Title $title + * @param Title|MediaWiki\Title\Title $title $title * @return string|null */ public static function getWikipageContent( $title ) { @@ -337,7 +339,7 @@ public static function getWikipageContent( $title ) { } /** - * @param Title $title + * @param Title|MediaWiki\Title\Title $title * @return WikiPage|null */ public static function getWikiPage( $title ) { @@ -443,30 +445,46 @@ public static function parseParameters( $parameters, $defaultParameters ) { } /** - * * @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', [ 'pageid' => 'cl_from' ], [ 'cl_to' => str_replace( ' ', '_', $category ) ], - __METHOD__ - ); - $ret = []; + __METHOD__, + $options + ); + $ret = []; foreach ( $res as $row ) { - $title_ = Title::newFromID( $row->pageid ); + $title_ = TitleClass::newFromID( $row->pageid ); 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 @@ -532,7 +550,8 @@ public static function setSemanticData( Title $title, $onlyProperties, $depth, $ if ( count( $onlyProperties ) && !in_array( $canonicalLabel, $onlyProperties ) - && !in_array( $preferredLabel, $onlyProperties ) ) { + && !in_array( $preferredLabel, $onlyProperties ) + ) { continue; } diff --git a/includes/aliases/Category.php b/includes/aliases/Category.php new file mode 100644 index 0000000..24edf85 --- /dev/null +++ b/includes/aliases/Category.php @@ -0,0 +1,19 @@ +isKnown() if ( $category_ ) { - $titles_ = \KnowledgeGraph::articlesInCategories( $categoryText ); + $titles_ = \KnowledgeGraph::articlesInCategories( + $categoryText, + $params['limit'], + $params['offset'] + ); foreach ( $titles_ as $title_ ) { $titles[$title_->getFullText()] = $title_; diff --git a/includes/api/KnowledgeGraphApiLoadNodes.php b/includes/api/KnowledgeGraphApiLoadNodes.php index ef6425c..0789c0a 100644 --- a/includes/api/KnowledgeGraphApiLoadNodes.php +++ b/includes/api/KnowledgeGraphApiLoadNodes.php @@ -7,6 +7,8 @@ * @author thomas-topway-it for KM-A */ +use MediaWiki\Extension\KnowledgeGraph\Aliases\Title as TitleClass; + class KnowledgeGraphApiLoadNodes extends ApiBase { /** @@ -39,7 +41,7 @@ public function execute() { $titles = explode( '|', $params['titles'] ); foreach ( $titles as $titleText ) { - $title_ = Title::newFromText( $titleText ); + $title_ = TitleClass::newFromText( $titleText ); if ( $title_ && $title_->isKnown() ) { if ( !isset( self::$data[$title_->getFullText()] ) ) { \KnowledgeGraph::setSemanticData( $title_, $params['properties'], 0, $params['depth'] ); diff --git a/includes/api/KnowledgeGraphApiLoadProperties.php b/includes/api/KnowledgeGraphApiLoadProperties.php index 65f4211..4c3084e 100644 --- a/includes/api/KnowledgeGraphApiLoadProperties.php +++ b/includes/api/KnowledgeGraphApiLoadProperties.php @@ -7,6 +7,8 @@ * @author thomas-topway-it for KM-A */ +use MediaWiki\Extension\KnowledgeGraph\Aliases\Title as TitleClass; + class KnowledgeGraphApiLoadProperties extends ApiBase { /** @@ -37,12 +39,13 @@ public function execute() { $titles_ = explode( '|', $params['properties'] ); $titles = []; foreach ( $titles_ as $titleText ) { - $title_ = Title::makeTitleSafe( SMW_NS_PROPERTY, $titleText ); + $title_ = TitleClass::makeTitleSafe( SMW_NS_PROPERTY, $titleText ); if ( $title_ && $title_->isKnown() ) { $subjects = \KnowledgeGraph::getSubjectsByProperty( - $title_->getText(), - $params['limit'], - $params['offset'] ); + $title_->getText(), + $params['limit'], + $params['offset'] + ); foreach ( $subjects as $title__ ) { $titles[$title__->getFullText()] = $title__; } diff --git a/includes/specials/SpecialKnowledgeGraphDesigner.php b/includes/specials/SpecialKnowledgeGraphDesigner.php index e5ce398..6431e84 100644 --- a/includes/specials/SpecialKnowledgeGraphDesigner.php +++ b/includes/specials/SpecialKnowledgeGraphDesigner.php @@ -1,5 +1,7 @@ isKnown() ) {