-(bug 24484) Add prop=pageprops module
authorX! <soxred93@users.mediawiki.org>
Sat, 7 Aug 2010 18:50:23 +0000 (18:50 +0000)
committerX! <soxred93@users.mediawiki.org>
Sat, 7 Aug 2010 18:50:23 +0000 (18:50 +0000)
-Add $wgPageProps global variable

RELEASE-NOTES
includes/AutoLoader.php
includes/DefaultSettings.php
includes/api/ApiQuery.php
includes/api/ApiQueryPageProps.php [new file with mode: 0644]

index f88beb5..2579818 100644 (file)
@@ -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 ===
 
index 568ae85..139be8e 100644 (file)
@@ -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',
index 532f624..32ca758 100644 (file)
@@ -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
index cb7080d..10e502b 100644 (file)
@@ -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 (file)
index 0000000..04d5687
--- /dev/null
@@ -0,0 +1,184 @@
+<?php
+
+/**
+ * Created on Sep 25, 2006
+ *
+ * API for MediaWiki 1.8+
+ *
+ * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@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$';
+       }
+}