* Using getAllowedParams(), makes an array of the values provided by the user,
* with key being the name of the variable, and value - validated value from user or default.
* This method can be used to generate local variables using extract().
+ *
+ * @param $prefix String: prepend this prefix to all parameter names.
*/
- public function extractRequestParams() {
+ public function extractRequestParams($prefix = '') {
$params = $this->getAllowedParams();
$results = array ();
foreach ($params as $paramName => $paramSettings)
- $results[$paramName] = $this->getParameter($paramName, $paramSettings);
+ $results[$paramName] = $this->getParameterFromSettings($prefix . $paramName, $paramSettings);
return $results;
}
- public function getParameter($paramName, $paramSettings) {
+ protected function getParameter($paramName, $prefix = '') {
+ $params = $this->getAllowedParams();
+ $paramSettings = $params[$paramName];
+ return $this->getParameterFromSettings($prefix . $paramName, $paramSettings);
+ }
+
+ protected function getParameterFromSettings($paramName, $paramSettings) {
global $wgRequest;
if (!is_array($paramSettings)) {
// Resolve redirects by querying the pagelinks table, and repeat the process
// Create a new linkBatch object for the next pass
//
- $linkBatch = $this->ResolveRedirectList($redirectIds);
+ $linkBatch = $this->resolveRedirectList($redirectIds);
// Redirects are always titles
$processTitles = true;
while (false !== ($set = $linkBatch->constructSet('page', $db)));
}
- private function ResolveRedirectList($redirectIds) {
+ private function resolveRedirectList($redirectIds) {
$linkBatch = new LinkBatch();
$db = $this->getDB();
}
public function execute() {
+ $this->profileIn();
$titles = $pageids = $revids = $redirects = null;
extract($this->extractRequestParams());
// Do nothing - some queries do not need any of the data sources.
break;
}
+ $this->profileOut();
}
+ /**
+ * This method is used by generators to pass the list of pageIDs internaly
+ */
+ public function executeForPageIDs($pageIDs) {
+ $this->profileIn();
+ $pageIDs = array_map( 'intval', $pageIDs ); // paranoia
+ $this->populatePages(null, $pageIDs, $this->getParameter('redirects'));
+ $this->profileOut();
+ }
+
protected function getAllowedParams() {
return array (
'titles' => array (
//
// Get page information for the given pageSet
//
- $this->mPageSet->profileIn();
$this->mPageSet->execute();
- $this->mPageSet->profileOut();
//
// Record page information
}
}
- protected function executeGenerator($generator) {
+ protected function executeGenerator($generatorName) {
// Find class that implements requested generator
- if (isset ($this->mQueryListModules[$generator]))
- $className = $this->mQueryListModules[$generator];
- elseif (isset ($this->mQueryPropModules[$generator])) $className = $this->mQueryPropModules[$generator];
+ if (isset ($this->mQueryListModules[$generatorName]))
+ $className = $this->mQueryListModules[$generatorName];
+ elseif (isset ($this->mQueryPropModules[$generatorName])) $className = $this->mQueryPropModules[$generatorName];
else
- ApiBase :: dieDebug(__METHOD__, "Unknown generator=$generator");
+ ApiBase :: dieDebug(__METHOD__, "Unknown generator=$generatorName");
- $module = new $className ($this, $generator, true);
- $module->requestExtraData();
+ $generator = new $className ($this, $generatorName, true);
+ if (!$generator->getCanGenerate())
+ $this->dieUsage("Module $generatorName cannot be used as a generator", "badgenerator");
+
+ $generator->requestExtraData();
// execute pageSet here to get the data required by the generator module
- $this->mPageSet->profileIn();
$this->mPageSet->execute();
- $this->mPageSet->profileOut();
-
- // change $this->mPageSet
- // TODO: implement
- $this->dieUsage('Generator execution has not been implemented', 'notimplemented');
+ $generator->profileIn();
+ $this->mPageSet = $generator->execute();
+ $generator->profileOut();
}
protected function getAllowedParams() {
'meta' => array (
ApiBase :: PARAM_ISMULTI => true,
ApiBase :: PARAM_TYPE => $this->mMetaModuleNames
- )
- // 'generator' => array (
- // ApiBase::PARAM_TYPE => $this->mAllowedGenerators
- // ),
-
-
+ ),
+ 'generator' => array (
+ ApiBase::PARAM_TYPE => $this->mAllowedGenerators
+ )
);
}
return $vers;
}
}
-?>
+?>
\ No newline at end of file
$this->profileDBOut();
$data = array ();
- ApiResult :: setIndexedTagName($data, 'p');
+ if(!$this->isGenerator())
+ ApiResult :: setIndexedTagName($data, 'p');
+
$count = 0;
while ($row = $db->fetchObject($res)) {
if (++ $count > $aplimit) {
// We've reached the one extra which shows that there are additional pages to be had. Stop here...
$msg = array (
- 'continue' => 'apfrom=' . ApiQueryBase :: keyToTitle($row->page_title
+ 'continue' => ($this->isGenerator() ? 'g' : '') . 'apfrom=' . ApiQueryBase :: keyToTitle($row->page_title
));
$this->getResult()->addValue('query-status', 'allpages', $msg);
break;
$title = Title :: makeTitle($row->page_namespace, $row->page_title);
// skip any pages that user has no rights to read
if ($title->userCanRead()) {
-
$id = intval($row->page_id);
- $pagedata = array ();
- $pagedata['id'] = $id;
- if ($title->getNamespace() !== 0)
- $pagedata['ns'] = $title->getNamespace();
- $pagedata['title'] = $title->getPrefixedText();
- $data[$id] = $pagedata;
+ if ($this->isGenerator()) {
+ $data[] = $id; // in generator mode, just assemble a list of page IDs.
+ } else {
+ $pagedata = array ();
+ $pagedata['id'] = $id;
+ if ($title->getNamespace() !== 0)
+ $pagedata['ns'] = $title->getNamespace();
+ $pagedata['title'] = $title->getPrefixedText();
+
+ $data[$id] = $pagedata;
+ }
}
}
$db->freeResult($res);
- $this->getResult()->addValue('query', 'allpages', $data);
+
+ if ($this->isGenerator()) {
+ $pageSet = new ApiPageSet($this->getQuery());
+ $pageSet->executeForPageIDs($data);
+ return $pageSet;
+ } else {
+ $this->getResult()->addValue('query', 'allpages', $data);
+ }
}
protected function getAllowedParams() {
protected function getExamples() {
return array (
'api.php?action=query&list=allpages',
- 'api.php?action=query&list=allpages&apfrom=B&aplimit=5'
+ 'api.php?action=query&list=allpages&apfrom=B&aplimit=5',
+ 'api.php?action=query&generator=allpages&gaplimit=4&prop=info (generator)'
);
}
abstract class ApiQueryBase extends ApiBase {
- private $mQueryModule, $mModuleName, $mGenerator;
+ private $mQueryModule, $mModuleName, $mIsGenerator;
- public function __construct($query, $moduleName, $generator = false) {
+ public function __construct($query, $moduleName, $isGenerator = false) {
parent :: __construct($query->getMain());
$this->mQueryModule = $query;
$this->mModuleName = $moduleName;
- $this->mGenerator = $generator;
+ $this->mIsGenerator = $isGenerator;
}
/**
return $this->mModuleName;
}
+ /**
+ * Overrides base class to prepend 'g' to every generator parameter
+ */
+ public function extractRequestParams() {
+ $prefix = '';
+ if($this->isGenerator())
+ $prefix = 'g';
+ return parent :: extractRequestParams($prefix);
+ }
+
/**
* Get the Query database connection (readonly)
*/
/**
* Return true if this instance is being used as a generator.
*/
- protected function getIsGenerator() {
- return $this->mGenerator;
+ protected function isGenerator() {
+ return $this->mIsGenerator;
}
/**