Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SEO tweaks #491

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@
},
"UserGetRightsRemove": {
"handler": "MirahezeMagicHooks"
},
"HtmlPageLinkRendererEnd": {
"handler": "MirahezeMagicHooks"
},
"BeforePageDisplay": {
colleirose marked this conversation as resolved.
Show resolved Hide resolved
"handler": "MirahezeMagicHooks"
}
},
"HookHandlers": {
Expand Down
126 changes: 114 additions & 12 deletions includes/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Miraheze\MirahezeMagic;

use Article;
use DeferredUpdates;
use ExtensionRegistry;
// Built-in MediaWiki hooks
use MediaWiki\Cache\Hook\MessageCacheFetchOverridesHook;
use MediaWiki\CommentStore\CommentStore;
use MediaWiki\Config\Config;
Expand All @@ -29,14 +27,10 @@
use MediaWiki\Page\Hook\ArticleViewHeaderHook;
use MediaWiki\Permissions\Hook\TitleReadWhitelistHook;
use MediaWiki\Permissions\Hook\UserGetRightsRemoveHook;
use MediaWiki\Shell\Shell;
use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\Status\Status;
use MediaWiki\Title\Title;
use MediaWiki\User\User;
use MediaWiki\WikiMap\WikiMap;
use Memcached;
use MessageCache;
use MediaWiki\Hook\BeforePageDisplayHook;
use MediaWiki\Linker\Hook\HtmlPageLinkRendererEndHook;

// Hooks from Miraheze extensions
use Miraheze\CreateWiki\Hooks\CreateWikiDeletionHook;
use Miraheze\CreateWiki\Hooks\CreateWikiReadPersistentModelHook;
use Miraheze\CreateWiki\Hooks\CreateWikiRenameHook;
Expand All @@ -46,6 +40,19 @@
use Miraheze\ImportDump\Hooks\ImportDumpJobAfterImportHook;
use Miraheze\ImportDump\Hooks\ImportDumpJobGetFileHook;
use Miraheze\ManageWiki\Helpers\ManageWikiSettings;

// Built-in MediaWiki imports that aren't hooks
use MediaWiki\Shell\Shell;
use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\Status\Status;
use MediaWiki\Title\Title;
use MediaWiki\User\User;
use MediaWiki\WikiMap\WikiMap;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\Output\OutputPage;
use Memcached;
use MessageCache;
use MobileContext;
use ParserOutput;
use Redis;
Expand All @@ -54,6 +61,9 @@
use Throwable;
use Wikimedia\IPUtils;
use Wikimedia\Rdbms\ILBFactory;
use Article;
colleirose marked this conversation as resolved.
Show resolved Hide resolved
use DeferredUpdates;
use ExtensionRegistry;
colleirose marked this conversation as resolved.
Show resolved Hide resolved

class Hooks implements
AbuseFilterShouldFilterActionHook,
Expand All @@ -75,7 +85,9 @@ class Hooks implements
SiteNoticeAfterHook,
SkinAddFooterLinksHook,
TitleReadWhitelistHook,
UserGetRightsRemoveHook
UserGetRightsRemoveHook,
HtmlPageLinkRendererEndHook,
BeforePageDisplayHook
colleirose marked this conversation as resolved.
Show resolved Hide resolved
{

colleirose marked this conversation as resolved.
Show resolved Hide resolved
/** @var ServiceOptions */
Expand Down Expand Up @@ -108,6 +120,94 @@ public function __construct(
$this->httpRequestFactory = $httpRequestFactory;
}

/**
* Prevent following redlinks for SEO purposes. This does not remove redlinks or make any other visual changes to them.
*
* @see https://github.com/marohh/mediawikiRemoveRedlinks/blob/master/includes/RemoveRedlinks.php
*/
colleirose marked this conversation as resolved.
Show resolved Hide resolved
public function onHtmlPageLinkRendererEnd(
$linkRenderer,
$target,
$isKnown,
&$text,
&$attribs,
&$ret
) {
if ( $isKnown || $target->isExternal() ) {
return true;
}

$attribs['rel'] = 'nofollow';

return true;
}
colleirose marked this conversation as resolved.
Show resolved Hide resolved

/**
* Add noindex to some pages for SEO purposes. Indexing pages that are not valuable to searchers is bad for SEO, so we'll reduce the indexing of such pages.
*
* @see https://ahrefs.com/blog/content-pruning/
* @see https://gitlab.com/hydrawiki/extensions/seo/-/blob/master/SEOHooks.php?ref_type=heads
colleirose marked this conversation as resolved.
Show resolved Hide resolved
*/
public function onBeforePageDisplay( $out, $skin ) : void {
$noIndexNamespaces = [
-1, // Special
15, // Category talk
colleirose marked this conversation as resolved.
Show resolved Hide resolved
8, // MediaWiki
9, // MediaWiki talk
2, // User
colleirose marked this conversation as resolved.
Show resolved Hide resolved
3 // User talk
];

if ( self::isRequestInBlacklist( $out->getRequest()->getValues() ) ||
in_array( $out->getTitle()->getNamespace(), $noIndexNamespaces )
) {
$out->setRobotPolicy( 'noindex,nofollow' );
}
}

/**
* Check a blacklist of URL parameters and values to see if we should add a noindex meta tag
*
* @see https://gitlab.com/hydrawiki/extensions/seo/-/blob/master/SEOHooks.php?ref_type=heads
*
* @param array $paramsAndValues URL Parameters and Values
*
colleirose marked this conversation as resolved.
Show resolved Hide resolved
* @return boolean
colleirose marked this conversation as resolved.
Show resolved Hide resolved
*/
private static function isRequestInBlacklist($paramsAndValues) {
colleirose marked this conversation as resolved.
Show resolved Hide resolved
$blockedURLParamKeys = [
colleirose marked this conversation as resolved.
Show resolved Hide resolved
'curid', 'diff', 'from', 'group', 'mobileaction', 'oldid',
'printable', 'profile', 'redirect', 'redlink', 'stableid'
];

$blockedURLParamKeyValuePairs = [
'action' => [
'delete', 'edit', 'history', 'info',
'pagevalues', 'purge', 'visualeditor', 'watch'
],
'feed' => ['rss'],
colleirose marked this conversation as resolved.
Show resolved Hide resolved
'limit' => ['500'],
BlankEclair marked this conversation as resolved.
Show resolved Hide resolved
'title' => [
'Category:Noindexed_pages',
'Category:Noindexed pages',
'Category:Noindexed%20pages'
],
'veaction' => ['edit']
];

foreach ($paramsAndValues as $key => $value) {
colleirose marked this conversation as resolved.
Show resolved Hide resolved
if (in_array($key, $blockedURLParamKeys)) {
return true;
}

if (isset($blockedURLParamKeyValuePairs[$key]) && in_array($value, $blockedURLParamKeyValuePairs[$key])) {
colleirose marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
}

return false;
}

/**
* @param Config $mainConfig
* @param CommentStore $commentStore
Expand Down Expand Up @@ -148,6 +248,8 @@ public static function factory(
);
}



colleirose marked this conversation as resolved.
Show resolved Hide resolved
/**
* Avoid filtering automatic account creation
*
Expand Down