Surface references via api query property

* The query request prop=references will return a JSON blob of all
references in the page
* Conveniently references are returned with an array id key that corresponds
to an anchor tag in the HTML
* When references storage is disabled the API request will trigger an
error.

Bug: T123290
Change-Id: I81a965bcb47d17df18f1e415e3c25f88f6b48ffc
This commit is contained in:
jdlrobson 2016-02-17 13:55:48 -08:00 committed by Jdlrobson
parent 342a3a0ec3
commit 509741dc17
4 changed files with 107 additions and 0 deletions

94
ApiQueryReferences.php Normal file
View file

@ -0,0 +1,94 @@
<?php
/**
* Expose reference information for a page via prop=references API.
*
* @see https://www.mediawiki.org/wiki/Extension:Cite#API
*
* @license WTFPL 2.0
*/
class ApiQueryReferences extends ApiQueryBase {
public function __construct( $query, $moduleName ) {
parent::__construct( $query, $moduleName, 'rf' );
}
public function getAllowedParams() {
return [
'continue' => [
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
],
];
}
public function execute() {
$config = ConfigFactory::getDefaultInstance()->makeConfig( 'cite' );
if ( !$config->get( 'CiteStoreReferencesData' ) ) {
$this->dieUsage( 'Cite extension reference storage is not enabled', 'citestoragedisabled' );
}
$params = $this->extractRequestParams();
$titles = $this->getPageSet()->getGoodTitles();
ksort( $titles );
if ( !is_null( $params['continue'] ) ) {
$startId = (int)$params['continue'];
// check it is definitely an int
$this->dieContinueUsageIf( strval( $startId ) !== $params['continue'] );
} else {
$startId = false;
}
foreach ( $titles as $pageId => $title ) {
// Skip until you have the correct starting point
if ( $startId !== false && $startId !== $pageId ) {
continue;
} else {
$startId = false;
}
$storedRefs = Cite::getStoredReferences( $title );
$allReferences = array();
// some pages may not have references stored
if ( $storedRefs !== false ) {
// a page can have multiple <references> tags but they all have unique keys
foreach ( $storedRefs['refs'] as $index => $grouping ) {
foreach ( $grouping as $group => $members ) {
foreach ( $members as $name => $ref ) {
$ref['name'] = $name;
$key = $ref['key'];
if ( is_string( $name ) ) {
$id = Cite::getReferencesKey( $name . '-' . $key );
} else {
$id = Cite::getReferencesKey( $key );
}
$ref['group'] = $group;
$ref['reflist'] = $index;
$allReferences[$id] = $ref;
}
}
}
}
// set some metadata since its an assoc data structure
ApiResult::setArrayType( $allReferences, 'kvp', 'id' );
// Ship a data representation of the combined references.
$fit = $this->addPageSubItems( $pageId, $allReferences );
if ( !$fit ) {
$this->setContinueEnumParameter( 'continue', $pageId );
break;
}
}
}
public function getCacheMode( $params ) {
return 'public';
}
/**
* @see ApiBase::getExamplesMessages()
*/
protected function getExamplesMessages() {
return array(
'action=query&prop=references&titles=Albert%20Einstein' =>
'apihelp-query+references-example-1',
);
}
}

View file

@ -18,6 +18,11 @@
"cite": "i18n",
"ve-cite": "modules/ve-cite/i18n"
},
"APIPropModules": {
"references": {
"class": "ApiQueryReferences"
}
},
"Hooks": {
"ParserFirstCallInit": [
"Cite::setHooks"
@ -164,6 +169,9 @@
"VisualEditorPluginModules": [
"ext.cite.visualEditor"
],
"ConfigRegistry": {
"cite": "GlobalVarConfig::newInstance"
},
"config": {
"AllowCiteGroups": true,
"CiteCacheReferences": false,
@ -171,6 +179,7 @@
"CiteCacheReferencesDataOnParse": false
},
"AutoloadClasses": {
"ApiQueryReferences": "ApiQueryReferences.php",
"Cite": "Cite_body.php",
"CiteHooks": "CiteHooks.php",
"CiteDataModule": "CiteDataModule.php",

View file

@ -15,6 +15,8 @@
"Ævar Arnfjörð Bjarmason"
]
},
"apihelp-query+references-description": "Return a data representation of references associated with the given pages.",
"apihelp-query+references-example-1": "References associated with <kbd>Albert Einstein</kbd>.",
"cite-desc": "Adds <nowiki><ref[ name=id]></nowiki> and <nowiki><references/></nowiki> tags, for citations",
"cite_error": "Cite error: $1",
"cite_error_ref_numeric_key": "Invalid <code>&lt;ref&gt;</code> tag;\nname cannot be a simple integer. Use a descriptive title",

View file

@ -15,6 +15,8 @@
"Umherirrender"
]
},
"apihelp-query+references-description": "{{doc-apihelp-description|query+references}}",
"apihelp-query+references-example-1": "{{doc-apihelp-example|query+references}}",
"cite-desc": "{{desc|name=Cite|url=https://www.mediawiki.org/wiki/Extension:Cite}}",
"cite_error": "Cite extension.\n\nUsed when there are errors in ref or references tags.\n\nParameters:\n* $1 - an error message",
"cite_error_ref_numeric_key": "Cite extension. Error message shown if the name of a ref tag only contains digits. Examples that cause this error are <code><nowiki><ref name=\"123\" /></nowiki></code> or <code><nowiki><ref name=\"456\">input</ref></nowiki></code>",