ApiQueryExternalLinks.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /*
  3. * Created on May 13, 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. * A query module to list all external URLs found on a given set of pages.
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryExternalLinks extends ApiQueryBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'el');
  36. }
  37. public function execute() {
  38. if ( $this->getPageSet()->getGoodTitleCount() == 0 )
  39. return;
  40. $params = $this->extractRequestParams();
  41. $this->addFields(array (
  42. 'el_from',
  43. 'el_to'
  44. ));
  45. $this->addTables('externallinks');
  46. $this->addWhereFld('el_from', array_keys($this->getPageSet()->getGoodTitles()));
  47. # Don't order by el_from if it's constant in the WHERE clause
  48. if(count($this->getPageSet()->getGoodTitles()) != 1)
  49. $this->addOption('ORDER BY', 'el_from');
  50. $this->addOption('LIMIT', $params['limit'] + 1);
  51. if(!is_null($params['offset']))
  52. $this->addOption('OFFSET', $params['offset']);
  53. $db = $this->getDB();
  54. $res = $this->select(__METHOD__);
  55. $count = 0;
  56. while ($row = $db->fetchObject($res)) {
  57. if (++$count > $params['limit']) {
  58. // We've reached the one extra which shows that
  59. // there are additional pages to be had. Stop here...
  60. $this->setContinueEnumParameter('offset', @$params['offset'] + $params['limit']);
  61. break;
  62. }
  63. $entry = array();
  64. ApiResult :: setContent($entry, $row->el_to);
  65. $fit = $this->addPageSubItem($row->el_from, $entry);
  66. if(!$fit)
  67. {
  68. $this->setContinueEnumParameter('offset', @$params['offset'] + $count - 1);
  69. break;
  70. }
  71. }
  72. $db->freeResult($res);
  73. }
  74. public function getAllowedParams() {
  75. return array(
  76. 'limit' => array(
  77. ApiBase :: PARAM_DFLT => 10,
  78. ApiBase :: PARAM_TYPE => 'limit',
  79. ApiBase :: PARAM_MIN => 1,
  80. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  81. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  82. ),
  83. 'offset' => null,
  84. );
  85. }
  86. public function getParamDescription () {
  87. return array(
  88. 'limit' => 'How many links to return',
  89. 'offset' => 'When more results are available, use this to continue',
  90. );
  91. }
  92. public function getDescription() {
  93. return 'Returns all external urls (not interwikies) from the given page(s)';
  94. }
  95. protected function getExamples() {
  96. return array (
  97. "Get a list of external links on the [[Main Page]]:",
  98. " api.php?action=query&prop=extlinks&titles=Main%20Page",
  99. );
  100. }
  101. public function getVersion() {
  102. return __CLASS__ . ': $Id: ApiQueryExternalLinks.php 46845 2009-02-05 14:30:59Z catrope $';
  103. }
  104. }