diff options
author | jenkins-bot <jenkins-bot@gerrit.wikimedia.org> | 2016-10-07 04:36:45 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@wikimedia.org> | 2016-10-07 04:36:45 +0000 |
commit | bddb40db1c34d4b4fd31f3227923b5bc3929f395 (patch) | |
tree | ed9a46ceb1c34700ad1e3fb6ff0c7cf7132a3934 /includes/api | |
parent | b6d8421ab62231c6de53ac71a1bfc0937695ecb3 (diff) | |
parent | a514341e365cecbc6b7e75dbc31242583d026fb1 (diff) | |
download | mediawikicore-bddb40db1c34d4b4fd31f3227923b5bc3929f395.tar.gz mediawikicore-bddb40db1c34d4b4fd31f3227923b5bc3929f395.zip |
Merge "API: Add hooks for ApiQueryBase's query and row-processing"
Diffstat (limited to 'includes/api')
-rw-r--r-- | includes/api/ApiQueryAllRevisions.php | 11 | ||||
-rw-r--r-- | includes/api/ApiQueryBase.php | 32 | ||||
-rw-r--r-- | includes/api/ApiQueryGeneratorBase.php | 9 | ||||
-rw-r--r-- | includes/api/ApiQueryRecentChanges.php | 6 | ||||
-rw-r--r-- | includes/api/ApiQueryRevisions.php | 6 | ||||
-rw-r--r-- | includes/api/ApiQueryUserContributions.php | 6 |
6 files changed, 58 insertions, 12 deletions
diff --git a/includes/api/ApiQueryAllRevisions.php b/includes/api/ApiQueryAllRevisions.php index d548c46ce028..b64b2c8401b1 100644 --- a/includes/api/ApiQueryAllRevisions.php +++ b/includes/api/ApiQueryAllRevisions.php @@ -166,7 +166,8 @@ class ApiQueryAllRevisions extends ApiQueryRevisionsBase { $orderby[] = "rev_id $sort"; $this->addOption( 'ORDER BY', $orderby ); - $res = $this->select( __METHOD__ ); + $hookData = []; + $res = $this->select( __METHOD__, [], $hookData ); $pageMap = []; // Maps rev_page to array index $count = 0; $nextIndex = 0; @@ -210,12 +211,12 @@ class ApiQueryAllRevisions extends ApiQueryRevisionsBase { ]; ApiResult::setIndexedTagName( $a['revisions'], 'rev' ); ApiQueryBase::addTitleInfo( $a, $title ); - $fit = $result->addValue( [ 'query', $this->getModuleName() ], $index, $a ); + $fit = $this->processRow( $row, $a['revisions'][0], $hookData ) && + $result->addValue( [ 'query', $this->getModuleName() ], $index, $a ); } else { $index = $pageMap[$row->rev_page]; - $fit = $result->addValue( - [ 'query', $this->getModuleName(), $index, 'revisions' ], - null, $rev ); + $fit = $this->processRow( $row, $rev, $hookData ) && + $result->addValue( [ 'query', $this->getModuleName(), $index, 'revisions' ], null, $rev ); } if ( !$fit ) { $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" ); diff --git a/includes/api/ApiQueryBase.php b/includes/api/ApiQueryBase.php index 36ad3a4be7ed..bba53755c111 100644 --- a/includes/api/ApiQueryBase.php +++ b/includes/api/ApiQueryBase.php @@ -347,9 +347,12 @@ abstract class ApiQueryBase extends ApiBase { * 'options' => ..., * 'join_conds' => ... * ] + * @param array|null &$hookData If set, the ApiQueryBaseBeforeQuery and + * ApiQueryBaseAfterQuery hooks will be called, and the + * ApiQueryBaseProcessRow hook will be expected. * @return ResultWrapper */ - protected function select( $method, $extraQuery = [] ) { + protected function select( $method, $extraQuery = [], array &$hookData = null ) { $tables = array_merge( $this->tables, @@ -372,12 +375,39 @@ abstract class ApiQueryBase extends ApiBase { isset( $extraQuery['join_conds'] ) ? (array)$extraQuery['join_conds'] : [] ); + if ( $hookData !== null ) { + Hooks::run( 'ApiQueryBaseBeforeQuery', + [ $this, &$tables, &$fields, &$where, &$options, &$join_conds, &$hookData ] + ); + } + $res = $this->getDB()->select( $tables, $fields, $where, $method, $options, $join_conds ); + if ( $hookData !== null ) { + Hooks::run( 'ApiQueryBaseAfterQuery', [ $this, $res, &$hookData ] ); + } + return $res; } /** + * Call the ApiQueryBaseProcessRow hook + * + * Generally, a module that passed $hookData to self::select() will call + * this just before calling ApiResult::addValue(), and treat a false return + * here in the same way it treats a false return from addValue(). + * + * @since 1.28 + * @param object $row Database row + * @param array &$data Data to be added to the result + * @param array &$hookData Hook data from ApiQueryBase::select() + * @return bool Return false if row processing should end with continuation + */ + protected function processRow( $row, array &$data, array &$hookData ) { + return Hooks::run( 'ApiQueryBaseProcessRow', [ $this, $row, &$data, &$hookData ] ); + } + + /** * @param string $query * @param string $protocol * @return null|string diff --git a/includes/api/ApiQueryGeneratorBase.php b/includes/api/ApiQueryGeneratorBase.php index 67fe0d61a7f6..f7b94c7c0156 100644 --- a/includes/api/ApiQueryGeneratorBase.php +++ b/includes/api/ApiQueryGeneratorBase.php @@ -46,6 +46,15 @@ abstract class ApiQueryGeneratorBase extends ApiQueryBase { } /** + * Indicate whether the module is in generator mode + * @since 1.28 + * @return bool + */ + public function isInGeneratorMode() { + return $this->mGeneratorPageSet !== null; + } + + /** * Get the PageSet object to work on. * If this module is generator, the pageSet object is different from other module's * @return ApiPageSet diff --git a/includes/api/ApiQueryRecentChanges.php b/includes/api/ApiQueryRecentChanges.php index c4c8afbdbf11..8b11dc2a47d2 100644 --- a/includes/api/ApiQueryRecentChanges.php +++ b/includes/api/ApiQueryRecentChanges.php @@ -361,9 +361,10 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase { $this->token = $params['token']; $this->addOption( 'LIMIT', $params['limit'] + 1 ); + $hookData = []; $count = 0; /* Perform the actual query. */ - $res = $this->select( __METHOD__ ); + $res = $this->select( __METHOD__, [], $hookData ); $revids = []; $titles = []; @@ -391,7 +392,8 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase { $vals = $this->extractRowInfo( $row ); /* Add that row's data to our final output. */ - $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals ); + $fit = $this->processRow( $row, $vals, $hookData ) && + $result->addValue( [ 'query', $this->getModuleName() ], null, $vals ); if ( !$fit ) { $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" ); break; diff --git a/includes/api/ApiQueryRevisions.php b/includes/api/ApiQueryRevisions.php index b816f4384291..3259927a23b6 100644 --- a/includes/api/ApiQueryRevisions.php +++ b/includes/api/ApiQueryRevisions.php @@ -313,7 +313,8 @@ class ApiQueryRevisions extends ApiQueryRevisionsBase { $count = 0; $generated = []; - $res = $this->select( __METHOD__ ); + $hookData = []; + $res = $this->select( __METHOD__, [], $hookData ); foreach ( $res as $row ) { if ( ++$count > $this->limit ) { @@ -350,7 +351,8 @@ class ApiQueryRevisions extends ApiQueryRevisionsBase { } } - $fit = $this->addPageSubItem( $row->rev_page, $rev, 'rev' ); + $fit = $this->processRow( $row, $rev, $hookData ) && + $this->addPageSubItem( $row->rev_page, $rev, 'rev' ); if ( !$fit ) { if ( $enumRevMode ) { $this->setContinueEnumParameter( 'continue', diff --git a/includes/api/ApiQueryUserContributions.php b/includes/api/ApiQueryUserContributions.php index f92a916f6cbc..b85bec4899c5 100644 --- a/includes/api/ApiQueryUserContributions.php +++ b/includes/api/ApiQueryUserContributions.php @@ -111,8 +111,9 @@ class ApiQueryContributions extends ApiQueryBase { $this->prepareQuery(); + $hookData = []; // Do the actual query. - $res = $this->select( __METHOD__ ); + $res = $this->select( __METHOD__, [], $hookData ); if ( $this->fld_sizediff ) { $revIds = []; @@ -139,7 +140,8 @@ class ApiQueryContributions extends ApiQueryBase { } $vals = $this->extractRowInfo( $row ); - $fit = $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals ); + $fit = $this->processRow( $row, $vals, $hookData ) && + $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals ); if ( !$fit ) { $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) ); break; |