Merge "Revert "Convert Special:WhatLinksHere from XML form to OOUI form""
[lhc/web/wiklou.git] / includes / title / MediaWikiPageLinkRenderer.php
1 <?php
2 /**
3 * A service for generating links from page titles
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @license GPL 2+
22 * @author Daniel Kinzler
23 */
24 use MediaWiki\Linker\LinkTarget;
25
26 /**
27 * A service for generating links from page titles.
28 *
29 * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
30 * @since 1.23
31 */
32 class MediaWikiPageLinkRenderer implements PageLinkRenderer {
33 /**
34 * @var TitleFormatter
35 */
36 protected $formatter;
37
38 /**
39 * @var string
40 */
41 protected $baseUrl;
42
43 /**
44 * @note $formatter and $baseUrl are currently not used for generating links,
45 * since we still rely on the Linker class to generate the actual HTML.
46 * Once this is reversed so that Linker becomes a legacy interface to
47 * HtmlPageLinkRenderer, we will be using them, so it seems prudent to
48 * already declare the dependency and inject them.
49 *
50 * @param TitleFormatter $formatter Formatter for generating the target title string
51 * @param string $baseUrl (currently unused, pending refactoring of Linker).
52 * Defaults to $wgArticlePath.
53 */
54 public function __construct( TitleFormatter $formatter, $baseUrl = null ) {
55 if ( $baseUrl === null ) {
56 $baseUrl = $GLOBALS['wgArticlePath'];
57 }
58
59 $this->formatter = $formatter;
60 $this->baseUrl = $baseUrl;
61 }
62
63 /**
64 * Returns the (partial) URL for the given page (including any section identifier).
65 *
66 * @param LinkTarget $page The link's target
67 * @param array $params Any additional URL parameters.
68 *
69 * @return string
70 */
71 public function getPageUrl( LinkTarget $page, $params = [] ) {
72 // TODO: move the code from Linker::linkUrl here!
73 // The below is just a rough estimation!
74
75 $name = $this->formatter->getPrefixedText( $page );
76 $name = str_replace( ' ', '_', $name );
77 $name = wfUrlencode( $name );
78
79 $url = $this->baseUrl . $name;
80
81 if ( $params ) {
82 $separator = ( strpos( $url, '?' ) ) ? '&' : '?';
83 $url .= $separator . wfArrayToCgi( $params );
84 }
85
86 $fragment = $page->getFragment();
87 if ( $fragment !== '' ) {
88 $url = $url . '#' . wfUrlencode( $fragment );
89 }
90
91 return $url;
92 }
93
94 /**
95 * Returns an HTML link to the given page, using the given surface text.
96 *
97 * @param LinkTarget $linkTarget The link's target
98 * @param string $text The link's surface text (will be derived from $page if not given).
99 *
100 * @return string
101 */
102 public function renderHtmlLink( LinkTarget $linkTarget, $text = null ) {
103 if ( $text === null ) {
104 $text = $this->formatter->getFullText( $linkTarget );
105 }
106
107 // TODO: move the logic implemented by Linker here,
108 // using $this->formatter and $this->baseUrl, and
109 // re-implement Linker to use a HtmlPageLinkRenderer.
110
111 $title = Title::newFromLinkTarget( $linkTarget );
112 $link = Linker::link( $title, htmlspecialchars( $text ) );
113
114 return $link;
115 }
116
117 /**
118 * Returns a wikitext link to the given page, using the given surface text.
119 *
120 * @param LinkTarget $page The link's target
121 * @param string $text The link's surface text (will be derived from $page if not given).
122 *
123 * @return string
124 */
125 public function renderWikitextLink( LinkTarget $page, $text = null ) {
126 if ( $text === null ) {
127 $text = $this->formatter->getFullText( $page );
128 }
129
130 $name = $this->formatter->getFullText( $page );
131
132 return '[[:' . $name . '|' . wfEscapeWikiText( $text ) . ']]';
133 }
134 }