Fix general File page annotation links

Signed-off-by: Justin Georgi <justin.georgi@gmail.com>
This commit is contained in:
2026-07-14 08:19:54 -07:00
parent f1fb8e6fef
commit 5d0b6371e1
2 changed files with 26 additions and 26 deletions

View File

@@ -34,8 +34,8 @@
},
"Hooks": {
"AlternateEditPreview": "MediaWiki\\Extension\\AnatImageViewer\\AnnotationHooks::onAlternateEditPreview",
"ParserAfterTidy": "MediaWiki\\Extension\\AnatImageViewer\\AnnotationHooks::onParserAfterTidy",
"EditFormPreloadText": "MediaWiki\\Extension\\AnatImageViewer\\AnnotationHooks::onEditFormPreloadText"
"EditFormPreloadText": "MediaWiki\\Extension\\AnatImageViewer\\AnnotationHooks::onEditFormPreloadText",
"ImagePageAfterImageLinks": "MediaWiki\\Extension\\AnatImageViewer\\AnnotationHooks::onImagePageAfterImageLinks"
},
"ContentHandlers": {
"annotation": "MediaWiki\\Extension\\AnatImageViewer\\AnnotationHandler"

View File

@@ -2,6 +2,9 @@
namespace MediaWiki\Extension\AnatImageViewer;
use Parser;
use ImagePage;
use MediaWiki\MediaWikiServices;
use MediaWiki\Title\Title;
class AnnotationHooks {
@@ -53,35 +56,32 @@ class AnnotationHooks {
}
/**
* MWHook: Add text to page after parsing
* MWHook: Add text to page after image links created
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ImagePageAfterImageLinks
*
* @param Parser $parser wikitext parser that was created the text
* @param string $text generated text
* @param ImagePage $imagePage
* @param string $html
* @return bool|void True or no return value to continue or false to abort
*/
public static function onParserAfterTidy( Parser $parser, &$text ) {
$title = $parser->getTitle();
if ($title->getNamespace() === NS_FILE) {
$titleText = $title->getBaseText();
public static function onImagePageAfterImageLinks( ImagePage $imagePage, &$html) {
//TODO: Skip this if this is already svg annotation
$newText = <<<ANHEAD
<h2 id="fileannotate">Annotated File</h2>
Click below to create or edit an annotated version of this image.<br/>
ANHEAD;
$titleText = $imagePage->getFile()->getTitle()->getBaseText();
$extPos = strpos($titleText,'.');
if ($extPos !== false) {
$imName = substr($titleText, 0, $extPos);
} else {
$imName = $titleText;
}
$findLoc = strpos($text, "<h3 data-mw-anchor");
if ($findLoc !== false) {
$mwLink = "[[Annotation:" . $imName . "]]";
$htmlLink = $parser->recursiveTagParseFully($mwLink);
$newText = <<<ANHEAD
<h2 id="fileannotate">Annotated File</h2>
Click below to create or edit an annotated version of this image.<br/>
ANHEAD;
$text = $text . $newText . $htmlLink;
}
}
$linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
$title = Title::newFromText( "Annotation:" . $imName );
$mwLink = $linkRenderer->makeLink( $title );
$html = $newText . $mwLink . $html;
}
/**