4 * Created on July 7, 2007
6 * API for MediaWiki 1.8+
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
32 * Query module to enumerate links from all pages together.
36 class ApiQueryAllLinks
extends ApiQueryGeneratorBase
{
38 public function __construct( $query, $moduleName ) {
39 parent
::__construct( $query, $moduleName, 'al' );
42 public function execute() {
46 public function getCacheMode( $params ) {
50 public function executeGenerator( $resultPageSet ) {
51 $this->run( $resultPageSet );
54 private function run( $resultPageSet = null ) {
56 $params = $this->extractRequestParams();
58 $prop = array_flip( $params['prop'] );
59 $fld_ids = isset( $prop['ids'] );
60 $fld_title = isset( $prop['title'] );
62 if ( $params['unique'] ) {
63 if ( !is_null( $resultPageSet ) ) {
64 $this->dieUsage( $this->getModuleName() . ' cannot be used as a generator in unique links mode', 'params' );
67 $this->dieUsage( $this->getModuleName() . ' cannot return corresponding page ids in unique links mode', 'params' );
69 $this->addOption( 'DISTINCT' );
72 $this->addTables( 'pagelinks' );
73 $this->addWhereFld( 'pl_namespace', $params['namespace'] );
75 if ( !is_null( $params['from'] ) && !is_null( $params['continue'] ) ) {
76 $this->dieUsage( 'alcontinue and alfrom cannot be used together', 'params' );
78 if ( !is_null( $params['continue'] ) ) {
79 $arr = explode( '|', $params['continue'] );
80 if ( count( $arr ) != 2 ) {
81 $this->dieUsage( 'Invalid continue parameter', 'badcontinue' );
83 $from = $this->getDB()->strencode( $this->titleToKey( $arr[0] ) );
84 $id = intval( $arr[1] );
86 "pl_title > '$from' OR " .
87 "(pl_title = '$from' AND " .
92 if ( !is_null( $params['from'] ) ) {
93 $this->addWhere( 'pl_title>=' . $db->addQuotes( $this->titlePartToKey( $params['from'] ) ) );
95 if ( isset( $params['prefix'] ) ) {
96 $this->addWhere( 'pl_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
99 $this->addFields( array(
102 $this->addFieldsIf( 'pl_from', !$params['unique'] );
104 $this->addOption( 'USE INDEX', 'pl_namespace' );
105 $limit = $params['limit'];
106 $this->addOption( 'LIMIT', $limit +
1 );
107 if ( $params['unique'] ) {
108 $this->addOption( 'ORDER BY', 'pl_title' );
110 $this->addOption( 'ORDER BY', 'pl_title, pl_from' );
113 $res = $this->select( __METHOD__
);
117 $result = $this->getResult();
118 foreach ( $res as $row ) {
119 if ( ++
$count > $limit ) {
120 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
121 // TODO: Security issue - if the user has no right to view next title, it will still be shown
122 if ( $params['unique'] ) {
123 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->pl_title
) );
125 $this->setContinueEnumParameter( 'continue', $this->keyToTitle( $row->pl_title
) . "|" . $row->pl_from
);
130 if ( is_null( $resultPageSet ) ) {
133 $vals['fromid'] = intval( $row->pl_from
);
136 $title = Title
::makeTitle( $params['namespace'], $row->pl_title
);
137 ApiQueryBase
::addTitleInfo( $vals, $title );
139 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
141 if ( $params['unique'] ) {
142 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->pl_title
) );
144 $this->setContinueEnumParameter( 'continue', $this->keyToTitle( $row->pl_title
) . "|" . $row->pl_from
);
149 $pageids[] = $row->pl_from
;
153 if ( is_null( $resultPageSet ) ) {
154 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'l' );
156 $resultPageSet->populateFromPageIDs( $pageids );
160 public function getAllowedParams() {
167 ApiBase
::PARAM_ISMULTI
=> true,
168 ApiBase
::PARAM_DFLT
=> 'title',
169 ApiBase
::PARAM_TYPE
=> array(
174 'namespace' => array(
175 ApiBase
::PARAM_DFLT
=> 0,
176 ApiBase
::PARAM_TYPE
=> 'namespace'
179 ApiBase
::PARAM_DFLT
=> 10,
180 ApiBase
::PARAM_TYPE
=> 'limit',
181 ApiBase
::PARAM_MIN
=> 1,
182 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
183 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
188 public function getParamDescription() {
189 $p = $this->getModulePrefix();
191 'from' => 'The page title to start enumerating from',
192 'prefix' => 'Search for all page titles that begin with this value',
193 'unique' => "Only show unique links. Cannot be used with generator or {$p}prop=ids",
195 'What pieces of information to include',
196 " ids - Adds pageid of where the link is from (Cannot be used with {$p}unique)",
197 ' title - Adds the title of the link',
199 'namespace' => 'The namespace to enumerate',
200 'limit' => 'How many total links to return',
201 'continue' => 'When more results are available, use this to continue',
205 public function getDescription() {
206 return 'Enumerate all links that point to a given namespace';
209 public function getPossibleErrors() {
210 $m = $this->getModuleName();
211 return array_merge( parent
::getPossibleErrors(), array(
212 array( 'code' => 'params', 'info' => "{$m} cannot be used as a generator in unique links mode" ),
213 array( 'code' => 'params', 'info' => "{$m} cannot return corresponding page ids in unique links mode" ),
214 array( 'code' => 'params', 'info' => 'alcontinue and alfrom cannot be used together' ),
215 array( 'code' => 'badcontinue', 'info' => 'Invalid continue parameter' ),
219 protected function getExamples() {
221 'api.php?action=query&list=alllinks&alunique&alfrom=B',
225 public function getVersion() {
226 return __CLASS__
. ': $Id$';