3 * A service for generating links from page titles
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 * @author Daniel Kinzler
26 * A service for generating links from page titles.
28 * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
31 class MediaWikiPageLinkRenderer
implements PageLinkRenderer
{
43 * @note $formatter and $baseUrl are currently not used for generating links,
44 * since we still rely on the Linker class to generate the actual HTML.
45 * Once this is reversed so that Linker becomes a legacy interface to
46 * HtmlPageLinkRenderer, we will be using them, so it seems prudent to
47 * already declare the dependency and inject them.
49 * @param TitleFormatter $formatter Formatter for generating the target title string
50 * @param string $baseUrl (currently unused, pending refactoring of Linker).
51 * Defaults to $wgArticlePath.
53 public function __construct( TitleFormatter
$formatter, $baseUrl = null ) {
54 if ( $baseUrl === null ) {
55 $baseUrl = $GLOBALS['wgArticlePath'];
58 $this->formatter
= $formatter;
59 $this->baseUrl
= $baseUrl;
63 * Returns the (partial) URL for the given page (including any section identifier).
65 * @param TitleValue $page The link's target
66 * @param array $params Any additional URL parameters.
70 public function getPageUrl( TitleValue
$page, $params = array() ) {
71 // TODO: move the code from Linker::linkUrl here!
72 // The below is just a rough estimation!
74 $name = $this->formatter
->getPrefixedText( $page );
75 $name = str_replace( ' ', '_', $name );
76 $name = wfUrlencode( $name );
78 $url = $this->baseUrl
. $name;
81 $separator = ( strpos( $url, '?' ) ) ?
'&' : '?';
82 $url .= $separator . wfArrayToCgi( $params );
85 $fragment = $page->getFragment();
86 if ( $fragment !== '' ) {
87 $url = $url . '#' . wfUrlencode( $fragment );
94 * Returns an HTML link to the given page, using the given surface text.
96 * @param TitleValue $page The link's target
97 * @param string $text The link's surface text (will be derived from $page if not given).
101 public function renderHtmlLink( TitleValue
$page, $text = null ) {
102 if ( $text === null ) {
103 $text = $this->formatter
->getFullText( $page );
106 // TODO: move the logic implemented by Linker here,
107 // using $this->formatter and $this->baseUrl, and
108 // re-implement Linker to use a HtmlPageLinkRenderer.
109 $title = Title
::newFromTitleValue( $page );
110 $link = Linker
::link( $title, htmlspecialchars( $text ) );
116 * Returns a wikitext link to the given page, using the given surface text.
118 * @param TitleValue $page The link's target
119 * @param string $text The link's surface text (will be derived from $page if not given).
123 public function renderWikitextLink( TitleValue
$page, $text = null ) {
124 if ( $text === null ) {
125 $text = $this->formatter
->getFullText( $page );
128 $name = $this->formatter
->getFullText( $page );
130 return '[[:' . $name . '|' . wfEscapeWikiText( $text ) . ']]';