3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
20 * @author Kunal Mehta <legoktm@member.fsf.org>
22 namespace MediaWiki\Linker
;
29 use MediaWiki\MediaWikiServices
;
35 * Class that generates HTML <a> links for pages.
42 * Whether to force the pretty article path
46 private $forceArticlePath = false;
49 * A PROTO_* constant or false
51 * @var string|bool|int
53 private $expandUrls = false;
56 * Whether extra classes should be added
60 private $noClasses = false;
65 private $stubThreshold = 0;
70 private $titleFormatter;
73 * Whether to run the legacy Linker hooks
77 private $runLegacyBeginHook = true;
80 * @param TitleFormatter $titleFormatter
82 public function __construct( TitleFormatter
$titleFormatter ) {
83 $this->titleFormatter
= $titleFormatter;
89 public function setForceArticlePath( $force ) {
90 $this->forceArticlePath
= $force;
96 public function getForceArticlePath() {
97 return $this->forceArticlePath
;
101 * @param string|bool|int $expand A PROTO_* constant or false
103 public function setExpandURLs( $expand ) {
104 $this->expandUrls
= $expand;
108 * @return string|bool|int a PROTO_* constant or false
110 public function getExpandURLs() {
111 return $this->expandUrls
;
117 public function setNoClasses( $no ) {
118 $this->noClasses
= $no;
124 public function getNoClasses() {
125 return $this->noClasses
;
129 * @param int $threshold
131 public function setStubThreshold( $threshold ) {
132 $this->stubThreshold
= $threshold;
138 public function getStubThreshold() {
139 return $this->stubThreshold
;
145 public function setRunLegacyBeginHook( $run ) {
146 $this->runLegacyBeginHook
= $run;
150 * @param LinkTarget $target
151 * @param string|HtmlArmor|null $text
152 * @param array $extraAttribs
153 * @param array $query
156 public function makeLink(
157 LinkTarget
$target, $text = null, array $extraAttribs = [], array $query = []
159 $title = Title
::newFromLinkTarget( $target );
160 if ( $title->isKnown() ) {
161 return $this->makeKnownLink( $target, $text, $extraAttribs, $query );
163 return $this->makeBrokenLink( $target, $text, $extraAttribs, $query );
168 * Get the options in the legacy format
170 * @param bool $isKnown Whether the link is known or broken
173 private function getLegacyOptions( $isKnown ) {
174 $options = [ 'stubThreshold' => $this->stubThreshold
];
175 if ( $this->noClasses
) {
176 $options[] = 'noclasses';
178 if ( $this->forceArticlePath
) {
179 $options[] = 'forcearticlepath';
181 if ( $this->expandUrls
=== PROTO_HTTP
) {
183 } elseif ( $this->expandUrls
=== PROTO_HTTPS
) {
184 $options[] = 'https';
187 $options[] = $isKnown ?
'known' : 'broken';
192 private function runBeginHook( LinkTarget
$target, &$text, &$extraAttribs, &$query, $isKnown ) {
194 if ( !Hooks
::run( 'HtmlPageLinkRendererBegin',
195 [ $this, $target, &$text, &$extraAttribs, &$query, &$ret ] )
200 // Now run the legacy hook
201 return $this->runLegacyBeginHook( $target, $text, $extraAttribs, $query, $isKnown );
204 private function runLegacyBeginHook( LinkTarget
$target, &$text, &$extraAttribs, &$query,
207 if ( !$this->runLegacyBeginHook ||
!Hooks
::isRegistered( 'LinkBegin' ) ) {
208 // Disabled, or nothing registered
212 $realOptions = $options = $this->getLegacyOptions( $isKnown );
214 $dummy = new DummyLinker();
215 $title = Title
::newFromLinkTarget( $target );
216 if ( $text !== null ) {
217 $realHtml = $html = HtmlArmor
::getHtml( $text );
219 $realHtml = $html = null;
221 if ( !Hooks
::run( 'LinkBegin',
222 [ $dummy, $title, &$html, &$extraAttribs, &$query, &$options, &$ret ] )
227 if ( $html !== null && $html !== $realHtml ) {
228 // &$html was modified, so re-armor it as $text
229 $text = new HtmlArmor( $html );
232 // Check if they changed any of the options, hopefully not!
233 if ( $options !== $realOptions ) {
234 $factory = MediaWikiServices
::getInstance()->getLinkRendererFactory();
235 // They did, so create a separate instance and have that take over the rest
236 $newRenderer = $factory->createFromLegacyOptions( $options );
237 // Don't recurse the hook...
238 $newRenderer->setRunLegacyBeginHook( false );
239 if ( in_array( 'known', $options, true ) ) {
240 return $newRenderer->makeKnownLink( $title, $text, $extraAttribs, $query );
241 } elseif ( in_array( 'broken', $options, true ) ) {
242 return $newRenderer->makeBrokenLink( $title, $text, $extraAttribs, $query );
244 return $newRenderer->makeLink( $title, $text, $extraAttribs, $query );
252 * @param LinkTarget $target
253 * @param string|HtmlArmor|null $text
254 * @param array $extraAttribs
255 * @param array $query
258 public function makeKnownLink(
259 LinkTarget
$target, $text = null, array $extraAttribs = [], array $query = []
262 $ret = $this->runBeginHook( $target, $text, $extraAttribs, $query, true );
263 if ( $ret !== null ) {
266 $target = $this->normalizeTarget( $target );
267 $url = $this->getLinkURL( $target, $query );
269 if ( !$this->noClasses
) {
271 if ( $target->isExternal() ) {
272 $classes[] = 'extiw';
274 $title = Title
::newFromLinkTarget( $target );
275 $colour = Linker
::getLinkColour( $title, $this->stubThreshold
);
276 if ( $colour !== '' ) {
277 $classes[] = $colour;
280 $attribs['class'] = implode( ' ', $classes );
284 $prefixedText = $this->titleFormatter
->getPrefixedText( $target );
285 if ( $prefixedText !== '' ) {
286 $attribs['title'] = $prefixedText;
291 ] +
$this->mergeAttribs( $attribs, $extraAttribs );
293 if ( $text === null ) {
294 $text = $this->getLinkText( $target );
297 return $this->buildAElement( $target, $text, $attribs, true );
301 * @param LinkTarget $target
302 * @param string|HtmlArmor|null $text
303 * @param array $extraAttribs
304 * @param array $query
307 public function makeBrokenLink(
308 LinkTarget
$target, $text = null, array $extraAttribs = [], array $query = []
311 $ret = $this->runBeginHook( $target, $text, $extraAttribs, $query, false );
312 if ( $ret !== null ) {
316 # We don't want to include fragments for broken links, because they
317 # generally make no sense.
318 if ( $target->hasFragment() ) {
319 $target = $target->createFragmentTarget( '' );
321 $target = $this->normalizeTarget( $target );
323 if ( !isset( $query['action'] ) && $target->getNamespace() !== NS_SPECIAL
) {
324 $query['action'] = 'edit';
325 $query['redlink'] = '1';
328 $url = $this->getLinkURL( $target, $query );
329 $attribs = $this->noClasses ?
[] : [ 'class' => 'new' ];
330 $prefixedText = $this->titleFormatter
->getPrefixedText( $target );
331 if ( $prefixedText !== '' ) {
332 // This ends up in parser cache!
333 $attribs['title'] = wfMessage( 'red-link-title', $prefixedText )
334 ->inContentLanguage()
340 ] +
$this->mergeAttribs( $attribs, $extraAttribs );
342 if ( $text === null ) {
343 $text = $this->getLinkText( $target );
346 return $this->buildAElement( $target, $text, $attribs, false );
350 * Builds the final <a> element
352 * @param LinkTarget $target
353 * @param string|HtmlArmor $text
354 * @param array $attribs
355 * @param bool $isKnown
356 * @return null|string
358 private function buildAElement( LinkTarget
$target, $text, array $attribs, $isKnown ) {
360 if ( !Hooks
::run( 'HtmlPageLinkRendererEnd',
361 [ $this, $target, $isKnown, &$text, &$attribs, &$ret ] )
366 $html = HtmlArmor
::getHtml( $text );
369 if ( Hooks
::isRegistered( 'LinkEnd' ) ) {
370 $dummy = new DummyLinker();
371 $title = Title
::newFromLinkTarget( $target );
372 $options = $this->getLegacyOptions( $isKnown );
373 if ( !Hooks
::run( 'LinkEnd',
374 [ $dummy, $title, $options, &$html, &$attribs, &$ret ] )
380 return Html
::rawElement( 'a', $attribs, $html );
384 * @param LinkTarget $target
385 * @return string non-escaped text
387 private function getLinkText( LinkTarget
$target ) {
388 $prefixedText = $this->titleFormatter
->getPrefixedText( $target );
389 // If the target is just a fragment, with no title, we return the fragment
390 // text. Otherwise, we return the title text itself.
391 if ( $prefixedText === '' && $target->hasFragment() ) {
392 return $target->getFragment();
395 return $prefixedText;
398 private function getLinkURL( LinkTarget
$target, array $query = [] ) {
399 // TODO: Use a LinkTargetResolver service instead of Title
400 $title = Title
::newFromLinkTarget( $target );
401 $proto = $this->expandUrls
!== false
404 if ( $this->forceArticlePath
) {
410 $url = $title->getLinkURL( $query, false, $proto );
412 if ( $this->forceArticlePath
&& $realQuery ) {
413 $url = wfAppendQuery( $url, $realQuery );
420 * Normalizes the provided target
422 * @todo move the code from Linker actually here
423 * @param LinkTarget $target
426 private function normalizeTarget( LinkTarget
$target ) {
427 return Linker
::normaliseSpecialPage( $target );
431 * Merges two sets of attributes
433 * @param array $defaults
434 * @param array $attribs
438 private function mergeAttribs( $defaults, $attribs ) {
442 # Merge the custom attribs with the default ones, and iterate
443 # over that, deleting all "false" attributes.
445 $merged = Sanitizer
::mergeAttributes( $defaults, $attribs );
446 foreach ( $merged as $key => $val ) {
447 # A false value suppresses the attribute
448 if ( $val !== false ) {