Hook examples

From Linkwiki
tbd @

Background

It is possible to use Hooks to change the behavior of MediaWiki to some degree. On this page you can find examples which were or are in use at Wiki Aventurica.

Examples

KI bots are scanning every single page, including all possible diffs. If this is degrading performance, anonymous users can be blocked from looking at diffs.

Code was taken from Handling web crawlers.

##########################
# no diffs for anonymous #
##########################

$wgHooks['MediaWikiPerformAction'][] = function ( $output, $article, $title, $user, $request, $wiki ) {
	if ( $request->getVal( 'diff' ) && !$user->isRegistered() ) {
		$output->addWikiTextAsInterface( "'''You must be [[Special:Login|logged in]] to view page diffs.'''" );
		$output->setStatusCode( 403 );
		return false;
	}
};

Extension:ContactPage adds Special:Contact to the wiki. This adds a link to this special page to the footer.

##################################
# link to contact page in footer #
##################################

$wgHooks['SkinAddFooterLinks'][] = function( Skin $skin, string $key, array &$footerlinks ) {
    if ( $key === 'places' ) {
        $footerlinks['contact'] = Html::element( 'a',
            [
                'href' => "https://your.webpage.com/wiki/Special:Contact",  // URL to "Special:Contact"
                'rel' => 'noreferrer noopener'  // not required, but recommended for security reasons
            ],
        $skin->msg( 'contactpage-title' )->text()
        );
    };
};
#################################
# add tag nofollow to red links #
#################################
function noFollowRedLinks($linkRenderer, $target, $isKnown, &$text, &$attribs, &$ret) {
    if (!$isKnown && preg_match('/\bnew\b/S', $attribs['class'] ?? "")) {
        $attribs['rel'] = 'nofollow';
    }
    return true;
}
$wgHooks['HtmlPageLinkRendererEnd'][] = 'noFollowRedLinks';

This should probably be done using Manual:$wgNamespaceRobotPolicies und Manual:$wgDefaultRobotPolicy.

#################################################
# add tag noarchive on every page #
#################################################

function noArchive( &$out, &$sk ) {
	$out->addMeta("robots", "noarchive");
	return true;
}
$wgHooks['BeforePageDisplay'][] = 'noArchive';

Extension:Lockdown enables tying the access to special pages to user groups.

Code was taken from Manual:Special pages.

#############################
# disable some special pages #
#############################

function disableSomeSpecialPages(&$list) {
  foreach (array('Fewestrevisions','Withoutinterwiki','Unwatchedpages','Shortpages','Mostinterwikis','Mostlinked','Mostlinkedtemplates') as $i) {
    unset($list[$i]);
  }
  return true;
}
$wgHooks['SpecialPage_initList'][]='disableSomeSpecialPages';

This adds "delete" links to the headline of grouped changes.

################################################
# add delete links to recentchanges for sysops #
################################################

function LoschLink( $test, &$links, $block ) {
  global $wgUser;
  if ( $wgUser->isAllowed('delete') ) {
                $block0 = $block[0];
                $params['action'] = 'delete';
                $links['delete'] = Linker::linkKnown(
                					$block0->getTitle(),
                					'Löschen',
                					array(),
                					$params
                				);
        }
  return true;
}

$wgHooks['EnhancedChangesList::getLogText'][] = 'LoschLink';
#########################################################################################
# add "edit" and "what links here" links to recentchanges, plus delete links for sysops #
#########################################################################################

function LetzteLinks( $test, &$articlelink, &$s, &$rc, $unpatrolled, $watched ) {
  $params['action'] = 'edit';
  $articlelink .= ' (';
  $articlelink .= Linker::linkKnown(
  	$rc->getTitle(),
  	'e',
  	array(),
  	$params
  );
  $articlelink .= ' ';
  $articlelink .= wfMessage( 'pipe-separator' )->text();
  $articlelink .= ' ';
  $articlelink .= Linker::linkKnown(
  	SpecialPage::getTitleFor( 'Whatlinkshere', $rc->getTitle() ),
  	'wlh',
  	array(),
  	array()
  );
  global $wgUser;
  if ( $wgUser->isAllowed('delete') ) {
    $articlelink .= ' ';
    $articlelink .= wfMessage( 'pipe-separator' )->text();
    $articlelink .= ' ';
    $params['action'] = 'delete';
		$articlelink .= Linker::linkKnown(
			$rc->getTitle(),
			'del',
			array(),
			$params
		);
  }
  $articlelink .= ')';
  return true;
}
$wgHooks['ChangesListInsertArticleLink'][] = 'LetzteLinks';

Adding Checkbox to Special:CreateAccount

Extension:NewSignupPage also adds a checkbox to Special:CreateAccount.

hooks
Manual:Hooks/UserCreateForm, Manual:Hooks/AbortNewAccount
thanks to
JaXtOr
##################################################
# create checkbox for protection of data privacy #
##################################################

function DatenschutzCheck( &$template ) {
  global $wgRequest;

  $template->addInputItem(
    'wpDatenschutz',
    $wgRequest->getCheck( 'wpDatenschutz' ),
    'checkbox',
    'waDatenschutz'
  );

  return true;
}
$wgHooks['UserCreateForm'][] = 'DatenschutzCheck';


function DatenschutzChecked( $user, &$message ) {
    if ($user->getRequest()->getCheck('wpDatenschutz')) {
      return true;
    }
    
    $message = wfMessage( 'waDatenschutz-warning' )->parse();
    return false;
}
$wgHooks['AbortNewAccount'][] = 'DatenschutzChecked';