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 executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
50 private function run( $resultPageSet = null ) {
52 $params = $this->extractRequestParams();
54 $prop = array_flip( $params['prop'] );
55 $fld_ids = isset( $prop['ids'] );
56 $fld_title = isset( $prop['title'] );
58 if ( $params['unique'] ) {
59 if ( !is_null( $resultPageSet ) ) {
60 $this->dieUsage( $this->getModuleName() . ' cannot be used as a generator in unique links mode', 'params' );
63 $this->dieUsage( $this->getModuleName() . ' cannot return corresponding page ids in unique links mode', 'params' );
65 $this->addOption( 'DISTINCT' );
68 $this->addTables( 'pagelinks' );
69 $this->addWhereFld( 'pl_namespace', $params['namespace'] );
71 if ( !is_null( $params['from'] ) && !is_null( $params['continue'] ) ) {
72 $this->dieUsage( 'alcontinue and alfrom cannot be used together', 'params' );
74 if ( !is_null( $params['continue'] ) ) {
75 $arr = explode( '|', $params['continue'] );
76 if ( count( $arr ) != 2 ) {
77 $this->dieUsage( 'Invalid continue parameter', 'badcontinue' );
79 $from = $this->getDB()->strencode( $this->titleToKey( $arr[0] ) );
80 $id = intval( $arr[1] );
82 "pl_title > '$from' OR " .
83 "(pl_title = '$from' AND " .
88 if ( !is_null( $params['from'] ) ) {
89 $this->addWhere( 'pl_title>=' . $db->addQuotes( $this->titlePartToKey( $params['from'] ) ) );
91 if ( isset( $params['prefix'] ) ) {
92 $this->addWhere( 'pl_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
95 $this->addFields( array(
98 $this->addFieldsIf( 'pl_from', !$params['unique'] );
100 $this->addOption( 'USE INDEX', 'pl_namespace' );
101 $limit = $params['limit'];
102 $this->addOption( 'LIMIT', $limit +
1 );
103 if ( $params['unique'] ) {
104 $this->addOption( 'ORDER BY', 'pl_title' );
106 $this->addOption( 'ORDER BY', 'pl_title, pl_from' );
109 $res = $this->select( __METHOD__
);
113 $result = $this->getResult();
114 foreach ( $res as $row ) {
115 if ( ++
$count > $limit ) {
116 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
117 // TODO: Security issue - if the user has no right to view next title, it will still be shown
118 if ( $params['unique'] ) {
119 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->pl_title
) );
121 $this->setContinueEnumParameter( 'continue', $this->keyToTitle( $row->pl_title
) . "|" . $row->pl_from
);
126 if ( is_null( $resultPageSet ) ) {
129 $vals['fromid'] = intval( $row->pl_from
);
132 $title = Title
::makeTitle( $params['namespace'], $row->pl_title
);
133 ApiQueryBase
::addTitleInfo( $vals, $title );
135 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
137 if ( $params['unique'] ) {
138 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->pl_title
) );
140 $this->setContinueEnumParameter( 'continue', $this->keyToTitle( $row->pl_title
) . "|" . $row->pl_from
);
145 $pageids[] = $row->pl_from
;
149 if ( is_null( $resultPageSet ) ) {
150 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'l' );
152 $resultPageSet->populateFromPageIDs( $pageids );
156 public function getAllowedParams() {
163 ApiBase
::PARAM_ISMULTI
=> true,
164 ApiBase
::PARAM_DFLT
=> 'title',
165 ApiBase
::PARAM_TYPE
=> array(
170 'namespace' => array(
171 ApiBase
::PARAM_DFLT
=> 0,
172 ApiBase
::PARAM_TYPE
=> 'namespace'
175 ApiBase
::PARAM_DFLT
=> 10,
176 ApiBase
::PARAM_TYPE
=> 'limit',
177 ApiBase
::PARAM_MIN
=> 1,
178 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
179 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
184 public function getParamDescription() {
185 $p = $this->getModulePrefix();
187 'from' => 'The page title to start enumerating from',
188 'prefix' => 'Search for all page titles that begin with this value',
189 'unique' => "Only show unique links. Cannot be used with generator or {$p}prop=ids",
191 'What pieces of information to include',
192 " ids - Adds pageid of where the link is from (Cannot be used with {$p}unique)",
193 ' title - Adds the title of the link',
195 'namespace' => 'The namespace to enumerate',
196 'limit' => 'How many total links to return',
197 'continue' => 'When more results are available, use this to continue',
201 public function getDescription() {
202 return 'Enumerate all links that point to a given namespace';
205 public function getPossibleErrors() {
206 $m = $this->getModuleName();
207 return array_merge( parent
::getPossibleErrors(), array(
208 array( 'code' => 'params', 'info' => "{$m} cannot be used as a generator in unique links mode" ),
209 array( 'code' => 'params', 'info' => "{$m} cannot return corresponding page ids in unique links mode" ),
210 array( 'code' => 'params', 'info' => 'alcontinue and alfrom cannot be used together' ),
211 array( 'code' => 'badcontinue', 'info' => 'Invalid continue parameter' ),
215 protected function getExamples() {
217 'api.php?action=query&list=alllinks&alunique&alfrom=B',
221 public function getVersion() {
222 return __CLASS__
. ': $Id$';