Topic on Project:Support desk

Adding a CSS class to category page links containing a specific string

1
Gaon12 (talkcontribs)

Hello MediaWiki community,

I am trying to add a CSS class to links on category pages that contain a specific string (e.g., '@@blur'). I have created a custom MediaWiki extension and tried using several hooks such as onParserBeforeInternalParse, onCategoryPageView, and onBeforePageDisplay. However, I am still unable to successfully add the desired class to the links on the category page.

Here's the code I have tried so far:

class BlurLinksHooks {
  public static function onParserBeforeInternalParse( &$parser, &$text, &$strip_state ) {
    $text = preg_replace_callback('/\[\[(.+?)@@blur\]\]/', function($matches) {
      $linkTitle = $matches[1];
      return "[[$linkTitle|<span class='blur-link'>$linkTitle</span>]]";
    }, $text);
    return true;
  }

  public static function onCategoryPageView(array &$links, CategoryViewer $categoryViewer) {
    $members = $categoryViewer->getMembers();
    foreach ($members as $member) {
        $title = $member->getTitle();
        $link = $title->getFullURL();
        if (strpos($link, '@@blur') !== false) {
            $current_classes = explode(' ', $link['attribs']['class']);
            if (!in_array('blur-link', $current_classes)) {
                $link['attribs']['class'] .= ' blur-link';
            }
            $link = str_replace('@@blur', '', $link);
        }
    }
    return true;
}

  public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) {
    $out->addModules( 'ext.blurLinks' );
  }
}

It seems like the @@blur string is being removed before I can check for its existence in the category page links. Any suggestions or guidance on how to properly implement this feature would be greatly appreciated. Are there any other hooks that I should be using for this task?

Thank you in advance for your help!