[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/api/ -> ApiQueryExternalLinks.php (source)

   1  <?php
   2  /**
   3   *
   4   *
   5   * Created on May 13, 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  /**
  28   * A query module to list all external URLs found on a given set of pages.
  29   *
  30   * @ingroup API
  31   */
  32  class ApiQueryExternalLinks extends ApiQueryBase {
  33  
  34  	public function __construct( ApiQuery $query, $moduleName ) {
  35          parent::__construct( $query, $moduleName, 'el' );
  36      }
  37  
  38  	public function execute() {
  39          if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
  40              return;
  41          }
  42  
  43          $params = $this->extractRequestParams();
  44  
  45          $query = $params['query'];
  46          $protocol = ApiQueryExtLinksUsage::getProtocolPrefix( $params['protocol'] );
  47  
  48          $this->addFields( array(
  49              'el_from',
  50              'el_to'
  51          ) );
  52  
  53          $this->addTables( 'externallinks' );
  54          $this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
  55  
  56          $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
  57  
  58          if ( $whereQuery !== null ) {
  59              $this->addWhere( $whereQuery );
  60          }
  61  
  62          // Don't order by el_from if it's constant in the WHERE clause
  63          if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
  64              $this->addOption( 'ORDER BY', 'el_from' );
  65          }
  66  
  67          // If we're querying all protocols, use DISTINCT to avoid repeating protocol-relative links twice
  68          if ( $protocol === null ) {
  69              $this->addOption( 'DISTINCT' );
  70          }
  71  
  72          $this->addOption( 'LIMIT', $params['limit'] + 1 );
  73          $offset = isset( $params['offset'] ) ? $params['offset'] : 0;
  74          if ( $offset ) {
  75              $this->addOption( 'OFFSET', $params['offset'] );
  76          }
  77  
  78          $res = $this->select( __METHOD__ );
  79  
  80          $count = 0;
  81          foreach ( $res as $row ) {
  82              if ( ++$count > $params['limit'] ) {
  83                  // We've reached the one extra which shows that
  84                  // there are additional pages to be had. Stop here...
  85                  $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
  86                  break;
  87              }
  88              $entry = array();
  89              $to = $row->el_to;
  90              // expand protocol-relative urls
  91              if ( $params['expandurl'] ) {
  92                  $to = wfExpandUrl( $to, PROTO_CANONICAL );
  93              }
  94              ApiResult::setContent( $entry, $to );
  95              $fit = $this->addPageSubItem( $row->el_from, $entry );
  96              if ( !$fit ) {
  97                  $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
  98                  break;
  99              }
 100          }
 101      }
 102  
 103  	public function getCacheMode( $params ) {
 104          return 'public';
 105      }
 106  
 107  	public function getAllowedParams() {
 108          return array(
 109              'limit' => array(
 110                  ApiBase::PARAM_DFLT => 10,
 111                  ApiBase::PARAM_TYPE => 'limit',
 112                  ApiBase::PARAM_MIN => 1,
 113                  ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
 114                  ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 115              ),
 116              'offset' => array(
 117                  ApiBase::PARAM_TYPE => 'integer'
 118              ),
 119              'protocol' => array(
 120                  ApiBase::PARAM_TYPE => ApiQueryExtLinksUsage::prepareProtocols(),
 121                  ApiBase::PARAM_DFLT => '',
 122              ),
 123              'query' => null,
 124              'expandurl' => false,
 125          );
 126      }
 127  
 128  	public function getParamDescription() {
 129          $p = $this->getModulePrefix();
 130  
 131          return array(
 132              'limit' => 'How many links to return',
 133              'offset' => 'When more results are available, use this to continue',
 134              'protocol' => array(
 135                  "Protocol of the URL. If empty and {$p}query set, the protocol is http.",
 136                  "Leave both this and {$p}query empty to list all external links"
 137              ),
 138              'query' => 'Search string without protocol. Useful for checking ' .
 139                  'whether a certain page contains a certain external url',
 140              'expandurl' => 'Expand protocol-relative URLs with the canonical protocol',
 141          );
 142      }
 143  
 144  	public function getDescription() {
 145          return 'Returns all external URLs (not interwikis) from the given page(s).';
 146      }
 147  
 148  	public function getExamples() {
 149          return array(
 150              'api.php?action=query&prop=extlinks&titles=Main%20Page'
 151                  => 'Get a list of external links on the [[Main Page]]',
 152          );
 153      }
 154  
 155  	public function getHelpUrls() {
 156          return 'https://www.mediawiki.org/wiki/API:Properties#extlinks_.2F_el';
 157      }
 158  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1