MediaWiki  REL1_23
ApiQueryExtLinksUsage.php
Go to the documentation of this file.
00001 <?php
00030 class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
00031 
00032     public function __construct( $query, $moduleName ) {
00033         parent::__construct( $query, $moduleName, 'eu' );
00034     }
00035 
00036     public function execute() {
00037         $this->run();
00038     }
00039 
00040     public function getCacheMode( $params ) {
00041         return 'public';
00042     }
00043 
00044     public function executeGenerator( $resultPageSet ) {
00045         $this->run( $resultPageSet );
00046     }
00047 
00052     private function run( $resultPageSet = null ) {
00053         $params = $this->extractRequestParams();
00054 
00055         $query = $params['query'];
00056         $protocol = self::getProtocolPrefix( $params['protocol'] );
00057 
00058         $this->addTables( array( 'page', 'externallinks' ) ); // must be in this order for 'USE INDEX'
00059         $this->addOption( 'USE INDEX', 'el_index' );
00060         $this->addWhere( 'page_id=el_from' );
00061 
00062         global $wgMiserMode;
00063         $miser_ns = array();
00064         if ( $wgMiserMode ) {
00065             $miser_ns = $params['namespace'];
00066         } else {
00067             $this->addWhereFld( 'page_namespace', $params['namespace'] );
00068         }
00069 
00070         $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
00071 
00072         if ( $whereQuery !== null ) {
00073             $this->addWhere( $whereQuery );
00074         }
00075 
00076         $prop = array_flip( $params['prop'] );
00077         $fld_ids = isset( $prop['ids'] );
00078         $fld_title = isset( $prop['title'] );
00079         $fld_url = isset( $prop['url'] );
00080 
00081         if ( is_null( $resultPageSet ) ) {
00082             $this->addFields( array(
00083                 'page_id',
00084                 'page_namespace',
00085                 'page_title'
00086             ) );
00087             $this->addFieldsIf( 'el_to', $fld_url );
00088         } else {
00089             $this->addFields( $resultPageSet->getPageTableFields() );
00090         }
00091 
00092         $limit = $params['limit'];
00093         $offset = $params['offset'];
00094         $this->addOption( 'LIMIT', $limit + 1 );
00095         if ( isset( $offset ) ) {
00096             $this->addOption( 'OFFSET', $offset );
00097         }
00098 
00099         $res = $this->select( __METHOD__ );
00100 
00101         $result = $this->getResult();
00102         $count = 0;
00103         foreach ( $res as $row ) {
00104             if ( ++$count > $limit ) {
00105                 // We've reached the one extra which shows that there are
00106                 // additional pages to be had. Stop here...
00107                 $this->setContinueEnumParameter( 'offset', $offset + $limit );
00108                 break;
00109             }
00110 
00111             if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
00112                 continue;
00113             }
00114 
00115             if ( is_null( $resultPageSet ) ) {
00116                 $vals = array();
00117                 if ( $fld_ids ) {
00118                     $vals['pageid'] = intval( $row->page_id );
00119                 }
00120                 if ( $fld_title ) {
00121                     $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00122                     ApiQueryBase::addTitleInfo( $vals, $title );
00123                 }
00124                 if ( $fld_url ) {
00125                     $to = $row->el_to;
00126                     // expand protocol-relative urls
00127                     if ( $params['expandurl'] ) {
00128                         $to = wfExpandUrl( $to, PROTO_CANONICAL );
00129                     }
00130                     $vals['url'] = $to;
00131                 }
00132                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
00133                 if ( !$fit ) {
00134                     $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
00135                     break;
00136                 }
00137             } else {
00138                 $resultPageSet->processDbRow( $row );
00139             }
00140         }
00141 
00142         if ( is_null( $resultPageSet ) ) {
00143             $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ),
00144                 $this->getModulePrefix() );
00145         }
00146     }
00147 
00148     public function getAllowedParams() {
00149         return array(
00150             'prop' => array(
00151                 ApiBase::PARAM_ISMULTI => true,
00152                 ApiBase::PARAM_DFLT => 'ids|title|url',
00153                 ApiBase::PARAM_TYPE => array(
00154                     'ids',
00155                     'title',
00156                     'url'
00157                 )
00158             ),
00159             'offset' => array(
00160                 ApiBase::PARAM_TYPE => 'integer'
00161             ),
00162             'protocol' => array(
00163                 ApiBase::PARAM_TYPE => self::prepareProtocols(),
00164                 ApiBase::PARAM_DFLT => '',
00165             ),
00166             'query' => null,
00167             'namespace' => array(
00168                 ApiBase::PARAM_ISMULTI => true,
00169                 ApiBase::PARAM_TYPE => 'namespace'
00170             ),
00171             'limit' => array(
00172                 ApiBase::PARAM_DFLT => 10,
00173                 ApiBase::PARAM_TYPE => 'limit',
00174                 ApiBase::PARAM_MIN => 1,
00175                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00176                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00177             ),
00178             'expandurl' => false,
00179         );
00180     }
00181 
00182     public static function prepareProtocols() {
00183         global $wgUrlProtocols;
00184         $protocols = array( '' );
00185         foreach ( $wgUrlProtocols as $p ) {
00186             if ( $p !== '//' ) {
00187                 $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
00188             }
00189         }
00190 
00191         return $protocols;
00192     }
00193 
00194     public static function getProtocolPrefix( $protocol ) {
00195         // Find the right prefix
00196         global $wgUrlProtocols;
00197         if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
00198             foreach ( $wgUrlProtocols as $p ) {
00199                 if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
00200                     $protocol = $p;
00201                     break;
00202                 }
00203             }
00204 
00205             return $protocol;
00206         } else {
00207             return null;
00208         }
00209     }
00210 
00211     public function getParamDescription() {
00212         global $wgMiserMode;
00213         $p = $this->getModulePrefix();
00214         $desc = array(
00215             'prop' => array(
00216                 'What pieces of information to include',
00217                 ' ids    - Adds the ID of page',
00218                 ' title  - Adds the title and namespace ID of the page',
00219                 ' url    - Adds the URL used in the page',
00220             ),
00221             'offset' => 'Used for paging. Use the value returned for "continue"',
00222             'protocol' => array(
00223                 "Protocol of the URL. If empty and {$p}query set, the protocol is http.",
00224                 "Leave both this and {$p}query empty to list all external links"
00225             ),
00226             'query' => 'Search string without protocol. See [[Special:LinkSearch]]. ' .
00227                 'Leave empty to list all external links',
00228             'namespace' => 'The page namespace(s) to enumerate.',
00229             'limit' => 'How many pages to return.',
00230             'expandurl' => 'Expand protocol-relative URLs with the canonical protocol',
00231         );
00232 
00233         if ( $wgMiserMode ) {
00234             $desc['namespace'] = array(
00235                 $desc['namespace'],
00236                 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
00237                 'returned before continuing; in extreme cases, zero results may be returned',
00238             );
00239         }
00240 
00241         return $desc;
00242     }
00243 
00244     public function getResultProperties() {
00245         return array(
00246             'ids' => array(
00247                 'pageid' => 'integer'
00248             ),
00249             'title' => array(
00250                 'ns' => 'namespace',
00251                 'title' => 'string'
00252             ),
00253             'url' => array(
00254                 'url' => 'string'
00255             )
00256         );
00257     }
00258 
00259     public function getDescription() {
00260         return 'Enumerate pages that contain a given URL.';
00261     }
00262 
00263     public function getPossibleErrors() {
00264         return array_merge( parent::getPossibleErrors(), array(
00265             array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
00266         ) );
00267     }
00268 
00269     public function getExamples() {
00270         return array(
00271             'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
00272         );
00273     }
00274 
00275     public function getHelpUrls() {
00276         return 'https://www.mediawiki.org/wiki/API:Exturlusage';
00277     }
00278 }