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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * A query module to show basic page information.
37 class ApiQueryPageProps
extends ApiQueryBase
{
41 public function __construct( $query, $moduleName ) {
42 parent
::__construct( $query, $moduleName, 'pp' );
45 public function execute() {
46 $this->params
= $this->extractRequestParams();
48 # Only operate on existing pages
49 $pages = $this->getPageSet()->getGoodTitles();
51 $this->addTables( 'page_props' );
52 $this->addFields( array( 'pp_page', 'pp_propname', 'pp_value' ) );
53 $this->addWhereFld( 'pp_page', array_keys( $pages ) );
55 if ( $this->params
['continue'] ) {
56 $this->addWhere( 'pp_page >=' . intval( $this->params
['continue'] ) );
59 # Force a sort order to ensure that properties are grouped by page
60 $this->addOption( 'ORDER BY', 'pp_page' );
62 $res = $this->select( __METHOD__
);
63 $currentPage = 0; # Id of the page currently processed
65 $result = $this->getResult();
67 foreach ( $res as $row ) {
68 if ( $currentPage != $row->pp_page
) {
69 # Different page than previous row, so add the properties to
70 # the result and save the new page id
73 if ( !$this->addPageProps( $result, $currentPage, $props ) ) {
74 # addPageProps() indicated that the result did not fit
75 # so stop adding data. Reset props so that it doesn't
76 # get added again after loop exit
85 $currentPage = $row->pp_page
;
88 $props[$row->pp_propname
] = $row->pp_value
;
91 if ( count( $props ) ) {
92 # Add any remaining properties to the results
93 $this->addPageProps( $result, $currentPage, $props );
98 * Add page properties to an ApiResult, adding a continue
99 * parameter if it doesn't fit.
101 * @param $result ApiResult
103 * @param $props array
104 * @return bool True if it fits in the result
106 private function addPageProps( $result, $page, $props ) {
107 $fit = $result->addValue( array( 'query', 'pages' ), $page, $props );
110 $this->setContinueEnumParameter( 'continue', $page );
115 public function getCacheMode( $params ) {
119 public function getAllowedParams() {
120 return array( 'continue' => null );
123 public function getParamDescription() {
124 return array( 'continue' => 'When more results are available, use this to continue' );
127 public function getDescription() {
128 return 'Get various properties defined in the page content';
131 public function getPossibleErrors() {
132 return array_merge( parent
::getPossibleErrors(), array(
133 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
137 protected function getExamples() {
139 'api.php?action=query&prop=pageprops&titles=Category:Foo',
143 public function getVersion() {
144 return __CLASS__
. ': $Id$';