From 685ef510eeead335039dceff2e65ab666744240d Mon Sep 17 00:00:00 2001 From: X! Date: Sat, 7 Aug 2010 18:50:23 +0000 Subject: [PATCH] -(bug 24484) Add prop=pageprops module -Add $wgPageProps global variable --- RELEASE-NOTES | 3 +- includes/AutoLoader.php | 1 + includes/DefaultSettings.php | 9 ++ includes/api/ApiQuery.php | 1 + includes/api/ApiQueryPageProps.php | 184 +++++++++++++++++++++++++++++ 5 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 includes/api/ApiQueryPageProps.php diff --git a/RELEASE-NOTES b/RELEASE-NOTES index f88beb57e7..2579818ad7 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -330,7 +330,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN the timestamp of the last edit * (bug 24677) axto= parameters added to allcategories, allimages, alllinks, allmessages, allpages, and allusers -* (bug 24236) Add add, remove, add-self, remove-self tags to meta=siteinfo&siprop=usergroups +* (bug 24236) Add add, remove, add-self, remove-self tags to meta=siteinfo&siprop=usergroups +* (bug 24484) Add prop=pageprops module === Languages updated in 1.17 === diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 568ae85c50..139be8eba4 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -306,6 +306,7 @@ $wgAutoloadLocalClasses = array( 'ApiQueryLangLinks' => 'includes/api/ApiQueryLangLinks.php', 'ApiQueryLinks' => 'includes/api/ApiQueryLinks.php', 'ApiQueryLogEvents' => 'includes/api/ApiQueryLogEvents.php', + 'ApiQueryPageProps' => 'includes/api/ApiQueryPageProps.php', 'ApiQueryProtectedTitles' => 'includes/api/ApiQueryProtectedTitles.php', 'ApiQueryRandom' => 'includes/api/ApiQueryRandom.php', 'ApiQueryRecentChanges' => 'includes/api/ApiQueryRecentChanges.php', diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 532f624492..32ca758559 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -4431,6 +4431,15 @@ $wgSpecialPageCacheUpdates = array( */ $wgExceptionHooks = array(); +/** + * List of page property names and descriptions of what they are. + * This is used for the API prop=pageprops module to know which + * page props to search for. + */ +$wgPageProps = array( + 'hiddencat' => 'Whether or not the page has a category with the __HIDDENCAT__ magic word', +); + /** * Page property link table invalidation lists. When a page property * changes, this may require other link tables to be updated (eg diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index cb7080df5c..10e502b3aa 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -58,6 +58,7 @@ class ApiQuery extends ApiBase { 'extlinks' => 'ApiQueryExternalLinks', 'categoryinfo' => 'ApiQueryCategoryInfo', 'duplicatefiles' => 'ApiQueryDuplicateFiles', + 'pageprops' => 'ApiQueryPageProps', ); private $mQueryListModules = array( diff --git a/includes/api/ApiQueryPageProps.php b/includes/api/ApiQueryPageProps.php new file mode 100644 index 0000000000..04d568703a --- /dev/null +++ b/includes/api/ApiQueryPageProps.php @@ -0,0 +1,184 @@ +@gmail.com + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + */ + +if ( !defined( 'MEDIAWIKI' ) ) { + // Eclipse helper - will be ignored in production + require_once( 'ApiQueryBase.php' ); +} + +/** + * A query module to show basic page information. + * + * @ingroup API + */ +class ApiQueryPageProps extends ApiQueryBase { + + public function __construct( $query, $moduleName ) { + parent::__construct( $query, $moduleName, 'pp' ); + } + + public function execute() { + $this->params = $this->extractRequestParams(); + + $pageSet = $this->getPageSet(); + $this->titles = $pageSet->getGoodTitles(); + $this->missing = $pageSet->getMissingTitles(); + $this->everything = $this->titles + $this->missing; + $result = $this->getResult(); + + uasort( $this->everything, array( 'Title', 'compare' ) ); + if ( !is_null( $this->params['continue'] ) ) { + // Throw away any titles we're gonna skip so they don't + // clutter queries + $cont = explode( '|', $this->params['continue'] ); + if ( count( $cont ) != 2 ) { + $this->dieUsage( 'Invalid continue param. You should pass the original ' . + 'value returned by the previous query', '_badcontinue' ); + } + $conttitle = Title::makeTitleSafe( $cont[0], $cont[1] ); + foreach ( $this->everything as $pageid => $title ) { + if ( Title::compare( $title, $conttitle ) >= 0 ) { + break; + } + unset( $this->titles[$pageid] ); + unset( $this->missing[$pageid] ); + unset( $this->everything[$pageid] ); + } + } + + + + foreach ( $this->everything as $pageid => $title ) { + $pageInfo = $this->extractPageInfo( $pageid, $title ); + $fit = $result->addValue( array( + 'query', + 'pages' + ), $pageid, $pageInfo ); + if ( !$fit ) { + $this->setContinueEnumParameter( 'continue', + $title->getNamespace() . '|' . + $title->getText() ); + break; + } + } + } + + /** + * Get a result array with information about a title + * @param $pageid int Page ID (negative for missing titles) + * @param $title Title object + * @return array + */ + private function extractPageInfo( $pageid, $title ) { + global $wgPageProps; + + $pageInfo = array(); + if ( $title->exists() ) { + + $dbr = wfGetDB( DB_SLAVE ); + + $res = $dbr->select( + 'page_props', + array( 'pp_propname', 'pp_value' ), + array( 'pp_page' => $pageid ), + __METHOD__ + ); + + foreach( $res as $row ) { + if( isset( $wgPageProps[$row->pp_propname] ) ) { + if( !is_null( $prop ) && !in_array( $row->pp_propname, $prop ) ) { + continue; + } + $pageInfo[$row->pp_propname] = $row->pp_value; + } + } + + } + + return $pageInfo; + } + + public function getCacheMode( $params ) { + return 'public'; + } + + public function getAllowedParams() { + global $wgPageProps; + + return array( + 'prop' => array( + ApiBase::PARAM_DFLT => null, + ApiBase::PARAM_ISMULTI => true, + ApiBase::PARAM_TYPE => array_keys( $wgPageProps ) + ), + 'continue' => null, + ); + } + + public function getParamDescription() { + global $wgPageProps; + + $ret = array( + 'prop' => array( + 'Which additional properties to get:', + ), + 'continue' => 'When more results are available, use this to continue', + ); + + $maxLen = max( array_map( 'strlen', array_keys( $wgPageProps ) ) ); + $matchLen = $maxLen + 2; + if( $maxLen < 12 ) { + $matchLen = 14; + } + + foreach( $wgPageProps as $propName => $desc ) { + $pretext = " $propName" . str_repeat( ' ', $matchLen - strlen( $propName ) ); + + $ret['prop'][] = "$pretext- $desc"; + } + + return $ret; + } + + public function getDescription() { + return 'Get various properties about a page...'; + } + + public function getPossibleErrors() { + return array_merge( parent::getPossibleErrors(), array( + array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ), + ) ); + } + + protected function getExamples() { + return array( + 'api.php?action=query&prop=pageprops&titles=Category:Foo', + ); + } + + public function getVersion() { + return __CLASS__ . ': $Id$'; + } +} -- 2.20.1