31 Октябрь 2012 г. 21:34:25 пользователь Brion Vibber
(brion(a)pobox.com) написал:
On Wed, Oct 31, 2012 at 6:53 AM, Daniel Barrett <danb(a)vistaprint.com> wrote:
Is there any easy way (via extension) to modify
the "src" attribute of
images on wiki pages?
I see hooks for modifying href values - LinkBegin and LinkEnd. But I
don't see something similar for images, whose URLs seem to be produced via
File::getUrl().
Purpose: I want to add a querystring parameter onto the ends of the URLs,
turning <img src="foo.jpg"/> into <img
src="foo.jpg?timestamp=xxxxxxxxxxx"/> to aid with caching.
There's not a great hook point for this I think, but you could probably add
one in.
Custom hooks are quite hard to get into "master" repository, unless one
is a Wikimedia programmer. I had one custom hook in EditPage, used by my extension;
however it wasn't possible to include it into core.
ThumbnailImage::toHtml() in
includes/media/MediaTransformOutput.php is the
function that actually produces the <img> tag. You could probably stash
something there, or on the constructor for the class, to modify the URL.
I use the following trick to get thumbnail html (albeit with surrounding
div's)
if ( $wgParserConf['class'] !== 'Parser' ) {
die( 'Extension:PrettyPhoto cannot override non-default Parser' );
}
$wgParserConf['class'] = 'PrettyPhotoParser';
class PrettyPhotoParser extends Parser {
function makeImage( $title, $options, $holders = false ) {
return PrettyPhoto::apply( parent::makeImage( $title, $options, $holders ) );
}
} /* end of PrettyPhotoParser class */
Where extension's own PrettyPhoto::apply() method modifies content of generated image
html via DOMDocument / xpath.
What bothers me, how much reliable parser class override is.
However, if another class "factories" were as flexible as Parser factory, one
could imagine something like this:
$wgDefaultClass['ThumbnailImage'] = 'MyThumbnailImage';
class MyThumbnailImage extends ThumbnailImage {
function toHtml( /* args */ ) {
$html = parent::toHtml( /* args */ );
...
}
}
I wish PHP has "monkey patching", so classes could be manipulated and extended
dynamically but unfortunately it does not. __get(), __set(), __call() are optional and
quite slow.
Dmitriy