Linker.php 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  1. <?php
  2. /**
  3. * Split off some of the internal bits from Skin.php. These functions are used
  4. * for primarily page content: links, embedded images, table of contents. Links
  5. * are also used in the skin. For the moment, Skin is a descendent class of
  6. * Linker. In the future, it should probably be further split so that every
  7. * other bit of the wiki doesn't have to go loading up Skin to get at it.
  8. *
  9. * @ingroup Skins
  10. */
  11. class Linker {
  12. /**
  13. * Flags for userToolLinks()
  14. */
  15. const TOOL_LINKS_NOBLOCK = 1;
  16. function __construct() {}
  17. /**
  18. * @deprecated
  19. */
  20. function postParseLinkColour( $s = null ) {
  21. wfDeprecated( __METHOD__ );
  22. return null;
  23. }
  24. /**
  25. * Get the appropriate HTML attributes to add to the "a" element of an ex-
  26. * ternal link, as created by [wikisyntax].
  27. *
  28. * @param string $title The (unescaped) title text for the link
  29. * @param string $unused Unused
  30. * @param string $class The contents of the class attribute; if an empty
  31. * string is passed, which is the default value, defaults to 'external'.
  32. */
  33. function getExternalLinkAttributes( $title, $unused = null, $class='' ) {
  34. return $this->getLinkAttributesInternal( $title, $class, 'external' );
  35. }
  36. /**
  37. * Get the appropriate HTML attributes to add to the "a" element of an in-
  38. * terwiki link.
  39. *
  40. * @param string $title The title text for the link, URL-encoded (???) but
  41. * not HTML-escaped
  42. * @param string $unused Unused
  43. * @param string $class The contents of the class attribute; if an empty
  44. * string is passed, which is the default value, defaults to 'external'.
  45. */
  46. function getInterwikiLinkAttributes( $title, $unused = null, $class='' ) {
  47. global $wgContLang;
  48. # FIXME: We have a whole bunch of handling here that doesn't happen in
  49. # getExternalLinkAttributes, why?
  50. $title = urldecode( $title );
  51. $title = $wgContLang->checkTitleEncoding( $title );
  52. $title = preg_replace( '/[\\x00-\\x1f]/', ' ', $title );
  53. return $this->getLinkAttributesInternal( $title, $class, 'external' );
  54. }
  55. /**
  56. * Get the appropriate HTML attributes to add to the "a" element of an in-
  57. * ternal link.
  58. *
  59. * @param string $title The title text for the link, URL-encoded (???) but
  60. * not HTML-escaped
  61. * @param string $unused Unused
  62. * @param string $class The contents of the class attribute, default none
  63. */
  64. function getInternalLinkAttributes( $title, $unused = null, $class='' ) {
  65. $title = urldecode( $title );
  66. $title = str_replace( '_', ' ', $title );
  67. return $this->getLinkAttributesInternal( $title, $class );
  68. }
  69. /**
  70. * Get the appropriate HTML attributes to add to the "a" element of an in-
  71. * ternal link, given the Title object for the page we want to link to.
  72. *
  73. * @param Title $nt The Title object
  74. * @param string $unused Unused
  75. * @param string $class The contents of the class attribute, default none
  76. * @param mixed $title Optional (unescaped) string to use in the title
  77. * attribute; if false, default to the name of the page we're linking to
  78. */
  79. function getInternalLinkAttributesObj( $nt, $unused = null, $class = '', $title = false ) {
  80. if( $title === false ) {
  81. $title = $nt->getPrefixedText();
  82. }
  83. return $this->getLinkAttributesInternal( $title, $class );
  84. }
  85. /**
  86. * Common code for getLinkAttributesX functions
  87. */
  88. private function getLinkAttributesInternal( $title, $class, $classDefault = false ) {
  89. $title = htmlspecialchars( $title );
  90. if( $class === '' and $classDefault !== false ) {
  91. # FIXME: Parameter defaults the hard way! We should just have
  92. # $class = 'external' or whatever as the default in the externally-
  93. # exposed functions, not $class = ''.
  94. $class = $classDefault;
  95. }
  96. $class = htmlspecialchars( $class );
  97. $r = '';
  98. if( $class !== '' ) {
  99. $r .= " class=\"$class\"";
  100. }
  101. $r .= " title=\"$title\"";
  102. return $r;
  103. }
  104. /**
  105. * Return the CSS colour of a known link
  106. *
  107. * @param Title $t
  108. * @param integer $threshold user defined threshold
  109. * @return string CSS class
  110. */
  111. function getLinkColour( $t, $threshold ) {
  112. $colour = '';
  113. if ( $t->isRedirect() ) {
  114. # Page is a redirect
  115. $colour = 'mw-redirect';
  116. } elseif ( $threshold > 0 &&
  117. $t->exists() && $t->getLength() < $threshold &&
  118. MWNamespace::isContent( $t->getNamespace() ) ) {
  119. # Page is a stub
  120. $colour = 'stub';
  121. }
  122. return $colour;
  123. }
  124. /**
  125. * This function returns an HTML link to the given target. It serves a few
  126. * purposes:
  127. * 1) If $target is a Title, the correct URL to link to will be figured
  128. * out automatically.
  129. * 2) It automatically adds the usual classes for various types of link
  130. * targets: "new" for red links, "stub" for short articles, etc.
  131. * 3) It escapes all attribute values safely so there's no risk of XSS.
  132. * 4) It provides a default tooltip if the target is a Title (the page
  133. * name of the target).
  134. * link() replaces the old functions in the makeLink() family.
  135. *
  136. * @param $target Title Can currently only be a Title, but this may
  137. * change to support Images, literal URLs, etc.
  138. * @param $text string The HTML contents of the <a> element, i.e.,
  139. * the link text. This is raw HTML and will not be escaped. If null,
  140. * defaults to the prefixed text of the Title; or if the Title is just a
  141. * fragment, the contents of the fragment.
  142. * @param $customAttribs array A key => value array of extra HTML attri-
  143. * butes, such as title and class. (href is ignored.) Classes will be
  144. * merged with the default classes, while other attributes will replace
  145. * default attributes. All passed attribute values will be HTML-escaped.
  146. * A false attribute value means to suppress that attribute.
  147. * @param $query array The query string to append to the URL
  148. * you're linking to, in key => value array form. Query keys and values
  149. * will be URL-encoded.
  150. * @param $options mixed String or array of strings:
  151. * 'known': Page is known to exist, so don't check if it does.
  152. * 'broken': Page is known not to exist, so don't check if it does.
  153. * 'noclasses': Don't add any classes automatically (includes "new",
  154. * "stub", "mw-redirect", "extiw"). Only use the class attribute
  155. * provided, if any, so you get a simple blue link with no funny i-
  156. * cons.
  157. * 'forcearticlepath': Use the article path always, even with a querystring.
  158. * Has compatibility issues on some setups, so avoid wherever possible.
  159. * @return string HTML <a> attribute
  160. */
  161. public function link( $target, $text = null, $customAttribs = array(), $query = array(), $options = array() ) {
  162. wfProfileIn( __METHOD__ );
  163. if( !$target instanceof Title ) {
  164. return "<!-- ERROR -->$text";
  165. }
  166. $options = (array)$options;
  167. $ret = null;
  168. if( !wfRunHooks( 'LinkBegin', array( $this, $target, &$text,
  169. &$customAttribs, &$query, &$options, &$ret ) ) ) {
  170. wfProfileOut( __METHOD__ );
  171. return $ret;
  172. }
  173. # Normalize the Title if it's a special page
  174. $target = $this->normaliseSpecialPage( $target );
  175. # If we don't know whether the page exists, let's find out.
  176. wfProfileIn( __METHOD__ . '-checkPageExistence' );
  177. if( !in_array( 'known', $options ) and !in_array( 'broken', $options ) ) {
  178. if( $target->isKnown() ) {
  179. $options []= 'known';
  180. } else {
  181. $options []= 'broken';
  182. }
  183. }
  184. wfProfileOut( __METHOD__ . '-checkPageExistence' );
  185. $oldquery = array();
  186. if( in_array( "forcearticlepath", $options ) && $query ){
  187. $oldquery = $query;
  188. $query = array();
  189. }
  190. # Note: we want the href attribute first, for prettiness.
  191. $attribs = array( 'href' => $this->linkUrl( $target, $query, $options ) );
  192. if( in_array( 'forcearticlepath', $options ) && $oldquery ){
  193. $attribs['href'] = wfAppendQuery( $attribs['href'], wfArrayToCgi( $oldquery ) );
  194. }
  195. $attribs = array_merge(
  196. $attribs,
  197. $this->linkAttribs( $target, $customAttribs, $options )
  198. );
  199. if( is_null( $text ) ) {
  200. $text = $this->linkText( $target );
  201. }
  202. $ret = null;
  203. if( wfRunHooks( 'LinkEnd', array( $this, $target, $options, &$text, &$attribs, &$ret ) ) ) {
  204. $ret = Xml::openElement( 'a', $attribs ) . $text . Xml::closeElement( 'a' );
  205. }
  206. wfProfileOut( __METHOD__ );
  207. return $ret;
  208. }
  209. private function linkUrl( $target, $query, $options ) {
  210. wfProfileIn( __METHOD__ );
  211. # We don't want to include fragments for broken links, because they
  212. # generally make no sense.
  213. if( in_array( 'broken', $options ) and $target->mFragment !== '' ) {
  214. $target = clone $target;
  215. $target->mFragment = '';
  216. }
  217. # If it's a broken link, add the appropriate query pieces, unless
  218. # there's already an action specified, or unless 'edit' makes no sense
  219. # (i.e., for a nonexistent special page).
  220. if( in_array( 'broken', $options ) and empty( $query['action'] )
  221. and $target->getNamespace() != NS_SPECIAL ) {
  222. $query['action'] = 'edit';
  223. $query['redlink'] = '1';
  224. }
  225. $ret = $target->getLinkUrl( $query );
  226. wfProfileOut( __METHOD__ );
  227. return $ret;
  228. }
  229. private function linkAttribs( $target, $attribs, $options ) {
  230. wfProfileIn( __METHOD__ );
  231. global $wgUser;
  232. $defaults = array();
  233. if( !in_array( 'noclasses', $options ) ) {
  234. wfProfileIn( __METHOD__ . '-getClasses' );
  235. # Now build the classes.
  236. $classes = array();
  237. if( in_array( 'broken', $options ) ) {
  238. $classes[] = 'new';
  239. }
  240. if( $target->isExternal() ) {
  241. $classes[] = 'extiw';
  242. }
  243. # Note that redirects never count as stubs here.
  244. if ( $target->isRedirect() ) {
  245. $classes[] = 'mw-redirect';
  246. } elseif( $target->isContentPage() ) {
  247. # Check for stub.
  248. $threshold = $wgUser->getOption( 'stubthreshold' );
  249. if( $threshold > 0 and $target->exists() and $target->getLength() < $threshold ) {
  250. $classes[] = 'stub';
  251. }
  252. }
  253. if( $classes != array() ) {
  254. $defaults['class'] = implode( ' ', $classes );
  255. }
  256. wfProfileOut( __METHOD__ . '-getClasses' );
  257. }
  258. # Get a default title attribute.
  259. # never want red-links
  260. #if( in_array( 'known', $options ) ) {
  261. $defaults['title'] = $target->getPrefixedText();
  262. #} else {
  263. # $defaults['title'] = wfMsg( 'red-link-title', $target->getPrefixedText() );
  264. #}
  265. # Finally, merge the custom attribs with the default ones, and iterate
  266. # over that, deleting all "false" attributes.
  267. $ret = array();
  268. $merged = Sanitizer::mergeAttributes( $defaults, $attribs );
  269. foreach( $merged as $key => $val ) {
  270. # A false value suppresses the attribute, and we don't want the
  271. # href attribute to be overridden.
  272. if( $key != 'href' and $val !== false ) {
  273. $ret[$key] = $val;
  274. }
  275. }
  276. wfProfileOut( __METHOD__ );
  277. return $ret;
  278. }
  279. private function linkText( $target ) {
  280. # We might be passed a non-Title by make*LinkObj(). Fail gracefully.
  281. if( !$target instanceof Title ) {
  282. return '';
  283. }
  284. # If the target is just a fragment, with no title, we return the frag-
  285. # ment text. Otherwise, we return the title text itself.
  286. if( $target->getPrefixedText() === '' and $target->getFragment() !== '' ) {
  287. return htmlspecialchars( $target->getFragment() );
  288. }
  289. return htmlspecialchars( $target->getPrefixedText() );
  290. }
  291. /**
  292. * @deprecated Use link()
  293. *
  294. * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call
  295. * it if you already have a title object handy. See makeLinkObj for further documentation.
  296. *
  297. * @param $title String: the text of the title
  298. * @param $text String: link text
  299. * @param $query String: optional query part
  300. * @param $trail String: optional trail. Alphabetic characters at the start of this string will
  301. * be included in the link text. Other characters will be appended after
  302. * the end of the link.
  303. */
  304. function makeLink( $title, $text = '', $query = '', $trail = '' ) {
  305. wfProfileIn( __METHOD__ );
  306. $nt = Title::newFromText( $title );
  307. if ( $nt instanceof Title ) {
  308. $result = $this->makeLinkObj( $nt, $text, $query, $trail );
  309. } else {
  310. wfDebug( 'Invalid title passed to Linker::makeLink(): "'.$title."\"\n" );
  311. $result = $text == "" ? $title : $text;
  312. }
  313. wfProfileOut( __METHOD__ );
  314. return $result;
  315. }
  316. /**
  317. * @deprecated Use link()
  318. *
  319. * This function is a shortcut to makeKnownLinkObj(Title::newFromText($title),...). Do not call
  320. * it if you already have a title object handy. See makeKnownLinkObj for further documentation.
  321. *
  322. * @param $title String: the text of the title
  323. * @param $text String: link text
  324. * @param $query String: optional query part
  325. * @param $trail String: optional trail. Alphabetic characters at the start of this string will
  326. * be included in the link text. Other characters will be appended after
  327. * the end of the link.
  328. */
  329. function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
  330. $nt = Title::newFromText( $title );
  331. if ( $nt instanceof Title ) {
  332. return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix , $aprops );
  333. } else {
  334. wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "'.$title."\"\n" );
  335. return $text == '' ? $title : $text;
  336. }
  337. }
  338. /**
  339. * @deprecated Use link()
  340. *
  341. * This function is a shortcut to makeBrokenLinkObj(Title::newFromText($title),...). Do not call
  342. * it if you already have a title object handy. See makeBrokenLinkObj for further documentation.
  343. *
  344. * @param string $title The text of the title
  345. * @param string $text Link text
  346. * @param string $query Optional query part
  347. * @param string $trail Optional trail. Alphabetic characters at the start of this string will
  348. * be included in the link text. Other characters will be appended after
  349. * the end of the link.
  350. */
  351. function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
  352. $nt = Title::newFromText( $title );
  353. if ( $nt instanceof Title ) {
  354. return $this->makeBrokenLinkObj( $nt, $text, $query, $trail );
  355. } else {
  356. wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "'.$title."\"\n" );
  357. return $text == '' ? $title : $text;
  358. }
  359. }
  360. /**
  361. * @deprecated Use link()
  362. *
  363. * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call
  364. * it if you already have a title object handy. See makeStubLinkObj for further documentation.
  365. *
  366. * @param $title String: the text of the title
  367. * @param $text String: link text
  368. * @param $query String: optional query part
  369. * @param $trail String: optional trail. Alphabetic characters at the start of this string will
  370. * be included in the link text. Other characters will be appended after
  371. * the end of the link.
  372. */
  373. function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
  374. wfDeprecated( __METHOD__ );
  375. $nt = Title::newFromText( $title );
  376. if ( $nt instanceof Title ) {
  377. return $this->makeStubLinkObj( $nt, $text, $query, $trail );
  378. } else {
  379. wfDebug( 'Invalid title passed to Linker::makeStubLink(): "'.$title."\"\n" );
  380. return $text == '' ? $title : $text;
  381. }
  382. }
  383. /**
  384. * @deprecated Use link()
  385. *
  386. * Make a link for a title which may or may not be in the database. If you need to
  387. * call this lots of times, pre-fill the link cache with a LinkBatch, otherwise each
  388. * call to this will result in a DB query.
  389. *
  390. * @param $nt Title: the title object to make the link from, e.g. from
  391. * Title::newFromText.
  392. * @param $text String: link text
  393. * @param $query String: optional query part
  394. * @param $trail String: optional trail. Alphabetic characters at the start of this string will
  395. * be included in the link text. Other characters will be appended after
  396. * the end of the link.
  397. * @param $prefix String: optional prefix. As trail, only before instead of after.
  398. */
  399. function makeLinkObj( $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
  400. global $wgUser;
  401. wfProfileIn( __METHOD__ );
  402. $query = wfCgiToArray( $query );
  403. list( $inside, $trail ) = Linker::splitTrail( $trail );
  404. if( $text === '' ) {
  405. $text = $this->linkText( $nt );
  406. }
  407. $ret = $this->link( $nt, "$prefix$text$inside", array(), $query ) . $trail;
  408. wfProfileOut( __METHOD__ );
  409. return $ret;
  410. }
  411. /**
  412. * @deprecated Use link()
  413. *
  414. * Make a link for a title which definitely exists. This is faster than makeLinkObj because
  415. * it doesn't have to do a database query. It's also valid for interwiki titles and special
  416. * pages.
  417. *
  418. * @param $nt Title object of target page
  419. * @param $text String: text to replace the title
  420. * @param $query String: link target
  421. * @param $trail String: text after link
  422. * @param $prefix String: text before link text
  423. * @param $aprops String: extra attributes to the a-element
  424. * @param $style String: style to apply - if empty, use getInternalLinkAttributesObj instead
  425. * @return the a-element
  426. */
  427. function makeKnownLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
  428. wfProfileIn( __METHOD__ );
  429. if ( $text == '' ) {
  430. $text = $this->linkText( $title );
  431. }
  432. $attribs = Sanitizer::mergeAttributes(
  433. Sanitizer::decodeTagAttributes( $aprops ),
  434. Sanitizer::decodeTagAttributes( $style )
  435. );
  436. $query = wfCgiToArray( $query );
  437. list( $inside, $trail ) = Linker::splitTrail( $trail );
  438. $ret = $this->link( $title, "$prefix$text$inside", $attribs, $query,
  439. array( 'known', 'noclasses' ) ) . $trail;
  440. wfProfileOut( __METHOD__ );
  441. return $ret;
  442. }
  443. /**
  444. * @deprecated Use link()
  445. *
  446. * Make a red link to the edit page of a given title.
  447. *
  448. * @param $nt Title object of the target page
  449. * @param $text String: Link text
  450. * @param $query String: Optional query part
  451. * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
  452. * be included in the link text. Other characters will be appended after
  453. * the end of the link.
  454. */
  455. function makeBrokenLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' ) {
  456. wfProfileIn( __METHOD__ );
  457. list( $inside, $trail ) = Linker::splitTrail( $trail );
  458. if( $text === '' ) {
  459. $text = $this->linkText( $title );
  460. }
  461. $nt = $this->normaliseSpecialPage( $title );
  462. $ret = $this->link( $title, "$prefix$text$inside", array(),
  463. wfCgiToArray( $query ), 'broken' ) . $trail;
  464. wfProfileOut( __METHOD__ );
  465. return $ret;
  466. }
  467. /**
  468. * @deprecated Use link()
  469. *
  470. * Make a brown link to a short article.
  471. *
  472. * @param $nt Title object of the target page
  473. * @param $text String: link text
  474. * @param $query String: optional query part
  475. * @param $trail String: optional trail. Alphabetic characters at the start of this string will
  476. * be included in the link text. Other characters will be appended after
  477. * the end of the link.
  478. */
  479. function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
  480. wfDeprecated( __METHOD__ );
  481. return $this->makeColouredLinkObj( $nt, 'stub', $text, $query, $trail, $prefix );
  482. }
  483. /**
  484. * @deprecated Use link()
  485. *
  486. * Make a coloured link.
  487. *
  488. * @param $nt Title object of the target page
  489. * @param $colour Integer: colour of the link
  490. * @param $text String: link text
  491. * @param $query String: optional query part
  492. * @param $trail String: optional trail. Alphabetic characters at the start of this string will
  493. * be included in the link text. Other characters will be appended after
  494. * the end of the link.
  495. */
  496. function makeColouredLinkObj( $nt, $colour, $text = '', $query = '', $trail = '', $prefix = '' ) {
  497. if($colour != ''){
  498. $style = $this->getInternalLinkAttributesObj( $nt, $text, $colour );
  499. } else $style = '';
  500. return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix, '', $style );
  501. }
  502. /**
  503. * Generate either a normal exists-style link or a stub link, depending
  504. * on the given page size.
  505. *
  506. * @param $size Integer
  507. * @param $nt Title object.
  508. * @param $text String
  509. * @param $query String
  510. * @param $trail String
  511. * @param $prefix String
  512. * @return string HTML of link
  513. */
  514. function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
  515. global $wgUser;
  516. $threshold = intval( $wgUser->getOption( 'stubthreshold' ) );
  517. $colour = ( $size < $threshold ) ? 'stub' : '';
  518. return $this->makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix );
  519. }
  520. /**
  521. * Make appropriate markup for a link to the current article. This is currently rendered
  522. * as the bold link text. The calling sequence is the same as the other make*LinkObj functions,
  523. * despite $query not being used.
  524. */
  525. function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
  526. if ( '' == $text ) {
  527. $text = htmlspecialchars( $nt->getPrefixedText() );
  528. }
  529. list( $inside, $trail ) = Linker::splitTrail( $trail );
  530. return "<strong class=\"selflink\">{$prefix}{$text}{$inside}</strong>{$trail}";
  531. }
  532. function normaliseSpecialPage( Title $title ) {
  533. if ( $title->getNamespace() == NS_SPECIAL ) {
  534. list( $name, $subpage ) = SpecialPage::resolveAliasWithSubpage( $title->getDBkey() );
  535. if ( !$name ) return $title;
  536. $ret = SpecialPage::getTitleFor( $name, $subpage );
  537. $ret->mFragment = $title->getFragment();
  538. return $ret;
  539. } else {
  540. return $title;
  541. }
  542. }
  543. /** @todo document */
  544. function fnamePart( $url ) {
  545. $basename = strrchr( $url, '/' );
  546. if ( false === $basename ) {
  547. $basename = $url;
  548. } else {
  549. $basename = substr( $basename, 1 );
  550. }
  551. return $basename;
  552. }
  553. /** Obsolete alias */
  554. function makeImage( $url, $alt = '' ) {
  555. wfDeprecated( __METHOD__ );
  556. return $this->makeExternalImage( $url, $alt );
  557. }
  558. /** @todo document */
  559. function makeExternalImage( $url, $alt = '' ) {
  560. if ( '' == $alt ) {
  561. $alt = $this->fnamePart( $url );
  562. }
  563. $img = '';
  564. $success = wfRunHooks('LinkerMakeExternalImage', array( &$url, &$alt, &$img ) );
  565. if(!$success) {
  566. wfDebug("Hook LinkerMakeExternalImage changed the output of external image with url {$url} and alt text {$alt} to {$img}\n", true);
  567. return $img;
  568. }
  569. return Xml::element( 'img',
  570. array(
  571. 'src' => $url,
  572. 'alt' => $alt ) );
  573. }
  574. /**
  575. * Creates the HTML source for images
  576. * @deprecated use makeImageLink2
  577. *
  578. * @param object $title
  579. * @param string $label label text
  580. * @param string $alt alt text
  581. * @param string $align horizontal alignment: none, left, center, right)
  582. * @param array $handlerParams Parameters to be passed to the media handler
  583. * @param boolean $framed shows image in original size in a frame
  584. * @param boolean $thumb shows image as thumbnail in a frame
  585. * @param string $manualthumb image name for the manual thumbnail
  586. * @param string $valign vertical alignment: baseline, sub, super, top, text-top, middle, bottom, text-bottom
  587. * @param string $time, timestamp of the file, set as false for current
  588. * @return string
  589. */
  590. function makeImageLinkObj( $title, $label, $alt, $align = '', $handlerParams = array(), $framed = false,
  591. $thumb = false, $manualthumb = '', $valign = '', $time = false )
  592. {
  593. $frameParams = array( 'alt' => $alt, 'caption' => $label );
  594. if ( $align ) {
  595. $frameParams['align'] = $align;
  596. }
  597. if ( $framed ) {
  598. $frameParams['framed'] = true;
  599. }
  600. if ( $thumb ) {
  601. $frameParams['thumbnail'] = true;
  602. }
  603. if ( $manualthumb ) {
  604. $frameParams['manualthumb'] = $manualthumb;
  605. }
  606. if ( $valign ) {
  607. $frameParams['valign'] = $valign;
  608. }
  609. $file = wfFindFile( $title, $time );
  610. return $this->makeImageLink2( $title, $file, $frameParams, $handlerParams, $time );
  611. }
  612. /**
  613. * Given parameters derived from [[Image:Foo|options...]], generate the
  614. * HTML that that syntax inserts in the page.
  615. *
  616. * @param Title $title Title object
  617. * @param File $file File object, or false if it doesn't exist
  618. *
  619. * @param array $frameParams Associative array of parameters external to the media handler.
  620. * Boolean parameters are indicated by presence or absence, the value is arbitrary and
  621. * will often be false.
  622. * thumbnail If present, downscale and frame
  623. * manualthumb Image name to use as a thumbnail, instead of automatic scaling
  624. * framed Shows image in original size in a frame
  625. * frameless Downscale but don't frame
  626. * upright If present, tweak default sizes for portrait orientation
  627. * upright_factor Fudge factor for "upright" tweak (default 0.75)
  628. * border If present, show a border around the image
  629. * align Horizontal alignment (left, right, center, none)
  630. * valign Vertical alignment (baseline, sub, super, top, text-top, middle,
  631. * bottom, text-bottom)
  632. * alt Alternate text for image (i.e. alt attribute). Plain text.
  633. * caption HTML for image caption.
  634. * link-url URL to link to
  635. * link-title Title object to link to
  636. * no-link Boolean, suppress description link
  637. *
  638. * @param array $handlerParams Associative array of media handler parameters, to be passed
  639. * to transform(). Typical keys are "width" and "page".
  640. * @param string $time, timestamp of the file, set as false for current
  641. * @param string $query, query params for desc url
  642. * @return string HTML for an image, with links, wrappers, etc.
  643. */
  644. function makeImageLink2( Title $title, $file, $frameParams = array(), $handlerParams = array(), $time = false, $query = "" ) {
  645. $res = null;
  646. if( !wfRunHooks( 'ImageBeforeProduceHTML', array( &$this, &$title,
  647. &$file, &$frameParams, &$handlerParams, &$time, &$res ) ) ) {
  648. return $res;
  649. }
  650. global $wgContLang, $wgUser, $wgThumbLimits, $wgThumbUpright;
  651. if ( $file && !$file->allowInlineDisplay() ) {
  652. wfDebug( __METHOD__.': '.$title->getPrefixedDBkey()." does not allow inline display\n" );
  653. return $this->link( $title );
  654. }
  655. // Shortcuts
  656. $fp =& $frameParams;
  657. $hp =& $handlerParams;
  658. // Clean up parameters
  659. $page = isset( $hp['page'] ) ? $hp['page'] : false;
  660. if ( !isset( $fp['align'] ) ) $fp['align'] = '';
  661. if ( !isset( $fp['alt'] ) ) $fp['alt'] = '';
  662. # Backward compatibility, title used to always be equal to alt text
  663. if ( !isset( $fp['title'] ) ) $fp['title'] = $fp['alt'];
  664. $prefix = $postfix = '';
  665. if ( 'center' == $fp['align'] ) {
  666. $prefix = '<div class="center">';
  667. $postfix = '</div>';
  668. $fp['align'] = 'none';
  669. }
  670. if ( $file && !isset( $hp['width'] ) ) {
  671. $hp['width'] = $file->getWidth( $page );
  672. if( isset( $fp['thumbnail'] ) || isset( $fp['framed'] ) || isset( $fp['frameless'] ) || !$hp['width'] ) {
  673. $wopt = $wgUser->getOption( 'thumbsize' );
  674. if( !isset( $wgThumbLimits[$wopt] ) ) {
  675. $wopt = User::getDefaultOption( 'thumbsize' );
  676. }
  677. // Reduce width for upright images when parameter 'upright' is used
  678. if ( isset( $fp['upright'] ) && $fp['upright'] == 0 ) {
  679. $fp['upright'] = $wgThumbUpright;
  680. }
  681. // Use width which is smaller: real image width or user preference width
  682. // For caching health: If width scaled down due to upright parameter, round to full __0 pixel to avoid the creation of a lot of odd thumbs
  683. $prefWidth = isset( $fp['upright'] ) ?
  684. round( $wgThumbLimits[$wopt] * $fp['upright'], -1 ) :
  685. $wgThumbLimits[$wopt];
  686. if ( $hp['width'] <= 0 || $prefWidth < $hp['width'] ) {
  687. $hp['width'] = $prefWidth;
  688. }
  689. }
  690. }
  691. if ( isset( $fp['thumbnail'] ) || isset( $fp['manualthumb'] ) || isset( $fp['framed'] ) ) {
  692. # Create a thumbnail. Alignment depends on language
  693. # writing direction, # right aligned for left-to-right-
  694. # languages ("Western languages"), left-aligned
  695. # for right-to-left-languages ("Semitic languages")
  696. #
  697. # If thumbnail width has not been provided, it is set
  698. # to the default user option as specified in Language*.php
  699. if ( $fp['align'] == '' ) {
  700. $fp['align'] = $wgContLang->isRTL() ? 'left' : 'right';
  701. }
  702. return $prefix.$this->makeThumbLink2( $title, $file, $fp, $hp, $time, $query ).$postfix;
  703. }
  704. if ( $file && isset( $fp['frameless'] ) ) {
  705. $srcWidth = $file->getWidth( $page );
  706. # For "frameless" option: do not present an image bigger than the source (for bitmap-style images)
  707. # This is the same behaviour as the "thumb" option does it already.
  708. if ( $srcWidth && !$file->mustRender() && $hp['width'] > $srcWidth ) {
  709. $hp['width'] = $srcWidth;
  710. }
  711. }
  712. if ( $file && $hp['width'] ) {
  713. # Create a resized image, without the additional thumbnail features
  714. $thumb = $file->transform( $hp );
  715. } else {
  716. $thumb = false;
  717. }
  718. if ( !$thumb ) {
  719. $s = $this->makeBrokenImageLinkObj( $title, '', '', '', '', $time==true );
  720. } else {
  721. $params = array(
  722. 'alt' => $fp['alt'],
  723. 'title' => $fp['title'],
  724. 'valign' => isset( $fp['valign'] ) ? $fp['valign'] : false ,
  725. 'img-class' => isset( $fp['border'] ) ? 'thumbborder' : false );
  726. if ( !empty( $fp['link-url'] ) ) {
  727. $params['custom-url-link'] = $fp['link-url'];
  728. } elseif ( !empty( $fp['link-title'] ) ) {
  729. $params['custom-title-link'] = $fp['link-title'];
  730. } elseif ( !empty( $fp['no-link'] ) ) {
  731. // No link
  732. } else {
  733. $params['desc-link'] = true;
  734. $params['desc-query'] = $query;
  735. }
  736. $s = $thumb->toHtml( $params );
  737. }
  738. if ( '' != $fp['align'] ) {
  739. $s = "<div class=\"float{$fp['align']}\">{$s}</div>";
  740. }
  741. return str_replace("\n", ' ',$prefix.$s.$postfix);
  742. }
  743. /**
  744. * Make HTML for a thumbnail including image, border and caption
  745. * @param Title $title
  746. * @param File $file File object or false if it doesn't exist
  747. */
  748. function makeThumbLinkObj( Title $title, $file, $label = '', $alt, $align = 'right', $params = array(), $framed=false , $manualthumb = "" ) {
  749. $frameParams = array(
  750. 'alt' => $alt,
  751. 'caption' => $label,
  752. 'align' => $align
  753. );
  754. if ( $framed ) $frameParams['framed'] = true;
  755. if ( $manualthumb ) $frameParams['manualthumb'] = $manualthumb;
  756. return $this->makeThumbLink2( $title, $file, $frameParams, $params );
  757. }
  758. function makeThumbLink2( Title $title, $file, $frameParams = array(), $handlerParams = array(), $time = false, $query = "" ) {
  759. global $wgStylePath, $wgContLang;
  760. $exists = $file && $file->exists();
  761. # Shortcuts
  762. $fp =& $frameParams;
  763. $hp =& $handlerParams;
  764. $page = isset( $hp['page'] ) ? $hp['page'] : false;
  765. if ( !isset( $fp['align'] ) ) $fp['align'] = 'right';
  766. if ( !isset( $fp['alt'] ) ) $fp['alt'] = '';
  767. # Backward compatibility, title used to always be equal to alt text
  768. if ( !isset( $fp['title'] ) ) $fp['title'] = $fp['alt'];
  769. if ( !isset( $fp['caption'] ) ) $fp['caption'] = '';
  770. if ( empty( $hp['width'] ) ) {
  771. // Reduce width for upright images when parameter 'upright' is used
  772. $hp['width'] = isset( $fp['upright'] ) ? 130 : 180;
  773. }
  774. $thumb = false;
  775. if ( !$exists ) {
  776. $outerWidth = $hp['width'] + 2;
  777. } else {
  778. if ( isset( $fp['manualthumb'] ) ) {
  779. # Use manually specified thumbnail
  780. $manual_title = Title::makeTitleSafe( NS_FILE, $fp['manualthumb'] );
  781. if( $manual_title ) {
  782. $manual_img = wfFindFile( $manual_title );
  783. if ( $manual_img ) {
  784. $thumb = $manual_img->getUnscaledThumb();
  785. } else {
  786. $exists = false;
  787. }
  788. }
  789. } elseif ( isset( $fp['framed'] ) ) {
  790. // Use image dimensions, don't scale
  791. $thumb = $file->getUnscaledThumb( $page );
  792. } else {
  793. # Do not present an image bigger than the source, for bitmap-style images
  794. # This is a hack to maintain compatibility with arbitrary pre-1.10 behaviour
  795. $srcWidth = $file->getWidth( $page );
  796. if ( $srcWidth && !$file->mustRender() && $hp['width'] > $srcWidth ) {
  797. $hp['width'] = $srcWidth;
  798. }
  799. $thumb = $file->transform( $hp );
  800. }
  801. if ( $thumb ) {
  802. $outerWidth = $thumb->getWidth() + 2;
  803. } else {
  804. $outerWidth = $hp['width'] + 2;
  805. }
  806. }
  807. # ThumbnailImage::toHtml() already adds page= onto the end of DjVu URLs
  808. # So we don't need to pass it here in $query. However, the URL for the
  809. # zoom icon still needs it, so we make a unique query for it. See bug 14771
  810. $url = $title->getLocalURL( $query );
  811. if( $page ) {
  812. $url = wfAppendQuery( $url, 'page=' . urlencode( $page ) );
  813. }
  814. $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
  815. $s = "<div class=\"thumb t{$fp['align']}\"><div class=\"thumbinner\" style=\"width:{$outerWidth}px;\">";
  816. if( !$exists ) {
  817. $s .= $this->makeBrokenImageLinkObj( $title, '', '', '', '', $time==true );
  818. $zoomicon = '';
  819. } elseif ( !$thumb ) {
  820. $s .= htmlspecialchars( wfMsg( 'thumbnail_error', '' ) );
  821. $zoomicon = '';
  822. } else {
  823. $s .= $thumb->toHtml( array(
  824. 'alt' => $fp['alt'],
  825. 'title' => $fp['title'],
  826. 'img-class' => 'thumbimage',
  827. 'desc-link' => true,
  828. 'desc-query' => $query ) );
  829. if ( isset( $fp['framed'] ) ) {
  830. $zoomicon="";
  831. } else {
  832. $zoomicon = '<div class="magnify">'.
  833. '<a href="'.$url.'" class="internal" title="'.$more.'">'.
  834. '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
  835. 'width="15" height="11" alt="" /></a></div>';
  836. }
  837. }
  838. $s .= ' <div class="thumbcaption">'.$zoomicon.$fp['caption']."</div></div></div>";
  839. return str_replace("\n", ' ', $s);
  840. }
  841. /**
  842. * Make a "broken" link to an image
  843. *
  844. * @param Title $title Image title
  845. * @param string $text Link label
  846. * @param string $query Query string
  847. * @param string $trail Link trail
  848. * @param string $prefix Link prefix
  849. * @param bool $time, a file of a certain timestamp was requested
  850. * @return string
  851. */
  852. public function makeBrokenImageLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '', $time = false ) {
  853. global $wgEnableUploads;
  854. if( $title instanceof Title ) {
  855. wfProfileIn( __METHOD__ );
  856. $currentExists = $time ? ( wfFindFile( $title ) != false ) : false;
  857. if( $wgEnableUploads && !$currentExists ) {
  858. $upload = SpecialPage::getTitleFor( 'Upload' );
  859. if( $text == '' )
  860. $text = htmlspecialchars( $title->getPrefixedText() );
  861. $redir = RepoGroup::singleton()->getLocalRepo()->checkRedirect( $title );
  862. if( $redir ) {
  863. return $this->makeKnownLinkObj( $title, $text, $query, $trail, $prefix );
  864. }
  865. $q = 'wpDestFile=' . $title->getPartialUrl();
  866. if( $query != '' )
  867. $q .= '&' . $query;
  868. list( $inside, $trail ) = self::splitTrail( $trail );
  869. $style = $this->getInternalLinkAttributesObj( $title, $text, 'new' );
  870. wfProfileOut( __METHOD__ );
  871. return '<a href="' . $upload->escapeLocalUrl( $q ) . '"'
  872. . $style . '>' . $prefix . $text . $inside . '</a>' . $trail;
  873. } else {
  874. wfProfileOut( __METHOD__ );
  875. return $this->makeKnownLinkObj( $title, $text, $query, $trail, $prefix );
  876. }
  877. } else {
  878. return "<!-- ERROR -->{$prefix}{$text}{$trail}";
  879. }
  880. }
  881. /** @deprecated use Linker::makeMediaLinkObj() */
  882. function makeMediaLink( $name, $unused = '', $text = '', $time = false ) {
  883. $nt = Title::makeTitleSafe( NS_FILE, $name );
  884. return $this->makeMediaLinkObj( $nt, $text, $time );
  885. }
  886. /**
  887. * Create a direct link to a given uploaded file.
  888. *
  889. * @param $title Title object.
  890. * @param $text String: pre-sanitized HTML
  891. * @param $time string: time image was created
  892. * @return string HTML
  893. *
  894. * @public
  895. * @todo Handle invalid or missing images better.
  896. */
  897. function makeMediaLinkObj( $title, $text = '', $time = false ) {
  898. if( is_null( $title ) ) {
  899. ### HOTFIX. Instead of breaking, return empty string.
  900. return $text;
  901. } else {
  902. $img = wfFindFile( $title, $time );
  903. if( $img ) {
  904. $url = $img->getURL();
  905. $class = 'internal';
  906. } else {
  907. $upload = SpecialPage::getTitleFor( 'Upload' );
  908. $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $title->getDBkey() ) );
  909. $class = 'new';
  910. }
  911. $alt = htmlspecialchars( $title->getText() );
  912. if( $text == '' ) {
  913. $text = $alt;
  914. }
  915. $u = htmlspecialchars( $url );
  916. return "<a href=\"{$u}\" class=\"$class\" title=\"{$alt}\">{$text}</a>";
  917. }
  918. }
  919. /** @todo document */
  920. function specialLink( $name, $key = '' ) {
  921. global $wgContLang;
  922. if ( '' == $key ) { $key = strtolower( $name ); }
  923. $pn = $wgContLang->ucfirst( $name );
  924. return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
  925. wfMsg( $key ) );
  926. }
  927. /**
  928. * Make an external link
  929. * @param String $url URL to link to
  930. * @param String $text text of link
  931. * @param boolean $escape Do we escape the link text?
  932. * @param String $linktype Type of external link. Gets added to the classes
  933. * @param array $attribs Array of extra attributes to <a>
  934. *
  935. * @TODO! @FIXME! This is a really crappy implementation. $linktype and
  936. * 'external' are mashed into the class attrib for the link (which is made
  937. * into a string). Then, if we've got additional params in $attribs, we
  938. * add to it. People using this might want to change the classes (or other
  939. * default link attributes), but passing $attribsText is just messy. Would
  940. * make a lot more sense to make put the classes into $attribs, let the
  941. * hook play with them, *then* expand it all at once.
  942. */
  943. function makeExternalLink( $url, $text, $escape = true, $linktype = '', $attribs = array() ) {
  944. $attribsText = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
  945. $url = htmlspecialchars( $url );
  946. if( $escape ) {
  947. $text = htmlspecialchars( $text );
  948. }
  949. $link = '';
  950. $success = wfRunHooks('LinkerMakeExternalLink', array( &$url, &$text, &$link, &$attribs, $linktype ) );
  951. if(!$success) {
  952. wfDebug("Hook LinkerMakeExternalLink changed the output of link with url {$url} and text {$text} to {$link}\n", true);
  953. return $link;
  954. }
  955. if ( $attribs ) {
  956. $attribsText .= Xml::expandAttributes( $attribs );
  957. }
  958. return '<a href="'.$url.'"'.$attribsText.'>'.$text.'</a>';
  959. }
  960. /**
  961. * Make user link (or user contributions for unregistered users)
  962. * @param $userId Integer: user id in database.
  963. * @param $userText String: user name in database
  964. * @return string HTML fragment
  965. * @private
  966. */
  967. function userLink( $userId, $userText ) {
  968. if( $userId == 0 ) {
  969. $page = SpecialPage::getTitleFor( 'Contributions', $userText );
  970. } else {
  971. $page = Title::makeTitle( NS_USER, $userText );
  972. }
  973. return $this->link( $page, htmlspecialchars( $userText ), array( 'class' => 'mw-userlink' ) );
  974. }
  975. /**
  976. * Generate standard user tool links (talk, contributions, block link, etc.)
  977. *
  978. * @param int $userId User identifier
  979. * @param string $userText User name or IP address
  980. * @param bool $redContribsWhenNoEdits Should the contributions link be red if the user has no edits?
  981. * @param int $flags Customisation flags (e.g. self::TOOL_LINKS_NOBLOCK)
  982. * @param int $edits, user edit count (optional, for performance)
  983. * @return string
  984. */
  985. public function userToolLinks( $userId, $userText, $redContribsWhenNoEdits = false, $flags = 0, $edits=null ) {
  986. global $wgUser, $wgDisableAnonTalk, $wgSysopUserBans, $wgLang;
  987. $talkable = !( $wgDisableAnonTalk && 0 == $userId );
  988. $blockable = ( $wgSysopUserBans || 0 == $userId ) && !$flags & self::TOOL_LINKS_NOBLOCK;
  989. $items = array();
  990. if( $talkable ) {
  991. $items[] = $this->userTalkLink( $userId, $userText );
  992. }
  993. if( $userId ) {
  994. // check if the user has an edit
  995. $attribs = array();
  996. if( $redContribsWhenNoEdits ) {
  997. $count = !is_null($edits) ? $edits : User::edits( $userId );
  998. if( $count == 0 ) {
  999. $attribs['class'] = 'new';
  1000. }
  1001. }
  1002. $contribsPage = SpecialPage::getTitleFor( 'Contributions', $userText );
  1003. $items[] = $this->link( $contribsPage, wfMsgHtml( 'contribslink' ), $attribs );
  1004. }
  1005. if( $blockable && $wgUser->isAllowed( 'block' ) ) {
  1006. $items[] = $this->blockLink( $userId, $userText );
  1007. }
  1008. if( $items ) {
  1009. return ' <span class="mw-usertoollinks">(' . $wgLang->pipeList( $items ) . ')</span>';
  1010. } else {
  1011. return '';
  1012. }
  1013. }
  1014. /**
  1015. * Alias for userToolLinks( $userId, $userText, true );
  1016. * @param int $userId User identifier
  1017. * @param string $userText User name or IP address
  1018. * @param int $edits, user edit count (optional, for performance)
  1019. */
  1020. public function userToolLinksRedContribs( $userId, $userText, $edits=null ) {
  1021. return $this->userToolLinks( $userId, $userText, true, 0, $edits );
  1022. }
  1023. /**
  1024. * @param $userId Integer: user id in database.
  1025. * @param $userText String: user name in database.
  1026. * @return string HTML fragment with user talk link
  1027. * @private
  1028. */
  1029. function userTalkLink( $userId, $userText ) {
  1030. $userTalkPage = Title::makeTitle( NS_USER_TALK, $userText );
  1031. $userTalkLink = $this->link( $userTalkPage, wfMsgHtml( 'talkpagelinktext' ) );
  1032. return $userTalkLink;
  1033. }
  1034. /**
  1035. * @param $userId Integer: userid
  1036. * @param $userText String: user name in database.
  1037. * @return string HTML fragment with block link
  1038. * @private
  1039. */
  1040. function blockLink( $userId, $userText ) {
  1041. $blockPage = SpecialPage::getTitleFor( 'Blockip', $userText );
  1042. $blockLink = $this->link( $blockPage, wfMsgHtml( 'blocklink' ) );
  1043. return $blockLink;
  1044. }
  1045. /**
  1046. * Generate a user link if the current user is allowed to view it
  1047. * @param $rev Revision object.
  1048. * @param $isPublic, bool, show only if all users can see it
  1049. * @return string HTML
  1050. */
  1051. function revUserLink( $rev, $isPublic = false ) {
  1052. if( $rev->isDeleted( Revision::DELETED_USER ) && $isPublic ) {
  1053. $link = wfMsgHtml( 'rev-deleted-user' );
  1054. } else if( $rev->userCan( Revision::DELETED_USER ) ) {
  1055. $link = $this->userLink( $rev->getUser( Revision::FOR_THIS_USER ),
  1056. $rev->getUserText( Revision::FOR_THIS_USER ) );
  1057. } else {
  1058. $link = wfMsgHtml( 'rev-deleted-user' );
  1059. }
  1060. if( $rev->isDeleted( Revision::DELETED_USER ) ) {
  1061. return '<span class="history-deleted">' . $link . '</span>';
  1062. }
  1063. return $link;
  1064. }
  1065. /**
  1066. * Generate a user tool link cluster if the current user is allowed to view it
  1067. * @param $rev Revision object.
  1068. * @param $isPublic, bool, show only if all users can see it
  1069. * @return string HTML
  1070. */
  1071. function revUserTools( $rev, $isPublic = false ) {
  1072. if( $rev->isDeleted( Revision::DELETED_USER ) && $isPublic ) {
  1073. $link = wfMsgHtml( 'rev-deleted-user' );
  1074. } else if( $rev->userCan( Revision::DELETED_USER ) ) {
  1075. $userId = $rev->getUser( Revision::FOR_THIS_USER );
  1076. $userText = $rev->getUserText( Revision::FOR_THIS_USER );
  1077. $link = $this->userLink( $userId, $userText ) .
  1078. ' ' . $this->userToolLinks( $userId, $userText );
  1079. } else {
  1080. $link = wfMsgHtml( 'rev-deleted-user' );
  1081. }
  1082. if( $rev->isDeleted( Revision::DELETED_USER ) ) {
  1083. return ' <span class="history-deleted">' . $link . '</span>';
  1084. }
  1085. return $link;
  1086. }
  1087. /**
  1088. * This function is called by all recent changes variants, by the page history,
  1089. * and by the user contributions list. It is responsible for formatting edit
  1090. * comments. It escapes any HTML in the comment, but adds some CSS to format
  1091. * auto-generated comments (from section editing) and formats [[wikilinks]].
  1092. *
  1093. * @author Erik Moeller <moeller@scireview.de>
  1094. *
  1095. * Note: there's not always a title to pass to this function.
  1096. * Since you can't set a default parameter for a reference, I've turned it
  1097. * temporarily to a value pass. Should be adjusted further. --brion
  1098. *
  1099. * @param string $comment
  1100. * @param mixed $title Title object (to generate link to the section in autocomment) or null
  1101. * @param bool $local Whether section links should refer to local page
  1102. */
  1103. function formatComment($comment, $title = NULL, $local = false) {
  1104. wfProfileIn( __METHOD__ );
  1105. # Sanitize text a bit:
  1106. $comment = str_replace( "\n", " ", $comment );
  1107. # Allow HTML entities (for bug 13815)
  1108. $comment = Sanitizer::escapeHtmlAllowEntities( $comment );
  1109. # Render autocomments and make links:
  1110. $comment = $this->formatAutoComments( $comment, $title, $local );
  1111. $comment = $this->formatLinksInComment( $comment );
  1112. wfProfileOut( __METHOD__ );
  1113. return $comment;
  1114. }
  1115. /**
  1116. * The pattern for autogen comments is / * foo * /, which makes for
  1117. * some nasty regex.
  1118. * We look for all comments, match any text before and after the comment,
  1119. * add a separator where needed and format the comment itself with CSS
  1120. * Called by Linker::formatComment.
  1121. *
  1122. * @param string $comment Comment text
  1123. * @param object $title An optional title object used to links to sections
  1124. * @return string $comment formatted comment
  1125. *
  1126. * @todo Document the $local parameter.
  1127. */
  1128. private function formatAutocomments( $comment, $title = null, $local = false ) {
  1129. // Bah!
  1130. $this->autocommentTitle = $title;
  1131. $this->autocommentLocal = $local;
  1132. $comment = preg_replace_callback(
  1133. '!(.*)/\*\s*(.*?)\s*\*/(.*)!',
  1134. array( $this, 'formatAutocommentsCallback' ),
  1135. $comment );
  1136. unset( $this->autocommentTitle );
  1137. unset( $this->autocommentLocal );
  1138. return $comment;
  1139. }
  1140. private function formatAutocommentsCallback( $match ) {
  1141. $title = $this->autocommentTitle;
  1142. $local = $this->autocommentLocal;
  1143. $pre=$match[1];
  1144. $auto=$match[2];
  1145. $post=$match[3];
  1146. $link='';
  1147. if( $title ) {
  1148. $section = $auto;
  1149. # Generate a valid anchor name from the section title.
  1150. # Hackish, but should generally work - we strip wiki
  1151. # syntax, including the magic [[: that is used to
  1152. # "link rather than show" in case of images and
  1153. # interlanguage links.
  1154. $section = str_replace( '[[:', '', $section );
  1155. $section = str_replace( '[[', '', $section );
  1156. $section = str_replace( ']]', '', $section );
  1157. if ( $local ) {
  1158. $sectionTitle = Title::newFromText( '#' . $section );
  1159. } else {
  1160. $sectionTitle = Title::makeTitleSafe( $title->getNamespace(),
  1161. $title->getDBkey(), $section );
  1162. }
  1163. if ( $sectionTitle ) {
  1164. $link = $this->link( $sectionTitle,
  1165. wfMsgForContent( 'sectionlink' ), array(), array(),
  1166. 'noclasses' );
  1167. } else {
  1168. $link = '';
  1169. }
  1170. }
  1171. $auto = "$link$auto";
  1172. if( $pre ) {
  1173. # written summary $presep autocomment (summary /* section */)
  1174. $auto = wfMsgExt( 'autocomment-prefix', array( 'escapenoentities', 'content' ) ) . $auto;
  1175. }
  1176. if( $post ) {
  1177. # autocomment $postsep written summary (/* section */ summary)
  1178. $auto .= wfMsgExt( 'colon-separator', array( 'escapenoentities', 'content' ) );
  1179. }
  1180. $auto = '<span class="autocomment">' . $auto . '</span>';
  1181. $comment = $pre . $auto . $post;
  1182. return $comment;
  1183. }
  1184. /**
  1185. * Formats wiki links and media links in text; all other wiki formatting
  1186. * is ignored
  1187. *
  1188. * @fixme doesn't handle sub-links as in image thumb texts like the main parser
  1189. * @param string $comment Text to format links in
  1190. * @return string
  1191. */
  1192. public function formatLinksInComment( $comment ) {
  1193. return preg_replace_callback(
  1194. '/\[\[:?(.*?)(\|(.*?))*\]\]([^[]*)/',
  1195. array( $this, 'formatLinksInCommentCallback' ),
  1196. $comment );
  1197. }
  1198. protected function formatLinksInCommentCallback( $match ) {
  1199. global $wgContLang;
  1200. $medians = '(?:' . preg_quote( MWNamespace::getCanonicalName( NS_MEDIA ), '/' ) . '|';
  1201. $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA ), '/' ) . '):';
  1202. $comment = $match[0];
  1203. # fix up urlencoded title texts (copied from Parser::replaceInternalLinks)
  1204. if( strpos( $match[1], '%' ) !== false ) {
  1205. $match[1] = str_replace( array('<', '>'), array('&lt;', '&gt;'), urldecode($match[1]) );
  1206. }
  1207. # Handle link renaming [[foo|text]] will show link as "text"
  1208. if( "" != $match[3] ) {
  1209. $text = $match[3];
  1210. } else {
  1211. $text = $match[1];
  1212. }
  1213. $submatch = array();
  1214. if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
  1215. # Media link; trail not supported.
  1216. $linkRegexp = '/\[\[(.*?)\]\]/';
  1217. $thelink = $this->makeMediaLink( $submatch[1], "", $text );
  1218. } else {
  1219. # Other kind of link
  1220. if( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
  1221. $trail = $submatch[1];
  1222. } else {
  1223. $trail = "";
  1224. }
  1225. $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
  1226. if (isset($match[1][0]) && $match[1][0] == ':')
  1227. $match[1] = substr($match[1], 1);
  1228. $thelink = $this->makeLink( $match[1], $text, "", $trail );
  1229. }
  1230. $comment = preg_replace( $linkRegexp, StringUtils::escapeRegexReplacement( $thelink ), $comment, 1 );
  1231. return $comment;
  1232. }
  1233. /**
  1234. * Wrap a comment in standard punctuation and formatting if
  1235. * it's non-empty, otherwise return empty string.
  1236. *
  1237. * @param string $comment
  1238. * @param mixed $title Title object (to generate link to section in autocomment) or null
  1239. * @param bool $local Whether section links should refer to local page
  1240. *
  1241. * @return string
  1242. */
  1243. function commentBlock( $comment, $title = NULL, $local = false ) {
  1244. // '*' used to be the comment inserted by the software way back
  1245. // in antiquity in case none was provided, here for backwards
  1246. // compatability, acc. to brion -ævar
  1247. if( $comment == '' || $comment == '*' ) {
  1248. return '';
  1249. } else {
  1250. $formatted = $this->formatComment( $comment, $title, $local );
  1251. return " <span class=\"comment\">($formatted)</span>";
  1252. }
  1253. }
  1254. /**
  1255. * Wrap and format the given revision's comment block, if the current
  1256. * user is allowed to view it.
  1257. *
  1258. * @param Revision $rev
  1259. * @param bool $local Whether section links should refer to local page
  1260. * @param $isPublic, show only if all users can see it
  1261. * @return string HTML
  1262. */
  1263. function revComment( Revision $rev, $local = false, $isPublic = false ) {
  1264. if( $rev->isDeleted( Revision::DELETED_COMMENT ) && $isPublic ) {
  1265. $block = " <span class=\"comment\">" . wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
  1266. } else if( $rev->userCan( Revision::DELETED_COMMENT ) ) {
  1267. $block = $this->commentBlock( $rev->getComment( Revision::FOR_THIS_USER ),
  1268. $rev->getTitle(), $local );
  1269. } else {
  1270. $block = " <span class=\"comment\">" . wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
  1271. }
  1272. if( $rev->isDeleted( Revision::DELETED_COMMENT ) ) {
  1273. return " <span class=\"history-deleted\">$block</span>";
  1274. }
  1275. return $block;
  1276. }
  1277. public function formatRevisionSize( $size ) {
  1278. if ( $size == 0 ) {
  1279. $stxt = wfMsgExt( 'historyempty', 'parsemag' );
  1280. } else {
  1281. global $wgLang;
  1282. $stxt = wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $size ) );
  1283. $stxt = "($stxt)";
  1284. }
  1285. $stxt = htmlspecialchars( $stxt );
  1286. return "<span class=\"history-size\">$stxt</span>";
  1287. }
  1288. /** @todo document */
  1289. function tocIndent() {
  1290. return "\n<ul>";
  1291. }
  1292. /** @todo document */
  1293. function tocUnindent($level) {
  1294. return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ? $level : 0 );
  1295. }
  1296. /**
  1297. * parameter level defines if we are on an indentation level
  1298. */
  1299. function tocLine( $anchor, $tocline, $tocnumber, $level ) {
  1300. return "\n<li class=\"toclevel-$level\"><a href=\"#" .
  1301. $anchor . '"><span class="tocnumber">' .
  1302. $tocnumber . '</span> <span class="toctext">' .
  1303. $tocline . '</span></a>';
  1304. }
  1305. /** @todo document */
  1306. function tocLineEnd() {
  1307. return "</li>\n";
  1308. }
  1309. /** @todo document */
  1310. function tocList($toc) {
  1311. global $wgJsMimeType;
  1312. $title = wfMsgHtml('toc') ;
  1313. return
  1314. '<table id="toc" class="toc" summary="' . $title .'"><tr><td>'
  1315. . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
  1316. . $toc
  1317. # no trailing newline, script should not be wrapped in a
  1318. # paragraph
  1319. . "</ul>\n</td></tr></table>"
  1320. . '<script type="' . $wgJsMimeType . '">'
  1321. . ' if (window.showTocToggle) {'
  1322. . ' var tocShowText = "' . Xml::escapeJsString( wfMsg('showtoc') ) . '";'
  1323. . ' var tocHideText = "' . Xml::escapeJsString( wfMsg('hidetoc') ) . '";'
  1324. . ' showTocToggle();'
  1325. . ' } '
  1326. . "</script>\n";
  1327. }
  1328. /**
  1329. * Used to generate section edit links that point to "other" pages
  1330. * (sections that are really part of included pages).
  1331. *
  1332. * @param $title Title string.
  1333. * @param $section Integer: section number.
  1334. */
  1335. public function editSectionLinkForOther( $title, $section ) {
  1336. wfDeprecated( __METHOD__ );
  1337. $title = Title::newFromText( $title );
  1338. return $this->doEditSectionLink( $title, $section );
  1339. }
  1340. /**
  1341. * @param $nt Title object.
  1342. * @param $section Integer: section number.
  1343. * @param $hint Link String: title, or default if omitted or empty
  1344. */
  1345. public function editSectionLink( Title $nt, $section, $hint = '' ) {
  1346. wfDeprecated( __METHOD__ );
  1347. if( $hint === '' ) {
  1348. # No way to pass an actual empty $hint here! The new interface al-
  1349. # lows this, so we have to do this for compatibility.
  1350. $hint = null;
  1351. }
  1352. return $this->doEditSectionLink( $nt, $section, $hint );
  1353. }
  1354. /**
  1355. * Create a section edit link. This supersedes editSectionLink() and
  1356. * editSectionLinkForOther().
  1357. *
  1358. * @param $nt Title The title being linked to (may not be the same as
  1359. * $wgTitle, if the section is included from a template)
  1360. * @param $section string The designation of the section being pointed to,
  1361. * to be included in the link, like "&section=$section"
  1362. * @param $tooltip string The tooltip to use for the link: will be escaped
  1363. * and wrapped in the 'editsectionhint' message
  1364. * @return string HTML to use for edit link
  1365. */
  1366. public function doEditSectionLink( Title $nt, $section, $tooltip = null ) {
  1367. $attribs = array();
  1368. if( !is_null( $tooltip ) ) {
  1369. $attribs['title'] = wfMsg( 'editsectionhint', $tooltip );
  1370. }
  1371. $link = $this->link( $nt, wfMsg('editsection'),
  1372. $attribs,
  1373. array( 'action' => 'edit', 'section' => $section ),
  1374. array( 'noclasses', 'known' )
  1375. );
  1376. # Run the old hook. This takes up half of the function . . . hopefully
  1377. # we can rid of it someday.
  1378. $attribs = '';
  1379. if( $tooltip ) {
  1380. $attribs = wfMsgHtml( 'editsectionhint', htmlspecialchars( $tooltip ) );
  1381. $attribs = " title=\"$attribs\"";
  1382. }
  1383. $result = null;
  1384. wfRunHooks( 'EditSectionLink', array( &$this, $nt, $section, $attribs, $link, &$result ) );
  1385. if( !is_null( $result ) ) {
  1386. # For reverse compatibility, add the brackets *after* the hook is
  1387. # run, and even add them to hook-provided text. (This is the main
  1388. # reason that the EditSectionLink hook is deprecated in favor of
  1389. # DoEditSectionLink: it can't change the brackets or the span.)
  1390. $result = wfMsgHtml( 'editsection-brackets', $result );
  1391. return "<span class=\"editsection\">$result</span>";
  1392. }
  1393. # Add the brackets and the span, and *then* run the nice new hook, with
  1394. # clean and non-redundant arguments.
  1395. $result = wfMsgHtml( 'editsection-brackets', $link );
  1396. $result = "<span class=\"editsection\">$result</span>";
  1397. wfRunHooks( 'DoEditSectionLink', array( $this, $nt, $section, $tooltip, &$result ) );
  1398. return $result;
  1399. }
  1400. /**
  1401. * Create a headline for content
  1402. *
  1403. * @param int $level The level of the headline (1-6)
  1404. * @param string $attribs Any attributes for the headline, starting with a space and ending with '>'
  1405. * This *must* be at least '>' for no attribs
  1406. * @param string $anchor The anchor to give the headline (the bit after the #)
  1407. * @param string $text The text of the header
  1408. * @param string $link HTML to add for the section edit link
  1409. * @param mixed $legacyAnchor A second, optional anchor to give for
  1410. * backward compatibility (false to omit)
  1411. *
  1412. * @return string HTML headline
  1413. */
  1414. public function makeHeadline( $level, $attribs, $anchor, $text, $link, $legacyAnchor = false ) {
  1415. $ret = "<a name=\"$anchor\" id=\"$anchor\"></a>"
  1416. . "<h$level$attribs"
  1417. . $link
  1418. . " <span class=\"mw-headline\">$text</span>"
  1419. . "</h$level>";
  1420. if ( $legacyAnchor !== false ) {
  1421. $ret = "<a name=\"$legacyAnchor\" id=\"$legacyAnchor\"></a>$ret";
  1422. }
  1423. return $ret;
  1424. }
  1425. /**
  1426. * Split a link trail, return the "inside" portion and the remainder of the trail
  1427. * as a two-element array
  1428. *
  1429. * @static
  1430. */
  1431. static function splitTrail( $trail ) {
  1432. static $regex = false;
  1433. if ( $regex === false ) {
  1434. global $wgContLang;
  1435. $regex = $wgContLang->linkTrail();
  1436. }
  1437. $inside = '';
  1438. if ( '' != $trail ) {
  1439. $m = array();
  1440. if ( preg_match( $regex, $trail, $m ) ) {
  1441. $inside = $m[1];
  1442. $trail = $m[2];
  1443. }
  1444. }
  1445. return array( $inside, $trail );
  1446. }
  1447. /**
  1448. * Generate a rollback link for a given revision. Currently it's the
  1449. * caller's responsibility to ensure that the revision is the top one. If
  1450. * it's not, of course, the user will get an error message.
  1451. *
  1452. * If the calling page is called with the parameter &bot=1, all rollback
  1453. * links also get that parameter. It causes the edit itself and the rollback
  1454. * to be marked as "bot" edits. Bot edits are hidden by default from recent
  1455. * changes, so this allows sysops to combat a busy vandal without bothering
  1456. * other users.
  1457. *
  1458. * @param Revision $rev
  1459. */
  1460. function generateRollback( $rev ) {
  1461. return '<span class="mw-rollback-link">['
  1462. . $this->buildRollbackLink( $rev )
  1463. . ']</span>';
  1464. }
  1465. /**
  1466. * Build a raw rollback link, useful for collections of "tool" links
  1467. *
  1468. * @param Revision $rev
  1469. * @return string
  1470. */
  1471. public function buildRollbackLink( $rev ) {
  1472. global $wgRequest, $wgUser;
  1473. $title = $rev->getTitle();
  1474. $query = array(
  1475. 'action' => 'rollback',
  1476. 'from' => $rev->getUserText()
  1477. );
  1478. if( $wgRequest->getBool( 'bot' ) ) {
  1479. $query['bot'] = '1';
  1480. $query['hidediff'] = '1'; // bug 15999
  1481. }
  1482. $query['token'] = $wgUser->editToken( array( $title->getPrefixedText(),
  1483. $rev->getUserText() ) );
  1484. return $this->link( $title, wfMsgHtml( 'rollbacklink' ),
  1485. array( 'title' => wfMsg( 'tooltip-rollback' ) ),
  1486. $query, array( 'known', 'noclasses' ) );
  1487. }
  1488. /**
  1489. * Returns HTML for the "templates used on this page" list.
  1490. *
  1491. * @param array $templates Array of templates from Article::getUsedTemplate
  1492. * or similar
  1493. * @param bool $preview Whether this is for a preview
  1494. * @param bool $section Whether this is for a section edit
  1495. * @return string HTML output
  1496. */
  1497. public function formatTemplates( $templates, $preview = false, $section = false ) {
  1498. wfProfileIn( __METHOD__ );
  1499. $outText = '';
  1500. if ( count( $templates ) > 0 ) {
  1501. # Do a batch existence check
  1502. $batch = new LinkBatch;
  1503. foreach( $templates as $title ) {
  1504. $batch->addObj( $title );
  1505. }
  1506. $batch->execute();
  1507. # Construct the HTML
  1508. $outText = '<div class="mw-templatesUsedExplanation">';
  1509. if ( $preview ) {
  1510. $outText .= wfMsgExt( 'templatesusedpreview', array( 'parse' ) );
  1511. } elseif ( $section ) {
  1512. $outText .= wfMsgExt( 'templatesusedsection', array( 'parse' ) );
  1513. } else {
  1514. $outText .= wfMsgExt( 'templatesused', array( 'parse' ) );
  1515. }
  1516. $outText .= "</div><ul>\n";
  1517. usort( $templates, array( 'Title', 'compare' ) );
  1518. foreach ( $templates as $titleObj ) {
  1519. $r = $titleObj->getRestrictions( 'edit' );
  1520. if ( in_array( 'sysop', $r ) ) {
  1521. $protected = wfMsgExt( 'template-protected', array( 'parseinline' ) );
  1522. } elseif ( in_array( 'autoconfirmed', $r ) ) {
  1523. $protected = wfMsgExt( 'template-semiprotected', array( 'parseinline' ) );
  1524. } else {
  1525. $protected = '';
  1526. }
  1527. if( $titleObj->quickUserCan( 'edit' ) ) {
  1528. $editLink = $this->makeLinkObj( $titleObj, wfMsg('editlink'), 'action=edit' );
  1529. } else {
  1530. $editLink = $this->makeLinkObj( $titleObj, wfMsg('viewsourcelink'), 'action=edit' );
  1531. }
  1532. $outText .= '<li>' . $this->link( $titleObj ) . ' (' . $editLink . ') ' . $protected . '</li>';
  1533. }
  1534. $outText .= '</ul>';
  1535. }
  1536. wfProfileOut( __METHOD__ );
  1537. return $outText;
  1538. }
  1539. /**
  1540. * Returns HTML for the "hidden categories on this page" list.
  1541. *
  1542. * @param array $hiddencats Array of hidden categories from Article::getHiddenCategories
  1543. * or similar
  1544. * @return string HTML output
  1545. */
  1546. public function formatHiddenCategories( $hiddencats ) {
  1547. global $wgLang;
  1548. wfProfileIn( __METHOD__ );
  1549. $outText = '';
  1550. if ( count( $hiddencats ) > 0 ) {
  1551. # Construct the HTML
  1552. $outText = '<div class="mw-hiddenCategoriesExplanation">';
  1553. $outText .= wfMsgExt( 'hiddencategories', array( 'parse' ), $wgLang->formatnum( count( $hiddencats ) ) );
  1554. $outText .= "</div><ul>\n";
  1555. foreach ( $hiddencats as $titleObj ) {
  1556. $outText .= '<li>' . $this->link( $titleObj, null, array(), array(), 'known' ) . "</li>\n"; # If it's hidden, it must exist - no need to check with a LinkBatch
  1557. }
  1558. $outText .= '</ul>';
  1559. }
  1560. wfProfileOut( __METHOD__ );
  1561. return $outText;
  1562. }
  1563. /**
  1564. * Format a size in bytes for output, using an appropriate
  1565. * unit (B, KB, MB or GB) according to the magnitude in question
  1566. *
  1567. * @param $size Size to format
  1568. * @return string
  1569. */
  1570. public function formatSize( $size ) {
  1571. global $wgLang;
  1572. return htmlspecialchars( $wgLang->formatSize( $size ) );
  1573. }
  1574. /**
  1575. * @deprecated Returns raw bits of HTML, use titleAttrib() and accesskey()
  1576. */
  1577. public function tooltipAndAccesskey( $name ) {
  1578. # FIXME: If Sanitizer::expandAttributes() treated "false" as "output
  1579. # no attribute" instead of "output '' as value for attribute", this
  1580. # would be three lines.
  1581. $attribs = array(
  1582. 'title' => $this->titleAttrib( $name, 'withaccess' ),
  1583. 'accesskey' => $this->accesskey( $name )
  1584. );
  1585. if ( $attribs['title'] === false ) {
  1586. unset( $attribs['title'] );
  1587. }
  1588. if ( $attribs['accesskey'] === false ) {
  1589. unset( $attribs['accesskey'] );
  1590. }
  1591. return Xml::expandAttributes( $attribs );
  1592. }
  1593. /** @deprecated Returns raw bits of HTML, use titleAttrib() */
  1594. public function tooltip( $name, $options = null ) {
  1595. # FIXME: If Sanitizer::expandAttributes() treated "false" as "output
  1596. # no attribute" instead of "output '' as value for attribute", this
  1597. # would be two lines.
  1598. $tooltip = $this->titleAttrib( $name, $options );
  1599. if ( $tooltip === false ) {
  1600. return '';
  1601. }
  1602. return Xml::expandAttributes( array(
  1603. 'title' => $this->titleAttrib( $name, $options )
  1604. ) );
  1605. }
  1606. /**
  1607. * Given the id of an interface element, constructs the appropriate title
  1608. * attribute from the system messages. (Note, this is usually the id but
  1609. * isn't always, because sometimes the accesskey needs to go on a different
  1610. * element than the id, for reverse-compatibility, etc.)
  1611. *
  1612. * @param string $name Id of the element, minus prefixes.
  1613. * @param mixed $options null or the string 'withaccess' to add an access-
  1614. * key hint
  1615. * @return string Contents of the title attribute (which you must HTML-
  1616. * escape), or false for no title attribute
  1617. */
  1618. public function titleAttrib( $name, $options = null ) {
  1619. wfProfileIn( __METHOD__ );
  1620. $tooltip = wfMsg( "tooltip-$name" );
  1621. # Compatibility: formerly some tooltips had [alt-.] hardcoded
  1622. $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
  1623. # Message equal to '-' means suppress it.
  1624. if ( wfEmptyMsg( "tooltip-$name", $tooltip ) || $tooltip == '-' ) {
  1625. $tooltip = false;
  1626. }
  1627. if ( $options == 'withaccess' ) {
  1628. $accesskey = $this->accesskey( $name );
  1629. if( $accesskey !== false ) {
  1630. if ( $tooltip === false || $tooltip === '' ) {
  1631. $tooltip = "[$accesskey]";
  1632. } else {
  1633. $tooltip .= " [$accesskey]";
  1634. }
  1635. }
  1636. }
  1637. wfProfileOut( __METHOD__ );
  1638. return $tooltip;
  1639. }
  1640. /**
  1641. * Given the id of an interface element, constructs the appropriate
  1642. * accesskey attribute from the system messages. (Note, this is usually
  1643. * the id but isn't always, because sometimes the accesskey needs to go on
  1644. * a different element than the id, for reverse-compatibility, etc.)
  1645. *
  1646. * @param string $name Id of the element, minus prefixes.
  1647. * @return string Contents of the accesskey attribute (which you must HTML-
  1648. * escape), or false for no accesskey attribute
  1649. */
  1650. public function accesskey( $name ) {
  1651. wfProfileIn( __METHOD__ );
  1652. $accesskey = wfMsg( "accesskey-$name" );
  1653. # FIXME: Per standard MW behavior, a value of '-' means to suppress the
  1654. # attribute, but this is broken for accesskey: that might be a useful
  1655. # value.
  1656. if( $accesskey != '' && $accesskey != '-' && !wfEmptyMsg( "accesskey-$name", $accesskey ) ) {
  1657. wfProfileOut( __METHOD__ );
  1658. return $accesskey;
  1659. }
  1660. wfProfileOut( __METHOD__ );
  1661. return false;
  1662. }
  1663. /**
  1664. * Creates a (show/hide) link for deleting revisions/log entries
  1665. *
  1666. * @param array $query Query parameters to be passed to link()
  1667. * @param bool $restricted Set to true to use a <strong> instead of a <span>
  1668. *
  1669. * @return string HTML <a> link to Special:Revisiondelete, wrapped in a
  1670. * span to allow for customization of appearance with CSS
  1671. */
  1672. public function revDeleteLink( $query = array(), $restricted = false ) {
  1673. $sp = SpecialPage::getTitleFor( 'Revisiondelete' );
  1674. $text = wfMsgHtml( 'rev-delundel' );
  1675. $tag = $restricted ? 'strong' : 'span';
  1676. $link = $this->link( $sp, $text, array(), $query, array( 'known', 'noclasses' ) );
  1677. return Xml::tags( $tag, array( 'class' => 'mw-revdelundel-link' ), "($link)" );
  1678. }
  1679. }