'MediaTransformOutput' => __DIR__ . '/includes/media/MediaTransformOutput.php',
'MediaWiki' => __DIR__ . '/includes/MediaWiki.php',
'MediaWikiI18N' => __DIR__ . '/includes/skins/MediaWikiI18N.php',
- 'MediaWikiPageLinkRenderer' => __DIR__ . '/includes/title/MediaWikiPageLinkRenderer.php',
'MediaWikiSite' => __DIR__ . '/includes/site/MediaWikiSite.php',
'MediaWikiTitleCodec' => __DIR__ . '/includes/title/MediaWikiTitleCodec.php',
'MediaWikiVersionFetcher' => __DIR__ . '/includes/MediaWikiVersionFetcher.php',
'PageArchive' => __DIR__ . '/includes/specials/SpecialUndelete.php',
'PageExists' => __DIR__ . '/maintenance/pageExists.php',
'PageLangLogFormatter' => __DIR__ . '/includes/logging/PageLangLogFormatter.php',
- 'PageLinkRenderer' => __DIR__ . '/includes/title/PageLinkRenderer.php',
'PageProps' => __DIR__ . '/includes/PageProps.php',
'PageQueryPage' => __DIR__ . '/includes/specialpage/PageQueryPage.php',
'Pager' => __DIR__ . '/includes/pager/Pager.php',
+++ /dev/null
-<?php
-/**
- * A service for generating links from page titles
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- * @license GPL 2+
- * @author Daniel Kinzler
- */
-use MediaWiki\Linker\LinkTarget;
-
-/**
- * A service for generating links from page titles.
- *
- * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
- * @since 1.23
- */
-class MediaWikiPageLinkRenderer implements PageLinkRenderer {
- /**
- * @var TitleFormatter
- */
- protected $formatter;
-
- /**
- * @var string
- */
- protected $baseUrl;
-
- /**
- * @note $formatter and $baseUrl are currently not used for generating links,
- * since we still rely on the Linker class to generate the actual HTML.
- * Once this is reversed so that Linker becomes a legacy interface to
- * HtmlPageLinkRenderer, we will be using them, so it seems prudent to
- * already declare the dependency and inject them.
- *
- * @param TitleFormatter $formatter Formatter for generating the target title string
- * @param string $baseUrl (currently unused, pending refactoring of Linker).
- * Defaults to $wgArticlePath.
- */
- public function __construct( TitleFormatter $formatter, $baseUrl = null ) {
- if ( $baseUrl === null ) {
- $baseUrl = $GLOBALS['wgArticlePath'];
- }
-
- $this->formatter = $formatter;
- $this->baseUrl = $baseUrl;
- }
-
- /**
- * Returns the (partial) URL for the given page (including any section identifier).
- *
- * @param LinkTarget $page The link's target
- * @param array $params Any additional URL parameters.
- *
- * @return string
- */
- public function getPageUrl( LinkTarget $page, $params = [] ) {
- // TODO: move the code from Linker::linkUrl here!
- // The below is just a rough estimation!
-
- $name = $this->formatter->getPrefixedText( $page );
- $name = str_replace( ' ', '_', $name );
- $name = wfUrlencode( $name );
-
- $url = $this->baseUrl . $name;
-
- if ( $params ) {
- $separator = ( strpos( $url, '?' ) ) ? '&' : '?';
- $url .= $separator . wfArrayToCgi( $params );
- }
-
- $fragment = $page->getFragment();
- if ( $fragment !== '' ) {
- $url = $url . '#' . wfUrlencode( $fragment );
- }
-
- return $url;
- }
-
- /**
- * Returns an HTML link to the given page, using the given surface text.
- *
- * @param LinkTarget $linkTarget The link's target
- * @param string $text The link's surface text (will be derived from $page if not given).
- *
- * @return string
- */
- public function renderHtmlLink( LinkTarget $linkTarget, $text = null ) {
- if ( $text === null ) {
- $text = $this->formatter->getFullText( $linkTarget );
- }
-
- // TODO: move the logic implemented by Linker here,
- // using $this->formatter and $this->baseUrl, and
- // re-implement Linker to use a HtmlPageLinkRenderer.
-
- $title = Title::newFromLinkTarget( $linkTarget );
- $link = Linker::link( $title, htmlspecialchars( $text ) );
-
- return $link;
- }
-
- /**
- * Returns a wikitext link to the given page, using the given surface text.
- *
- * @param LinkTarget $page The link's target
- * @param string $text The link's surface text (will be derived from $page if not given).
- *
- * @return string
- */
- public function renderWikitextLink( LinkTarget $page, $text = null ) {
- if ( $text === null ) {
- $text = $this->formatter->getFullText( $page );
- }
-
- $name = $this->formatter->getFullText( $page );
-
- return '[[:' . $name . '|' . wfEscapeWikiText( $text ) . ']]';
- }
-}
+++ /dev/null
-<?php
-/**
- * Represents a link rendering service for %MediaWiki.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- * @license GPL 2+
- * @author Daniel Kinzler
- */
-use MediaWiki\Linker\LinkTarget;
-
-/**
- * Represents a link rendering service for %MediaWiki.
- *
- * This is designed to encapsulate the knowledge about how page titles map to
- * URLs, and how links are encoded in a given output format.
- *
- * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
- * @since 1.23
- */
-interface PageLinkRenderer {
- /**
- * Returns the URL for the given page.
- *
- * @todo expand this to cover the functionality of Linker::linkUrl
- *
- * @param LinkTarget $page The link's target
- * @param array $params Any additional URL parameters.
- *
- * @return string
- */
- public function getPageUrl( LinkTarget $page, $params = [] );
-
- /**
- * Returns an HTML link to the given page, using the given surface text.
- *
- * @todo expand this to cover the functionality of Linker::link
- *
- * @param LinkTarget $page The link's target
- * @param string $text The link's surface text (will be derived from $page if not given).
- *
- * @return string
- */
- public function renderHtmlLink( LinkTarget $page, $text = null );
-
- /**
- * Returns a wikitext link to the given page, using the given surface text.
- *
- * @param LinkTarget $page The link's target
- * @param string $text The link's surface text (will be derived from $page if not given).
- *
- * @return string
- */
- public function renderWikitextLink( LinkTarget $page, $text = null );
-}
+++ /dev/null
-<?php
-/**
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- * @author Daniel Kinzler
- */
-
-/**
- * @covers MediaWikiPageLinkRenderer
- *
- * @group Title
- * @group Database
- */
-class MediaWikiPageLinkRendererTest extends MediaWikiTestCase {
-
- protected function setUp() {
- parent::setUp();
-
- $this->setMwGlobals( [
- 'wgContLang' => Language::factory( 'en' ),
- ] );
- }
-
- /**
- * Returns a mock GenderCache that will return "female" always.
- *
- * @return GenderCache
- */
- private function getGenderCache() {
- $genderCache = $this->getMockBuilder( 'GenderCache' )
- ->disableOriginalConstructor()
- ->getMock();
-
- $genderCache->expects( $this->any() )
- ->method( 'getGenderOf' )
- ->will( $this->returnValue( 'female' ) );
-
- return $genderCache;
- }
-
- public static function provideGetPageUrl() {
- return [
- [
- new TitleValue( NS_MAIN, 'Foo_Bar' ),
- [],
- '/Foo_Bar'
- ],
- [
- new TitleValue( NS_USER, 'Hansi_Maier', 'stuff' ),
- [ 'foo' => 'bar' ],
- '/User:Hansi_Maier?foo=bar#stuff'
- ],
- ];
- }
-
- /**
- * @dataProvider provideGetPageUrl
- */
- public function testGetPageUrl( TitleValue $title, $params, $url ) {
- // NOTE: was of Feb 2014, MediaWikiPageLinkRenderer *ignores* the
- // WikitextTitleFormatter we pass here, and relies on the Linker
- // class for generating the link! This may break the test e.g.
- // of Linker uses a different language for the namespace names.
-
- $lang = Language::factory( 'en' );
-
- $formatter = new MediaWikiTitleCodec( $lang, $this->getGenderCache() );
- $renderer = new MediaWikiPageLinkRenderer( $formatter, '/' );
- $actual = $renderer->getPageUrl( $title, $params );
-
- $this->assertEquals( $url, $actual );
- }
-
- public static function provideRenderHtmlLink() {
- return [
- [
- new TitleValue( NS_MAIN, 'Foo_Bar' ),
- 'Foo Bar',
- '!<a .*href=".*?Foo_Bar.*?".*?>Foo Bar</a>!'
- ],
- [
- // NOTE: Linker doesn't include fragments in "broken" links
- // NOTE: once this no longer uses Linker, we will get "2" instead of "User" for the namespace.
- new TitleValue( NS_USER, 'Hansi_Maier', 'stuff' ),
- 'Hansi Maier\'s Stuff',
- '!<a .*href=".*?User:Hansi_Maier.*?>Hansi Maier\'s Stuff</a>!'
- ],
- [
- // NOTE: Linker doesn't include fragments in "broken" links
- // NOTE: once this no longer uses Linker, we will get "2" instead of "User" for the namespace.
- new TitleValue( NS_USER, 'Hansi_Maier', 'stuff' ),
- null,
- '!<a .*href=".*?User:Hansi_Maier.*?>User:Hansi Maier#stuff</a>!'
- ],
- ];
- }
-
- /**
- * @dataProvider provideRenderHtmlLink
- */
- public function testRenderHtmlLink( TitleValue $title, $text, $pattern ) {
- // NOTE: was of Feb 2014, MediaWikiPageLinkRenderer *ignores* the
- // WikitextTitleFormatter we pass here, and relies on the Linker
- // class for generating the link! This may break the test e.g.
- // of Linker uses a different language for the namespace names.
-
- $lang = Language::factory( 'en' );
-
- $formatter = new MediaWikiTitleCodec( $lang, $this->getGenderCache() );
- $renderer = new MediaWikiPageLinkRenderer( $formatter );
- $actual = $renderer->renderHtmlLink( $title, $text );
-
- $this->assertRegExp( $pattern, $actual );
- }
-
- public static function provideRenderWikitextLink() {
- return [
- [
- new TitleValue( NS_MAIN, 'Foo_Bar' ),
- 'Foo Bar',
- '[[:0:Foo Bar|Foo Bar]]'
- ],
- [
- new TitleValue( NS_USER, 'Hansi_Maier', 'stuff' ),
- 'Hansi Maier\'s Stuff',
- '[[:2:Hansi Maier#stuff|Hansi Maier's Stuff]]'
- ],
- [
- new TitleValue( NS_USER, 'Hansi_Maier', 'stuff' ),
- null,
- '[[:2:Hansi Maier#stuff|2:Hansi Maier#stuff]]'
- ],
- ];
- }
-
- /**
- * @dataProvider provideRenderWikitextLink
- */
- public function testRenderWikitextLink( TitleValue $title, $text, $expected ) {
- $formatter = $this->getMock( 'TitleFormatter' );
- $formatter->expects( $this->any() )
- ->method( 'getFullText' )
- ->will( $this->returnCallback(
- function ( TitleValue $title ) {
- return str_replace( '_', ' ', "$title" );
- }
- ) );
-
- $renderer = new MediaWikiPageLinkRenderer( $formatter, '/' );
- $actual = $renderer->renderWikitextLink( $title, $text );
-
- $this->assertEquals( $expected, $actual );
- }
-}