linker.php
来自「php 开发的内容管理系统」· PHP 代码 · 共 1,102 行 · 第 1/3 页
PHP
1,102 行
<?php/** * Split off some of the internal bits from Skin.php. * These functions are used for primarily page content: * links, embedded images, table of contents. Links are * also used in the skin. * @package MediaWiki *//** * For the moment, Skin is a descendent class of Linker. * In the future, it should probably be further split * so that ever other bit of the wiki doesn't have to * go loading up Skin to get at it. * * @package MediaWiki */class Linker { function Linker() {} /** * @deprecated */ function postParseLinkColour( $s = NULL ) { return NULL; } /** @todo document */ function getExternalLinkAttributes( $link, $text, $class='' ) { $link = htmlspecialchars( $link ); $r = ($class != '') ? " class=\"$class\"" : " class=\"external\""; $r .= " title=\"{$link}\""; return $r; } function getInterwikiLinkAttributes( $link, $text, $class='' ) { global $wgContLang; $same = ($link == $text); $link = urldecode( $link ); $link = $wgContLang->checkTitleEncoding( $link ); $link = preg_replace( '/[\\x00-\\x1f]/', ' ', $link ); $link = htmlspecialchars( $link ); $r = ($class != '') ? " class=\"$class\"" : " class=\"external\""; $r .= " title=\"{$link}\""; return $r; } /** @todo document */ function getInternalLinkAttributes( $link, $text, $broken = false ) { $link = urldecode( $link ); $link = str_replace( '_', ' ', $link ); $link = htmlspecialchars( $link ); if( $broken == 'stub' ) { $r = ' class="stub"'; } else if ( $broken == 'yes' ) { $r = ' class="new"'; } else { $r = ''; } $r .= " title=\"{$link}\""; return $r; } /** * @param $nt Title object. * @param $text String: FIXME * @param $broken Boolean: FIXME, default 'false'. */ function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) { if( $broken == 'stub' ) { $r = ' class="stub"'; } else if ( $broken == 'yes' ) { $r = ' class="new"'; } else { $r = ''; } $r .= ' title="' . $nt->getEscapedText() . '"'; return $r; } /** * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call * it if you already have a title object handy. See makeLinkObj for further documentation. * * @param $title String: the text of the title * @param $text String: link text * @param $query String: optional query part * @param $trail String: optional trail. Alphabetic characters at the start of this string will * be included in the link text. Other characters will be appended after * the end of the link. */ function makeLink( $title, $text = '', $query = '', $trail = '' ) { wfProfileIn( 'Linker::makeLink' ); $nt = Title::newFromText( $title ); if ($nt) { $result = $this->makeLinkObj( Title::newFromText( $title ), $text, $query, $trail ); } else { wfDebug( 'Invalid title passed to Linker::makeLink(): "'.$title."\"\n" ); $result = $text == "" ? $title : $text; } wfProfileOut( 'Linker::makeLink' ); return $result; } /** * This function is a shortcut to makeKnownLinkObj(Title::newFromText($title),...). Do not call * it if you already have a title object handy. See makeKnownLinkObj for further documentation. * * @param $title String: the text of the title * @param $text String: link text * @param $query String: optional query part * @param $trail String: optional trail. Alphabetic characters at the start of this string will * be included in the link text. Other characters will be appended after * the end of the link. */ function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') { $nt = Title::newFromText( $title ); if ($nt) { return $this->makeKnownLinkObj( Title::newFromText( $title ), $text, $query, $trail, $prefix , $aprops ); } else { wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "'.$title."\"\n" ); return $text == '' ? $title : $text; } } /** * This function is a shortcut to makeBrokenLinkObj(Title::newFromText($title),...). Do not call * it if you already have a title object handy. See makeBrokenLinkObj for further documentation. * * @param string $title The text of the title * @param string $text Link text * @param string $query Optional query part * @param string $trail Optional trail. Alphabetic characters at the start of this string will * be included in the link text. Other characters will be appended after * the end of the link. */ function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) { $nt = Title::newFromText( $title ); if ($nt) { return $this->makeBrokenLinkObj( Title::newFromText( $title ), $text, $query, $trail ); } else { wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "'.$title."\"\n" ); return $text == '' ? $title : $text; } } /** * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call * it if you already have a title object handy. See makeStubLinkObj for further documentation. * * @param $title String: the text of the title * @param $text String: link text * @param $query String: optional query part * @param $trail String: optional trail. Alphabetic characters at the start of this string will * be included in the link text. Other characters will be appended after * the end of the link. */ function makeStubLink( $title, $text = '', $query = '', $trail = '' ) { $nt = Title::newFromText( $title ); if ($nt) { return $this->makeStubLinkObj( Title::newFromText( $title ), $text, $query, $trail ); } else { wfDebug( 'Invalid title passed to Linker::makeStubLink(): "'.$title."\"\n" ); return $text == '' ? $title : $text; } } /** * Make a link for a title which may or may not be in the database. If you need to * call this lots of times, pre-fill the link cache with a LinkBatch, otherwise each * call to this will result in a DB query. * * @param $title String: the text of the title * @param $text String: link text * @param $query String: optional query part * @param $trail String: optional trail. Alphabetic characters at the start of this string will * be included in the link text. Other characters will be appended after * the end of the link. */ function makeLinkObj( $nt, $text= '', $query = '', $trail = '', $prefix = '' ) { global $wgUser; $fname = 'Linker::makeLinkObj'; wfProfileIn( $fname ); # Fail gracefully if ( ! is_object($nt) ) { # throw new MWException(); wfProfileOut( $fname ); return "<!-- ERROR -->{$prefix}{$text}{$trail}"; } $ns = $nt->getNamespace(); $dbkey = $nt->getDBkey(); if ( $nt->isExternal() ) { $u = $nt->getFullURL(); $link = $nt->getPrefixedURL(); if ( '' == $text ) { $text = $nt->getPrefixedText(); } $style = $this->getInterwikiLinkAttributes( $link, $text, 'extiw' ); $inside = ''; if ( '' != $trail ) { if ( preg_match( '/^([a-z]+)(.*)$$/sD', $trail, $m ) ) { $inside = $m[1]; $trail = $m[2]; } } # Check for anchors, normalize the anchor $parts = explode( '#', $u, 2 ); if ( count( $parts ) == 2 ) { $anchor = urlencode( Sanitizer::decodeCharReferences( str_replace(' ', '_', $parts[1] ) ) ); $replacearray = array( '%3A' => ':', '%' => '.' ); $u = $parts[0] . '#' . str_replace( array_keys( $replacearray ), array_values( $replacearray ), $anchor ); } $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>"; wfProfileOut( $fname ); return $t; } elseif ( $nt->isAlwaysKnown() ) { # Image links, special page links and self-links with fragements are always known. $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix ); } else { wfProfileIn( $fname.'-immediate' ); # Work out link colour immediately $aid = $nt->getArticleID() ; if ( 0 == $aid ) { $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix ); } else { $threshold = $wgUser->getOption('stubthreshold') ; if ( $threshold > 0 ) { $dbr =& wfGetDB( DB_SLAVE ); $s = $dbr->selectRow( array( 'page' ), array( 'page_len', 'page_namespace', 'page_is_redirect' ), array( 'page_id' => $aid ), $fname ) ; if ( $s !== false ) { $size = $s->page_len; if ( $s->page_is_redirect OR $s->page_namespace != NS_MAIN ) { $size = $threshold*2 ; # Really big } } else { $size = $threshold*2 ; # Really big } } else { $size = 1 ; } if ( $size < $threshold ) { $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix ); } else { $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix ); } } wfProfileOut( $fname.'-immediate' ); } wfProfileOut( $fname ); return $retVal; } /** * Make a link for a title which definitely exists. This is faster than makeLinkObj because * it doesn't have to do a database query. It's also valid for interwiki titles and special * pages. * * @param $nt Title object of target page * @param $text String: text to replace the title * @param $query String: link target * @param $trail String: text after link * @param $prefix String: text before link text * @param $aprops String: extra attributes to the a-element * @param $style String: style to apply - if empty, use getInternalLinkAttributesObj instead * @return the a-element */ function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) { $fname = 'Linker::makeKnownLinkObj'; wfProfileIn( $fname ); if ( !is_object( $nt ) ) { wfProfileOut( $fname ); return $text; } $u = $nt->escapeLocalURL( $query ); if ( $nt->getFragment() != '' ) { if( $nt->getPrefixedDbkey() == '' ) { $u = ''; if ( '' == $text ) { $text = htmlspecialchars( $nt->getFragment() ); } } $anchor = urlencode( Sanitizer::decodeCharReferences( str_replace( ' ', '_', $nt->getFragment() ) ) ); $replacearray = array( '%3A' => ':', '%' => '.' ); $u .= '#' . str_replace(array_keys($replacearray),array_values($replacearray),$anchor); } if ( $text == '' ) { $text = htmlspecialchars( $nt->getPrefixedText() ); } if ( $style == '' ) { $style = $this->getInternalLinkAttributesObj( $nt, $text ); } if ( $aprops !== '' ) $aprops = ' ' . $aprops; list( $inside, $trail ) = Linker::splitTrail( $trail ); $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}"; wfProfileOut( $fname ); return $r; } /** * Make a red link to the edit page of a given title. * * @param $title String: The text of the title * @param $text String: Link text * @param $query String: Optional query part * @param $trail String: Optional trail. Alphabetic characters at the start of this string will * be included in the link text. Other characters will be appended after * the end of the link. */ function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) { # Fail gracefully if ( ! isset($nt) ) { # throw new MWException(); return "<!-- ERROR -->{$prefix}{$text}{$trail}"; } $fname = 'Linker::makeBrokenLinkObj'; wfProfileIn( $fname ); if ( '' == $query ) { $q = 'action=edit'; } else { $q = 'action=edit&'.$query; } $u = $nt->escapeLocalURL( $q ); if ( '' == $text ) { $text = htmlspecialchars( $nt->getPrefixedText() ); } $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" ); list( $inside, $trail ) = Linker::splitTrail( $trail ); $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}"; wfProfileOut( $fname );
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?