Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / api / ApiQueryPageProps.php
blob2de57106e9f2f8884f2b4ca99100463f1f59e5ef
1 <?php
2 /**
5 * Created on Aug 7, 2010
7 * Copyright © 2010 soxred93, Bryan Tong Minh
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
27 /**
28 * A query module to show basic page information.
30 * @ingroup API
32 class ApiQueryPageProps extends ApiQueryBase {
34 private $params;
36 public function __construct( $query, $moduleName ) {
37 parent::__construct( $query, $moduleName, 'pp' );
40 public function execute() {
41 # Only operate on existing pages
42 $pages = $this->getPageSet()->getGoodTitles();
43 if ( !count( $pages ) ) {
44 # Nothing to do
45 return;
48 $this->params = $this->extractRequestParams();
50 $this->addTables( 'page_props' );
51 $this->addFields( array( 'pp_page', 'pp_propname', 'pp_value' ) );
52 $this->addWhereFld( 'pp_page', array_keys( $pages ) );
54 if ( $this->params['continue'] ) {
55 $this->addWhere( 'pp_page >=' . intval( $this->params['continue'] ) );
58 if ( $this->params['prop'] ) {
59 $this->addWhereFld( 'pp_propname', $this->params['prop'] );
62 # Force a sort order to ensure that properties are grouped by page
63 # But only if pp_page is not constant in the WHERE clause.
64 if ( count( $pages ) > 1 ) {
65 $this->addOption( 'ORDER BY', 'pp_page' );
68 $res = $this->select( __METHOD__ );
69 $currentPage = 0; # Id of the page currently processed
70 $props = array();
71 $result = $this->getResult();
73 foreach ( $res as $row ) {
74 if ( $currentPage != $row->pp_page ) {
75 # Different page than previous row, so add the properties to
76 # the result and save the new page id
78 if ( $currentPage ) {
79 if ( !$this->addPageProps( $result, $currentPage, $props ) ) {
80 # addPageProps() indicated that the result did not fit
81 # so stop adding data. Reset props so that it doesn't
82 # get added again after loop exit
84 $props = array();
85 break;
88 $props = array();
91 $currentPage = $row->pp_page;
94 $props[$row->pp_propname] = $row->pp_value;
97 if ( count( $props ) ) {
98 # Add any remaining properties to the results
99 $this->addPageProps( $result, $currentPage, $props );
104 * Add page properties to an ApiResult, adding a continue
105 * parameter if it doesn't fit.
107 * @param $result ApiResult
108 * @param $page int
109 * @param $props array
110 * @return bool True if it fits in the result
112 private function addPageProps( $result, $page, $props ) {
113 $fit = $result->addValue( array( 'query', 'pages', $page ), 'pageprops', $props );
115 if ( !$fit ) {
116 $this->setContinueEnumParameter( 'continue', $page );
118 return $fit;
121 public function getCacheMode( $params ) {
122 return 'public';
125 public function getAllowedParams() {
126 return array(
127 'continue' => null,
128 'prop' => array(
129 ApiBase::PARAM_ISMULTI => true,
134 public function getParamDescription() {
135 return array(
136 'continue' => 'When more results are available, use this to continue',
137 'prop' => 'Only list these props. Useful for checking whether a certain page uses a certain page prop',
141 public function getDescription() {
142 return 'Get various properties defined in the page content';
145 public function getExamples() {
146 return array(
147 'api.php?action=query&prop=pageprops&titles=Category:Foo',
151 public function getHelpUrls() {
152 return 'https://www.mediawiki.org/wiki/API:Properties#pageprops_.2F_pp';