ApiQueryLinks.php 6.7 KB

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