Skip to content

Commit

Permalink
Merge pull request #13 from strimo378/main
Browse files Browse the repository at this point in the history
Passing Additional Parameters to the Template
  • Loading branch information
bovender authored Nov 28, 2023
2 parents b776356 + 78dad40 commit 0477178
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,33 @@ without causing superfluous download requests from Pubmed. The next time you
edit a page, you can remove the 'reload' option, and the article information
will be retrieved from cache again.

### Specifying a Custom Template Name

In addition to using the default template specified in the extension's configuration, the PubmedParser extension allows users to specify a different template name directly within the `#pmid` parser function call. This can be particularly useful if you have multiple templates for different types of articles or display formats and want to choose among them on a case-by-case basis.

To specify a custom template name, simply add a parameter prefixed by `#`. For example:

```mediawiki
{{#pmid:19782018|#anothertemplate}}
{{#pmid:19782018|Alon2009|#anothertemplate}}
```

In the example above, instead of using the default template (e.g., `pubmed`), the extension will use the `anothertemplate` template to format and display the fetched Pubmed article information. Ensure that the custom template you specify exists in your wiki. If it doesn't, a red link to the non-existent template will be displayed on the page.

Of course! Here's a suitable explanation that you can incorporate into the documentation:

### Passing Additional Parameters to the Template

The PubmedParser extension supports direct parameter passing in the form of `key=value` within the `#pmid` parser function. This feature is especially valuable when you want to provide supplementary information or customize the display of the fetched article data in the template.

For example, if you wish to provide an additional full-text URL for the article:

```mediawiki
{{#pmid:19782018|Alon2009|fulltexturl=https://example.com/fulltext.pdf}}
```

In the above syntax, the `fulltexturl` parameter with the value `https://example.com/fulltext.pdf` will be passed directly to the template. Within the template, you can then use the `{{{fulltexturl}}}` placeholder to display or link to the provided full-text URL.

## Customization

You can customize the name of the template as well as the names of the
Expand Down
11 changes: 8 additions & 3 deletions includes/PubmedParser_Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ class Core
* name MUST be prefixed with '#' (configurable in MediaWiki system
* messages).
*/
function __construct( $pmid = 0, $param1 = '', $param2 = '' ) {
function __construct( $pmid = 0, $param ) {
$this->status = PUBMEDPARSER_INVALIDPMID;
$this->template = Extension::$templateName;
$this->pmid = $pmid;
$this->apiKey = $this->getApiKey();
$this->parseParam( $param1 );
$this->parseParam( $param2 );
foreach ($param AS $p)
$this->parseParam( $p );
$this->lookUp();
}

Expand All @@ -69,6 +69,9 @@ private function parseParam( $param ) {
if ( substr( $param, 0, 1 ) === PUBMEDPARSER_TEMPLATECHAR ) {
$this->template = substr( $param, 1 );
}
elseif ( strpos( $param, '=' ) !== false ) {
$this->addargs .= '|' . $param;
}
elseif ( $param === Extension::$reload ) {
$this->reload = true;
}
Expand Down Expand Up @@ -133,6 +136,7 @@ function buildTemplate( Article $article ) {
. '|' . Extension::$abstract . '=' . $article->abstract
. '|' . Extension::$title . '=' . $article->title
. '|' . Extension::$keywords . '=' . $article->allKeywords()
. $this->addargs
. '}}';
}

Expand Down Expand Up @@ -309,6 +313,7 @@ function statusMsg() {
private $reference; ///< Optional name of a footnote reference.
private $status; ///< holds status information (0 if everything is ok)
private $template; ///< Name of the template to use.
private $addargs; ///< Additional template arguments
private static $_readDb = null;
private static $_writeDb = null;
}
Expand Down
4 changes: 2 additions & 2 deletions includes/PubmedParser_Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public static function createTable( \DatabaseUpdater $updater ) {
/**
* Static function that is hooked to the 'pmid' magic hook.
*/
public static function render( &$parser, $param1 = '', $param2 = '', $param3 = '' ) {
public static function render( &$parser, $pmid, ...$param ) {
if ( ! is_string( self::$authors ) ) self::loadMessages();
$core = new Core( $param1, $param2, $param3 );
$core = new Core( $pmid, $param );
return $core->execute();
}

Expand Down

0 comments on commit 0477178

Please sign in to comment.