MediaWiki  REL1_23
MediaWikiPageLinkRenderer.php
Go to the documentation of this file.
00001 <?php
00030 class MediaWikiPageLinkRenderer implements PageLinkRenderer {
00031 
00035     protected $formatter;
00036 
00040     protected $baseUrl;
00041 
00053     public function __construct( TitleFormatter $formatter, $baseUrl = null ) {
00054         if ( $baseUrl === null ) {
00055             $baseUrl = $GLOBALS['wgArticlePath'];
00056         }
00057 
00058         $this->formatter = $formatter;
00059         $this->baseUrl = $baseUrl;
00060     }
00061 
00070     public function getPageUrl( TitleValue $page, $params = array() ) {
00071         //TODO: move the code from Linker::linkUrl here!
00072         //The below is just a rough estimation!
00073 
00074         $name = $this->formatter->getPrefixedText( $page );
00075         $name = str_replace( ' ', '_', $name );
00076         $name = wfUrlencode( $name );
00077 
00078         $url = $this->baseUrl . $name;
00079 
00080         if ( $params ) {
00081             $separator = ( strpos( $url, '?' ) ) ? '&' : '?';
00082             $url .= $separator . wfArrayToCgi( $params );
00083         }
00084 
00085         $fragment = $page->getFragment();
00086         if ( $fragment !== '' ) {
00087             $url = $url . '#' . wfUrlencode( $fragment );
00088         }
00089 
00090         return $url;
00091     }
00092 
00101     public function renderHtmlLink( TitleValue $page, $text = null ) {
00102         if ( $text === null ) {
00103             $text = $this->formatter->getFullText( $page );
00104         }
00105 
00106         // TODO: move the logic implemented by Linker here,
00107         // using $this->formatter and $this->baseUrl, and
00108         // re-implement Linker to use a HtmlPageLinkRenderer.
00109         $title = Title::newFromTitleValue( $page );
00110         $link = Linker::link( $title, htmlspecialchars( $text ) );
00111         return $link;
00112     }
00113 
00122     public function renderWikitextLink( TitleValue $page, $text = null ) {
00123         if ( $text === null ) {
00124             $text = $this->formatter->getFullText( $page );
00125         }
00126 
00127         $name = $this->formatter->getFullText( $page );
00128 
00129         return '[[:' . $name . '|' . wfEscapeWikiText( $text ) . ']]';
00130     }
00131 }