ApiQueryExtLinksUsage.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on July 7, 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. * @ingroup API
  28. */
  29. class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
  30. public function __construct( ApiQuery $query, $moduleName ) {
  31. parent::__construct( $query, $moduleName, 'eu' );
  32. }
  33. public function execute() {
  34. $this->run();
  35. }
  36. public function getCacheMode( $params ) {
  37. return 'public';
  38. }
  39. public function executeGenerator( $resultPageSet ) {
  40. $this->run( $resultPageSet );
  41. }
  42. /**
  43. * @param ApiPageSet $resultPageSet
  44. * @return void
  45. */
  46. private function run( $resultPageSet = null ) {
  47. $params = $this->extractRequestParams();
  48. $query = $params['query'];
  49. $protocol = self::getProtocolPrefix( $params['protocol'] );
  50. $this->addTables( [ 'page', 'externallinks' ] ); // must be in this order for 'USE INDEX'
  51. $this->addOption( 'USE INDEX', 'el_index' );
  52. $this->addWhere( 'page_id=el_from' );
  53. $miser_ns = [];
  54. if ( $this->getConfig()->get( 'MiserMode' ) ) {
  55. $miser_ns = $params['namespace'];
  56. } else {
  57. $this->addWhereFld( 'page_namespace', $params['namespace'] );
  58. }
  59. // Normalize query to match the normalization applied for the externallinks table
  60. $query = Parser::normalizeLinkUrl( $query );
  61. $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
  62. if ( $whereQuery !== null ) {
  63. $this->addWhere( $whereQuery );
  64. }
  65. $prop = array_flip( $params['prop'] );
  66. $fld_ids = isset( $prop['ids'] );
  67. $fld_title = isset( $prop['title'] );
  68. $fld_url = isset( $prop['url'] );
  69. if ( is_null( $resultPageSet ) ) {
  70. $this->addFields( [
  71. 'page_id',
  72. 'page_namespace',
  73. 'page_title'
  74. ] );
  75. $this->addFieldsIf( 'el_to', $fld_url );
  76. } else {
  77. $this->addFields( $resultPageSet->getPageTableFields() );
  78. }
  79. $limit = $params['limit'];
  80. $offset = $params['offset'];
  81. $this->addOption( 'LIMIT', $limit + 1 );
  82. if ( isset( $offset ) ) {
  83. $this->addOption( 'OFFSET', $offset );
  84. }
  85. $res = $this->select( __METHOD__ );
  86. $result = $this->getResult();
  87. $count = 0;
  88. foreach ( $res as $row ) {
  89. if ( ++$count > $limit ) {
  90. // We've reached the one extra which shows that there are
  91. // additional pages to be had. Stop here...
  92. $this->setContinueEnumParameter( 'offset', $offset + $limit );
  93. break;
  94. }
  95. if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
  96. continue;
  97. }
  98. if ( is_null( $resultPageSet ) ) {
  99. $vals = [
  100. ApiResult::META_TYPE => 'assoc',
  101. ];
  102. if ( $fld_ids ) {
  103. $vals['pageid'] = intval( $row->page_id );
  104. }
  105. if ( $fld_title ) {
  106. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  107. ApiQueryBase::addTitleInfo( $vals, $title );
  108. }
  109. if ( $fld_url ) {
  110. $to = $row->el_to;
  111. // expand protocol-relative urls
  112. if ( $params['expandurl'] ) {
  113. $to = wfExpandUrl( $to, PROTO_CANONICAL );
  114. }
  115. $vals['url'] = $to;
  116. }
  117. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
  118. if ( !$fit ) {
  119. $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
  120. break;
  121. }
  122. } else {
  123. $resultPageSet->processDbRow( $row );
  124. }
  125. }
  126. if ( is_null( $resultPageSet ) ) {
  127. $result->addIndexedTagName( [ 'query', $this->getModuleName() ],
  128. $this->getModulePrefix() );
  129. }
  130. }
  131. public function getAllowedParams() {
  132. $ret = [
  133. 'prop' => [
  134. ApiBase::PARAM_ISMULTI => true,
  135. ApiBase::PARAM_DFLT => 'ids|title|url',
  136. ApiBase::PARAM_TYPE => [
  137. 'ids',
  138. 'title',
  139. 'url'
  140. ],
  141. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  142. ],
  143. 'offset' => [
  144. ApiBase::PARAM_TYPE => 'integer',
  145. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  146. ],
  147. 'protocol' => [
  148. ApiBase::PARAM_TYPE => self::prepareProtocols(),
  149. ApiBase::PARAM_DFLT => '',
  150. ],
  151. 'query' => null,
  152. 'namespace' => [
  153. ApiBase::PARAM_ISMULTI => true,
  154. ApiBase::PARAM_TYPE => 'namespace'
  155. ],
  156. 'limit' => [
  157. ApiBase::PARAM_DFLT => 10,
  158. ApiBase::PARAM_TYPE => 'limit',
  159. ApiBase::PARAM_MIN => 1,
  160. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  161. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  162. ],
  163. 'expandurl' => false,
  164. ];
  165. if ( $this->getConfig()->get( 'MiserMode' ) ) {
  166. $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
  167. 'api-help-param-limited-in-miser-mode',
  168. ];
  169. }
  170. return $ret;
  171. }
  172. public static function prepareProtocols() {
  173. global $wgUrlProtocols;
  174. $protocols = [ '' ];
  175. foreach ( $wgUrlProtocols as $p ) {
  176. if ( $p !== '//' ) {
  177. $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
  178. }
  179. }
  180. return $protocols;
  181. }
  182. public static function getProtocolPrefix( $protocol ) {
  183. // Find the right prefix
  184. global $wgUrlProtocols;
  185. if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
  186. foreach ( $wgUrlProtocols as $p ) {
  187. if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
  188. $protocol = $p;
  189. break;
  190. }
  191. }
  192. return $protocol;
  193. } else {
  194. return null;
  195. }
  196. }
  197. protected function getExamplesMessages() {
  198. return [
  199. 'action=query&list=exturlusage&euquery=www.mediawiki.org'
  200. => 'apihelp-query+exturlusage-example-simple',
  201. ];
  202. }
  203. public function getHelpUrls() {
  204. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Exturlusage';
  205. }
  206. }