Files
AnatImageViewer/includes/AnnotationHooks.php
2026-07-21 09:18:16 -07:00

100 lines
3.5 KiB
PHP

<?php
namespace MediaWiki\Extension\AnatImageViewer;
use Parser;
use ImagePage;
use MediaWiki\MediaWikiServices;
use MediaWiki\Title\Title;
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"
NEWAN;
}
return true;
}
/**
* MWHook: Add text to page after image links created
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ImagePageAfterImageLinks
*
* @param ImagePage $imagePage
* @param string $html
* @return bool|void True or no return value to continue or false to abort
*/
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;
}
$linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
$title = Title::newFromText( "Annotation:" . $imName );
$mwLink = $linkRenderer->makeLink( $title );
$html = $newText . $mwLink . $html;
}
/**
* 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;
}
}