MediaWiki  REL1_21
ApiQueryPageProps.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryPageProps extends ApiQueryBase {
00033 
00034         private $params;
00035 
00036         public function __construct( $query, $moduleName ) {
00037                 parent::__construct( $query, $moduleName, 'pp' );
00038         }
00039 
00040         public function execute() {
00041                 # Only operate on existing pages
00042                 $pages = $this->getPageSet()->getGoodTitles();
00043                 if ( !count( $pages ) ) {
00044                         # Nothing to do
00045                         return;
00046                 }
00047 
00048                 $this->params = $this->extractRequestParams();
00049 
00050                 $this->addTables( 'page_props' );
00051                 $this->addFields( array( 'pp_page', 'pp_propname', 'pp_value' ) );
00052                 $this->addWhereFld( 'pp_page', array_keys( $pages ) );
00053 
00054                 if ( $this->params['continue'] ) {
00055                         $this->addWhere( 'pp_page >=' . intval( $this->params['continue'] ) );
00056                 }
00057 
00058                 if ( $this->params['prop'] ) {
00059                         $this->addWhereFld( 'pp_propname', $this->params['prop'] );
00060                 }
00061 
00062                 # Force a sort order to ensure that properties are grouped by page
00063                 # But only if pp_page is not constant in the WHERE clause.
00064                 if ( count( $pages ) > 1 ) {
00065                         $this->addOption( 'ORDER BY', 'pp_page' );
00066                 }
00067 
00068                 $res = $this->select( __METHOD__ );
00069                 $currentPage = 0; # Id of the page currently processed
00070                 $props = array();
00071                 $result = $this->getResult();
00072 
00073                 foreach ( $res as $row ) {
00074                         if ( $currentPage != $row->pp_page ) {
00075                                 # Different page than previous row, so add the properties to
00076                                 # the result and save the new page id
00077 
00078                                 if ( $currentPage ) {
00079                                         if ( !$this->addPageProps( $result, $currentPage, $props ) ) {
00080                                                 # addPageProps() indicated that the result did not fit
00081                                                 # so stop adding data. Reset props so that it doesn't
00082                                                 # get added again after loop exit
00083 
00084                                                 $props = array();
00085                                                 break;
00086                                         }
00087 
00088                                         $props = array();
00089                                 }
00090 
00091                                 $currentPage = $row->pp_page;
00092                         }
00093 
00094                         $props[$row->pp_propname] = $row->pp_value;
00095                 }
00096 
00097                 if ( count( $props ) ) {
00098                         # Add any remaining properties to the results
00099                         $this->addPageProps( $result, $currentPage, $props );
00100                 }
00101         }
00102 
00112         private function addPageProps( $result, $page, $props ) {
00113                 $fit = $result->addValue( array( 'query', 'pages', $page ), 'pageprops', $props );
00114 
00115                 if ( !$fit ) {
00116                         $this->setContinueEnumParameter( 'continue', $page );
00117                 }
00118                 return $fit;
00119         }
00120 
00121         public function getCacheMode( $params ) {
00122                 return 'public';
00123         }
00124 
00125         public function getAllowedParams() {
00126                 return array(
00127                         'continue' => null,
00128                         'prop' => array(
00129                                 ApiBase::PARAM_ISMULTI => true,
00130                         ),
00131                 );
00132         }
00133 
00134         public function getParamDescription() {
00135                 return array(
00136                         'continue' => 'When more results are available, use this to continue',
00137                         'prop' => 'Only list these props. Useful for checking whether a certain page uses a certain page prop',
00138                 );
00139         }
00140 
00141         public function getDescription() {
00142                 return 'Get various properties defined in the page content';
00143         }
00144 
00145         public function getExamples() {
00146                 return array(
00147                         'api.php?action=query&prop=pageprops&titles=Category:Foo',
00148                 );
00149         }
00150 
00151         public function getHelpUrls() {
00152                 return 'https://www.mediawiki.org/wiki/API:Properties#pageprops_.2F_pp';
00153         }
00154 }