ApiQueryLinks.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. * A query module to list all wiki links on a given set of pages.
  24. *
  25. * @ingroup API
  26. */
  27. class ApiQueryLinks extends ApiQueryGeneratorBase {
  28. const LINKS = 'links';
  29. const TEMPLATES = 'templates';
  30. private $table, $prefix, $titlesParam, $helpUrl;
  31. public function __construct( ApiQuery $query, $moduleName ) {
  32. switch ( $moduleName ) {
  33. case self::LINKS:
  34. $this->table = 'pagelinks';
  35. $this->prefix = 'pl';
  36. $this->titlesParam = 'titles';
  37. $this->helpUrl = 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Links';
  38. break;
  39. case self::TEMPLATES:
  40. $this->table = 'templatelinks';
  41. $this->prefix = 'tl';
  42. $this->titlesParam = 'templates';
  43. $this->helpUrl = 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Templates';
  44. break;
  45. default:
  46. ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
  47. }
  48. parent::__construct( $query, $moduleName, $this->prefix );
  49. }
  50. public function execute() {
  51. $this->run();
  52. }
  53. public function getCacheMode( $params ) {
  54. return 'public';
  55. }
  56. public function executeGenerator( $resultPageSet ) {
  57. $this->run( $resultPageSet );
  58. }
  59. /**
  60. * @param ApiPageSet $resultPageSet
  61. */
  62. private function run( $resultPageSet = null ) {
  63. if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
  64. return; // nothing to do
  65. }
  66. $params = $this->extractRequestParams();
  67. $this->addFields( [
  68. 'pl_from' => $this->prefix . '_from',
  69. 'pl_namespace' => $this->prefix . '_namespace',
  70. 'pl_title' => $this->prefix . '_title'
  71. ] );
  72. $this->addTables( $this->table );
  73. $this->addWhereFld( $this->prefix . '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
  74. $multiNS = true;
  75. $multiTitle = true;
  76. if ( $params[$this->titlesParam] ) {
  77. // Filter the titles in PHP so our ORDER BY bug avoidance below works right.
  78. $filterNS = $params['namespace'] ? array_flip( $params['namespace'] ) : false;
  79. $lb = new LinkBatch;
  80. foreach ( $params[$this->titlesParam] as $t ) {
  81. $title = Title::newFromText( $t );
  82. if ( !$title ) {
  83. $this->addWarning( [ 'apiwarn-invalidtitle', wfEscapeWikiText( $t ) ] );
  84. } elseif ( !$filterNS || isset( $filterNS[$title->getNamespace()] ) ) {
  85. $lb->addObj( $title );
  86. }
  87. }
  88. $cond = $lb->constructSet( $this->prefix, $this->getDB() );
  89. if ( $cond ) {
  90. $this->addWhere( $cond );
  91. $multiNS = count( $lb->data ) !== 1;
  92. $multiTitle = count( array_merge( ...$lb->data ) ) !== 1;
  93. } else {
  94. // No titles so no results
  95. return;
  96. }
  97. } elseif ( $params['namespace'] ) {
  98. $this->addWhereFld( $this->prefix . '_namespace', $params['namespace'] );
  99. $multiNS = $params['namespace'] === null || count( $params['namespace'] ) !== 1;
  100. }
  101. if ( !is_null( $params['continue'] ) ) {
  102. $cont = explode( '|', $params['continue'] );
  103. $this->dieContinueUsageIf( count( $cont ) != 3 );
  104. $op = $params['dir'] == 'descending' ? '<' : '>';
  105. $plfrom = (int)$cont[0];
  106. $plns = (int)$cont[1];
  107. $pltitle = $this->getDB()->addQuotes( $cont[2] );
  108. $this->addWhere(
  109. "{$this->prefix}_from $op $plfrom OR " .
  110. "({$this->prefix}_from = $plfrom AND " .
  111. "({$this->prefix}_namespace $op $plns OR " .
  112. "({$this->prefix}_namespace = $plns AND " .
  113. "{$this->prefix}_title $op= $pltitle)))"
  114. );
  115. }
  116. $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
  117. // Here's some MySQL craziness going on: if you use WHERE foo='bar'
  118. // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
  119. // but instead goes and filesorts, because the index for foo was used
  120. // already. To work around this, we drop constant fields in the WHERE
  121. // clause from the ORDER BY clause
  122. $order = [];
  123. if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
  124. $order[] = $this->prefix . '_from' . $sort;
  125. }
  126. if ( $multiNS ) {
  127. $order[] = $this->prefix . '_namespace' . $sort;
  128. }
  129. if ( $multiTitle ) {
  130. $order[] = $this->prefix . '_title' . $sort;
  131. }
  132. if ( $order ) {
  133. $this->addOption( 'ORDER BY', $order );
  134. }
  135. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  136. $res = $this->select( __METHOD__ );
  137. if ( is_null( $resultPageSet ) ) {
  138. $count = 0;
  139. foreach ( $res as $row ) {
  140. if ( ++$count > $params['limit'] ) {
  141. // We've reached the one extra which shows that
  142. // there are additional pages to be had. Stop here...
  143. $this->setContinueEnumParameter( 'continue',
  144. "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
  145. break;
  146. }
  147. $vals = [];
  148. ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( $row->pl_namespace, $row->pl_title ) );
  149. $fit = $this->addPageSubItem( $row->pl_from, $vals );
  150. if ( !$fit ) {
  151. $this->setContinueEnumParameter( 'continue',
  152. "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
  153. break;
  154. }
  155. }
  156. } else {
  157. $titles = [];
  158. $count = 0;
  159. foreach ( $res as $row ) {
  160. if ( ++$count > $params['limit'] ) {
  161. // We've reached the one extra which shows that
  162. // there are additional pages to be had. Stop here...
  163. $this->setContinueEnumParameter( 'continue',
  164. "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
  165. break;
  166. }
  167. $titles[] = Title::makeTitle( $row->pl_namespace, $row->pl_title );
  168. }
  169. $resultPageSet->populateFromTitles( $titles );
  170. }
  171. }
  172. public function getAllowedParams() {
  173. return [
  174. 'namespace' => [
  175. ApiBase::PARAM_TYPE => 'namespace',
  176. ApiBase::PARAM_ISMULTI => true,
  177. ApiBase::PARAM_EXTRA_NAMESPACES => [ NS_MEDIA, NS_SPECIAL ],
  178. ],
  179. 'limit' => [
  180. ApiBase::PARAM_DFLT => 10,
  181. ApiBase::PARAM_TYPE => 'limit',
  182. ApiBase::PARAM_MIN => 1,
  183. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  184. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  185. ],
  186. 'continue' => [
  187. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  188. ],
  189. $this->titlesParam => [
  190. ApiBase::PARAM_ISMULTI => true,
  191. ],
  192. 'dir' => [
  193. ApiBase::PARAM_DFLT => 'ascending',
  194. ApiBase::PARAM_TYPE => [
  195. 'ascending',
  196. 'descending'
  197. ]
  198. ],
  199. ];
  200. }
  201. protected function getExamplesMessages() {
  202. $name = $this->getModuleName();
  203. $path = $this->getModulePath();
  204. return [
  205. "action=query&prop={$name}&titles=Main%20Page"
  206. => "apihelp-{$path}-example-simple",
  207. "action=query&generator={$name}&titles=Main%20Page&prop=info"
  208. => "apihelp-{$path}-example-generator",
  209. "action=query&prop={$name}&titles=Main%20Page&{$this->prefix}namespace=2|10"
  210. => "apihelp-{$path}-example-namespaces",
  211. ];
  212. }
  213. public function getHelpUrls() {
  214. return $this->helpUrl;
  215. }
  216. }