[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

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

   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  /**
  28   * @ingroup API
  29   */
  30  class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
  31  
  32  	public function __construct( ApiQuery $query, $moduleName ) {
  33          parent::__construct( $query, $moduleName, 'eu' );
  34      }
  35  
  36  	public function execute() {
  37          $this->run();
  38      }
  39  
  40  	public function getCacheMode( $params ) {
  41          return 'public';
  42      }
  43  
  44  	public function executeGenerator( $resultPageSet ) {
  45          $this->run( $resultPageSet );
  46      }
  47  
  48      /**
  49       * @param ApiPageSet $resultPageSet
  50       * @return void
  51       */
  52  	private function run( $resultPageSet = null ) {
  53          $params = $this->extractRequestParams();
  54  
  55          $query = $params['query'];
  56          $protocol = self::getProtocolPrefix( $params['protocol'] );
  57  
  58          $this->addTables( array( 'page', 'externallinks' ) ); // must be in this order for 'USE INDEX'
  59          $this->addOption( 'USE INDEX', 'el_index' );
  60          $this->addWhere( 'page_id=el_from' );
  61  
  62          $miser_ns = array();
  63          if ( $this->getConfig()->get( 'MiserMode' ) ) {
  64              $miser_ns = $params['namespace'];
  65          } else {
  66              $this->addWhereFld( 'page_namespace', $params['namespace'] );
  67          }
  68  
  69          $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
  70  
  71          if ( $whereQuery !== null ) {
  72              $this->addWhere( $whereQuery );
  73          }
  74  
  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  
  80          if ( is_null( $resultPageSet ) ) {
  81              $this->addFields( array(
  82                  'page_id',
  83                  'page_namespace',
  84                  'page_title'
  85              ) );
  86              $this->addFieldsIf( 'el_to', $fld_url );
  87          } else {
  88              $this->addFields( $resultPageSet->getPageTableFields() );
  89          }
  90  
  91          $limit = $params['limit'];
  92          $offset = $params['offset'];
  93          $this->addOption( 'LIMIT', $limit + 1 );
  94          if ( isset( $offset ) ) {
  95              $this->addOption( 'OFFSET', $offset );
  96          }
  97  
  98          $res = $this->select( __METHOD__ );
  99  
 100          $result = $this->getResult();
 101          $count = 0;
 102          foreach ( $res as $row ) {
 103              if ( ++$count > $limit ) {
 104                  // We've reached the one extra which shows that there are
 105                  // additional pages to be had. Stop here...
 106                  $this->setContinueEnumParameter( 'offset', $offset + $limit );
 107                  break;
 108              }
 109  
 110              if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
 111                  continue;
 112              }
 113  
 114              if ( is_null( $resultPageSet ) ) {
 115                  $vals = array();
 116                  if ( $fld_ids ) {
 117                      $vals['pageid'] = intval( $row->page_id );
 118                  }
 119                  if ( $fld_title ) {
 120                      $title = Title::makeTitle( $row->page_namespace, $row->page_title );
 121                      ApiQueryBase::addTitleInfo( $vals, $title );
 122                  }
 123                  if ( $fld_url ) {
 124                      $to = $row->el_to;
 125                      // expand protocol-relative urls
 126                      if ( $params['expandurl'] ) {
 127                          $to = wfExpandUrl( $to, PROTO_CANONICAL );
 128                      }
 129                      $vals['url'] = $to;
 130                  }
 131                  $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
 132                  if ( !$fit ) {
 133                      $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
 134                      break;
 135                  }
 136              } else {
 137                  $resultPageSet->processDbRow( $row );
 138              }
 139          }
 140  
 141          if ( is_null( $resultPageSet ) ) {
 142              $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ),
 143                  $this->getModulePrefix() );
 144          }
 145      }
 146  
 147  	public function getAllowedParams() {
 148          return array(
 149              'prop' => array(
 150                  ApiBase::PARAM_ISMULTI => true,
 151                  ApiBase::PARAM_DFLT => 'ids|title|url',
 152                  ApiBase::PARAM_TYPE => array(
 153                      'ids',
 154                      'title',
 155                      'url'
 156                  )
 157              ),
 158              'offset' => array(
 159                  ApiBase::PARAM_TYPE => 'integer'
 160              ),
 161              'protocol' => array(
 162                  ApiBase::PARAM_TYPE => self::prepareProtocols(),
 163                  ApiBase::PARAM_DFLT => '',
 164              ),
 165              'query' => null,
 166              'namespace' => array(
 167                  ApiBase::PARAM_ISMULTI => true,
 168                  ApiBase::PARAM_TYPE => 'namespace'
 169              ),
 170              'limit' => array(
 171                  ApiBase::PARAM_DFLT => 10,
 172                  ApiBase::PARAM_TYPE => 'limit',
 173                  ApiBase::PARAM_MIN => 1,
 174                  ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
 175                  ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 176              ),
 177              'expandurl' => false,
 178          );
 179      }
 180  
 181  	public static function prepareProtocols() {
 182          global $wgUrlProtocols;
 183          $protocols = array( '' );
 184          foreach ( $wgUrlProtocols as $p ) {
 185              if ( $p !== '//' ) {
 186                  $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
 187              }
 188          }
 189  
 190          return $protocols;
 191      }
 192  
 193  	public static function getProtocolPrefix( $protocol ) {
 194          // Find the right prefix
 195          global $wgUrlProtocols;
 196          if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
 197              foreach ( $wgUrlProtocols as $p ) {
 198                  if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
 199                      $protocol = $p;
 200                      break;
 201                  }
 202              }
 203  
 204              return $protocol;
 205          } else {
 206              return null;
 207          }
 208      }
 209  
 210  	public function getParamDescription() {
 211          $p = $this->getModulePrefix();
 212          $desc = array(
 213              'prop' => array(
 214                  'What pieces of information to include',
 215                  ' ids    - Adds the ID of page',
 216                  ' title  - Adds the title and namespace ID of the page',
 217                  ' url    - Adds the URL used in the page',
 218              ),
 219              'offset' => 'Used for paging. Use the value returned for "continue"',
 220              'protocol' => array(
 221                  "Protocol of the URL. If empty and {$p}query set, the protocol is http.",
 222                  "Leave both this and {$p}query empty to list all external links"
 223              ),
 224              'query' => 'Search string without protocol. See [[Special:LinkSearch]]. ' .
 225                  'Leave empty to list all external links',
 226              'namespace' => 'The page namespace(s) to enumerate.',
 227              'limit' => 'How many pages to return.',
 228              'expandurl' => 'Expand protocol-relative URLs with the canonical protocol',
 229          );
 230  
 231          if ( $this->getConfig()->get( 'MiserMode' ) ) {
 232              $desc['namespace'] = array(
 233                  $desc['namespace'],
 234                  "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
 235                  'returned before continuing; in extreme cases, zero results may be returned',
 236              );
 237          }
 238  
 239          return $desc;
 240      }
 241  
 242  	public function getDescription() {
 243          return 'Enumerate pages that contain a given URL.';
 244      }
 245  
 246  	public function getExamples() {
 247          return array(
 248              'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
 249          );
 250      }
 251  
 252  	public function getHelpUrls() {
 253          return 'https://www.mediawiki.org/wiki/API:Exturlusage';
 254      }
 255  }


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