/** * @param ApiPageSet $resultPageSet * @return */ private function run($resultPageSet = null) { $params = $this->extractRequestParams(); $exclude = false; $this->requireOnlyOneParameter($params, 'coord', 'page'); if (isset($params['coord'])) { $arr = explode('|', $params['coord']); if (count($arr) != 2 || !GeoData::validateCoord($arr[0], $arr[1], $params['globe'])) { $this->dieUsage('Invalid coordinate provided', '_invalid-coord'); } $lat = $arr[0]; $lon = $arr[1]; } elseif (isset($params['page'])) { $t = Title::newFromText($params['page']); if (!$t || !$t->canExist()) { $this->dieUsage("Invalid page title ``{$params['page']}'' provided", '_invalid-page'); } if (!$t->exists()) { $this->dieUsage("Page ``{$params['page']}'' does not exist", '_nonexistent-page'); } $coord = GeoData::getPageCoordinates($t); if (!$coord) { $this->dieUsage('Page coordinates unknown', '_no-coordinates'); } $lat = $coord->lat; $lon = $coord->lon; $exclude = $t->getArticleID(); } $lat = floatval($lat); $lon = floatval($lon); $radius = intval($params['radius']); $rect = GeoMath::rectAround($lat, $lon, $radius); $dbr = wfGetDB(DB_SLAVE); $this->addTables(array('page', 'geo_tags')); $this->addFields(array('gt_lat', 'gt_lon', 'gt_primary')); // retrieve some fields only if page set needs them if (is_null($resultPageSet)) { $this->addFields(array('page_id', 'page_namespace', 'page_title')); } else { $this->addFields(WikiPage::selectFields()); } foreach ($params['prop'] as $prop) { if (isset(Coord::$fieldMapping[$prop])) { $this->addFields(Coord::$fieldMapping[$prop]); } } $this->addWhereFld('gt_globe', $params['globe']); $this->addWhereFld('gt_lat_int', self::intRange($rect["minLat"], $rect["maxLat"])); $this->addWhereFld('gt_lon_int', self::intRange($rect["minLon"], $rect["maxLon"])); $this->addWhereRange('gt_lat', 'newer', $rect["minLat"], $rect["maxLat"], false); if ($rect["minLon"] > $rect["maxLon"]) { $this->addWhere("gt_lon < {$rect['maxLon']} OR gt_lon > {$rect['minLon']}"); } else { $this->addWhereRange('gt_lon', 'newer', $rect["minLon"], $rect["maxLon"], false); } $this->addWhereFld('page_namespace', $params['namespace']); $this->addWhere('gt_page_id = page_id'); if ($exclude) { $this->addWhere("gt_page_id <> {$exclude}"); } if (isset($params['maxdim'])) { $this->addWhere('gt_dim < ' . intval($params['maxdim'])); } $primary = array_flip($params['primary']); $this->addWhereIf(array('gt_primary' => 1), isset($primary['yes']) && !isset($primary['no'])); $this->addWhereIf(array('gt_primary' => 0), !isset($primary['yes']) && isset($primary['no'])); $this->addOption('USE INDEX', 'gt_spatial'); $limit = $params['limit']; $res = $this->select(__METHOD__); $rows = array(); foreach ($res as $row) { $row->dist = GeoMath::distance($lat, $lon, $row->gt_lat, $row->gt_lon); $rows[] = $row; } // sort in PHP because sorting via SQL involves a filesort usort($rows, 'ApiQueryGeoSearch::compareRows'); $result = $this->getResult(); foreach ($rows as $row) { if (!$limit--) { break; } if (is_null($resultPageSet)) { $title = Title::newFromRow($row); $vals = array('pageid' => intval($row->page_id), 'ns' => intval($title->getNamespace()), 'title' => $title->getPrefixedText(), 'lat' => floatval($row->gt_lat), 'lon' => floatval($row->gt_lon), 'dist' => round($row->dist, 1)); if ($row->gt_primary) { $vals['primary'] = ''; } foreach ($params['prop'] as $prop) { if (isset(Coord::$fieldMapping[$prop]) && isset($row->{Coord::$fieldMapping[$prop]})) { $field = Coord::$fieldMapping[$prop]; $vals[$prop] = $row->{$field}; } } $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals); if (!$fit) { break; } } else { $resultPageSet->processDbRow($row); } } if (is_null($resultPageSet)) { $result->setIndexedTagName_internal(array('query', $this->getModuleName()), $this->getModulePrefix()); } }
/** * @param $resultPageSet ApiPageSet * @return */ private function run($resultPageSet = null) { $params = $this->extractRequestParams(); $exclude = false; $this->requireOnlyOneParameter($params, 'coord', 'page'); if (isset($params['coord'])) { $arr = explode('|', $params['coord']); if (count($arr) != 2 || !GeoData::validateCoord($arr[0], $arr[1], $params['globe'])) { $this->dieUsage('Invalid coordinate provided', '_invalid-coord'); } $lat = $arr[0]; $lon = $arr[1]; } elseif (isset($params['page'])) { $t = Title::newFromText($params['page']); if (!$t || !$t->canExist()) { $this->dieUsage("Invalid page title ``{$params['page']}'' provided", '_invalid-page'); } if (!$t->exists()) { $this->dieUsage("Page ``{$params['page']}'' does not exist", '_nonexistent-page'); } $coord = GeoData::getPageCoordinates($t); if (!$coord) { $this->dieUsage('Page coordinates unknown', '_no-coordinates'); } $lat = $coord->lat; $lon = $coord->lon; $exclude = $t->getArticleID(); } $lat = floatval($lat); $lon = floatval($lon); $radius = intval($params['radius']); $rect = GeoMath::rectAround($lat, $lon, $radius); $dbr = wfGetDB(DB_SLAVE); $this->addTables(array('geo_tags', 'page')); $this->addFields(array('gt_lat', 'gt_lon', 'gt_primary', "{$dbr->tablePrefix()}gd_distance( {$lat}, {$lon}, gt_lat, gt_lon ) AS dist")); // retrieve some fields only if page set needs them if (is_null($resultPageSet)) { $this->addFields(array('page_id', 'page_namespace', 'page_title')); } else { $this->addFields(array("{$dbr->tableName('page')}.*")); } foreach ($params['prop'] as $prop) { if (isset(Coord::$fieldMapping[$prop])) { $this->addFields(Coord::$fieldMapping[$prop]); } } $this->addWhereFld('gt_globe', $params['globe']); $this->addWhereRange('gt_lat', 'newer', $rect["minLat"], $rect["maxLat"], false); $this->addWhereRange('gt_lon', 'newer', $rect["minLon"], $rect["maxLon"], false); //$this->addWhere( 'dist < ' . intval( $radius ) ); hasta be in HAVING, not WHERE $this->addWhereFld('page_namespace', $params['namespace']); $this->addWhere('gt_page_id = page_id'); if ($exclude) { $this->addWhere("gt_page_id <> {$exclude}"); } if (isset($params['maxdim'])) { $this->addWhere('gt_dim < ' . intval($params['maxdim'])); } $primary = array_flip($params['primary']); $this->addWhereIf(array('gt_primary' => 1), isset($primary['yes']) && !isset($primary['no'])); $this->addWhereIf(array('gt_primary' => 0), !isset($primary['yes']) && isset($primary['no'])); $this->addOption('ORDER BY', 'dist'); $limit = $params['limit']; $this->addOption('LIMIT', $limit); $res = $this->select(__METHOD__); $result = $this->getResult(); foreach ($res as $row) { if (is_null($resultPageSet)) { $title = Title::newFromRow($row); $vals = array('pageid' => intval($row->page_id), 'ns' => intval($title->getNamespace()), 'title' => $title->getPrefixedText(), 'lat' => floatval($row->gt_lat), 'lon' => floatval($row->gt_lon), 'dist' => round($row->dist, 1)); if ($row->gt_primary) { $vals['primary'] = ''; } foreach ($params['prop'] as $prop) { if (isset(Coord::$fieldMapping[$prop]) && isset($row->{$field})) { $field = Coord::$fieldMapping[$prop]; $vals[$prop] = $row->{$field}; } } $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals); if (!$fit) { break; } } else { $resultPageSet->processDbRow($row); } } }