00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if (!defined('MEDIAWIKI')) {
00027
00028 require_once ('ApiQueryBase.php');
00029 }
00030
00034 class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
00035
00036 public function __construct($query, $moduleName) {
00037 parent :: __construct($query, $moduleName, 'eu');
00038 }
00039
00040 public function execute() {
00041 $this->run();
00042 }
00043
00044 public function getCacheMode( $params ) {
00045 return 'public';
00046 }
00047
00048 public function executeGenerator($resultPageSet) {
00049 $this->run($resultPageSet);
00050 }
00051
00052 private function run($resultPageSet = null) {
00053
00054 $params = $this->extractRequestParams();
00055
00056 $protocol = $params['protocol'];
00057 $query = $params['query'];
00058
00059
00060 global $wgUrlProtocols;
00061 if($protocol && !in_array($protocol, $wgUrlProtocols))
00062 {
00063 foreach ($wgUrlProtocols as $p) {
00064 if( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
00065 $protocol = $p;
00066 break;
00067 }
00068 }
00069 }
00070 else
00071 $protocol = null;
00072
00073 $db = $this->getDB();
00074 $this->addTables(array('page','externallinks'));
00075 $this->addOption('USE INDEX', 'el_index');
00076 $this->addWhere('page_id=el_from');
00077 $this->addWhereFld('page_namespace', $params['namespace']);
00078
00079 if(!is_null($query) || $query != '')
00080 {
00081 if(is_null($protocol))
00082 $protocol = 'http://';
00083
00084 $likeQuery = LinkFilter::makeLike($query, $protocol);
00085 if (!$likeQuery)
00086 $this->dieUsage('Invalid query', 'bad_query');
00087 $likeQuery = substr($likeQuery, 0, strpos($likeQuery,'%')+1);
00088 $this->addWhere('el_index LIKE ' . $db->addQuotes( $likeQuery ));
00089 }
00090 else if(!is_null($protocol))
00091 $this->addWhere('el_index LIKE ' . $db->addQuotes( "$protocol%" ));
00092
00093 $prop = array_flip($params['prop']);
00094 $fld_ids = isset($prop['ids']);
00095 $fld_title = isset($prop['title']);
00096 $fld_url = isset($prop['url']);
00097
00098 if (is_null($resultPageSet)) {
00099 $this->addFields(array (
00100 'page_id',
00101 'page_namespace',
00102 'page_title'
00103 ));
00104 $this->addFieldsIf('el_to', $fld_url);
00105 } else {
00106 $this->addFields($resultPageSet->getPageTableFields());
00107 }
00108
00109 $limit = $params['limit'];
00110 $offset = $params['offset'];
00111 $this->addOption('LIMIT', $limit +1);
00112 if (isset ($offset))
00113 $this->addOption('OFFSET', $offset);
00114
00115 $res = $this->select(__METHOD__);
00116
00117 $result = $this->getResult();
00118 $count = 0;
00119 while ($row = $db->fetchObject($res)) {
00120 if (++ $count > $limit) {
00121
00122 $this->setContinueEnumParameter('offset', $offset+$limit);
00123 break;
00124 }
00125
00126 if (is_null($resultPageSet)) {
00127 $vals = array();
00128 if ($fld_ids)
00129 $vals['pageid'] = intval($row->page_id);
00130 if ($fld_title) {
00131 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
00132 ApiQueryBase::addTitleInfo($vals, $title);
00133 }
00134 if ($fld_url)
00135 $vals['url'] = $row->el_to;
00136 $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
00137 if(!$fit)
00138 {
00139 $this->setContinueEnumParameter('offset', $offset + $count - 1);
00140 break;
00141 }
00142 } else {
00143 $resultPageSet->processDbRow($row);
00144 }
00145 }
00146 $db->freeResult($res);
00147
00148 if (is_null($resultPageSet)) {
00149 $result->setIndexedTagName_internal(array('query', $this->getModuleName()),
00150 $this->getModulePrefix());
00151 }
00152 }
00153
00154 public function getAllowedParams() {
00155 global $wgUrlProtocols;
00156 $protocols = array('');
00157 foreach ($wgUrlProtocols as $p) {
00158 $protocols[] = substr($p, 0, strpos($p,':'));
00159 }
00160
00161 return array (
00162 'prop' => array (
00163 ApiBase :: PARAM_ISMULTI => true,
00164 ApiBase :: PARAM_DFLT => 'ids|title|url',
00165 ApiBase :: PARAM_TYPE => array (
00166 'ids',
00167 'title',
00168 'url'
00169 )
00170 ),
00171 'offset' => array (
00172 ApiBase :: PARAM_TYPE => 'integer'
00173 ),
00174 'protocol' => array (
00175 ApiBase :: PARAM_TYPE => $protocols,
00176 ApiBase :: PARAM_DFLT => '',
00177 ),
00178 'query' => null,
00179 'namespace' => array (
00180 ApiBase :: PARAM_ISMULTI => true,
00181 ApiBase :: PARAM_TYPE => 'namespace'
00182 ),
00183 'limit' => array (
00184 ApiBase :: PARAM_DFLT => 10,
00185 ApiBase :: PARAM_TYPE => 'limit',
00186 ApiBase :: PARAM_MIN => 1,
00187 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00188 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00189 )
00190 );
00191 }
00192
00193 public function getParamDescription() {
00194 return array (
00195 'prop' => 'What pieces of information to include',
00196 'offset' => 'Used for paging. Use the value returned for "continue"',
00197 'protocol' => array( 'Protocol of the url. If empty and euquery set, the protocol is http.',
00198 'Leave both this and euquery empty to list all external links'),
00199 'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links',
00200 'namespace' => 'The page namespace(s) to enumerate.',
00201 'limit' => 'How many pages to return.'
00202 );
00203 }
00204
00205 public function getDescription() {
00206 return 'Enumerate pages that contain a given URL';
00207 }
00208
00209 protected function getExamples() {
00210 return array (
00211 'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
00212 );
00213 }
00214
00215 public function getVersion() {
00216 return __CLASS__ . ': $Id: ApiQueryExtLinksUsage.php 69986 2010-07-27 03:57:39Z tstarling $';
00217 }
00218 }