1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
<?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
*/
/**
* 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 TitleValue $page The link's target
* @param array $params Any additional URL parameters.
*
* @return string
*/
public function getPageUrl( TitleValue $page, $params = array() ) {
//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 TitleValue $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( TitleValue $page, $text = null ) {
if ( $text === null ) {
$text = $this->formatter->getFullText( $page );
}
// TODO: move the logic implemented by Linker here,
// using $this->formatter and $this->baseUrl, and
// re-implement Linker to use a HtmlPageLinkRenderer.
$title = Title::newFromTitleValue( $page );
$link = Linker::link( $title, htmlspecialchars( $text ) );
return $link;
}
/**
* Returns a wikitext link to the given page, using the given surface text.
*
* @param TitleValue $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( TitleValue $page, $text = null ) {
if ( $text === null ) {
$text = $this->formatter->getFullText( $page );
}
$name = $this->formatter->getFullText( $page );
return '[[:' . $name . '|' . wfEscapeWikiText( $text ) . ']]';
}
}
|