[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

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

   1  <?php
   2  /**
   3   *
   4   *
   5   * Created on May 12, 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 wiki links on a given set of pages.
  29   *
  30   * @ingroup API
  31   */
  32  class ApiQueryLinks extends ApiQueryGeneratorBase {
  33  
  34      const LINKS = 'links';
  35      const TEMPLATES = 'templates';
  36  
  37      private $table, $prefix, $description, $helpUrl;
  38  
  39  	public function __construct( ApiQuery $query, $moduleName ) {
  40          switch ( $moduleName ) {
  41              case self::LINKS:
  42                  $this->table = 'pagelinks';
  43                  $this->prefix = 'pl';
  44                  $this->description = 'link';
  45                  $this->titlesParam = 'titles';
  46                  $this->titlesParamDescription = 'Only list links to these titles. Useful ' .
  47                      'for checking whether a certain page links to a certain title.';
  48                  $this->helpUrl = 'https://www.mediawiki.org/wiki/API:Properties#links_.2F_pl';
  49                  break;
  50              case self::TEMPLATES:
  51                  $this->table = 'templatelinks';
  52                  $this->prefix = 'tl';
  53                  $this->description = 'template';
  54                  $this->titlesParam = 'templates';
  55                  $this->titlesParamDescription = 'Only list these templates. Useful ' .
  56                      'for checking whether a certain page uses a certain template.';
  57                  $this->helpUrl = 'https://www.mediawiki.org/wiki/API:Properties#templates_.2F_tl';
  58                  break;
  59              default:
  60                  ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
  61          }
  62  
  63          parent::__construct( $query, $moduleName, $this->prefix );
  64      }
  65  
  66  	public function execute() {
  67          $this->run();
  68      }
  69  
  70  	public function getCacheMode( $params ) {
  71          return 'public';
  72      }
  73  
  74  	public function executeGenerator( $resultPageSet ) {
  75          $this->run( $resultPageSet );
  76      }
  77  
  78      /**
  79       * @param ApiPageSet $resultPageSet
  80       */
  81  	private function run( $resultPageSet = null ) {
  82          if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
  83              return; // nothing to do
  84          }
  85  
  86          $params = $this->extractRequestParams();
  87  
  88          $this->addFields( array(
  89              'pl_from' => $this->prefix . '_from',
  90              'pl_namespace' => $this->prefix . '_namespace',
  91              'pl_title' => $this->prefix . '_title'
  92          ) );
  93  
  94          $this->addTables( $this->table );
  95          $this->addWhereFld( $this->prefix . '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
  96          $this->addWhereFld( $this->prefix . '_namespace', $params['namespace'] );
  97  
  98          if ( !is_null( $params[$this->titlesParam] ) ) {
  99              $lb = new LinkBatch;
 100              foreach ( $params[$this->titlesParam] as $t ) {
 101                  $title = Title::newFromText( $t );
 102                  if ( !$title ) {
 103                      $this->setWarning( "\"$t\" is not a valid title" );
 104                  } else {
 105                      $lb->addObj( $title );
 106                  }
 107              }
 108              $cond = $lb->constructSet( $this->prefix, $this->getDB() );
 109              if ( $cond ) {
 110                  $this->addWhere( $cond );
 111              }
 112          }
 113  
 114          if ( !is_null( $params['continue'] ) ) {
 115              $cont = explode( '|', $params['continue'] );
 116              $this->dieContinueUsageIf( count( $cont ) != 3 );
 117              $op = $params['dir'] == 'descending' ? '<' : '>';
 118              $plfrom = intval( $cont[0] );
 119              $plns = intval( $cont[1] );
 120              $pltitle = $this->getDB()->addQuotes( $cont[2] );
 121              $this->addWhere(
 122                  "{$this->prefix}_from $op $plfrom OR " .
 123                  "({$this->prefix}_from = $plfrom AND " .
 124                  "({$this->prefix}_namespace $op $plns OR " .
 125                  "({$this->prefix}_namespace = $plns AND " .
 126                  "{$this->prefix}_title $op= $pltitle)))"
 127              );
 128          }
 129  
 130          $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
 131          // Here's some MySQL craziness going on: if you use WHERE foo='bar'
 132          // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
 133          // but instead goes and filesorts, because the index for foo was used
 134          // already. To work around this, we drop constant fields in the WHERE
 135          // clause from the ORDER BY clause
 136          $order = array();
 137          if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
 138              $order[] = $this->prefix . '_from' . $sort;
 139          }
 140          if ( count( $params['namespace'] ) != 1 ) {
 141              $order[] = $this->prefix . '_namespace' . $sort;
 142          }
 143  
 144          $order[] = $this->prefix . '_title' . $sort;
 145          $this->addOption( 'ORDER BY', $order );
 146          $this->addOption( 'USE INDEX', $this->prefix . '_from' );
 147          $this->addOption( 'LIMIT', $params['limit'] + 1 );
 148  
 149          $res = $this->select( __METHOD__ );
 150  
 151          if ( is_null( $resultPageSet ) ) {
 152              $count = 0;
 153              foreach ( $res as $row ) {
 154                  if ( ++$count > $params['limit'] ) {
 155                      // We've reached the one extra which shows that
 156                      // there are additional pages to be had. Stop here...
 157                      $this->setContinueEnumParameter( 'continue',
 158                          "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
 159                      break;
 160                  }
 161                  $vals = array();
 162                  ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( $row->pl_namespace, $row->pl_title ) );
 163                  $fit = $this->addPageSubItem( $row->pl_from, $vals );
 164                  if ( !$fit ) {
 165                      $this->setContinueEnumParameter( 'continue',
 166                          "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
 167                      break;
 168                  }
 169              }
 170          } else {
 171              $titles = array();
 172              $count = 0;
 173              foreach ( $res as $row ) {
 174                  if ( ++$count > $params['limit'] ) {
 175                      // We've reached the one extra which shows that
 176                      // there are additional pages to be had. Stop here...
 177                      $this->setContinueEnumParameter( 'continue',
 178                          "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
 179                      break;
 180                  }
 181                  $titles[] = Title::makeTitle( $row->pl_namespace, $row->pl_title );
 182              }
 183              $resultPageSet->populateFromTitles( $titles );
 184          }
 185      }
 186  
 187  	public function getAllowedParams() {
 188          return array(
 189              'namespace' => array(
 190                  ApiBase::PARAM_TYPE => 'namespace',
 191                  ApiBase::PARAM_ISMULTI => true
 192              ),
 193              'limit' => array(
 194                  ApiBase::PARAM_DFLT => 10,
 195                  ApiBase::PARAM_TYPE => 'limit',
 196                  ApiBase::PARAM_MIN => 1,
 197                  ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
 198                  ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 199              ),
 200              'continue' => null,
 201              $this->titlesParam => array(
 202                  ApiBase::PARAM_ISMULTI => true,
 203              ),
 204              'dir' => array(
 205                  ApiBase::PARAM_DFLT => 'ascending',
 206                  ApiBase::PARAM_TYPE => array(
 207                      'ascending',
 208                      'descending'
 209                  )
 210              ),
 211          );
 212      }
 213  
 214  	public function getParamDescription() {
 215          $desc = $this->description;
 216  
 217          return array(
 218              'namespace' => "Show {$desc}s in this namespace(s) only",
 219              'limit' => "How many {$desc}s to return",
 220              'continue' => 'When more results are available, use this to continue',
 221              $this->titlesParam => $this->titlesParamDescription,
 222              'dir' => 'The direction in which to list',
 223          );
 224      }
 225  
 226  	public function getDescription() {
 227          return "Returns all {$this->description}s from the given page(s).";
 228      }
 229  
 230  	public function getExamples() {
 231          $desc = $this->description;
 232          $name = $this->getModuleName();
 233  
 234          return array(
 235              "api.php?action=query&prop={$name}&titles=Main%20Page" => "Get {$desc}s from the [[Main Page]]",
 236              "api.php?action=query&generator={$name}&titles=Main%20Page&prop=info"
 237                  => "Get information about the {$desc} pages in the [[Main Page]]",
 238              "api.php?action=query&prop={$name}&titles=Main%20Page&{$this->prefix}namespace=2|10"
 239                  => "Get {$desc}s from the Main Page in the User and Template namespaces",
 240          );
 241      }
 242  
 243  	public function getHelpUrls() {
 244          return $this->helpUrl;
 245      }
 246  }


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