Skip to content

Commit ba47cbc

Browse files
add class aliases, fix categories limit-offset (#13)
* add class aliases, fix categories limit-offset * fix phpcs
1 parent e11d426 commit ba47cbc

File tree

9 files changed

+100
-26
lines changed

9 files changed

+100
-26
lines changed

.phpcs.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
<ruleset>
33
<rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki">
44
<exclude name="MediaWiki.Usage.ExtendClassUsage.FunctionVarUsage" />
5+
<exclude name="Generic.Files.OneObjectStructurePerFile.MultipleFound" />
56
</rule>
67
<file>.</file>
78
<exclude-pattern>/(vendor|conf)/</exclude-pattern>
89
<exclude-pattern type="relative">extensions/*</exclude-pattern>
910
<arg name="extensions" value="php"/>
1011
<arg name="encoding" value="UTF-8"/>
11-
</ruleset>
12+
</ruleset>

extension.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
"ExtensionMessagesFiles": {
1818
"KnowledgeGraphMagic": "KnowledgeGraph.i18n.magic.php"
1919
},
20+
"AutoloadNamespaces": {
21+
"MediaWiki\\Extension\\KnowledgeGraph\\Aliases\\": "includes/aliases/"
22+
},
2023
"AutoloadClasses": {
2124
"KnowledgeGraph":"includes/KnowledgeGraph.php",
2225
"SpecialKnowledgeGraphDesigner":"includes/specials/SpecialKnowledgeGraphDesigner.php",

includes/KnowledgeGraph.php

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @author thomas-topway-it for KM-A
88
*/
99

10+
// use MediaWiki\Extension\KnowledgeGraph\Aliases\Category as CategoryClass;
11+
use MediaWiki\Extension\KnowledgeGraph\Aliases\Title as TitleClass;
1012
use MediaWiki\MediaWikiServices;
1113
use MediaWiki\Revision\SlotRecord;
1214
use SMW\MediaWiki\Specials\SearchByProperty\PageRequestOptions;
@@ -142,7 +144,7 @@ public static function onParserFirstCallInit( Parser $parser ) {
142144
public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater = null ) {
143145
$text = file_get_contents( __DIR__ . '/../data/KnowledgeGraphOptions.js' );
144146
$user = RequestContext::getMain()->getUser();
145-
$title = Title::makeTitleSafe( NS_MEDIAWIKI, 'KnowledgeGraphOptions' );
147+
$title = TitleClass::makeTitleSafe( NS_MEDIAWIKI, 'KnowledgeGraphOptions' );
146148

147149
$wikiPage = self::getWikiPage( $title );
148150
$pageUpdater = $wikiPage->newPageUpdater( $user );
@@ -226,15 +228,15 @@ public static function parserFunctionKnowledgeGraph( Parser $parser, ...$argv )
226228
// property-related options
227229
foreach ( $values as $val ) {
228230
if ( preg_match( '/^property-options(\?(.+))?=(.+)/', $val, $match ) ) {
229-
$title_ = Title::makeTitleSafe( \SMW_NS_PROPERTY, $match[2] );
231+
$title_ = TitleClass::makeTitleSafe( \SMW_NS_PROPERTY, $match[2] );
230232
if ( $title_ ) {
231233
$propertyOptions[$title_->getText()] = $match[3];
232234
}
233235
}
234236
}
235237

236238
foreach ( $params['nodes'] as $titleText ) {
237-
$title_ = Title::newFromText( $titleText );
239+
$title_ = TitleClass::newFromText( $titleText );
238240
if ( $title_ && $title_->isKnown() ) {
239241
if ( !isset( self::$data[$title_->getFullText()] ) ) {
240242
self::setSemanticData( $title_, $params['properties'], 0, $params['depth'] );
@@ -245,7 +247,7 @@ public static function parserFunctionKnowledgeGraph( Parser $parser, ...$argv )
245247
$graphOptions = [];
246248
if ( !empty( $params['graph-options'] ) ) {
247249
// , NS_KNOWLEDGEGRAPH
248-
$title_ = Title::newFromText( $params['graph-options'], NS_MEDIAWIKI );
250+
$title_ = TitleClass::newFromText( $params['graph-options'], NS_MEDIAWIKI );
249251

250252
if ( $title_ && $title_->isKnown() ) {
251253
// $graphOptions = json_decode( self::getWikipageContent( $title_ ), true );
@@ -254,7 +256,7 @@ public static function parserFunctionKnowledgeGraph( Parser $parser, ...$argv )
254256
}
255257

256258
foreach ( $propertyOptions as $property => $titleText ) {
257-
$title_ = Title::newFromText( $titleText, NS_MEDIAWIKI );
259+
$title_ = TitleClass::newFromText( $titleText, NS_MEDIAWIKI );
258260
if ( $title_ && $title_->isKnown() ) {
259261
// $propertyOptions[$property] = json_decode( self::getWikipageContent( $title_ ), true );
260262
$propertyOptions[$property] = self::getWikipageContent( $title_ );
@@ -320,7 +322,7 @@ public static function getSubjectsByProperty( $propertyText, $limit = 100, $offs
320322
}
321323

322324
/**
323-
* @param Title $title
325+
* @param Title|MediaWiki\Title\Title $title $title
324326
* @return string|null
325327
*/
326328
public static function getWikipageContent( $title ) {
@@ -337,7 +339,7 @@ public static function getWikipageContent( $title ) {
337339
}
338340

339341
/**
340-
* @param Title $title
342+
* @param Title|MediaWiki\Title\Title $title
341343
* @return WikiPage|null
342344
*/
343345
public static function getWikiPage( $title ) {
@@ -443,30 +445,46 @@ public static function parseParameters( $parameters, $defaultParameters ) {
443445
}
444446

445447
/**
446-
*
447448
* @param string $category
449+
* @param int $limit
450+
* @param int $offset
448451
* @return array
449452
*/
450-
public static function articlesInCategories( $category ) {
451-
$dbr = wfGetDB( DB_REPLICA );
452-
$res = $dbr->select( 'categorylinks',
453+
public static function articlesInCategories( $category, $limit, $offset ) {
454+
$options = [
455+
'LIMIT' => $limit,
456+
'OFFSET' => $offset
457+
];
458+
$dbr = wfGetDB( DB_REPLICA );
459+
$res = $dbr->select( 'categorylinks',
453460
[ 'pageid' => 'cl_from' ],
454461
[ 'cl_to' => str_replace( ' ', '_', $category ) ],
455-
__METHOD__
456-
);
457-
$ret = [];
462+
__METHOD__,
463+
$options
464+
);
465+
$ret = [];
458466
foreach ( $res as $row ) {
459-
$title_ = Title::newFromID( $row->pageid );
467+
$title_ = TitleClass::newFromID( $row->pageid );
460468
if ( $title_ ) {
461469
$ret[] = $title_;
462470
}
463471
}
464472
return $ret;
473+
474+
// *** this does not work with numerical offset
475+
// $cat = CategoryClass::newFromName( str_replace( ' ', '_', $category ) );
476+
// $iterator_ = $cat->getMembers( $limit, $offset );
477+
// $ret = [];
478+
// while ( $iterator_->valid() ) {
479+
// $ret[] = $iterator_->current();
480+
// $iterator_->next();
481+
// }
482+
// return $ret;
465483
}
466484

467485
/**
468486
* @see https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/PageProperties/+/refs/heads/1.0.3/includes/PageProperties.php
469-
* @param Title $title
487+
* @param Title|MediaWiki\Title\Title $title
470488
* @param array $onlyProperties
471489
* @param int $depth
472490
* @param int $maxDepth
@@ -532,7 +550,8 @@ public static function setSemanticData( Title $title, $onlyProperties, $depth, $
532550

533551
if ( count( $onlyProperties )
534552
&& !in_array( $canonicalLabel, $onlyProperties )
535-
&& !in_array( $preferredLabel, $onlyProperties ) ) {
553+
&& !in_array( $preferredLabel, $onlyProperties )
554+
) {
536555
continue;
537556
}
538557

includes/aliases/Category.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* KnowledgeGraph
5+
*
6+
* @license GPL-2.0-or-later
7+
* @author thomas-topway-it for KM-A
8+
*/
9+
10+
namespace MediaWiki\Extension\KnowledgeGraph\Aliases;
11+
12+
if ( class_exists( 'Category' ) ) {
13+
class Category extends \Category {
14+
}
15+
} else {
16+
// phpcs:ignore Generic.Classes.DuplicateClassName.Found
17+
class Category extends \Mediawiki\Title\Category {
18+
}
19+
}

includes/aliases/Title.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* KnowledgeGraph
5+
*
6+
* @license GPL-2.0-or-later
7+
* @author thomas-topway-it for KM-A
8+
*/
9+
10+
namespace MediaWiki\Extension\KnowledgeGraph\Aliases;
11+
12+
if ( class_exists( 'Title' ) ) {
13+
class Title extends \Title {
14+
}
15+
} else {
16+
// phpcs:ignore Generic.Classes.DuplicateClassName.Found
17+
class Title extends \Mediawiki\Title\Title {
18+
}
19+
}

includes/api/KnowledgeGraphApiLoadCategories.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @author thomas-topway-it for KM-A
88
*/
99

10+
use MediaWiki\Extension\KnowledgeGraph\Aliases\Title as TitleClass;
11+
1012
class KnowledgeGraphApiLoadCategories extends ApiBase {
1113

1214
/**
@@ -38,10 +40,14 @@ public function execute() {
3840

3941
$titles = [];
4042
foreach ( $categories as $categoryText ) {
41-
$category_ = Title::makeTitleSafe( NS_CATEGORY, $categoryText );
43+
$category_ = TitleClass::makeTitleSafe( NS_CATEGORY, $categoryText );
4244
// && $category_->isKnown()
4345
if ( $category_ ) {
44-
$titles_ = \KnowledgeGraph::articlesInCategories( $categoryText );
46+
$titles_ = \KnowledgeGraph::articlesInCategories(
47+
$categoryText,
48+
$params['limit'],
49+
$params['offset']
50+
);
4551

4652
foreach ( $titles_ as $title_ ) {
4753
$titles[$title_->getFullText()] = $title_;

includes/api/KnowledgeGraphApiLoadNodes.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @author thomas-topway-it for KM-A
88
*/
99

10+
use MediaWiki\Extension\KnowledgeGraph\Aliases\Title as TitleClass;
11+
1012
class KnowledgeGraphApiLoadNodes extends ApiBase {
1113

1214
/**
@@ -39,7 +41,7 @@ public function execute() {
3941

4042
$titles = explode( '|', $params['titles'] );
4143
foreach ( $titles as $titleText ) {
42-
$title_ = Title::newFromText( $titleText );
44+
$title_ = TitleClass::newFromText( $titleText );
4345
if ( $title_ && $title_->isKnown() ) {
4446
if ( !isset( self::$data[$title_->getFullText()] ) ) {
4547
\KnowledgeGraph::setSemanticData( $title_, $params['properties'], 0, $params['depth'] );

includes/api/KnowledgeGraphApiLoadProperties.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @author thomas-topway-it for KM-A
88
*/
99

10+
use MediaWiki\Extension\KnowledgeGraph\Aliases\Title as TitleClass;
11+
1012
class KnowledgeGraphApiLoadProperties extends ApiBase {
1113

1214
/**
@@ -37,12 +39,13 @@ public function execute() {
3739
$titles_ = explode( '|', $params['properties'] );
3840
$titles = [];
3941
foreach ( $titles_ as $titleText ) {
40-
$title_ = Title::makeTitleSafe( SMW_NS_PROPERTY, $titleText );
42+
$title_ = TitleClass::makeTitleSafe( SMW_NS_PROPERTY, $titleText );
4143
if ( $title_ && $title_->isKnown() ) {
4244
$subjects = \KnowledgeGraph::getSubjectsByProperty(
43-
$title_->getText(),
44-
$params['limit'],
45-
$params['offset'] );
45+
$title_->getText(),
46+
$params['limit'],
47+
$params['offset']
48+
);
4649
foreach ( $subjects as $title__ ) {
4750
$titles[$title__->getFullText()] = $title__;
4851
}

includes/specials/SpecialKnowledgeGraphDesigner.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use MediaWiki\Extension\KnowledgeGraph\Aliases\Title as TitleClass;
4+
35
/**
46
* @ingroup SpecialPage
57
*/
@@ -43,7 +45,7 @@ public function execute( $par ) {
4345
$params = \KnowledgeGraph::applyDefaultParams( $defaultParameters, $params );
4446

4547
\KnowledgeGraph::initSMW();
46-
$title_ = Title::makeTitleSafe( NS_MEDIAWIKI, 'KnowledgeGraphOptions' );
48+
$title_ = TitleClass::makeTitleSafe( NS_MEDIAWIKI, 'KnowledgeGraphOptions' );
4749

4850
$graphOptions = [];
4951
if ( $title_ && $title_->isKnown() ) {

0 commit comments

Comments
 (0)