r38162 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r38161‎ | r38162 | r38163 >
Date:00:08, 29 July 2008
Author:simetrical
Status:old
Tags:
Comment:
Linker.php cleanup:
* Allow makeLinkObj to accept an associative array of arguments for $aprops, so Brion's eyes can be saved from melting.
* Fail fast when various methods are passed non-Titles, don't just return some garbage and hope no one notices.
* Whitespace, wfDeprecated().
Modified paths:
  • /trunk/phase3/includes/Linker.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Linker.php
@@ -234,16 +234,10 @@
235235 * the end of the link.
236236 * @param $prefix String: optional prefix. As trail, only before instead of after.
237237 */
238 - function makeLinkObj( $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
 238+ function makeLinkObj( Title $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
239239 global $wgUser;
240240 wfProfileIn( __METHOD__ );
241241
242 - if ( !$nt instanceof Title ) {
243 - # Fail gracefully
244 - wfProfileOut( __METHOD__ );
245 - return "<!-- ERROR -->{$prefix}{$text}{$trail}";
246 - }
247 -
248242 if ( $nt->isExternal() ) {
249243 $u = $nt->getFullURL();
250244 $link = $nt->getPrefixedURL();
@@ -308,19 +302,16 @@
309303 * @param $query String: link target
310304 * @param $trail String: text after link
311305 * @param $prefix String: text before link text
312 - * @param $aprops String: extra attributes to the a-element
 306+ * @param $aprops Mixed: extra attributes to the a-element. If a string,
 307+ * inserted literally into the HTML, with a space prepended. It can also
 308+ * be an associative array. In this case the keys are attributes, and
 309+ * values are *unescaped* attribute values.
313310 * @param $style String: style to apply - if empty, use getInternalLinkAttributesObj instead
314311 * @return the a-element
315312 */
316 - function makeKnownLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
 313+ function makeKnownLinkObj( Title $title, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
317314 wfProfileIn( __METHOD__ );
318315
319 - if ( !$title instanceof Title ) {
320 - # Fail gracefully
321 - wfProfileOut( __METHOD__ );
322 - return "<!-- ERROR -->{$prefix}{$text}{$trail}";
323 - }
324 -
325316 $nt = $this->normaliseSpecialPage( $title );
326317
327318 $u = $nt->escapeLocalURL( $query );
@@ -340,7 +331,16 @@
341332 $style = $this->getInternalLinkAttributesObj( $nt, $text );
342333 }
343334
344 - if ( $aprops !== '' ) $aprops = ' ' . $aprops;
 335+ if( is_string( $aprops ) && $aprops != '' ) {
 336+ $aprops = " $aprops";
 337+ } elseif( is_array( $aprops ) ) {
 338+ $attributes = $aprops;
 339+ $aprops = '';
 340+ foreach( $attributes as $key => $value ) {
 341+ $value = htmlspecialchars( $value );
 342+ $aprops .= " $key=\"$value\"";
 343+ }
 344+ }
345345
346346 list( $inside, $trail ) = Linker::splitTrail( $trail );
347347 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
@@ -358,15 +358,9 @@
359359 * be included in the link text. Other characters will be appended after
360360 * the end of the link.
361361 */
362 - function makeBrokenLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' ) {
 362+ function makeBrokenLinkObj( Title $title, $text = '', $query = '', $trail = '', $prefix = '' ) {
363363 wfProfileIn( __METHOD__ );
364364
365 - if ( !$title instanceof Title ) {
366 - # Fail gracefully
367 - wfProfileOut( __METHOD__ );
368 - return "<!-- ERROR -->{$prefix}{$text}{$trail}";
369 - }
370 -
371365 $nt = $this->normaliseSpecialPage( $title );
372366
373367 if( $nt->getNamespace() == NS_SPECIAL ) {
@@ -406,6 +400,7 @@
407401 * the end of the link.
408402 */
409403 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
 404+ wfDeprecated( __METHOD__ );
410405 return $this->makeColouredLinkObj( $nt, 'stub', $text, $query, $trail, $prefix );
411406 }
412407
@@ -421,7 +416,6 @@
422417 * the end of the link.
423418 */
424419 function makeColouredLinkObj( $nt, $colour, $text = '', $query = '', $trail = '', $prefix = '' ) {
425 -
426420 if($colour != ''){
427421 $style = $this->getInternalLinkAttributesObj( $nt, $text, $colour );
428422 } else $style = '';
@@ -1304,21 +1298,22 @@
13051299 * @return string HTML to use for edit link
13061300 */
13071301 public function doEditSectionLink( Title $nt, $section, $tooltip='' ) {
1308 - $attribs = '';
1309 - if( $tooltip ) {
1310 - $attribs = wfMsgHtml( 'editsectionhint', htmlspecialchars( $tooltip ) );
1311 - $attribs = " title=\"$attribs\"";
1312 - }
1313 -
13141302 $url = $this->makeKnownLinkObj(
13151303 $nt,
13161304 htmlspecialchars(wfMsg('editsection')),
13171305 "action=edit&section=$section",
1318 - '', '', '', $attribs
 1306+ '', '', '',
 1307+ array( 'title' => wfMsg( 'editsectionhint', $tooltip ) )
13191308 );
13201309
1321 - # Run the old hook
 1310+ # Run the old hook. This takes up most of the function . . . hopefully
 1311+ # we can rid of it someday.
13221312 $result = null;
 1313+ $attribs = '';
 1314+ if( $tooltip ) {
 1315+ $attribs = wfMsgHtml( 'editsectionhint', htmlspecialchars( $tooltip ) );
 1316+ $attribs = " title=\"$attribs\"";
 1317+ }
13231318 wfRunHooks( 'EditSectionLink', array( &$this, $nt, $section, $attribs, $url, &$result ) );
13241319 if( !is_null( $result ) ) {
13251320 # For reverse compatibility, add the brackets *after* the hook is

Follow-up revisions

RevisionCommit summaryAuthorDate
r38163Partially revert r38162. Caused bugs due to incredibly incomprehensible para...simetrical00:35, 29 July 2008
r40496Don't fail fatally when Linker::link() is passed a non-object...simetrical15:35, 5 September 2008

Status & tagging log