Ricordisamoa has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/168282

Change subject: Provide a standard way to get the target of a redirect page
......................................................................

Provide a standard way to get the target of a redirect page

The new Scribunto_LuaTitleLibrary::redirectTarget() method is
used by mw.title objects as read-only attribute 'redirectTarget'.

If the page does not exist or it is not a redirect, the value
of the attribute is `nil`; otherwise, it is the target of the
redirect page, as mw.title object.

This is a proper alternative to parsing wikitext as it is done in:
https://en.wikipedia.org/wiki/Module:Redirect

bug: 66974
Change-Id: Id4d9b0f8c1cd09ebc42c031d4d3fc0c33eea44aa
---
M engines/LuaCommon/TitleLibrary.php
M engines/LuaCommon/lualib/mw.title.lua
M tests/engines/LuaCommon/TitleLibraryTest.php
M tests/engines/LuaCommon/TitleLibraryTests.lua
4 files changed, 50 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Scribunto 
refs/changes/82/168282/1

diff --git a/engines/LuaCommon/TitleLibrary.php 
b/engines/LuaCommon/TitleLibrary.php
index 5094a17..a9c374d 100644
--- a/engines/LuaCommon/TitleLibrary.php
+++ b/engines/LuaCommon/TitleLibrary.php
@@ -17,6 +17,7 @@
                        'fileExists' => array( $this, 'fileExists' ),
                        'protectionLevels' => array( $this, 'protectionLevels' 
),
                        'cascadingProtection' => array( $this, 
'cascadingProtection' ),
+                       'redirectTarget' => array( $this, 'redirectTarget' ),
                );
                return $this->getEngine()->registerInterface( 'mw.title.lua', 
$lib, array(
                        'thisTitle' => $this->returnTitleToLua( 
$this->getTitle() ),
@@ -305,4 +306,31 @@
                        'restrictions' => array_map( 
'Scribunto_LuaTitleLibrary::makeArrayOneBased', $restrictions )
                ) );
        }
+
+       public function redirectTarget( $text ) {
+               $this->checkType( 'redirectTarget', 1, $text, 'string' );
+               $title = Title::newFromText( $text );
+               if ( !$title ) {
+                       return array( null );
+               }
+
+               if ( is_callable( array( $this->getParser(), 
'fetchCurrentRevisionOfTitle' ) ) ) {
+                       $rev = $this->getParser()->fetchCurrentRevisionOfTitle( 
$title );
+               } else {
+                       $rev = Revision::newFromTitle( $title );
+               }
+               if ( !$rev ) {
+                       return array( null );
+               }
+               $content = $rev->getContent();
+               if ( !$content ) {
+                       return array( null );
+               }
+
+               $redirTitle = $content ? $content->getRedirectTarget() : null;
+               if ( !$redirTitle ) {
+                       return array( null );
+               }
+               return array( $this->returnTitleToLua( $redirTitle ) );
+       }
 }
diff --git a/engines/LuaCommon/lualib/mw.title.lua 
b/engines/LuaCommon/lualib/mw.title.lua
index bc1caa7..647ec4e 100644
--- a/engines/LuaCommon/lualib/mw.title.lua
+++ b/engines/LuaCommon/lualib/mw.title.lua
@@ -166,6 +166,7 @@
                fileExists = true,
                protectionLevels = true,
                cascadingProtection = true,
+               redirectTarget = true,
        }
        for k in pairs( data ) do
                readOnlyFields[k] = true
@@ -243,6 +244,12 @@
                                end
                                return data.cascadingProtection
                        end
+                       if k == 'redirectTarget' then
+                               if data.redirectTarget == nil then
+                                       data.redirectTarget = makeTitleObject( 
php.redirectTarget( data.prefixedText ) )
+                               end
+                               return data.redirectTarget
+                       end
 
                        return data[k]
                end,
diff --git a/tests/engines/LuaCommon/TitleLibraryTest.php 
b/tests/engines/LuaCommon/TitleLibraryTest.php
index 02db20b..9867422 100644
--- a/tests/engines/LuaCommon/TitleLibraryTest.php
+++ b/tests/engines/LuaCommon/TitleLibraryTest.php
@@ -49,6 +49,13 @@
                        'Summary'
                );
 
+               // Page for redirectTarget test
+               $page = WikiPage::factory( Title::newFromText( 
'ScribuntoTestRedirect' ) );
+               $page->doEditContent(
+                       new WikitextContent( '#REDIRECT 
[[ScribuntoTestTarget]]' ),
+                       'Summary'
+               );
+
                // Set restrictions for protectionLevels and 
cascadingProtection tests
                // Since mRestrictionsLoaded is true, they don't count as 
expensive
                $title = Title::newFromText( 'Main Page' );
diff --git a/tests/engines/LuaCommon/TitleLibraryTests.lua 
b/tests/engines/LuaCommon/TitleLibraryTests.lua
index 7619b9f..31d55a5 100644
--- a/tests/engines/LuaCommon/TitleLibraryTests.lua
+++ b/tests/engines/LuaCommon/TitleLibraryTests.lua
@@ -60,6 +60,10 @@
        return mw.title.new( 'ScribuntoTestPage' ):getContent()
 end
 
+local function test_redirectTarget()
+       return mw.title.new( 'ScribuntoTestRedirect' ).redirectTarget.fullText
+end
+
 -- Tests
 local tests = {
        { name = 'tostring', func = identity, type = 'ToString',
@@ -356,6 +360,10 @@
          expect = { 
'{{int:mainpage}}<includeonly>...</includeonly><noinclude>...</noinclude>' }
        },
 
+       { name = '.redirectTarget', func = test_redirectTarget,
+         expect = { 'ScribuntoTestTarget' }
+       },
+
        { name = 'expensive functions', func = test_expensive,
          expect = 'too many expensive function calls'
        },

-- 
To view, visit https://gerrit.wikimedia.org/r/168282
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id4d9b0f8c1cd09ebc42c031d4d3fc0c33eea44aa
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Scribunto
Gerrit-Branch: master
Gerrit-Owner: Ricordisamoa <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to