Topic on Project:Support desk

[RESOLVED] Passing {{filepath:filename.ext}} to an extension

6
Henryhartley (talkcontribs)

I'm trying to write a simple extension. I want to pass it the name of an uploaded media file but I can't seem to get it to the extension. Specifically, I don't want to have to put the path to the file in the call, I'd like to just put {{filepath:filename.ext}} and have the location found for me. I've stripped the extension down to just a few lines:

<?php
$wgHooks['ParserFirstCallInit'][] = 'henryinit';
function henryinit( $parser ) {
  $parser->setHook( 'Henry', 'henryrender' );
  return true;
}
function henryrender($input, $args) {
  global $wgScriptPath;
  return "File: ".$args['file']."<br>" ;
}
?>

Then, in a page, I put the following (where GH-Scouts.mp4 is a previously uploaded video):

<henry width="400" height="300" file="{{filepath:GH-Scouts.mp4}}" />

Filepath: {{filepath:GH-Scouts.mp4}}

This returns the following:

File: {{filepath:GH-Scouts.mp4}}

Filepath: http://www.mysite.com/images/a/ad/GH-Scouts.mp4 

As you can see, the second line correctly displays the full path to the file but the first, which calls the extension, reports the file path as it was entered. I feel like I need to have my henryinit associated with a different hook but can't figure out which one. If, for instance, I use 'ParserBeforeTidy' it finds the path, but then it doesn't run my extension. Can someone point me to an example or discussion of how I can do this? Thanks.

Florianschmidtwelzow (talkcontribs)

I think you missunderstood something :) setHook() is a method to add a tag (like <example></example>), that's called a tag extensions. If you try to add a parser function, please read the documentation of it :)

Henryhartley (talkcontribs)

I'm not trying to add a parser function. I'm simply trying to use one (that already exists) in my tag extension. Is that not possible? At least that's what I think I'm trying to do.

Henryhartley (talkcontribs)

That is to say, if I put this in a page, my extension works just fine and I can do whatever I need with the video.

<henry width="400" height="300" file="/images/a/ad/GH-Scouts.mp4" />

But, I don't want the user to have to find the path to a file. I want them to be able to enter the tag like this and have that translated by the filepath parser function into the full path.

<henry width="400" height="300" file="{{filepath:GH-Scouts.mp4}}" />

But the file attribute gets passed as a string (including the curly braces) to my tag extension. I want {{filepath:GH-Scouts.mp4}} to return the path first and pass that to my extension. It seems that my tag extension is parsed first, before the parser function (filepath) has a chance to get processed. I thought making my tag get processed later would do the trick, but apparently that was wrong. Does that make sense?

Note, I also tried it like this with no better results:

<henry width="400" height="300">{{filepath:GH-Scouts.mp4}}</henry>
Florianschmidtwelzow (talkcontribs)

Content that is passed to a tag extension is never parsed, iirc. So the easiest way should be:

Let the users pass the filename ("name.mp4") to your extension and you do the following:

$file = wfFindFile( $filename );
$fileUrl = $file->getFullUrl(); // this will contain a fully qualified url to the file

Documentation: https://github.com/wikimedia/mediawiki-core/blob/61c989cfc04b98f221e5e9106018ebfed3255c39/includes/parser/CoreParserFunctions.php#L908

https://doc.wikimedia.org/mediawiki-core/master/php/html/GlobalFunctions_8php.html#a6f62cc18b743211dffaf3919d8d1dfdf

https://doc.wikimedia.org/mediawiki-core/master/php/html/classFile.html#af8c5eb22ef823da22ef335f2fdb11e30

Reply to "[RESOLVED] Passing {{filepath:filename.ext}} to an extension"