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