From 88e42b579b8b00628b7399bcedcefa49e33c5c45 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Sat, 21 Oct 2006 08:26:32 +0000 Subject: [PATCH] API * Common field output function to simplify result generation * Recent changes parameters --- includes/api/ApiBase.php | 77 ++++++------ includes/api/ApiFormatBase.php | 6 +- includes/api/ApiPageSet.php | 2 +- includes/api/ApiQuery.php | 4 +- includes/api/ApiQueryAllpages.php | 24 ++-- includes/api/ApiQueryBase.php | 156 ++++++++++++++++++++++--- includes/api/ApiQueryInfo.php | 4 +- includes/api/ApiQueryLogEvents.php | 40 +------ includes/api/ApiQueryRecentchanges.php | 61 +++++----- includes/api/ApiQueryRevisions.php | 141 +++++++--------------- includes/api/ApiQueryWatchlist.php | 88 ++++++-------- 11 files changed, 312 insertions(+), 291 deletions(-) diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 719d724127..53deef3e97 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -293,46 +293,47 @@ abstract class ApiBase { // More validation only when choices were not given // choices were validated in parseMultiValue() - if (!is_array($type) && isset ($value)) { - - switch ($type) { - case 'NULL' : // nothing to do - break; - case 'string' : // nothing to do - break; - case 'integer' : // Force everything using intval() - $value = is_array($value) ? array_map('intval', $value) : intval($value); - break; - case 'limit' : - if (!isset ($paramSettings[self :: PARAM_MAX1]) || !isset ($paramSettings[self :: PARAM_MAX2])) - ApiBase :: dieDebug(__METHOD__, "MAX1 or MAX2 are not defined for the limit $paramName"); - if ($multi) - ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName"); - $min = isset ($paramSettings[self :: PARAM_MIN]) ? $paramSettings[self :: PARAM_MIN] : 0; - $value = intval($value); - $this->validateLimit($paramName, $value, $min, $paramSettings[self :: PARAM_MAX1], $paramSettings[self :: PARAM_MAX2]); - break; - case 'boolean' : - if ($multi) - ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName"); - break; - case 'timestamp' : - if ($multi) - ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName"); - if (!preg_match('/^[0-9]{14}$/', $value)) { - $valueName = ""; // TODO: initialization - $this->dieUsage("Invalid value '$value' for timestamp parameter $paramName", "badtimestamp_{$valueName}"); - } - break; - default : - ApiBase :: dieDebug(__METHOD__, "Param $paramName's type is unknown - $type"); - + if (isset ($value)) { + if (!is_array($type)) { + switch ($type) { + case 'NULL' : // nothing to do + break; + case 'string' : // nothing to do + break; + case 'integer' : // Force everything using intval() + $value = is_array($value) ? array_map('intval', $value) : intval($value); + break; + case 'limit' : + if (!isset ($paramSettings[self :: PARAM_MAX1]) || !isset ($paramSettings[self :: PARAM_MAX2])) + ApiBase :: dieDebug(__METHOD__, "MAX1 or MAX2 are not defined for the limit $paramName"); + if ($multi) + ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName"); + $min = isset ($paramSettings[self :: PARAM_MIN]) ? $paramSettings[self :: PARAM_MIN] : 0; + $value = intval($value); + $this->validateLimit($paramName, $value, $min, $paramSettings[self :: PARAM_MAX1], $paramSettings[self :: PARAM_MAX2]); + break; + case 'boolean' : + if ($multi) + ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName"); + break; + case 'timestamp' : + if ($multi) + ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName"); + $value = wfTimestamp(TS_UNIX, $value); + if ($value === 0) + $this->dieUsage("Invalid value '$value' for timestamp parameter $paramName", "badtimestamp_{$paramName}"); + $value = wfTimestamp(TS_MW, $value); + break; + default : + ApiBase :: dieDebug(__METHOD__, "Param $paramName's type is unknown - $type"); + + } } - } - // There should never be any duplicate values in a list - if (is_array($value)) - $value = array_unique($value); + // There should never be any duplicate values in a list + if (is_array($value)) + $value = array_unique($value); + } return $value; } diff --git a/includes/api/ApiFormatBase.php b/includes/api/ApiFormatBase.php index 9bc4e425c3..013c075294 100644 --- a/includes/api/ApiFormatBase.php +++ b/includes/api/ApiFormatBase.php @@ -100,7 +100,7 @@ abstract class ApiFormatBase extends ApiBase {
You are looking at the HTML representation of the mFormat?> format.
-HTML is good for debugging, but probably not suitable for your application.
+HTML is good for debugging, but probably is not suitable for your application.
Please see "format" parameter documentation at the API help for more information.
@@ -217,7 +217,7 @@ class ApiFormatFeedWrapper extends ApiFormatBase { $feed->outFooter(); } else { // Error has occured, print something usefull - // TODO: make this error more informative using $this->dieDebug() or similar + // TODO: make this error more informative using ApiBase :: dieDebug() or similar wfHttpError(500, 'Internal Server Error', ''); } } @@ -226,4 +226,4 @@ class ApiFormatFeedWrapper extends ApiFormatBase { return __CLASS__ . ': $Id$'; } } -?> \ No newline at end of file +?> diff --git a/includes/api/ApiPageSet.php b/includes/api/ApiPageSet.php index e8587be8d9..3cf480b552 100644 --- a/includes/api/ApiPageSet.php +++ b/includes/api/ApiPageSet.php @@ -335,7 +335,7 @@ class ApiPageSet extends ApiQueryBase { */ private function initFromQueryResult($db, $res, &$remaining = null, $processTitles = null) { if (!is_null($remaining) && is_null($processTitles)) - $this->dieDebug('Missing $processTitles parameter when $remaining is provided'); + ApiBase :: dieDebug('Missing $processTitles parameter when $remaining is provided'); while ($row = $db->fetchObject($res)) { diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index 9addeb1079..cda708d800 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -229,7 +229,7 @@ class ApiQuery extends ApiBase { // Report any missing page ids foreach ($pageSet->getMissingPageIDs() as $pageid) { $pages[$pageid] = array ( - 'id' => $pageid, + 'pageid' => $pageid, 'missing' => '' ); } @@ -237,7 +237,7 @@ class ApiQuery extends ApiBase { // Output general page information for found titles foreach ($pageSet->getGoodTitles() as $pageid => $title) { $pages[$pageid] = array ( - 'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText(), 'id' => $pageid); + 'pageid' => $pageid, 'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText()); } if (!empty ($pages)) { diff --git a/includes/api/ApiQueryAllpages.php b/includes/api/ApiQueryAllpages.php index 123264ff76..9835f0be68 100644 --- a/includes/api/ApiQueryAllpages.php +++ b/includes/api/ApiQueryAllpages.php @@ -53,8 +53,8 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase { $db = $this->getDB(); $this->addTables('page'); - $this->addWhereIf('page_is_redirect = 1', $filterredir === 'redirects'); - $this->addWhereIf('page_is_redirect = 0', $filterredir === 'nonredirects'); + if( !$this->addWhereIf('page_is_redirect = 1', $filterredir === 'redirects')) + $this->addWhereIf('page_is_redirect = 0', $filterredir === 'nonredirects'); $this->addWhereFld('page_namespace', $namespace); if (isset ($from)) $this->addWhere('page_title>=' . $db->addQuotes(ApiQueryBase :: titleToKey($from))); @@ -85,16 +85,14 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase { break; } - $title = Title :: makeTitle($row->page_namespace, $row->page_title); - // skip any pages that user has no rights to read - if ($title->userCanRead()) { - - if (is_null($resultPageSet)) { - $id = intval($row->page_id); - $data[$id] = array ( - 'id' => $id, - 'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText()); - } else { + if (is_null($resultPageSet)) { + $vals = $this->addRowInfo('page', $row); + if($vals) + $data[intval($row->page_id)] = $vals; + } else { + $title = Title :: makeTitle($row->page_namespace, $row->page_title); + // skip any pages that user has no rights to read + if ($title->userCanRead()) { $resultPageSet->processDbRow($row); } } @@ -166,4 +164,4 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase { return __CLASS__ . ': $Id$'; } } -?> \ No newline at end of file +?> diff --git a/includes/api/ApiQueryBase.php b/includes/api/ApiQueryBase.php index 297f8e11fd..5a6ca8be75 100644 --- a/includes/api/ApiQueryBase.php +++ b/includes/api/ApiQueryBase.php @@ -44,20 +44,12 @@ abstract class ApiQueryBase extends ApiBase { } protected function addTables($value) { - if(!is_array($this->tables)) - $this->dieDebug(__METHOD__, 'Must not call setTablesAsExpression() before this method'); if(is_array($value)) $this->tables = array_merge($this->tables, $value); else $this->tables[] = $value; } - protected function setTablesAsExpression($value) { - if(!empty($this->tables)) - $this->dieDebug(__METHOD__, 'Must not call addTables() before this method'); - $this->tables = $value; - } - protected function addFields($value) { if(is_array($value)) $this->fields = array_merge($this->fields, $value); @@ -66,8 +58,11 @@ abstract class ApiQueryBase extends ApiBase { } protected function addFieldsIf($value, $condition) { - if ($condition) + if ($condition) { $this->addFields($value); + return true; + } + return false; } protected function addWhere($value) { @@ -78,8 +73,11 @@ abstract class ApiQueryBase extends ApiBase { } protected function addWhereIf($value, $condition) { - if ($condition) + if ($condition) { $this->addWhere($value); + return true; + } + return false; } protected function addWhereFld($field, $value) { @@ -102,6 +100,10 @@ abstract class ApiQueryBase extends ApiBase { $this->addOption('ORDER BY', $field . ($isDirNewer ? '' : ' DESC')); } + protected function addOption($name, $value) { + $this->options[$name] = $value; + } + protected function select($method) { $this->profileDBIn(); $res = $this->getDB()->select($this->tables, $this->fields, $this->where, $method, $this->options); @@ -109,10 +111,136 @@ abstract class ApiQueryBase extends ApiBase { return $res; } - protected function addOption($name, $value) { - $this->options[$name] = $value; + + protected function addRowInfo($prefix, $row) { + + $vals = array(); + + // ID + @$tmp = $row->{$prefix . '_id'}; + if(!is_null($tmp)) $vals[$prefix . 'id'] = intval($tmp); + + // Title + $title = ApiQueryBase::addRowInfo_title($row, $prefix . '_namespace', $prefix . '_title'); + if ($title) { + if (!$title->userCanRead()) + return false; + $vals['ns'] = $title->getNamespace(); + $vals['title'] = $title->getPrefixedText(); + } + + if ($prefix === 'rc') { + + // PageId + @$tmp = $row->rc_cur_id; + if(!is_null($tmp)) $vals['pageid'] = intval($tmp); + + @$tmp = $row->rc_this_oldid; + if(!is_null($tmp)) $vals['revid'] = intval($tmp); + + @$tmp = $row->rc_last_oldid; + if(!is_null($tmp)) $vals['old_revid'] = intval($tmp); + + $title = ApiQueryBase::addRowInfo_title($row, 'rc_moved_to_ns', 'rc_moved_to_title'); + if ($title) { + if (!$title->userCanRead()) + return false; + $vals['new_ns'] = $title->getNamespace(); + $vals['new_title'] = $title->getPrefixedText(); + } + + @$tmp = $row->rc_patrolled; + if(!is_null($tmp)) $vals['patrolled'] = ''; + + } elseif ($prefix === 'log') { + + // PageId + @$tmp = $row->page_id; + if(!is_null($tmp)) $vals['pageid'] = intval($tmp); + + if ($row->log_params !== '') { + $params = explode("\n", $row->log_params); + if ($row->log_type == 'move' && isset ($params[0])) { + $newTitle = Title :: newFromText($params[0]); + if ($newTitle) { + $vals['new_ns'] = $newTitle->getNamespace(); + $vals['new_title'] = $newTitle->getPrefixedText(); + $params = null; + } + } + + if (!empty ($params)) { + $this->getResult()->setIndexedTagName($params, 'param'); + $vals = array_merge($vals, $params); + } + } + + } elseif ($prefix === 'rev') { + + // PageId + @$tmp = $row->rev_page; + if(!is_null($tmp)) $vals['pageid'] = intval($tmp); + } + + // Type + @$tmp = $row->{$prefix . '_type'}; + if(!is_null($tmp)) $vals['type'] = $tmp; + + // Action + @$tmp = $row->{$prefix . '_action'}; + if(!is_null($tmp)) $vals['action'] = $tmp; + + // Old ID + @$tmp = $row->{$prefix . '_text_id'}; + if(!is_null($tmp)) $vals['oldid'] = intval($tmp); + + // User Name / Anon IP + @$tmp = $row->{$prefix . '_user_text'}; + if(is_null($tmp)) @$tmp = $row->user_name; + if(!is_null($tmp)) { + $vals['user'] = $tmp; + @$tmp = !$row->{$prefix . '_user'}; + if(!is_null($tmp) && $tmp) + $vals['anon'] = ''; + } + + // Bot Edit + @$tmp = $row->{$prefix . '_bot'}; + if(!is_null($tmp) && $tmp) $vals['bot'] = ''; + + // New Edit + @$tmp = $row->{$prefix . '_new'}; + if(is_null($tmp)) @$tmp = $row->{$prefix . '_is_new'}; + if(!is_null($tmp) && $tmp) $vals['new'] = ''; + + // Minor Edit + @$tmp = $row->{$prefix . '_minor_edit'}; + if(is_null($tmp)) @$tmp = $row->{$prefix . '_minor'}; + if(!is_null($tmp) && $tmp) $vals['minor'] = ''; + + // Timestamp + @$tmp = $row->{$prefix . '_timestamp'}; + if(!is_null($tmp)) + $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $tmp); + + // Comment + @$tmp = $row->{$prefix . '_comment'}; + if(!empty($tmp)) // optimize bandwidth + $vals['comment'] = $tmp; + + return $vals; + } + + private static function addRowInfo_title($row, $nsfld, $titlefld) { + @$ns = $row->$nsfld; + if(!is_null($ns)) { + @$title = $row->$titlefld; + if(!empty($title)) + return Title :: makeTitle($ns, $title); + } + return false; } - + /** * Override this method to request extra fields from the pageSet * using $this->getPageSet()->requestField('fieldName') @@ -196,4 +324,4 @@ abstract class ApiQueryGeneratorBase extends ApiQueryBase { */ public abstract function executeGenerator($resultPageSet); } -?> \ No newline at end of file +?> diff --git a/includes/api/ApiQueryInfo.php b/includes/api/ApiQueryInfo.php index 9b4918808a..4ad8194a52 100644 --- a/includes/api/ApiQueryInfo.php +++ b/includes/api/ApiQueryInfo.php @@ -55,7 +55,7 @@ class ApiQueryInfo extends ApiQueryBase { foreach ($titles as $pageid => $title) { $pageInfo = array ( 'touched' => $pageTouched[$pageid], - 'lastrevid' => $pageLatest[$pageid] + 'lastrevid' => intval($pageLatest[$pageid]) ); if ($pageIsRedir[$pageid]) @@ -82,4 +82,4 @@ class ApiQueryInfo extends ApiQueryBase { return __CLASS__ . ': $Id$'; } } -?> \ No newline at end of file +?> diff --git a/includes/api/ApiQueryLogEvents.php b/includes/api/ApiQueryLogEvents.php index decde3e969..fbe7740b7b 100644 --- a/includes/api/ApiQueryLogEvents.php +++ b/includes/api/ApiQueryLogEvents.php @@ -42,7 +42,7 @@ class ApiQueryLogEvents extends ApiQueryBase { $db = $this->getDB(); extract($db->tableNames('logging', 'page', 'user'), EXTR_PREFIX_ALL, 'tbl'); - $this->setTablesAsExpression("$tbl_logging LEFT OUTER JOIN $tbl_page ON " . + $this->addTables("$tbl_logging LEFT OUTER JOIN $tbl_page ON " . "log_namespace=page_namespace AND log_title=page_title " . "INNER JOIN $tbl_user ON user_id=log_user"); @@ -90,39 +90,9 @@ class ApiQueryLogEvents extends ApiQueryBase { break; } - $vals = array ( - 'action' => "$row->log_type/$row->log_action", - 'timestamp' => $row->log_timestamp, - 'comment' => $row->log_comment, - 'pageid' => intval($row->page_id - )); - - $title = Title :: makeTitle($row->log_namespace, $row->log_title); - $vals['ns'] = $title->getNamespace(); - $vals['title'] = $title->getPrefixedText(); - - if ($row->log_params !== '') { - $params = explode("\n", $row->log_params); - if ($row->log_type == 'move' && isset ($params[0])) { - $destTitle = Title :: newFromText($params[0]); - if ($destTitle) { - $vals['tons'] = $destTitle->getNamespace(); - $vals['totitle'] = $destTitle->getPrefixedText(); - $params = null; - } - } - - if (!empty ($params)) { - $this->getResult()->setIndexedTagName($params, 'param'); - $vals = array_merge($vals, $params); - } - } - - if (!$row->log_user) - $vals['anon'] = ''; - $vals['user'] = $row->user_name; - - $data[] = $vals; + $vals = $this->addRowInfo('log', $row); + if($vals) + $data[] = $vals; } $db->freeResult($res); @@ -198,4 +168,4 @@ class ApiQueryLogEvents extends ApiQueryBase { return __CLASS__ . ': $Id$'; } } -?> \ No newline at end of file +?> diff --git a/includes/api/ApiQueryRecentchanges.php b/includes/api/ApiQueryRecentchanges.php index 34f1c46326..892e59e0ab 100644 --- a/includes/api/ApiQueryRecentchanges.php +++ b/includes/api/ApiQueryRecentchanges.php @@ -36,7 +36,7 @@ class ApiQueryRecentChanges extends ApiQueryBase { } public function execute() { - $limit = $from = $namespace = $hide = $dir = $start = $end = null; + $limit = $prop = $from = $namespace = $hide = $dir = $start = $end = null; extract($this->extractRequestParams()); $this->addTables('recentchanges'); @@ -45,6 +45,8 @@ class ApiQueryRecentChanges extends ApiQueryBase { if (!is_null($hide)) { $hide = array_flip($hide); + if(isset ($hide['anons']) && isset ($hide['liu'])) + $this->dieUsage( "Both 'anons' and 'liu' cannot be set at the same time", 'hide' ); $this->addWhereIf('rc_minor = 0', isset ($hide['minor'])); $this->addWhereIf('rc_bot = 0', isset ($hide['bots'])); $this->addWhereIf('rc_user != 0', isset ($hide['anons'])); @@ -53,14 +55,8 @@ class ApiQueryRecentChanges extends ApiQueryBase { $this->addFields(array ( 'rc_timestamp', - 'rc_user', - 'rc_user_text', 'rc_namespace', 'rc_title', - 'rc_comment', - 'rc_minor', - 'rc_bot', - 'rc_new', 'rc_cur_id', 'rc_this_oldid', 'rc_last_oldid', @@ -69,6 +65,20 @@ class ApiQueryRecentChanges extends ApiQueryBase { 'rc_moved_to_title' )); + if (!is_null($prop)) { + $prop = array_flip($prop); + $this->addFieldsIf('rc_comment', isset ($prop['comment'])); + if (isset ($prop['user'])) { + $this->addFields('rc_user'); + $this->addFields('rc_user_text'); + } + if (isset ($prop['flags'])) { + $this->addFields('rc_minor'); + $this->addFields('rc_bot'); + $this->addFields('rc_new'); + } + } + $this->addOption('LIMIT', $limit +1); $data = array (); @@ -82,32 +92,9 @@ class ApiQueryRecentChanges extends ApiQueryBase { break; } - $title = Title :: makeTitle($row->rc_namespace, $row->rc_title); - // skip any pages that user has no rights to read - if ($title->userCanRead()) { - - $id = intval($row->rc_cur_id); - $data[] = array ( - 'id' => $id, - 'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText(), - 'timestamp' => $row->rc_timestamp, - 'user' => $row->rc_user_text, - 'comment' => $row->rc_comment, - 'this_oldid' => $row->rc_this_oldid, - 'last_oldid' => $row->rc_last_oldid, - 'type' => $row->rc_type, - 'moved_to_ns' => $row->rc_moved_to_ns, - 'moved_to_title' => $row->rc_moved_to_title); - - if (!$row->rc_user) - $vals['anon'] = ''; - if ($row->rc_new) - $vals['new'] = ''; - if ($row->rc_bot) - $vals['bot'] = ''; - if ($row->rc_minor) - $vals['minor'] = ''; - } + $vals = $this->addRowInfo('rc', $row); + if($vals) + $data[] = $vals; } $db->freeResult($res); @@ -136,6 +123,14 @@ class ApiQueryRecentChanges extends ApiQueryBase { ApiBase :: PARAM_DFLT => 0, ApiBase :: PARAM_TYPE => $namespaces ), + 'prop' => array ( + ApiBase :: PARAM_ISMULTI => true, + ApiBase :: PARAM_TYPE => array ( + 'user', + 'comment', + 'flags' + ) + ), 'hide' => array ( ApiBase :: PARAM_ISMULTI => true, ApiBase :: PARAM_TYPE => array ( diff --git a/includes/api/ApiQueryRevisions.php b/includes/api/ApiQueryRevisions.php index 6b51c482d7..0ff8b33a40 100644 --- a/includes/api/ApiQueryRevisions.php +++ b/includes/api/ApiQueryRevisions.php @@ -39,16 +39,11 @@ class ApiQueryRevisions extends ApiQueryBase { $limit = $startid = $endid = $start = $end = $dir = $prop = null; extract($this->extractRequestParams()); - $db = $this->getDB(); - - // true when ordered by timestamp from older to newer, false otherwise - $dirNewer = ($dir === 'newer'); - // If any of those parameters are used, work in 'enumeration' mode. // Enum mode can only be used when exactly one page is provided. // Enumerating revisions on multiple pages make it extremelly // difficult to manage continuations and require additional sql indexes - $enumRevMode = (!is_null($limit) || !is_null($startid) || !is_null($endid) || $dirNewer || !is_null($start) || !is_null($end)); + $enumRevMode = (!is_null($limit) || !is_null($startid) || !is_null($endid) || $dir === 'newer' || !is_null($start) || !is_null($end)); $pageSet = $this->getPageSet(); $pageCount = $pageSet->getGoodTitleCount(); @@ -64,48 +59,32 @@ class ApiQueryRevisions extends ApiQueryBase { if ($pageCount > 1 && $enumRevMode) $this->dieUsage('titles, pageids or a generator was used to supply multiple pages, but the limit, startid, endid, dirNewer, start, and end parameters may only be used on a single page.', 'multpages'); - $tables = array ( - 'revision' - ); - $fields = array ( + $this->addTables('revision'); + $this->addFields(array ( 'rev_id', 'rev_page', 'rev_text_id', 'rev_minor_edit' - ); - $where = array ( - 'rev_deleted' => 0 - ); - $options = array (); + )); + $this->addWhere('rev_deleted=0'); + + $showContent = false; - $showTimestamp = $showUser = $showComment = $showContent = false; if (!is_null($prop)) { - foreach ($prop as $p) { - switch ($p) { - case 'timestamp' : - $fields[] = 'rev_timestamp'; - $showTimestamp = true; - break; - case 'user' : - $fields[] = 'rev_user'; - $fields[] = 'rev_user_text'; - $showUser = true; - break; - case 'comment' : - $fields[] = 'rev_comment'; - $showComment = true; - break; - case 'content' : - $tables[] = 'text'; - $where[] = 'rev_text_id=old_id'; - $fields[] = 'old_id'; - $fields[] = 'old_text'; - $fields[] = 'old_flags'; - $showContent = true; - break; - default : - ApiBase :: dieDebug(__METHOD__, "unknown prop $p"); - } + $prop = array_flip($prop); + $this->addFieldsIf('rev_timestamp', isset ($prop['timestamp'])); + $this->addFieldsIf('rev_comment', isset ($prop['comment'])); + if (isset ($prop['user'])) { + $this->addFields('rev_user'); + $this->addFields('rev_user_text'); + } + if (isset ($prop['content'])) { + $this->addTables('text'); + $this->addWhere('rev_text_id=old_id'); + $this->addFields('old_id'); + $this->addFields('old_text'); + $this->addFields('old_flags'); + $showContent = true; } } @@ -127,40 +106,30 @@ class ApiQueryRevisions extends ApiQueryBase { // Switching to rev_id removes the potential problem of having more than // one row with the same timestamp for the same page. // The order needs to be the same as start parameter to avoid SQL filesort. - $options['ORDER BY'] = (!is_null($startid) ? 'rev_id' : 'rev_timestamp') . ($dirNewer ? '' : ' DESC'); - $before = ($dirNewer ? '<=' : '>='); - $after = ($dirNewer ? '>=' : '<='); - - if (!is_null($startid)) - $where[] = 'rev_id' . $after . intval($startid); - if (!is_null($endid)) - $where[] = 'rev_id' . $before . intval($endid); - if (!is_null($start)) - $where[] = 'rev_timestamp' . $after . $db->addQuotes($start); - if (!is_null($end)) - $where[] = 'rev_timestamp' . $before . $db->addQuotes($end); + if (is_null($startid)) + $this->addWhereRange('rev_id', $dir, $startid, $endid); + else + $this->addWhereRange('rev_timestamp', $dir, $start, $end); // must manually initialize unset limit if (is_null($limit)) $limit = 10; - $this->validateLimit($this->encodeParamName('limit'), $limit, 1, $userMax, $botMax); // There is only one ID, use it - $where['rev_page'] = array_pop(array_keys($pageSet->getGoodTitles())); - + $this->addWhereFld('rev_page', array_pop(array_keys($pageSet->getGoodTitles()))); } elseif ($pageCount > 0) { // When working in multi-page non-enumeration mode, // limit to the latest revision only - $tables[] = 'page'; - $where[] = 'page_id=rev_page'; - $where[] = 'page_latest=rev_id'; + $this->addTables('page'); + $this->addWhere('page_id=rev_page'); + $this->addWhere('page_latest=rev_id'); $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax); // Get all page IDs - $where['page_id'] = array_keys($pageSet->getGoodTitles()); + $this->addWhereFld('page_id', array_keys($pageSet->getGoodTitles())); $limit = $pageCount; // assumption testing -- we should never get more then $pageCount rows. } @@ -168,20 +137,18 @@ class ApiQueryRevisions extends ApiQueryBase { $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax); // Get all revision IDs - $where['rev_id'] = array_keys($pageSet->getRevisionIDs()); + $this->addWhereFld('rev_id', array_keys($pageSet->getRevisionIDs())); $limit = $revCount; // assumption testing -- we should never get more then $revCount rows. } else ApiBase :: dieDebug(__METHOD__, 'param validation?'); - $options['LIMIT'] = $limit +1; - - $this->profileDBIn(); - $res = $db->select($tables, $fields, $where, __METHOD__, $options); - $this->profileDBOut(); + $this->addOption('LIMIT', $limit +1); $data = array (); $count = 0; + $db = $this->getDB(); + $res = $this->select(__METHOD__); while ($row = $db->fetchObject($res)) { if (++ $count > $limit) { @@ -192,41 +159,23 @@ class ApiQueryRevisions extends ApiQueryBase { break; } - $vals = array ( - 'revid' => intval($row->rev_id - ), 'oldid' => intval($row->rev_text_id)); - - if ($row->rev_minor_edit) { - $vals['minor'] = ''; - } - - if ($showTimestamp) - $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp); + $vals = $this->addRowInfo('rev', $row); + if ($vals) { + if ($showContent) + ApiResult :: setContent($vals, Revision :: getRevisionText($row)); - if ($showUser) { - $vals['user'] = $row->rev_user_text; - if (!$row->rev_user) - $vals['anon'] = ''; + $this->getResult()->addValue(array ( + 'query', + 'pages', + intval($row->rev_page + ), 'revisions'), intval($row->rev_id), $vals); } - - if ($showComment) - $vals['comment'] = $row->rev_comment; - - if ($showContent) { - ApiResult :: setContent($vals, Revision :: getRevisionText($row)); - } - - $this->getResult()->addValue(array ( - 'query', - 'pages', - intval($row->rev_page - ), 'revisions'), intval($row->rev_id), $vals); } $db->freeResult($res); // Ensure that all revisions are shown as '' elements $result = $this->getResult(); - if( $result->getIsRawMode()) { + if ($result->getIsRawMode()) { $data = & $result->getData(); foreach ($data['query']['pages'] as & $page) { if (is_array($page) && array_key_exists('revisions', $page)) { @@ -315,4 +264,4 @@ class ApiQueryRevisions extends ApiQueryBase { return __CLASS__ . ': $Id$'; } } -?> \ No newline at end of file +?> diff --git a/includes/api/ApiQueryWatchlist.php b/includes/api/ApiQueryWatchlist.php index 9b42698da2..9cb5accc22 100644 --- a/includes/api/ApiQueryWatchlist.php +++ b/includes/api/ApiQueryWatchlist.php @@ -71,33 +71,33 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase { if (is_null($resultPageSet)) { $this->addFields(array ( - 'rc_cur_id AS page_id', - 'rc_this_oldid AS rev_id', - 'rc_namespace AS page_namespace', - 'rc_title AS page_title', - 'rc_new AS page_is_new', - 'rc_minor AS rev_minor_edit', - 'rc_timestamp AS rev_timestamp' + 'rc_cur_id', + 'rc_this_oldid', + 'rc_namespace', + 'rc_title', + 'rc_new', + 'rc_minor', + 'rc_timestamp' )); - $this->addFieldsIf('rc_user AS rev_user', $user); - $this->addFieldsIf('rc_user_text AS rev_user_text', $user); - $this->addFieldsIf('rc_comment AS rev_comment', $comment); + $this->addFieldsIf('rc_user', $user); + $this->addFieldsIf('rc_user_text', $user); + $this->addFieldsIf('rc_comment', $comment); $this->addFieldsIf('rc_patrolled', $patrol); } elseif ($allrev) { $this->addFields(array ( - 'rc_this_oldid AS rev_id', - 'rc_namespace AS page_namespace', - 'rc_title AS page_title', - 'rc_timestamp AS rev_timestamp' + 'rc_this_oldid', + 'rc_namespace', + 'rc_title', + 'rc_timestamp' )); } else { $this->addFields(array ( - 'rc_cur_id AS page_id', - 'rc_namespace AS page_namespace', - 'rc_title AS page_title', - 'rc_timestamp AS rev_timestamp' + 'rc_cur_id', + 'rc_namespace', + 'rc_title', + 'rc_timestamp' )); } @@ -118,7 +118,7 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase { $this->addWhereFld('wl_namespace', $namespace); $this->addWhereIf('rc_this_oldid=page_latest', !$allrev); $this->addWhereIf("rc_timestamp > ''", !isset ($start) && !isset ($end)); - + $this->addOption('LIMIT', $limit +1); $data = array (); @@ -129,47 +129,27 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase { while ($row = $db->fetchObject($res)) { if (++ $count > $limit) { // We've reached the one extra which shows that there are additional pages to be had. Stop here... - $this->setContinueEnumParameter('from', $row->rev_timestamp); + $this->setContinueEnumParameter('from', $row->rc_timestamp); break; } - $title = Title :: makeTitle($row->page_namespace, $row->page_title); - // skip any pages that user has no rights to read - if ($title->userCanRead()) { - - if (is_null($resultPageSet)) { - $vals = array (); - $vals['pageid'] = intval($row->page_id); - $vals['revid'] = intval($row->rev_id); - $vals['ns'] = $title->getNamespace(); - $vals['title'] = $title->getPrefixedText(); - - if ($row->page_is_new) - $vals['new'] = ''; - if ($row->rev_minor_edit) - $vals['minor'] = ''; - - if ($user) { - if (!$row->rev_user) - $vals['anon'] = ''; - $vals['user'] = $row->rev_user_text; - } - if ($comment) - $vals['comment'] = $row->rev_comment; - if ($timestamp) - $vals['timestamp'] = $row->rev_timestamp; - if ($patrol && $row->rc_patrolled) - $vals['patrolled'] = ''; - + if (is_null($resultPageSet)) { + $vals = $this->addRowInfo('rc', $row); + if($vals) $data[] = $vals; - } - elseif ($allrev) { - $data[] = intval($row->rev_id); - } else { - $data[] = intval($row->page_id); + } else { + $title = Title :: makeTitle($row->rc_namespace, $row->rc_title); + // skip any pages that user has no rights to read + if ($title->userCanRead()) { + if ($allrev) { + $data[] = intval($row->rc_this_oldid); + } else { + $data[] = intval($row->rc_cur_id); + } } } } + $db->freeResult($res); if (is_null($resultPageSet)) { @@ -252,4 +232,4 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase { return __CLASS__ . ': $Id$'; } } -?> \ No newline at end of file +?> -- 2.20.1