jenkins-bot has submitted this change and it was merged. ( 
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, 260 insertions(+), 0 deletions(-)

Approvals:
  Pwirth: Looks good to me, approved
  jenkins-bot: Verified



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..9efdeac
--- /dev/null
+++ b/tests/phpunit/Utility/TitleParamsResolverTest.php
@@ -0,0 +1,92 @@
+<?php
+
+namespace BlueSpice\Tests\Utility;
+
+use BlueSpice\Utility\TitleParamsResolver;
+
+/**
+ * @group BlueSpice
+ * @group BlueSpiceFoundation
+ * @group medium
+ * @group Database
+ */
+class TitleParamsResolverTest extends \MediaWikiTestCase {
+
+       protected function setUp() {
+               parent::setUp();
+               $this->tablesUsed += [ 'page', 'revision', 'text' ];
+
+               $this->insertPage( 'TitleParamsResolverTest' );
+               $this->insertPage( 'Help:TitleParamsResolverTest' );
+       }
+
+       /**
+        *
+        * @param array $params
+        * @param string $expectedPrefixedText
+        * @param \Title $defaultTitle
+        * @param string $message
+        * @dataProvider provideMediaWikiApiTitleOrPageIdData
+        */
+       public function testMediaWikiApiTitleOrPageId( $params, 
$expectedPrefixedText, $defaultTitle, $message ) {
+               $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();
+               /**
+                * HINT: For all "page_id" and "revision_id" related test we 
need to
+                * rely on MediaWiki's build in "UTPage", because we can not 
determine
+                * the "page_id"/"revision_id" of a page that we create in 
"setUp" as
+                * they depend on the CI environment. Unfortunately PHPUnit 
runs the
+                * provider methods before "setUp" so we also can not use the id
+                * returned by "$this-insertPage".
+                */
+               return [
+                       [ [ 'pageid' => 1 ], 'UTPage', null, 'Should resolve 
from "pageid"' ],
+                       [ [ 'title' => 'TitleParamsResolverTest' ], 
'TitleParamsResolverTest', null, 'Should resolve from "title"' ],
+                       [ [ 'target' => 'TitleParamsResolverTest' ], 
'TitleParamsResolverTest', null, 'Should resolve from "target"' ],
+                       [ [ 'page' => 'Help:TitleParamsResolverTest' ], 
'Help:TitleParamsResolverTest', 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"' ],
+                       [ [ 'revid' => 1 ], 'UTPage', null, 'Should resolve 
from "revid"' ],
+                       [ [ 'oldid' => 1 ], 'UTPage', null, 'Should resolve 
from "oldid"' ]
+               ];
+       }
+
+       /**
+        *
+        * @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' => '1|2' ], [ 'UTPage', 
'TitleParamsResolverTest'], 'Should resolve from "pageids"' ],
+                       [ [ 'pageids' => '2|-1' ], [ 
'TitleParamsResolverTest'], 'Should resolve from "pageids" with invalid pageid' 
],
+                       [ [ 'titles' => 'UTPage|Help:TitleParamsResolverTest' 
], [ 'UTPage', 'Help:TitleParamsResolverTest'], '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: merged
Gerrit-Change-Id: Ic9651e591c8d3875bcfb5abe853ced050ad2602c
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/BlueSpiceFoundation
Gerrit-Branch: master
Gerrit-Owner: Robert Vogel <[email protected]>
Gerrit-Reviewer: Ljonka <[email protected]>
Gerrit-Reviewer: Mglaser <[email protected]>
Gerrit-Reviewer: Pwirth <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to