3 * Expose reference information for a page via prop=references API.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @see https://www.mediawiki.org/wiki/Extension:Cite#API
24 class ApiQueryReferences extends ApiQueryBase {
26 public function __construct( $query, $moduleName ) {
27 parent::__construct( $query, $moduleName, 'rf' );
30 public function getAllowedParams() {
33 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
38 public function execute() {
39 $config = ConfigFactory::getDefaultInstance()->makeConfig( 'cite' );
40 if ( !$config->get( 'CiteStoreReferencesData' ) ) {
41 if ( is_callable( [ $this, 'dieWithError' ] ) ) {
42 $this->dieWithError( 'apierror-citestoragedisabled' );
44 $this->dieUsage( 'Cite extension reference storage is not enabled', 'citestoragedisabled' );
47 $params = $this->extractRequestParams();
48 $titles = $this->getPageSet()->getGoodTitles();
50 if ( !is_null( $params['continue'] ) ) {
51 $startId = (int)$params['continue'];
52 // check it is definitely an int
53 $this->dieContinueUsageIf( strval( $startId ) !== $params['continue'] );
58 foreach ( $titles as $pageId => $title ) {
59 // Skip until you have the correct starting point
60 if ( $startId !== false && $startId !== $pageId ) {
65 $storedRefs = Cite::getStoredReferences( $title );
67 // some pages may not have references stored
68 if ( $storedRefs !== false ) {
69 // a page can have multiple <references> tags but they all have unique keys
70 foreach ( $storedRefs['refs'] as $index => $grouping ) {
71 foreach ( $grouping as $group => $members ) {
72 foreach ( $members as $name => $ref ) {
75 if ( is_string( $name ) ) {
76 $id = Cite::getReferencesKey( $name . '-' . $key );
78 $id = Cite::getReferencesKey( $key );
80 $ref['group'] = $group;
81 $ref['reflist'] = $index;
82 $allReferences[$id] = $ref;
87 // set some metadata since its an assoc data structure
88 ApiResult::setArrayType( $allReferences, 'kvp', 'id' );
89 // Ship a data representation of the combined references.
90 $fit = $this->addPageSubItems( $pageId, $allReferences );
92 $this->setContinueEnumParameter( 'continue', $pageId );
98 public function getCacheMode( $params ) {
103 * @see ApiBase::getExamplesMessages()
105 protected function getExamplesMessages() {
107 'action=query&prop=references&titles=Albert%20Einstein' =>
108 'apihelp-query+references-example-1',