Robert Vogel has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/380918 )
Change subject: Added TitleParamsResolver
......................................................................
Added TitleParamsResolver
This class allwows to create title objects from a variety of paramters, mainly
used in WebAPI context.
Change-Id: Ic9651e591c8d3875bcfb5abe853ced050ad2602c
---
A src/Utility/TitleParamsResolver.php
A tests/phpunit/Utility/TitleParamsResolverTest.php
2 files changed, 250 insertions(+), 0 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BlueSpiceFoundation
refs/changes/18/380918/1
diff --git a/src/Utility/TitleParamsResolver.php
b/src/Utility/TitleParamsResolver.php
new file mode 100644
index 0000000..6e4db15
--- /dev/null
+++ b/src/Utility/TitleParamsResolver.php
@@ -0,0 +1,168 @@
+<?php
+
+namespace BlueSpice\Utility;
+
+/**
+ * A lot of MediaWiki (Web)APIs accept a title context information (e.g.
+ * ApiParse)
+ * - "pageid=233"
+ * - "title=Some page"
+ * - "page=Some page"
+ * - "target=Some page"
+ *
+ * Some even lists of titles (e.g. ApiPurge)
+ * - "pageids=233|453|2843"
+ * - "titles=First|Second|Third"
+ *
+ * Then there are some that accept one or more revision ids (e.g. ApiReview)
+ * - "revid=3424"
+ * - "revids=34234|67556|435"
+ * - "oldid=3647"
+ * - "previd=3898"
+ * - "baserevid=7487"
+ *
+ * Last but not least there are BlueSpice TaskAPIs that provide things like
+ * - "page_id:324"
+ * - "pid:324"
+ * - "page_prefixedtitle:'Help:Some_page'"
+ * - "page_title:'Some_page',page_namespace:12"
+ */
+class TitleParamsResolver {
+
+ /**
+ *
+ * @var array
+ */
+ protected $params = [];
+
+ /**
+ *
+ * @var \Title[]
+ */
+ protected $titles = null;
+
+ /**
+ *
+ * @var \Title
+ */
+ protected $default = null;
+
+ /**
+ *
+ * @param array $params
+ * @params \Title $default
+ */
+ public function __construct( $params, $default = null ) {
+ $this->params = $params;
+ $this->default = $default;
+
+ if( $this->default === null ) {
+ //Question: Better be "\Title::newMainPage()"?
+ $this->default = \Title::makeTitle( NS_SPECIAL,
'Badtitle/dummy title' );
+ }
+ }
+
+ /**
+ *
+ * @return \Title[] all titles that could be found in the provided
params
+ */
+ public function resolve() {
+ foreach( $this->params as $paramName => $paramValue ) {
+ $this->resolvePageIds( $paramName, $paramValue );
+ $this->resolvePageNames( $paramName, $paramValue );
+ $this->resolveRevisionIds( $paramName, $paramValue );
+ }
+
+ return $this->getResultOrDefault();
+ }
+
+ /**
+ *
+ * @return \Title[]
+ */
+ protected function getResultOrDefault() {
+ if( empty( $this->titles ) ) {
+ return [ $this->default ];
+ }
+ return array_values( $this->titles );
+ }
+
+ protected function resolvePageIds( $paramName, $paramValue ) {
+ $pageIds = [];
+ if( $this->isPageIdParam( $paramName ) ) {
+ $pageIds = [ $paramValue ];
+ }
+ else if( $this->isPageIdsParam( $paramName ) ) {
+ $pageIds = explode( '|', $paramValue );
+ }
+
+ foreach( $pageIds as $pageId ) {
+ $title = \Title::newFromID( $pageId );
+ if( $title instanceof \Title ) {
+ $this->titles[$title->getPrefixedDBkey()] =
$title;
+ }
+ }
+ }
+
+ protected function isPageIdParam( $paramName ) {
+ return in_array( $paramName, [ 'pageid', 'page_id', 'pid' ] );
+ }
+
+ protected function isPageIdsParam( $paramName ) {
+ return in_array( $paramName, [ 'pageids' ] );
+ }
+
+ protected function resolvePageNames( $paramName, $paramValue ) {
+ $pageNames = [];
+ if( $this->isPageNameParam( $paramName ) ) {
+ $pageNames = [ $paramValue ];
+ }
+ else if( $this->isPageNamesParam( $paramName ) ) {
+ $pageNames = explode( '|', $paramValue );
+ }
+
+ foreach( $pageNames as $pageName ) {
+ $title = \Title::newFromText( $pageName );
+ if( $title instanceof \Title ) {
+ $this->titles[$title->getPrefixedDBkey()] =
$title;
+ }
+ }
+ }
+
+ protected function isPageNameParam( $paramName ) {
+ return in_array( $paramName, [ 'title', 'page', 'target' ] );
+ }
+
+ protected function isPageNamesParam( $paramName ) {
+ return in_array( $paramName, [ 'titles' ] );
+ }
+
+ protected function resolveRevisionIds( $paramName, $paramValue ) {
+ $revisionIds = [];
+ if( $this->isRevisionIdParam( $paramName ) ) {
+ $revisionIds = [ $paramValue ];
+ }
+ else if( $this->isRevisionIdsParam( $paramName ) ) {
+ $revisionIds = explode( '|', $paramValue );
+ }
+
+ foreach( $revisionIds as $revId ) {
+ $revision = \Revision::newFromId( $revId );
+ if( $revision instanceof \Revision ) {
+ $title = $revision->getTitle();
+ if( $title instanceof \Title ) {
+
$this->titles[$title->getPrefixedDBkey()] = $title;
+ }
+ }
+ }
+ }
+
+ protected function isRevisionIdParam( $paramName ) {
+ return in_array( $paramName, [ 'revid', 'oldid', 'previd',
'baserevid' ] );
+ }
+
+ protected function isRevisionIdsParam( $paramName ) {
+ return in_array( $paramName, [ 'revids' ] );
+ }
+
+}
diff --git a/tests/phpunit/Utility/TitleParamsResolverTest.php
b/tests/phpunit/Utility/TitleParamsResolverTest.php
new file mode 100644
index 0000000..29fc162
--- /dev/null
+++ b/tests/phpunit/Utility/TitleParamsResolverTest.php
@@ -0,0 +1,82 @@
+<?php
+
+namespace BlueSpice\Tests\Utility;
+
+use BlueSpice\Utility\TitleParamsResolver;
+
+class TitleParamsResolverTest extends \MediaWikiTestCase {
+
+ protected function setUp() {
+ parent::setUp();
+
+ //"UTPage" hat ID:1, REVID:1
+ $this->insertPage( 'TitleParamsResolverTest_ID02' );//ID:2,
REVID:2
+ $this->insertPage( 'TitleParamsResolverTest_ID03' );//ID:3,
REVID:3
+ $this->insertPage( 'Help:TitleParamsResolverTest_ID04'
);//ID:4, REVID:4, NSID:12
+ }
+
+ /**
+ *
+ * @param array $params
+ * @param string $expectedPrefixedText
+ * @param \Title $defaultTitle
+ * @param string $message
+ * @dataProvider provideMediaWikiApiTitleOrPageIdData
+ */
+ public function testMediaWikiApiTitleOrPageId( $params,
$expectedPrefixedText, $defaultTitle, $message ) {
+ /*$dbr = wfGetDB( DB_REPLICA );
+ $res = $dbr->select( 'page', '*' );
+ foreach( $res as $row ) {
+ var_dump( $row );
+ }*/
+ $resolver = new TitleParamsResolver( $params, $defaultTitle );
+ $resolvedTitles = $resolver->resolve();
+ $resolvedTitle = $resolvedTitles[0];
+
+ $this->assertTrue( $resolvedTitle instanceof \Title );
+ $this->assertEquals( $expectedPrefixedText,
$resolvedTitle->getPrefixedText(), $message );
+ }
+
+ public function provideMediaWikiApiTitleOrPageIdData() {
+ $mainPage = \Title::newMainPage();
+ $mainPageTitleText = $mainPage->getPrefixedText();
+ return [
+ [ [ 'pageid' => 2 ], 'TitleParamsResolverTest ID02',
null, 'Should resolve from "pageid"' ],
+ [ [ 'title' => 'TitleParamsResolverTest_ID02' ],
'TitleParamsResolverTest ID02', null, 'Should resolve from "title"' ],
+ [ [ 'target' => 'TitleParamsResolverTest_ID03' ],
'TitleParamsResolverTest ID03', null, 'Should resolve from "target"' ],
+ [ [ 'page' => 'TitleParamsResolverTest_ID04' ],
'TitleParamsResolverTest ID04', null, 'Should resolve from "page"' ],
+ [ [ 'pageid' => -1 ], $mainPageTitleText, $mainPage,
'With invalid "page_id", should fallback to "default"' ],
+ [ [ 'title' => 'Invalid[Title]' ], $mainPageTitleText,
$mainPage, 'With invalid "title", should fallback to "default"' ],
+ [ [ 'target' => 'Invalid[Title]' ], $mainPageTitleText,
$mainPage, 'With invalid "target", should fallback to "default"' ],
+ [ [ 'page' => 'Invalid[Title]' ], $mainPageTitleText,
$mainPage, 'With invalid "page", should fallback to "default"' ]
+ ];
+ }
+
+ /**
+ *
+ * @param array $params
+ * @param string[] $expectedPrefixedTexts
+ * @param string $message
+ * @dataProvider provideMediaWikiApiTitlesOrPageIdsData
+ */
+ public function testMediaWikiApiTitlesOrPageIds( $params,
$expectedPrefixedTexts, $message ) {
+ $resolver = new TitleParamsResolver( $params );
+ $resolvedTitles = $resolver->resolve();
+
+ for( $i = 0; $i < count( $resolvedTitles ); $i++ ) {
+ $resolvedTitle = $resolvedTitles[$i];
+ $expectedPrefixedText = $expectedPrefixedTexts[$i];
+
+ $this->assertTrue( $resolvedTitle instanceof \Title );
+ $this->assertEquals( $expectedPrefixedText,
$resolvedTitle->getPrefixedText(), $message );
+ }
+ }
+
+ public function provideMediaWikiApiTitlesOrPageIdsData() {
+ return [
+ [ [ 'pageids' => '2|3' ], [ 'TitleParamsResolverTest
ID02', 'TitleParamsResolverTest ID03'], 'Should resolve from "pageids"' ],
+ [ [ 'pageids' => '2|-1' ], [ 'TitleParamsResolverTest
ID02'], 'Should resolve from "pageids" with invalid pageid' ],
+ [ [ 'titles' => 'TitleParamsResolverTest
ID02|TitleParamsResolverTest ID03' ], [ 'TitleParamsResolverTest ID02',
'TitleParamsResolverTest ID03'], 'Should resolve from "titles"' ],
+ ];
+ }
+}
\ No newline at end of file
--
To view, visit https://gerrit.wikimedia.org/r/380918
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic9651e591c8d3875bcfb5abe853ced050ad2602c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/BlueSpiceFoundation
Gerrit-Branch: master
Gerrit-Owner: Robert Vogel <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits