Files
AnatImageViewer/includes/AnnotationHooks.php
2026-07-10 15:58:41 -07:00

105 lines
3.6 KiB
PHP

<?php
namespace MediaWiki\Extension\AnatImageViewer;
use Parser;
class AnnotationHooks {
/**
* MWHook: Display model when model file page edits are previewed
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/AlternateEditPreview
*
* @param EditPage $editor access to information about the edit page itself
* @param Content $content access to the current content of the editor
* @param string &$previewHTML by reference access to the resultant compiled preview html
* @param ?ParserOutput &$parserOutput
* @return bool|void True to continue default processing or false to abort for custom processing
*/
public static function onAlternateEditPreview( $editor, $content, string &$previewHTML, ?ParserOutput &$parserOutput ) {
$title = $editor->getTitle();
if ( $title->getNamespace() === NS_AV_ANNOT ) {
$out = $editor->getContext()->getOutput();
$out->addModules('ext.annot.prev');
}
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 ) {
if ( $title->getNamespace() === NS_AV_ANNOT ) {
$titleText = $title->getBaseText();
$text = <<<NEWAN
baseImage = "$titleText.jpg"
[view]
direction = "L"
# [annotation.1]
# position =
# leader.1 =
# light = true
NEWAN;
}
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
*
* Usage:
* echo '<script>';
* self::console_log('logged string');
* echo '</script>';
*
* @param $data information to display
* @param bool $add_script_tags true to put information is inside complete script tag
*/
public static function console_log($data, $add_script_tags = false) {
$command = 'console.log('. json_encode($data, JSON_HEX_TAG).');';
if ($add_script_tags) {
$command = '<script>'. $command . '</script>';
}
echo $command;
}
}