ApiQueryExtLinksUsage.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /*
  3. * Created on July 7, 2007
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ('ApiQueryBase.php');
  27. }
  28. /**
  29. * @ingroup API
  30. */
  31. class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
  32. public function __construct($query, $moduleName) {
  33. parent :: __construct($query, $moduleName, 'eu');
  34. }
  35. public function execute() {
  36. $this->run();
  37. }
  38. public function executeGenerator($resultPageSet) {
  39. $this->run($resultPageSet);
  40. }
  41. private function run($resultPageSet = null) {
  42. $params = $this->extractRequestParams();
  43. $protocol = $params['protocol'];
  44. $query = $params['query'];
  45. // Find the right prefix
  46. global $wgUrlProtocols;
  47. if($protocol && !in_array($protocol, $wgUrlProtocols))
  48. {
  49. foreach ($wgUrlProtocols as $p) {
  50. if( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
  51. $protocol = $p;
  52. break;
  53. }
  54. }
  55. }
  56. else
  57. $protocol = null;
  58. $db = $this->getDB();
  59. $this->addTables(array('page','externallinks')); // must be in this order for 'USE INDEX'
  60. $this->addOption('USE INDEX', 'el_index');
  61. $this->addWhere('page_id=el_from');
  62. $this->addWhereFld('page_namespace', $params['namespace']);
  63. if(!is_null($query) || $query != '')
  64. {
  65. if(is_null($protocol))
  66. $protocol = 'http://';
  67. $likeQuery = LinkFilter::makeLike($query, $protocol);
  68. if (!$likeQuery)
  69. $this->dieUsage('Invalid query', 'bad_query');
  70. $likeQuery = substr($likeQuery, 0, strpos($likeQuery,'%')+1);
  71. $this->addWhere('el_index LIKE ' . $db->addQuotes( $likeQuery ));
  72. }
  73. else if(!is_null($protocol))
  74. $this->addWhere('el_index LIKE ' . $db->addQuotes( "$protocol%" ));
  75. $prop = array_flip($params['prop']);
  76. $fld_ids = isset($prop['ids']);
  77. $fld_title = isset($prop['title']);
  78. $fld_url = isset($prop['url']);
  79. if (is_null($resultPageSet)) {
  80. $this->addFields(array (
  81. 'page_id',
  82. 'page_namespace',
  83. 'page_title'
  84. ));
  85. $this->addFieldsIf('el_to', $fld_url);
  86. } else {
  87. $this->addFields($resultPageSet->getPageTableFields());
  88. }
  89. $limit = $params['limit'];
  90. $offset = $params['offset'];
  91. $this->addOption('LIMIT', $limit +1);
  92. if (isset ($offset))
  93. $this->addOption('OFFSET', $offset);
  94. $res = $this->select(__METHOD__);
  95. $result = $this->getResult();
  96. $count = 0;
  97. while ($row = $db->fetchObject($res)) {
  98. if (++ $count > $limit) {
  99. // We've reached the one extra which shows that there are additional pages to be had. Stop here...
  100. $this->setContinueEnumParameter('offset', $offset+$limit);
  101. break;
  102. }
  103. if (is_null($resultPageSet)) {
  104. $vals = array();
  105. if ($fld_ids)
  106. $vals['pageid'] = intval($row->page_id);
  107. if ($fld_title) {
  108. $title = Title :: makeTitle($row->page_namespace, $row->page_title);
  109. ApiQueryBase::addTitleInfo($vals, $title);
  110. }
  111. if ($fld_url)
  112. $vals['url'] = $row->el_to;
  113. $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
  114. if(!$fit)
  115. {
  116. $this->setContinueEnumParameter('offset', $offset + $count - 1);
  117. break;
  118. }
  119. } else {
  120. $resultPageSet->processDbRow($row);
  121. }
  122. }
  123. $db->freeResult($res);
  124. if (is_null($resultPageSet)) {
  125. $result->setIndexedTagName_internal(array('query', $this->getModuleName()),
  126. $this->getModulePrefix());
  127. }
  128. }
  129. public function getAllowedParams() {
  130. global $wgUrlProtocols;
  131. $protocols = array('');
  132. foreach ($wgUrlProtocols as $p) {
  133. $protocols[] = substr($p, 0, strpos($p,':'));
  134. }
  135. return array (
  136. 'prop' => array (
  137. ApiBase :: PARAM_ISMULTI => true,
  138. ApiBase :: PARAM_DFLT => 'ids|title|url',
  139. ApiBase :: PARAM_TYPE => array (
  140. 'ids',
  141. 'title',
  142. 'url'
  143. )
  144. ),
  145. 'offset' => array (
  146. ApiBase :: PARAM_TYPE => 'integer'
  147. ),
  148. 'protocol' => array (
  149. ApiBase :: PARAM_TYPE => $protocols,
  150. ApiBase :: PARAM_DFLT => '',
  151. ),
  152. 'query' => null,
  153. 'namespace' => array (
  154. ApiBase :: PARAM_ISMULTI => true,
  155. ApiBase :: PARAM_TYPE => 'namespace'
  156. ),
  157. 'limit' => array (
  158. ApiBase :: PARAM_DFLT => 10,
  159. ApiBase :: PARAM_TYPE => 'limit',
  160. ApiBase :: PARAM_MIN => 1,
  161. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  162. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  163. )
  164. );
  165. }
  166. public function getParamDescription() {
  167. return array (
  168. 'prop' => 'What pieces of information to include',
  169. 'offset' => 'Used for paging. Use the value returned for "continue"',
  170. 'protocol' => array( 'Protocol of the url. If empty and euquery set, the protocol is http.',
  171. 'Leave both this and euquery empty to list all external links'),
  172. 'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links',
  173. 'namespace' => 'The page namespace(s) to enumerate.',
  174. 'limit' => 'How many pages to return.'
  175. );
  176. }
  177. public function getDescription() {
  178. return 'Enumerate pages that contain a given URL';
  179. }
  180. protected function getExamples() {
  181. return array (
  182. 'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
  183. );
  184. }
  185. public function getVersion() {
  186. return __CLASS__ . ': $Id: ApiQueryExtLinksUsage.php 47865 2009-02-27 16:03:01Z catrope $';
  187. }
  188. }