r75282 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75281‎ | r75282 | r75283 >
Date:18:53, 23 October 2010
Author:btongminh
Status:resolved (Comments)
Tags:
Comment:
Follow-up r70638:
* Use a single query to get the page_props instead of one per page
* Removed the $wgPageProps global
* Removed a lot of useless crap
Modified paths:
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryPageProps.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQueryPageProps.php
@@ -2,9 +2,9 @@
33 /**
44 * API for MediaWiki 1.8+
55 *
6 - * Created on Sep 25, 2006
 6+ * Created on Aug 7, 2010
77 *
8 - * Copyright © 2010 Yuri Astrakhan <Firstname><Lastname>@gmail.com
 8+ * Copyright © 2010 soxred93, Bryan Tong Minh
99 *
1010 * This program is free software; you can redistribute it and/or modify
1111 * it under the terms of the GNU General Public License as published by
@@ -44,130 +44,76 @@
4545
4646 public function execute() {
4747 $this->params = $this->extractRequestParams();
48 -
49 - $pageSet = $this->getPageSet();
50 - $this->titles = $pageSet->getGoodTitles();
51 - $this->missing = $pageSet->getMissingTitles();
52 - $this->everything = $this->titles + $this->missing;
 48+
 49+ $pages = $this->getPageSet()->getGoodTitles();
 50+
 51+ $this->addTables( 'page_props' );
 52+ $this->addFields( array( 'pp_page', 'pp_propname', 'pp_value' ) );
 53+ $this->addWhereFld( 'pp_page', array_keys( $pages ) );
 54+
 55+ if ( $this->params['continue'] ) {
 56+ $this->addWhereFld( 'pp_page >=' . intval( $this->params['continue'] ) );
 57+ }
 58+
 59+ $this->addOption( 'ORDER BY', 'pp_page' );
 60+
 61+ $res = $this->select( __METHOD__ );
 62+ $currentPage = 0;
 63+ $props = array();
5364 $result = $this->getResult();
54 -
55 - uasort( $this->everything, array( 'Title', 'compare' ) );
56 - if ( !is_null( $this->params['continue'] ) ) {
57 - // Throw away any titles we're gonna skip so they don't
58 - // clutter queries
59 - $cont = explode( '|', $this->params['continue'] );
60 - if ( count( $cont ) != 2 ) {
61 - $this->dieUsage( 'Invalid continue param. You should pass the original ' .
62 - 'value returned by the previous query', '_badcontinue' );
63 - }
64 - $conttitle = Title::makeTitleSafe( $cont[0], $cont[1] );
65 - foreach ( $this->everything as $pageid => $title ) {
66 - if ( Title::compare( $title, $conttitle ) >= 0 ) {
67 - break;
 65+ foreach ( $res as $row ) {
 66+ if ( $currentPage != $row->pp_page ) {
 67+ if ( $currentPage ) {
 68+ if ( !$this->addPageProps( $result, $currentPage, $props ) ) {
 69+ break;
 70+ }
 71+
 72+ $props = array();
 73+ } else {
 74+ $currentPage = $row->pp_page;
6875 }
69 - unset( $this->titles[$pageid] );
70 - unset( $this->missing[$pageid] );
71 - unset( $this->everything[$pageid] );
7276 }
 77+
 78+ $props[$row->pp_propname] = $row->pp_value;
7379 }
74 -
75 - foreach ( $this->everything as $pageid => $title ) {
76 - $pageInfo = $this->extractPageInfo( $pageid, $title, $this->params['prop'] );
77 - $fit = $result->addValue( array(
78 - 'query',
79 - 'pages'
80 - ), $pageid, $pageInfo );
81 - if ( !$fit ) {
82 - $this->setContinueEnumParameter( 'continue',
83 - $title->getNamespace() . '|' .
84 - $title->getText() );
85 - break;
86 - }
 80+
 81+ if ( count( $props ) ) {
 82+ $this->addPageProps( $result, $currentPage, $props );
8783 }
8884 }
8985
9086 /**
91 - * Get a result array with information about a title
92 - * @param $pageid int Page ID (negative for missing titles)
93 - * @param $title Title object
94 - * @return array
 87+ * Add page properties to an ApiResult, adding a continue
 88+ * parameter if it doesn't fit.
 89+ *
 90+ * @param $result ApiResult
 91+ * @param $page int
 92+ * @param $props array
 93+ * @return bool True if it fits in the result
9594 */
96 - private function extractPageInfo( $pageid, $title, $prop ) {
97 - global $wgPageProps;
 95+ private function addPageProps( $result, $page, $props ) {
 96+ $fit = $result->addValue( array( 'query', 'pages' ), $page, $props );
9897
99 - $pageInfo = array();
100 - if ( $title->exists() ) {
101 -
102 - $dbr = wfGetDB( DB_SLAVE );
103 -
104 - $res = $dbr->select(
105 - 'page_props',
106 - array( 'pp_propname', 'pp_value' ),
107 - array( 'pp_page' => $pageid ),
108 - __METHOD__
109 - );
110 -
111 - foreach( $res as $row ) {
112 - if( isset( $wgPageProps[$row->pp_propname] ) ) {
113 - if( !is_null( $prop ) && !in_array( $row->pp_propname, $prop ) ) {
114 - continue;
115 - }
116 - $pageInfo[$row->pp_propname] = $row->pp_value;
117 - }
118 - }
119 -
 98+ if ( !$fit ) {
 99+ $this->setContinueEnumParameter( 'continue', $page );
120100 }
121 -
122 - return $pageInfo;
 101+ return $fit;
123102 }
124103
125104 public function getCacheMode( $params ) {
126105 return 'public';
127106 }
128107
129 - public function getAllowedParams() {
130 - global $wgPageProps;
131 -
132 - return array(
133 - 'prop' => array(
134 - ApiBase::PARAM_DFLT => null,
135 - ApiBase::PARAM_ISMULTI => true,
136 - ApiBase::PARAM_TYPE => array_keys( $wgPageProps )
137 - ),
138 - 'continue' => null,
139 - );
 108+ public function getAllowedParams() {
 109+ return array( 'continue' => null );
140110 }
141111
142112 public function getParamDescription() {
143 - global $wgPageProps;
144 -
145 - $ret = array(
146 - 'prop' => array(
147 - 'Which additional properties to get:',
148 - ),
149 - 'continue' => 'When more results are available, use this to continue',
150 - );
151 -
152 - //This mess of code first gets the length of the biggest propname, and adds two onto it to make
153 - //the number of characters should be used before the dash. If the biggest propname is shorter than 12 characters,
154 - //the number of characters before the dash become 14.
155 - $maxLen = max( array_map( 'strlen', array_keys( $wgPageProps ) ) );
156 - $matchLen = $maxLen + 2;
157 - if( $maxLen < 12 ) {
158 - $matchLen = 14;
159 - }
160 -
161 - foreach( $wgPageProps as $propName => $desc ) {
162 - $pretext = " $propName" . str_repeat( ' ', $matchLen - strlen( $propName ) );
163 -
164 - $ret['prop'][] = "$pretext- $desc";
165 - }
166 -
167 - return $ret;
 113+ return array( 'continue' => 'When more results are available, use this to continue' );
168114 }
169115
170116 public function getDescription() {
171 - return 'Get various properties about a page...';
 117+ return 'Get various properties defined in the page content';
172118 }
173119
174120 public function getPossibleErrors() {
Property changes on: trunk/phase3/includes/api/ApiQueryPageProps.php
___________________________________________________________________
Deleted: keywords
175121 - Id
Added: svn:keywords
176122 + id
Index: trunk/phase3/includes/DefaultSettings.php
@@ -4396,17 +4396,6 @@
43974397 */
43984398 $wgExceptionHooks = array();
43994399
4400 -/**
4401 - * List of page property names and descriptions of what they are.
4402 - * This is used for the API prop=pageprops module to know which
4403 - * page props to search for. The help message is only seen from
4404 - * the API help page.
4405 - */
4406 -$wgPageProps = array(
4407 - 'displaytitle' => 'Value of the {{DISPLAYTITLE}} tag',
4408 - 'defaultsort' => 'Value of the {{DEFAULTSORT}} tag',
4409 - 'hiddencat' => 'Whether or not the page has a category with the __HIDDENCAT__ magic word',
4410 -);
44114400
44124401 /**
44134402 * Page property link table invalidation lists. When a page property

Follow-up revisions

RevisionCommit summaryAuthorDate
r75440Followup r75282, remove now unused class variablesreedy16:37, 26 October 2010
r75459Follow-up r75282 ...btongminh19:31, 26 October 2010
r79199Follow-up to r75282: don't query if there are no pages to querybtongminh20:32, 29 December 2010
r82312* (bug 27479) API error when using both prop=pageprops and prop=info&inprop=d...reedy23:38, 16 February 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r70638-(bug 24484) Add prop=pageprops module...soxred9318:50, 7 August 2010

Comments

#Comment by Reedy (talk | contribs)   16:35, 26 October 2010
if ( $this->params['continue'] ) {
			$this->addWhereFld( 'pp_page >=' . intval( $this->params['continue'] ) );
		}

I think you want $this->addWhere();

#Comment by Bryan (talk | contribs)   18:11, 26 October 2010

Also note to self `+ $currentPage = $row->pp_page;` should be outside of the else block.

#Comment by Bryan (talk | contribs)   19:32, 26 October 2010
#Comment by Duplicatebug (talk | contribs)   20:27, 29 December 2010

When there are no goodTitles in the pageSet, the full 'page_props' table is added to the result.

#Comment by Bryan (talk | contribs)   20:33, 29 December 2010

Fixed in r79199

Status & tagging log