MediaWiki  REL1_19
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                 $this->addOption( 'ORDER BY', 'pp_page' );
00064 
00065                 $res = $this->select( __METHOD__ );
00066                 $currentPage = 0; # Id of the page currently processed
00067                 $props = array();
00068                 $result = $this->getResult();
00069 
00070                 foreach ( $res as $row ) {
00071                         if ( $currentPage != $row->pp_page ) {
00072                                 # Different page than previous row, so add the properties to
00073                                 # the result and save the new page id
00074 
00075                                 if ( $currentPage ) {
00076                                         if ( !$this->addPageProps( $result, $currentPage, $props ) ) {
00077                                                 # addPageProps() indicated that the result did not fit
00078                                                 # so stop adding data. Reset props so that it doesn't
00079                                                 # get added again after loop exit
00080 
00081                                                 $props = array();
00082                                                 break;
00083                                         }
00084 
00085                                         $props = array();
00086                                 }
00087 
00088                                 $currentPage = $row->pp_page;
00089                         }
00090 
00091                         $props[$row->pp_propname] = $row->pp_value;
00092                 }
00093 
00094                 if ( count( $props ) ) {
00095                         # Add any remaining properties to the results
00096                         $this->addPageProps( $result, $currentPage, $props );
00097                 }
00098         }
00099 
00109         private function addPageProps( $result, $page, $props ) {
00110                 $fit = $result->addValue( array( 'query', 'pages', $page ), 'pageprops', $props );
00111 
00112                 if ( !$fit ) {
00113                         $this->setContinueEnumParameter( 'continue', $page );
00114                 }
00115                 return $fit;
00116         }
00117 
00118         public function getCacheMode( $params ) {
00119                 return 'public';
00120         }
00121 
00122         public function getAllowedParams() {
00123                 return array(
00124                         'continue' => null,
00125                         'prop' => null,
00126                 );
00127         }
00128 
00129         public function getParamDescription() {
00130                 return array(
00131                         'continue' => 'When more results are available, use this to continue',
00132                         'prop' => 'Page prop to look on the page for. Useful for checking whether a certain page uses a certain page prop.'
00133                 );
00134         }
00135 
00136         public function getDescription() {
00137                 return 'Get various properties defined in the page content';
00138         }
00139 
00140         public function getExamples() {
00141                 return array(
00142                         'api.php?action=query&prop=pageprops&titles=Category:Foo',
00143                 );
00144         }
00145 
00146         public function getHelpUrls() {
00147                 return 'https://www.mediawiki.org/wiki/API:Properties#pageprops_.2F_pp';
00148         }
00149 
00150         public function getVersion() {
00151                 return __CLASS__ . ': $Id$';
00152         }
00153 }