ApiQueryAllLinks.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /*
  3. * Created on July 7, 2007
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ('ApiQueryBase.php');
  27. }
  28. /**
  29. * Query module to enumerate links from all pages together.
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryAllLinks extends ApiQueryGeneratorBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'al');
  36. }
  37. public function execute() {
  38. $this->run();
  39. }
  40. public function executeGenerator($resultPageSet) {
  41. $this->run($resultPageSet);
  42. }
  43. private function run($resultPageSet = null) {
  44. $db = $this->getDB();
  45. $params = $this->extractRequestParams();
  46. $prop = array_flip($params['prop']);
  47. $fld_ids = isset($prop['ids']);
  48. $fld_title = isset($prop['title']);
  49. if ($params['unique']) {
  50. if (!is_null($resultPageSet))
  51. $this->dieUsage($this->getModuleName() . ' cannot be used as a generator in unique links mode', 'params');
  52. if ($fld_ids)
  53. $this->dieUsage($this->getModuleName() . ' cannot return corresponding page ids in unique links mode', 'params');
  54. $this->addOption('DISTINCT');
  55. }
  56. $this->addTables('pagelinks');
  57. $this->addWhereFld('pl_namespace', $params['namespace']);
  58. if (!is_null($params['from']) && !is_null($params['continue']))
  59. $this->dieUsage('alcontinue and alfrom cannot be used together', 'params');
  60. if (!is_null($params['continue']))
  61. {
  62. $arr = explode('|', $params['continue']);
  63. if(count($arr) != 2)
  64. $this->dieUsage("Invalid continue parameter", 'badcontinue');
  65. $from = $this->getDB()->strencode($this->titleToKey($arr[0]));
  66. $id = intval($arr[1]);
  67. $this->addWhere("pl_title > '$from' OR " .
  68. "(pl_title = '$from' AND " .
  69. "pl_from > $id)");
  70. }
  71. if (!is_null($params['from']))
  72. $this->addWhere('pl_title>=' . $db->addQuotes($this->titlePartToKey($params['from'])));
  73. if (isset ($params['prefix']))
  74. $this->addWhere("pl_title LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
  75. $this->addFields(array (
  76. 'pl_title',
  77. ));
  78. $this->addFieldsIf('pl_from', !$params['unique']);
  79. $this->addOption('USE INDEX', 'pl_namespace');
  80. $limit = $params['limit'];
  81. $this->addOption('LIMIT', $limit+1);
  82. if($params['unique'])
  83. $this->addOption('ORDER BY', 'pl_title');
  84. else
  85. $this->addOption('ORDER BY', 'pl_title, pl_from');
  86. $res = $this->select(__METHOD__);
  87. $pageids = array ();
  88. $count = 0;
  89. $result = $this->getResult();
  90. while ($row = $db->fetchObject($res)) {
  91. if (++ $count > $limit) {
  92. // We've reached the one extra which shows that there are additional pages to be had. Stop here...
  93. // TODO: Security issue - if the user has no right to view next title, it will still be shown
  94. if($params['unique'])
  95. $this->setContinueEnumParameter('from', $this->keyToTitle($row->pl_title));
  96. else
  97. $this->setContinueEnumParameter('continue', $this->keyToTitle($row->pl_title) . "|" . $row->pl_from);
  98. break;
  99. }
  100. if (is_null($resultPageSet)) {
  101. $vals = array();
  102. if ($fld_ids)
  103. $vals['fromid'] = intval($row->pl_from);
  104. if ($fld_title) {
  105. $title = Title :: makeTitle($params['namespace'], $row->pl_title);
  106. ApiQueryBase::addTitleInfo($vals, $title);
  107. }
  108. $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
  109. if(!$fit)
  110. {
  111. if($params['unique'])
  112. $this->setContinueEnumParameter('from', $this->keyToTitle($row->pl_title));
  113. else
  114. $this->setContinueEnumParameter('continue', $this->keyToTitle($row->pl_title) . "|" . $row->pl_from);
  115. break;
  116. }
  117. } else {
  118. $pageids[] = $row->pl_from;
  119. }
  120. }
  121. $db->freeResult($res);
  122. if (is_null($resultPageSet)) {
  123. $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'l');
  124. } else {
  125. $resultPageSet->populateFromPageIDs($pageids);
  126. }
  127. }
  128. public function getAllowedParams() {
  129. return array (
  130. 'continue' => null,
  131. 'from' => null,
  132. 'prefix' => null,
  133. 'unique' => false,
  134. 'prop' => array (
  135. ApiBase :: PARAM_ISMULTI => true,
  136. ApiBase :: PARAM_DFLT => 'title',
  137. ApiBase :: PARAM_TYPE => array (
  138. 'ids',
  139. 'title'
  140. )
  141. ),
  142. 'namespace' => array (
  143. ApiBase :: PARAM_DFLT => 0,
  144. ApiBase :: PARAM_TYPE => 'namespace'
  145. ),
  146. 'limit' => array (
  147. ApiBase :: PARAM_DFLT => 10,
  148. ApiBase :: PARAM_TYPE => 'limit',
  149. ApiBase :: PARAM_MIN => 1,
  150. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  151. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  152. )
  153. );
  154. }
  155. public function getParamDescription() {
  156. return array (
  157. 'from' => 'The page title to start enumerating from.',
  158. 'prefix' => 'Search for all page titles that begin with this value.',
  159. 'unique' => 'Only show unique links. Cannot be used with generator or prop=ids',
  160. 'prop' => 'What pieces of information to include',
  161. 'namespace' => 'The namespace to enumerate.',
  162. 'limit' => 'How many total links to return.',
  163. 'continue' => 'When more results are available, use this to continue.',
  164. );
  165. }
  166. public function getDescription() {
  167. return 'Enumerate all links that point to a given namespace';
  168. }
  169. protected function getExamples() {
  170. return array (
  171. 'api.php?action=query&list=alllinks&alunique&alfrom=B',
  172. );
  173. }
  174. public function getVersion() {
  175. return __CLASS__ . ': $Id: ApiQueryAllLinks.php 47865 2009-02-27 16:03:01Z catrope $';
  176. }
  177. }