ApiQueryExtLinksUsage.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. * @ingroup API
  24. */
  25. class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
  26. public function __construct( ApiQuery $query, $moduleName ) {
  27. parent::__construct( $query, $moduleName, 'eu' );
  28. }
  29. public function execute() {
  30. $this->run();
  31. }
  32. public function getCacheMode( $params ) {
  33. return 'public';
  34. }
  35. public function executeGenerator( $resultPageSet ) {
  36. $this->run( $resultPageSet );
  37. }
  38. /**
  39. * @param ApiPageSet $resultPageSet
  40. * @return void
  41. */
  42. private function run( $resultPageSet = null ) {
  43. $params = $this->extractRequestParams();
  44. $db = $this->getDB();
  45. $query = $params['query'];
  46. $protocol = self::getProtocolPrefix( $params['protocol'] );
  47. $this->addTables( [ 'page', 'externallinks' ] );
  48. $this->addWhere( 'page_id=el_from' );
  49. $miser_ns = [];
  50. if ( $this->getConfig()->get( 'MiserMode' ) ) {
  51. $miser_ns = $params['namespace'] ?: [];
  52. } else {
  53. $this->addWhereFld( 'page_namespace', $params['namespace'] );
  54. }
  55. $orderBy = [];
  56. if ( $query !== null && $query !== '' ) {
  57. if ( $protocol === null ) {
  58. $protocol = 'http://';
  59. }
  60. // Normalize query to match the normalization applied for the externallinks table
  61. $query = Parser::normalizeLinkUrl( $protocol . $query );
  62. $conds = LinkFilter::getQueryConditions( $query, [
  63. 'protocol' => '',
  64. 'oneWildcard' => true,
  65. 'db' => $db
  66. ] );
  67. if ( !$conds ) {
  68. $this->dieWithError( 'apierror-badquery' );
  69. }
  70. $this->addWhere( $conds );
  71. if ( !isset( $conds['el_index_60'] ) ) {
  72. $orderBy[] = 'el_index_60';
  73. }
  74. } else {
  75. $orderBy[] = 'el_index_60';
  76. if ( $protocol !== null ) {
  77. $this->addWhere( 'el_index_60' . $db->buildLike( "$protocol", $db->anyString() ) );
  78. } else {
  79. // We're querying all protocols, filter out duplicate protocol-relative links
  80. $this->addWhere( $db->makeList( [
  81. 'el_to NOT' . $db->buildLike( '//', $db->anyString() ),
  82. 'el_index_60 ' . $db->buildLike( 'http://', $db->anyString() ),
  83. ], LIST_OR ) );
  84. }
  85. }
  86. $orderBy[] = 'el_id';
  87. $this->addOption( 'ORDER BY', $orderBy );
  88. $this->addFields( $orderBy ); // Make sure
  89. $prop = array_flip( $params['prop'] );
  90. $fld_ids = isset( $prop['ids'] );
  91. $fld_title = isset( $prop['title'] );
  92. $fld_url = isset( $prop['url'] );
  93. if ( is_null( $resultPageSet ) ) {
  94. $this->addFields( [
  95. 'page_id',
  96. 'page_namespace',
  97. 'page_title'
  98. ] );
  99. $this->addFieldsIf( 'el_to', $fld_url );
  100. } else {
  101. $this->addFields( $resultPageSet->getPageTableFields() );
  102. }
  103. $limit = $params['limit'];
  104. $this->addOption( 'LIMIT', $limit + 1 );
  105. if ( $params['continue'] !== null ) {
  106. $cont = explode( '|', $params['continue'] );
  107. $this->dieContinueUsageIf( count( $cont ) !== count( $orderBy ) );
  108. $i = count( $cont ) - 1;
  109. $cond = $orderBy[$i] . ' >= ' . $db->addQuotes( rawurldecode( $cont[$i] ) );
  110. while ( $i-- > 0 ) {
  111. $field = $orderBy[$i];
  112. $v = $db->addQuotes( rawurldecode( $cont[$i] ) );
  113. $cond = "($field > $v OR ($field = $v AND $cond))";
  114. }
  115. $this->addWhere( $cond );
  116. }
  117. $res = $this->select( __METHOD__ );
  118. $result = $this->getResult();
  119. $count = 0;
  120. foreach ( $res as $row ) {
  121. if ( ++$count > $limit ) {
  122. // We've reached the one extra which shows that there are
  123. // additional pages to be had. Stop here...
  124. $this->setContinue( $orderBy, $row );
  125. break;
  126. }
  127. if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
  128. continue;
  129. }
  130. if ( is_null( $resultPageSet ) ) {
  131. $vals = [
  132. ApiResult::META_TYPE => 'assoc',
  133. ];
  134. if ( $fld_ids ) {
  135. $vals['pageid'] = (int)$row->page_id;
  136. }
  137. if ( $fld_title ) {
  138. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  139. ApiQueryBase::addTitleInfo( $vals, $title );
  140. }
  141. if ( $fld_url ) {
  142. $to = $row->el_to;
  143. // expand protocol-relative urls
  144. if ( $params['expandurl'] ) {
  145. $to = wfExpandUrl( $to, PROTO_CANONICAL );
  146. }
  147. $vals['url'] = $to;
  148. }
  149. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
  150. if ( !$fit ) {
  151. $this->setContinue( $orderBy, $row );
  152. break;
  153. }
  154. } else {
  155. $resultPageSet->processDbRow( $row );
  156. }
  157. }
  158. if ( is_null( $resultPageSet ) ) {
  159. $result->addIndexedTagName( [ 'query', $this->getModuleName() ],
  160. $this->getModulePrefix() );
  161. }
  162. }
  163. private function setContinue( $orderBy, $row ) {
  164. $fields = [];
  165. foreach ( $orderBy as $field ) {
  166. $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] );
  167. }
  168. $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) );
  169. }
  170. public function getAllowedParams() {
  171. $ret = [
  172. 'prop' => [
  173. ApiBase::PARAM_ISMULTI => true,
  174. ApiBase::PARAM_DFLT => 'ids|title|url',
  175. ApiBase::PARAM_TYPE => [
  176. 'ids',
  177. 'title',
  178. 'url'
  179. ],
  180. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  181. ],
  182. 'continue' => [
  183. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  184. ],
  185. 'protocol' => [
  186. ApiBase::PARAM_TYPE => self::prepareProtocols(),
  187. ApiBase::PARAM_DFLT => '',
  188. ],
  189. 'query' => null,
  190. 'namespace' => [
  191. ApiBase::PARAM_ISMULTI => true,
  192. ApiBase::PARAM_TYPE => 'namespace'
  193. ],
  194. 'limit' => [
  195. ApiBase::PARAM_DFLT => 10,
  196. ApiBase::PARAM_TYPE => 'limit',
  197. ApiBase::PARAM_MIN => 1,
  198. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  199. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  200. ],
  201. 'expandurl' => false,
  202. ];
  203. if ( $this->getConfig()->get( 'MiserMode' ) ) {
  204. $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
  205. 'api-help-param-limited-in-miser-mode',
  206. ];
  207. }
  208. return $ret;
  209. }
  210. public static function prepareProtocols() {
  211. global $wgUrlProtocols;
  212. $protocols = [ '' ];
  213. foreach ( $wgUrlProtocols as $p ) {
  214. if ( $p !== '//' ) {
  215. $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
  216. }
  217. }
  218. return $protocols;
  219. }
  220. public static function getProtocolPrefix( $protocol ) {
  221. // Find the right prefix
  222. global $wgUrlProtocols;
  223. if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
  224. foreach ( $wgUrlProtocols as $p ) {
  225. if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
  226. $protocol = $p;
  227. break;
  228. }
  229. }
  230. return $protocol;
  231. } else {
  232. return null;
  233. }
  234. }
  235. protected function getExamplesMessages() {
  236. return [
  237. 'action=query&list=exturlusage&euquery=www.mediawiki.org'
  238. => 'apihelp-query+exturlusage-example-simple',
  239. ];
  240. }
  241. public function getHelpUrls() {
  242. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Exturlusage';
  243. }
  244. }