Skip to content

Commit

Permalink
Handle continuation. Close #34.
Browse files Browse the repository at this point in the history
  • Loading branch information
elifoster committed Mar 6, 2016
1 parent 28117bd commit e4ae150
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions api/OreDictQuerySearchApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public function getAllowedParams() {
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_DFLT => '',
),
'offset' => array(
ApiBase::PARAM_TYPE => 'integer',
ApiBase::PARAM_DFLT => 0,
ApiBase::PARAM_MIN => 0,
)
);
}

Expand All @@ -40,6 +45,7 @@ public function getParamDescription() {
'mod' => 'Restricts results to this mod',
'tag' => 'Restricts results to this tag name',
'name' => 'Restricts results to this item name',
'offset' => 'The query offset, like a continue param.'
);
}

Expand All @@ -60,6 +66,7 @@ public function execute() {
$mod = $this->getParameter('mod');
$name = $this->getParameter('name');
$limit = $this->getParameter('limit');
$offset = $this->getParameter('offset');
$dbr = wfGetDB(DB_SLAVE);

$conditions = array();
Expand All @@ -80,18 +87,31 @@ public function execute() {
'ext_oredict_items',
'*',
$conditions,
__METHOD__,
array('LIMIT' => $limit)
__METHOD__
);

$ret = array();

$i = 0;
$more = $results->numRows() - $offset > $limit;
if ($results->numRows() > 0) {
foreach ($results as $row) {
$ret[] = OreDict::getArrayFromRow($row);
if ($i < $offset) {
$i++;
continue;
}
if (count($ret) < $limit) {
$i++;
$ret[] = OreDict::getArrayFromRow($row);
}
}
}

if ($more) {
$this->getResult()->addValue('continue', 'offset', $i);
}

$this->getResult()->addValue('query', 'totalhits', $results->numRows());
$this->getResult()->addValue('query', 'oredictentries', $ret);
}
}
}

0 comments on commit e4ae150

Please sign in to comment.