ApiQueryAllLinks.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  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. */
  22. /**
  23. * Query module to enumerate links from all pages together.
  24. *
  25. * @ingroup API
  26. */
  27. class ApiQueryAllLinks extends ApiQueryGeneratorBase {
  28. private $table, $tablePrefix, $indexTag;
  29. private $fieldTitle = 'title';
  30. private $dfltNamespace = NS_MAIN;
  31. private $hasNamespace = true;
  32. private $useIndex = null;
  33. private $props = [];
  34. public function __construct( ApiQuery $query, $moduleName ) {
  35. switch ( $moduleName ) {
  36. case 'alllinks':
  37. $prefix = 'al';
  38. $this->table = 'pagelinks';
  39. $this->tablePrefix = 'pl_';
  40. $this->useIndex = 'pl_namespace';
  41. $this->indexTag = 'l';
  42. break;
  43. case 'alltransclusions':
  44. $prefix = 'at';
  45. $this->table = 'templatelinks';
  46. $this->tablePrefix = 'tl_';
  47. $this->dfltNamespace = NS_TEMPLATE;
  48. $this->useIndex = 'tl_namespace';
  49. $this->indexTag = 't';
  50. break;
  51. case 'allfileusages':
  52. $prefix = 'af';
  53. $this->table = 'imagelinks';
  54. $this->tablePrefix = 'il_';
  55. $this->fieldTitle = 'to';
  56. $this->dfltNamespace = NS_FILE;
  57. $this->hasNamespace = false;
  58. $this->indexTag = 'f';
  59. break;
  60. case 'allredirects':
  61. $prefix = 'ar';
  62. $this->table = 'redirect';
  63. $this->tablePrefix = 'rd_';
  64. $this->indexTag = 'r';
  65. $this->props = [
  66. 'fragment' => 'rd_fragment',
  67. 'interwiki' => 'rd_interwiki',
  68. ];
  69. break;
  70. default:
  71. ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
  72. }
  73. parent::__construct( $query, $moduleName, $prefix );
  74. }
  75. public function execute() {
  76. $this->run();
  77. }
  78. public function getCacheMode( $params ) {
  79. return 'public';
  80. }
  81. public function executeGenerator( $resultPageSet ) {
  82. $this->run( $resultPageSet );
  83. }
  84. /**
  85. * @param ApiPageSet $resultPageSet
  86. * @return void
  87. */
  88. private function run( $resultPageSet = null ) {
  89. $db = $this->getDB();
  90. $params = $this->extractRequestParams();
  91. $pfx = $this->tablePrefix;
  92. $fieldTitle = $this->fieldTitle;
  93. $prop = array_flip( $params['prop'] );
  94. $fld_ids = isset( $prop['ids'] );
  95. $fld_title = isset( $prop['title'] );
  96. if ( $this->hasNamespace ) {
  97. $namespace = $params['namespace'];
  98. } else {
  99. $namespace = $this->dfltNamespace;
  100. }
  101. if ( $params['unique'] ) {
  102. $matches = array_intersect_key( $prop, $this->props + [ 'ids' => 1 ] );
  103. if ( $matches ) {
  104. $p = $this->getModulePrefix();
  105. $this->dieWithError(
  106. [
  107. 'apierror-invalidparammix-cannotusewith',
  108. "{$p}prop=" . implode( '|', array_keys( $matches ) ),
  109. "{$p}unique"
  110. ],
  111. 'invalidparammix'
  112. );
  113. }
  114. $this->addOption( 'DISTINCT' );
  115. }
  116. $this->addTables( $this->table );
  117. if ( $this->hasNamespace ) {
  118. $this->addWhereFld( $pfx . 'namespace', $namespace );
  119. }
  120. $continue = !is_null( $params['continue'] );
  121. if ( $continue ) {
  122. $continueArr = explode( '|', $params['continue'] );
  123. $op = $params['dir'] == 'descending' ? '<' : '>';
  124. if ( $params['unique'] ) {
  125. $this->dieContinueUsageIf( count( $continueArr ) != 1 );
  126. $continueTitle = $db->addQuotes( $continueArr[0] );
  127. $this->addWhere( "{$pfx}{$fieldTitle} $op= $continueTitle" );
  128. } else {
  129. $this->dieContinueUsageIf( count( $continueArr ) != 2 );
  130. $continueTitle = $db->addQuotes( $continueArr[0] );
  131. $continueFrom = (int)$continueArr[1];
  132. $this->addWhere(
  133. "{$pfx}{$fieldTitle} $op $continueTitle OR " .
  134. "({$pfx}{$fieldTitle} = $continueTitle AND " .
  135. "{$pfx}from $op= $continueFrom)"
  136. );
  137. }
  138. }
  139. // 'continue' always overrides 'from'
  140. $from = $continue || $params['from'] === null ? null :
  141. $this->titlePartToKey( $params['from'], $namespace );
  142. $to = $params['to'] === null ? null :
  143. $this->titlePartToKey( $params['to'], $namespace );
  144. $this->addWhereRange( $pfx . $fieldTitle, 'newer', $from, $to );
  145. if ( isset( $params['prefix'] ) ) {
  146. $this->addWhere( $pfx . $fieldTitle . $db->buildLike( $this->titlePartToKey(
  147. $params['prefix'], $namespace ), $db->anyString() ) );
  148. }
  149. $this->addFields( [ 'pl_title' => $pfx . $fieldTitle ] );
  150. $this->addFieldsIf( [ 'pl_from' => $pfx . 'from' ], !$params['unique'] );
  151. foreach ( $this->props as $name => $field ) {
  152. $this->addFieldsIf( $field, isset( $prop[$name] ) );
  153. }
  154. if ( $this->useIndex ) {
  155. $this->addOption( 'USE INDEX', $this->useIndex );
  156. }
  157. $limit = $params['limit'];
  158. $this->addOption( 'LIMIT', $limit + 1 );
  159. $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
  160. $orderBy = [];
  161. $orderBy[] = $pfx . $fieldTitle . $sort;
  162. if ( !$params['unique'] ) {
  163. $orderBy[] = $pfx . 'from' . $sort;
  164. }
  165. $this->addOption( 'ORDER BY', $orderBy );
  166. $res = $this->select( __METHOD__ );
  167. $pageids = [];
  168. $titles = [];
  169. $count = 0;
  170. $result = $this->getResult();
  171. foreach ( $res as $row ) {
  172. if ( ++$count > $limit ) {
  173. // We've reached the one extra which shows that there are
  174. // additional pages to be had. Stop here...
  175. if ( $params['unique'] ) {
  176. $this->setContinueEnumParameter( 'continue', $row->pl_title );
  177. } else {
  178. $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from );
  179. }
  180. break;
  181. }
  182. if ( is_null( $resultPageSet ) ) {
  183. $vals = [
  184. ApiResult::META_TYPE => 'assoc',
  185. ];
  186. if ( $fld_ids ) {
  187. $vals['fromid'] = (int)$row->pl_from;
  188. }
  189. if ( $fld_title ) {
  190. $title = Title::makeTitle( $namespace, $row->pl_title );
  191. ApiQueryBase::addTitleInfo( $vals, $title );
  192. }
  193. foreach ( $this->props as $name => $field ) {
  194. if ( isset( $prop[$name] ) && $row->$field !== null && $row->$field !== '' ) {
  195. $vals[$name] = $row->$field;
  196. }
  197. }
  198. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
  199. if ( !$fit ) {
  200. if ( $params['unique'] ) {
  201. $this->setContinueEnumParameter( 'continue', $row->pl_title );
  202. } else {
  203. $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from );
  204. }
  205. break;
  206. }
  207. } elseif ( $params['unique'] ) {
  208. $titles[] = Title::makeTitle( $namespace, $row->pl_title );
  209. } else {
  210. $pageids[] = $row->pl_from;
  211. }
  212. }
  213. if ( is_null( $resultPageSet ) ) {
  214. $result->addIndexedTagName( [ 'query', $this->getModuleName() ], $this->indexTag );
  215. } elseif ( $params['unique'] ) {
  216. $resultPageSet->populateFromTitles( $titles );
  217. } else {
  218. $resultPageSet->populateFromPageIDs( $pageids );
  219. }
  220. }
  221. public function getAllowedParams() {
  222. $allowedParams = [
  223. 'continue' => [
  224. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  225. ],
  226. 'from' => null,
  227. 'to' => null,
  228. 'prefix' => null,
  229. 'unique' => false,
  230. 'prop' => [
  231. ApiBase::PARAM_ISMULTI => true,
  232. ApiBase::PARAM_DFLT => 'title',
  233. ApiBase::PARAM_TYPE => array_merge(
  234. [ 'ids', 'title' ], array_keys( $this->props )
  235. ),
  236. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  237. ],
  238. 'namespace' => [
  239. ApiBase::PARAM_DFLT => $this->dfltNamespace,
  240. ApiBase::PARAM_TYPE => 'namespace',
  241. ApiBase::PARAM_EXTRA_NAMESPACES => [ NS_MEDIA, NS_SPECIAL ],
  242. ],
  243. 'limit' => [
  244. ApiBase::PARAM_DFLT => 10,
  245. ApiBase::PARAM_TYPE => 'limit',
  246. ApiBase::PARAM_MIN => 1,
  247. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  248. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  249. ],
  250. 'dir' => [
  251. ApiBase::PARAM_DFLT => 'ascending',
  252. ApiBase::PARAM_TYPE => [
  253. 'ascending',
  254. 'descending'
  255. ]
  256. ],
  257. ];
  258. if ( !$this->hasNamespace ) {
  259. unset( $allowedParams['namespace'] );
  260. }
  261. return $allowedParams;
  262. }
  263. protected function getExamplesMessages() {
  264. $p = $this->getModulePrefix();
  265. $name = $this->getModuleName();
  266. $path = $this->getModulePath();
  267. return [
  268. "action=query&list={$name}&{$p}from=B&{$p}prop=ids|title"
  269. => "apihelp-$path-example-b",
  270. "action=query&list={$name}&{$p}unique=&{$p}from=B"
  271. => "apihelp-$path-example-unique",
  272. "action=query&generator={$name}&g{$p}unique=&g{$p}from=B"
  273. => "apihelp-$path-example-unique-generator",
  274. "action=query&generator={$name}&g{$p}from=B"
  275. => "apihelp-$path-example-generator",
  276. ];
  277. }
  278. public function getHelpUrls() {
  279. $name = ucfirst( $this->getModuleName() );
  280. return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";
  281. }
  282. }