]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - extensions/Cite/includes/ApiQueryReferences.php
MediaWiki 1.30.2-scripts
[autoinstallsdev/mediawiki.git] / extensions / Cite / includes / ApiQueryReferences.php
1 <?php
2 /**
3  * Expose reference information for a page via prop=references API.
4  *
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.
9  *
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.
14  *
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
19  *
20  * @file
21  * @see https://www.mediawiki.org/wiki/Extension:Cite#API
22  */
23
24 class ApiQueryReferences extends ApiQueryBase {
25
26         public function __construct( $query, $moduleName ) {
27                 parent::__construct( $query, $moduleName, 'rf' );
28         }
29
30         public function getAllowedParams() {
31                 return [
32                    'continue' => [
33                            ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
34                    ],
35                 ];
36         }
37
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' );
43                         } else {
44                                 $this->dieUsage( 'Cite extension reference storage is not enabled', 'citestoragedisabled' );
45                         }
46                 }
47                 $params = $this->extractRequestParams();
48                 $titles = $this->getPageSet()->getGoodTitles();
49                 ksort( $titles );
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'] );
54                 } else {
55                         $startId = false;
56                 }
57
58                 foreach ( $titles as $pageId => $title ) {
59                         // Skip until you have the correct starting point
60                         if ( $startId !== false && $startId !== $pageId ) {
61                                 continue;
62                         } else {
63                                 $startId = false;
64                         }
65                         $storedRefs = Cite::getStoredReferences( $title );
66                         $allReferences = [];
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 ) {
73                                                         $ref['name'] = $name;
74                                                         $key = $ref['key'];
75                                                         if ( is_string( $name ) ) {
76                                                                 $id = Cite::getReferencesKey( $name . '-' . $key );
77                                                         } else {
78                                                                 $id = Cite::getReferencesKey( $key );
79                                                         }
80                                                         $ref['group'] = $group;
81                                                         $ref['reflist'] = $index;
82                                                         $allReferences[$id] = $ref;
83                                                 }
84                                         }
85                                 }
86                         }
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 );
91                         if ( !$fit ) {
92                                 $this->setContinueEnumParameter( 'continue', $pageId );
93                                 break;
94                         }
95                 }
96         }
97
98         public function getCacheMode( $params ) {
99                 return 'public';
100         }
101
102         /**
103          * @see ApiBase::getExamplesMessages()
104          */
105         protected function getExamplesMessages() {
106                 return [
107                         'action=query&prop=references&titles=Albert%20Einstein' =>
108                                 'apihelp-query+references-example-1',
109                 ];
110         }
111
112 }