Add annotation link to file pages

Signed-off-by: Justin Georgi <justin.georgi@gmail.com>
This commit is contained in:
2026-07-10 15:58:41 -07:00
parent 39d486633a
commit f1fb8e6fef
2 changed files with 47 additions and 2 deletions

View File

@@ -34,6 +34,7 @@
}, },
"Hooks": { "Hooks": {
"AlternateEditPreview": "MediaWiki\\Extension\\AnatImageViewer\\AnnotationHooks::onAlternateEditPreview", "AlternateEditPreview": "MediaWiki\\Extension\\AnatImageViewer\\AnnotationHooks::onAlternateEditPreview",
"ParserAfterTidy": "MediaWiki\\Extension\\AnatImageViewer\\AnnotationHooks::onParserAfterTidy",
"EditFormPreloadText": "MediaWiki\\Extension\\AnatImageViewer\\AnnotationHooks::onEditFormPreloadText" "EditFormPreloadText": "MediaWiki\\Extension\\AnatImageViewer\\AnnotationHooks::onEditFormPreloadText"
}, },
"ContentHandlers": { "ContentHandlers": {

View File

@@ -1,6 +1,8 @@
<?php <?php
namespace MediaWiki\Extension\AnatImageViewer; namespace MediaWiki\Extension\AnatImageViewer;
use Parser;
class AnnotationHooks { class AnnotationHooks {
/** /**
@@ -23,13 +25,23 @@ class AnnotationHooks {
return true; return true;
} }
/**
* MWHook: Add preloaded text to editor on page creation
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/EditFormPreloadText
*
* @param string $text editor text
* @param Title $Title title of the created page
* @return bool|void True or no return value to continue or false to abort
*/
public static function onEditFormPreloadText( &$text, &$title ) { public static function onEditFormPreloadText( &$text, &$title ) {
if ( $title->getNamespace() === NS_AV_ANNOT ) { if ( $title->getNamespace() === NS_AV_ANNOT ) {
$titleText = $title->getBaseText();
$text = <<<NEWAN $text = <<<NEWAN
baseImage = "" baseImage = "$titleText.jpg"
[view] [view]
direction = "" direction = "L"
# [annotation.1] # [annotation.1]
# position = # position =
@@ -40,6 +52,38 @@ class AnnotationHooks {
return true; return true;
} }
/**
* MWHook: Add text to page after parsing
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
*
* @param Parser $parser wikitext parser that was created the text
* @param string $text generated text
* @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();
$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;
}
}
}
/** /**
* Small helper function to display information on the browser console * Small helper function to display information on the browser console
* *