]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryAllLinks.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.git] / includes / api / ApiQueryAllLinks.php
1 <?php
2 /**
3  * API for MediaWiki 1.8+
4  *
5  * Created on July 7, 2007
6  *
7  * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  * http://www.gnu.org/copyleft/gpl.html
23  *
24  * @file
25  */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28         // Eclipse helper - will be ignored in production
29         require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33  * Query module to enumerate links from all pages together.
34  *
35  * @ingroup API
36  */
37 class ApiQueryAllLinks extends ApiQueryGeneratorBase {
38
39         public function __construct( $query, $moduleName ) {
40                 parent::__construct( $query, $moduleName, 'al' );
41         }
42
43         public function execute() {
44                 $this->run();
45         }
46
47         public function getCacheMode( $params ) {
48                 return 'public';
49         }
50
51         public function executeGenerator( $resultPageSet ) {
52                 $this->run( $resultPageSet );
53         }
54
55         private function run( $resultPageSet = null ) {
56                 $db = $this->getDB();
57                 $params = $this->extractRequestParams();
58
59                 $prop = array_flip( $params['prop'] );
60                 $fld_ids = isset( $prop['ids'] );
61                 $fld_title = isset( $prop['title'] );
62
63                 if ( $params['unique'] ) {
64                         if ( !is_null( $resultPageSet ) ) {
65                                 $this->dieUsage( $this->getModuleName() . ' cannot be used as a generator in unique links mode', 'params' );
66                         }
67                         if ( $fld_ids ) {
68                                 $this->dieUsage( $this->getModuleName() . ' cannot return corresponding page ids in unique links mode', 'params' );
69                         }
70                         $this->addOption( 'DISTINCT' );
71                 }
72
73                 $this->addTables( 'pagelinks' );
74                 $this->addWhereFld( 'pl_namespace', $params['namespace'] );
75
76                 if ( !is_null( $params['from'] ) && !is_null( $params['continue'] ) ) {
77                         $this->dieUsage( 'alcontinue and alfrom cannot be used together', 'params' );
78                 }
79                 if ( !is_null( $params['continue'] ) ) {
80                         $arr = explode( '|', $params['continue'] );
81                         if ( count( $arr ) != 2 ) {
82                                 $this->dieUsage( 'Invalid continue parameter', 'badcontinue' );
83                         }
84                         $from = $this->getDB()->strencode( $this->titleToKey( $arr[0] ) );
85                         $id = intval( $arr[1] );
86                         $this->addWhere(
87                                 "pl_title > '$from' OR " .
88                                 "(pl_title = '$from' AND " .
89                                 "pl_from > $id)"
90                         );
91                 }
92
93                 if ( !is_null( $params['from'] ) ) {
94                         $this->addWhere( 'pl_title>=' . $db->addQuotes( $this->titlePartToKey( $params['from'] ) ) );
95                 }
96                 if ( !is_null( $params['to'] ) ) {
97                         $this->addWhere( 'pl_title<=' . $db->addQuotes( $this->titlePartToKey( $params['to'] ) ) );
98                 }
99                 if ( isset( $params['prefix'] ) ) {
100                         $this->addWhere( 'pl_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
101                 }
102
103                 $this->addFields( array(
104                         'pl_title',
105                 ) );
106                 $this->addFieldsIf( 'pl_from', !$params['unique'] );
107
108                 $this->addOption( 'USE INDEX', 'pl_namespace' );
109                 $limit = $params['limit'];
110                 $this->addOption( 'LIMIT', $limit + 1 );
111                 if ( $params['unique'] ) {
112                         $this->addOption( 'ORDER BY', 'pl_title' );
113                 } else {
114                         $this->addOption( 'ORDER BY', 'pl_title, pl_from' );
115                 }
116
117                 $res = $this->select( __METHOD__ );
118
119                 $pageids = array();
120                 $count = 0;
121                 $result = $this->getResult();
122                 foreach ( $res as $row ) {
123                         if ( ++ $count > $limit ) {
124                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
125                                 // TODO: Security issue - if the user has no right to view next title, it will still be shown
126                                 if ( $params['unique'] ) {
127                                         $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->pl_title ) );
128                                 } else {
129                                         $this->setContinueEnumParameter( 'continue', $this->keyToTitle( $row->pl_title ) . "|" . $row->pl_from );
130                                 }
131                                 break;
132                         }
133
134                         if ( is_null( $resultPageSet ) ) {
135                                 $vals = array();
136                                 if ( $fld_ids ) {
137                                         $vals['fromid'] = intval( $row->pl_from );
138                                 }
139                                 if ( $fld_title ) {
140                                         $title = Title::makeTitle( $params['namespace'], $row->pl_title );
141                                         ApiQueryBase::addTitleInfo( $vals, $title );
142                                 }
143                                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
144                                 if ( !$fit ) {
145                                         if ( $params['unique'] ) {
146                                                 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->pl_title ) );
147                                         } else {
148                                                 $this->setContinueEnumParameter( 'continue', $this->keyToTitle( $row->pl_title ) . "|" . $row->pl_from );
149                                         }
150                                         break;
151                                 }
152                         } else {
153                                 $pageids[] = $row->pl_from;
154                         }
155                 }
156
157                 if ( is_null( $resultPageSet ) ) {
158                         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'l' );
159                 } else {
160                         $resultPageSet->populateFromPageIDs( $pageids );
161                 }
162         }
163
164         public function getAllowedParams() {
165                 return array(
166                         'continue' => null,
167                         'from' => null,
168                         'to' => null,
169                         'prefix' => null,
170                         'unique' => false,
171                         'prop' => array(
172                                 ApiBase::PARAM_ISMULTI => true,
173                                 ApiBase::PARAM_DFLT => 'title',
174                                 ApiBase::PARAM_TYPE => array(
175                                         'ids',
176                                         'title'
177                                 )
178                         ),
179                         'namespace' => array(
180                                 ApiBase::PARAM_DFLT => 0,
181                                 ApiBase::PARAM_TYPE => 'namespace'
182                         ),
183                         'limit' => array(
184                                 ApiBase::PARAM_DFLT => 10,
185                                 ApiBase::PARAM_TYPE => 'limit',
186                                 ApiBase::PARAM_MIN => 1,
187                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
188                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
189                         )
190                 );
191         }
192
193         public function getParamDescription() {
194                 $p = $this->getModulePrefix();
195                 return array(
196                         'from' => 'The page title to start enumerating from',
197                         'to' => 'The page title to stop enumerating at',
198                         'prefix' => 'Search for all page titles that begin with this value',
199                         'unique' => "Only show unique links. Cannot be used with generator or {$p}prop=ids",
200                         'prop' => array(
201                                 'What pieces of information to include',
202                                 " ids    - Adds pageid of where the link is from (Cannot be used with {$p}unique)",
203                                 ' title  - Adds the title of the link',
204                         ),
205                         'namespace' => 'The namespace to enumerate',
206                         'limit' => 'How many total links to return',
207                         'continue' => 'When more results are available, use this to continue',
208                 );
209         }
210
211         public function getDescription() {
212                 return 'Enumerate all links that point to a given namespace';
213         }
214
215         public function getPossibleErrors() {
216                 $m = $this->getModuleName();
217                 return array_merge( parent::getPossibleErrors(), array(
218                         array( 'code' => 'params', 'info' => "{$m} cannot be used as a generator in unique links mode" ),
219                         array( 'code' => 'params', 'info' => "{$m} cannot return corresponding page ids in unique links mode" ),
220                         array( 'code' => 'params', 'info' => 'alcontinue and alfrom cannot be used together' ),
221                         array( 'code' => 'badcontinue', 'info' => 'Invalid continue parameter' ),
222                 ) );
223         }
224
225         protected function getExamples() {
226                 return array(
227                         'api.php?action=query&list=alllinks&alunique=&alfrom=B',
228                 );
229         }
230
231         public function getVersion() {
232                 return __CLASS__ . ': $Id$';
233         }
234 }