Merge "ChangesListSpecialPage: Implement two new hooks superseding 4 old ones"
[lhc/web/wiklou.git] / includes / api / ApiQueryAllLinks.php
index 3744e3c..7b5123d 100644 (file)
  */
 class ApiQueryAllLinks extends ApiQueryGeneratorBase {
 
+       private $table, $tablePrefix, $indexTag,
+               $description, $descriptionWhat, $descriptionTargets, $descriptionLinking;
+       private $fieldTitle = 'title';
+       private $dfltNamespace = NS_MAIN;
+       private $hasNamespace = true;
+       private $useIndex = null;
+       private $props = array(), $propHelp = array();
+
        public function __construct( $query, $moduleName ) {
                switch ( $moduleName ) {
                        case 'alllinks':
                                $prefix = 'al';
                                $this->table = 'pagelinks';
                                $this->tablePrefix = 'pl_';
-                               $this->dfltNamespace = NS_MAIN;
+                               $this->useIndex = 'pl_namespace';
                                $this->indexTag = 'l';
                                $this->description = 'Enumerate all links that point to a given namespace';
                                $this->descriptionWhat = 'link';
@@ -49,12 +57,45 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase {
                                $this->table = 'templatelinks';
                                $this->tablePrefix = 'tl_';
                                $this->dfltNamespace = NS_TEMPLATE;
+                               $this->useIndex = 'tl_namespace';
                                $this->indexTag = 't';
-                               $this->description = 'List all transclusions (pages embedded using {{x}}), including non-existing';
+                               $this->description =
+                                       'List all transclusions (pages embedded using {{x}}), including non-existing';
                                $this->descriptionWhat = 'transclusion';
                                $this->descriptionTargets = 'transcluded titles';
                                $this->descriptionLinking = 'transcluding';
                                break;
+                       case 'allfileusages':
+                               $prefix = 'af';
+                               $this->table = 'imagelinks';
+                               $this->tablePrefix = 'il_';
+                               $this->fieldTitle = 'to';
+                               $this->dfltNamespace = NS_FILE;
+                               $this->hasNamespace = false;
+                               $this->indexTag = 'f';
+                               $this->description = 'List all file usages, including non-existing';
+                               $this->descriptionWhat = 'file';
+                               $this->descriptionTargets = 'file titles';
+                               $this->descriptionLinking = 'using';
+                               break;
+                       case 'allredirects':
+                               $prefix = 'ar';
+                               $this->table = 'redirect';
+                               $this->tablePrefix = 'rd_';
+                               $this->indexTag = 'r';
+                               $this->description = 'List all redirects to a namespace';
+                               $this->descriptionWhat = 'redirect';
+                               $this->descriptionTargets = 'target pages';
+                               $this->descriptionLinking = 'redirecting';
+                               $this->props = array(
+                                       'fragment' => 'rd_fragment',
+                                       'interwiki' => 'rd_interwiki',
+                               );
+                               $this->propHelp = array(
+                                       ' fragment - Adds the fragment from the redirect, if any',
+                                       ' interwiki - Adds the interwiki prefix from the redirect, if any',
+                               );
+                               break;
                        default:
                                ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
                }
@@ -83,21 +124,32 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase {
                $params = $this->extractRequestParams();
 
                $pfx = $this->tablePrefix;
+               $fieldTitle = $this->fieldTitle;
                $prop = array_flip( $params['prop'] );
                $fld_ids = isset( $prop['ids'] );
                $fld_title = isset( $prop['title'] );
+               if ( $this->hasNamespace ) {
+                       $namespace = $params['namespace'];
+               } else {
+                       $namespace = $this->dfltNamespace;
+               }
 
                if ( $params['unique'] ) {
-                       if ( $fld_ids ) {
+                       $matches = array_intersect_key( $prop, $this->props + array( 'ids' => 1 ) );
+                       if ( $matches ) {
+                               $p = $this->getModulePrefix();
                                $this->dieUsage(
-                                       "{$this->getModuleName()} cannot return corresponding page ids in unique {$this->descriptionWhat}s mode",
-                                       'params' );
+                                       "Cannot use {$p}prop=" . join( '|', array_keys( $matches ) ) . " with {$p}unique",
+                                       'params'
+                               );
                        }
                        $this->addOption( 'DISTINCT' );
                }
 
                $this->addTables( $this->table );
-               $this->addWhereFld( $pfx . 'namespace', $params['namespace'] );
+               if ( $this->hasNamespace ) {
+                       $this->addWhereFld( $pfx . 'namespace', $namespace );
+               }
 
                $continue = !is_null( $params['continue'] );
                if ( $continue ) {
@@ -106,38 +158,46 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase {
                        if ( $params['unique'] ) {
                                $this->dieContinueUsageIf( count( $continueArr ) != 1 );
                                $continueTitle = $db->addQuotes( $continueArr[0] );
-                               $this->addWhere( "{$pfx}title $op= $continueTitle" );
+                               $this->addWhere( "{$pfx}{$fieldTitle} $op= $continueTitle" );
                        } else {
                                $this->dieContinueUsageIf( count( $continueArr ) != 2 );
                                $continueTitle = $db->addQuotes( $continueArr[0] );
                                $continueFrom = intval( $continueArr[1] );
                                $this->addWhere(
-                                       "{$pfx}title $op $continueTitle OR " .
-                                       "({$pfx}title = $continueTitle AND " .
+                                       "{$pfx}{$fieldTitle} $op $continueTitle OR " .
+                                       "({$pfx}{$fieldTitle} = $continueTitle AND " .
                                        "{$pfx}from $op= $continueFrom)"
                                );
                        }
                }
 
                // 'continue' always overrides 'from'
-               $from = ( $continue || is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
-               $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
-               $this->addWhereRange( $pfx . 'title', 'newer', $from, $to );
+               $from = ( $continue || $params['from'] === null ? null :
+                       $this->titlePartToKey( $params['from'], $namespace ) );
+               $to = ( $params['to'] === null ? null :
+                       $this->titlePartToKey( $params['to'], $namespace ) );
+               $this->addWhereRange( $pfx . $fieldTitle, 'newer', $from, $to );
 
                if ( isset( $params['prefix'] ) ) {
-                       $this->addWhere( $pfx . 'title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
+                       $this->addWhere( $pfx . $fieldTitle . $db->buildLike( $this->titlePartToKey(
+                               $params['prefix'], $namespace ), $db->anyString() ) );
                }
 
-               $this->addFields( array( 'pl_title' => $pfx . 'title' ) );
+               $this->addFields( array( 'pl_title' => $pfx . $fieldTitle ) );
                $this->addFieldsIf( array( 'pl_from' => $pfx . 'from' ), !$params['unique'] );
+               foreach ( $this->props as $name => $field ) {
+                       $this->addFieldsIf( $field, isset( $prop[$name] ) );
+               }
 
-               $this->addOption( 'USE INDEX', $pfx . 'namespace' );
+               if ( $this->useIndex ) {
+                       $this->addOption( 'USE INDEX', $this->useIndex );
+               }
                $limit = $params['limit'];
                $this->addOption( 'LIMIT', $limit + 1 );
 
                $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
                $orderBy = array();
-               $orderBy[] = $pfx . 'title' . $sort;
+               $orderBy[] = $pfx . $fieldTitle . $sort;
                if ( !$params['unique'] ) {
                        $orderBy[] = $pfx . 'from' . $sort;
                }
@@ -150,8 +210,9 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase {
                $count = 0;
                $result = $this->getResult();
                foreach ( $res as $row ) {
-                       if ( ++ $count > $limit ) {
-                               // We've reached the one extra which shows that there are additional pages to be had. Stop here...
+                       if ( ++$count > $limit ) {
+                               // We've reached the one extra which shows that there are
+                               // additional pages to be had. Stop here...
                                if ( $params['unique'] ) {
                                        $this->setContinueEnumParameter( 'continue', $row->pl_title );
                                } else {
@@ -166,9 +227,14 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase {
                                        $vals['fromid'] = intval( $row->pl_from );
                                }
                                if ( $fld_title ) {
-                                       $title = Title::makeTitle( $params['namespace'], $row->pl_title );
+                                       $title = Title::makeTitle( $namespace, $row->pl_title );
                                        ApiQueryBase::addTitleInfo( $vals, $title );
                                }
+                               foreach ( $this->props as $name => $field ) {
+                                       if ( isset( $prop[$name] ) && $row->$field !== null && $row->$field !== '' ) {
+                                               $vals[$name] = $row->$field;
+                                       }
+                               }
                                $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
                                if ( !$fit ) {
                                        if ( $params['unique'] ) {
@@ -179,7 +245,7 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase {
                                        break;
                                }
                        } elseif ( $params['unique'] ) {
-                               $titles[] = Title::makeTitle( $params['namespace'], $row->pl_title );
+                               $titles[] = Title::makeTitle( $namespace, $row->pl_title );
                        } else {
                                $pageids[] = $row->pl_from;
                        }
@@ -195,7 +261,7 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase {
        }
 
        public function getAllowedParams() {
-               return array(
+               $allowedParams = array(
                        'continue' => null,
                        'from' => null,
                        'to' => null,
@@ -204,10 +270,9 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase {
                        'prop' => array(
                                ApiBase::PARAM_ISMULTI => true,
                                ApiBase::PARAM_DFLT => 'title',
-                               ApiBase::PARAM_TYPE => array(
-                                       'ids',
-                                       'title'
-                               )
+                               ApiBase::PARAM_TYPE => array_merge(
+                                       array( 'ids', 'title' ), array_keys( $this->props )
+                               ),
                        ),
                        'namespace' => array(
                                ApiBase::PARAM_DFLT => $this->dfltNamespace,
@@ -228,6 +293,11 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase {
                                )
                        ),
                );
+               if ( !$this->hasNamespace ) {
+                       unset( $allowedParams['namespace'] );
+               }
+
+               return $allowedParams;
        }
 
        public function getParamDescription() {
@@ -235,24 +305,33 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase {
                $what = $this->descriptionWhat;
                $targets = $this->descriptionTargets;
                $linking = $this->descriptionLinking;
-               return array(
+               $paramDescription = array(
                        'from' => "The title of the $what to start enumerating from",
                        'to' => "The title of the $what to stop enumerating at",
                        'prefix' => "Search for all $targets that begin with this value",
                        'unique' => array(
-                               "Only show distinct $targets. Cannot be used with {$p}prop=ids.",
+                               "Only show distinct $targets. Cannot be used with {$p}prop=" .
+                                       join( '|', array_keys( array( 'ids' => 1 ) + $this->props ) ) . '.',
                                'When used as a generator, yields target pages instead of source pages.',
                        ),
                        'prop' => array(
                                'What pieces of information to include',
-                               " ids    - Adds the pageid of the $linking page (Cannot be used with {$p}unique)",
-                               " title  - Adds the title of the $what",
+                               " ids      - Adds the pageid of the $linking page (Cannot be used with {$p}unique)",
+                               " title    - Adds the title of the $what",
                        ),
                        'namespace' => 'The namespace to enumerate',
                        'limit' => 'How many total items to return',
                        'continue' => 'When more results are available, use this to continue',
                        'dir' => 'The direction in which to list',
                );
+               foreach ( $this->propHelp as $help ) {
+                       $paramDescription['prop'][] = "$help (Cannot be used with {$p}unique)";
+               }
+               if ( !$this->hasNamespace ) {
+                       unset( $paramDescription['namespace'] );
+               }
+
+               return $paramDescription;
        }
 
        public function getResultProperties() {
@@ -274,8 +353,12 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase {
        public function getPossibleErrors() {
                $m = $this->getModuleName();
                $what = $this->descriptionWhat;
+
                return array_merge( parent::getPossibleErrors(), array(
-                       array( 'code' => 'params', 'info' => "{$m} cannot return corresponding page ids in unique {$what}s mode" ),
+                       array(
+                               'code' => 'params',
+                               'info' => "{$m} cannot return corresponding page ids in unique {$what}s mode"
+                       ),
                ) );
        }
 
@@ -284,20 +367,22 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase {
                $name = $this->getModuleName();
                $what = $this->descriptionWhat;
                $targets = $this->descriptionTargets;
+
                return array(
                        "api.php?action=query&list={$name}&{$p}from=B&{$p}prop=ids|title"
-                                       => "List $targets with page ids they are from, including missing ones. Start at B",
+                               => "List $targets with page ids they are from, including missing ones. Start at B",
                        "api.php?action=query&list={$name}&{$p}unique=&{$p}from=B"
-                                       => "List unique $targets",
+                               => "List unique $targets",
                        "api.php?action=query&generator={$name}&g{$p}unique=&g{$p}from=B"
-                                       => "Gets all $targets, marking the missing ones",
+                               => "Gets all $targets, marking the missing ones",
                        "api.php?action=query&generator={$name}&g{$p}from=B"
-                                       => "Gets pages containing the {$what}s",
+                               => "Gets pages containing the {$what}s",
                );
        }
 
        public function getHelpUrls() {
                $name = ucfirst( $this->getModuleName() );
+
                return "https://www.mediawiki.org/wiki/API:{$name}";
        }
 }