r70638 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r70637‎ | r70638 | r70639 >
Date:18:50, 7 August 2010
Author:soxred93
Status:resolved (Comments)
Tags:
Comment:
-(bug 24484) Add prop=pageprops module
-Add $wgPageProps global variable
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQuery.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryPageProps.php (added) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQueryPageProps.php
@@ -0,0 +1,184 @@
 2+<?php
 3+
 4+/**
 5+ * Created on Sep 25, 2006
 6+ *
 7+ * API for MediaWiki 1.8+
 8+ *
 9+ * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
 10+ *
 11+ * This program is free software; you can redistribute it and/or modify
 12+ * it under the terms of the GNU General Public License as published by
 13+ * the Free Software Foundation; either version 2 of the License, or
 14+ * (at your option) any later version.
 15+ *
 16+ * This program is distributed in the hope that it will be useful,
 17+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 19+ * GNU General Public License for more details.
 20+ *
 21+ * You should have received a copy of the GNU General Public License along
 22+ * with this program; if not, write to the Free Software Foundation, Inc.,
 23+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 24+ * http://www.gnu.org/copyleft/gpl.html
 25+ */
 26+
 27+if ( !defined( 'MEDIAWIKI' ) ) {
 28+ // Eclipse helper - will be ignored in production
 29+ require_once( 'ApiQueryBase.php' );
 30+}
 31+
 32+/**
 33+ * A query module to show basic page information.
 34+ *
 35+ * @ingroup API
 36+ */
 37+class ApiQueryPageProps extends ApiQueryBase {
 38+
 39+ public function __construct( $query, $moduleName ) {
 40+ parent::__construct( $query, $moduleName, 'pp' );
 41+ }
 42+
 43+ public function execute() {
 44+ $this->params = $this->extractRequestParams();
 45+
 46+ $pageSet = $this->getPageSet();
 47+ $this->titles = $pageSet->getGoodTitles();
 48+ $this->missing = $pageSet->getMissingTitles();
 49+ $this->everything = $this->titles + $this->missing;
 50+ $result = $this->getResult();
 51+
 52+ uasort( $this->everything, array( 'Title', 'compare' ) );
 53+ if ( !is_null( $this->params['continue'] ) ) {
 54+ // Throw away any titles we're gonna skip so they don't
 55+ // clutter queries
 56+ $cont = explode( '|', $this->params['continue'] );
 57+ if ( count( $cont ) != 2 ) {
 58+ $this->dieUsage( 'Invalid continue param. You should pass the original ' .
 59+ 'value returned by the previous query', '_badcontinue' );
 60+ }
 61+ $conttitle = Title::makeTitleSafe( $cont[0], $cont[1] );
 62+ foreach ( $this->everything as $pageid => $title ) {
 63+ if ( Title::compare( $title, $conttitle ) >= 0 ) {
 64+ break;
 65+ }
 66+ unset( $this->titles[$pageid] );
 67+ unset( $this->missing[$pageid] );
 68+ unset( $this->everything[$pageid] );
 69+ }
 70+ }
 71+
 72+
 73+
 74+ foreach ( $this->everything as $pageid => $title ) {
 75+ $pageInfo = $this->extractPageInfo( $pageid, $title );
 76+ $fit = $result->addValue( array(
 77+ 'query',
 78+ 'pages'
 79+ ), $pageid, $pageInfo );
 80+ if ( !$fit ) {
 81+ $this->setContinueEnumParameter( 'continue',
 82+ $title->getNamespace() . '|' .
 83+ $title->getText() );
 84+ break;
 85+ }
 86+ }
 87+ }
 88+
 89+ /**
 90+ * Get a result array with information about a title
 91+ * @param $pageid int Page ID (negative for missing titles)
 92+ * @param $title Title object
 93+ * @return array
 94+ */
 95+ private function extractPageInfo( $pageid, $title ) {
 96+ global $wgPageProps;
 97+
 98+ $pageInfo = array();
 99+ if ( $title->exists() ) {
 100+
 101+ $dbr = wfGetDB( DB_SLAVE );
 102+
 103+ $res = $dbr->select(
 104+ 'page_props',
 105+ array( 'pp_propname', 'pp_value' ),
 106+ array( 'pp_page' => $pageid ),
 107+ __METHOD__
 108+ );
 109+
 110+ foreach( $res as $row ) {
 111+ if( isset( $wgPageProps[$row->pp_propname] ) ) {
 112+ if( !is_null( $prop ) && !in_array( $row->pp_propname, $prop ) ) {
 113+ continue;
 114+ }
 115+ $pageInfo[$row->pp_propname] = $row->pp_value;
 116+ }
 117+ }
 118+
 119+ }
 120+
 121+ return $pageInfo;
 122+ }
 123+
 124+ public function getCacheMode( $params ) {
 125+ return 'public';
 126+ }
 127+
 128+ public function getAllowedParams() {
 129+ global $wgPageProps;
 130+
 131+ return array(
 132+ 'prop' => array(
 133+ ApiBase::PARAM_DFLT => null,
 134+ ApiBase::PARAM_ISMULTI => true,
 135+ ApiBase::PARAM_TYPE => array_keys( $wgPageProps )
 136+ ),
 137+ 'continue' => null,
 138+ );
 139+ }
 140+
 141+ public function getParamDescription() {
 142+ global $wgPageProps;
 143+
 144+ $ret = array(
 145+ 'prop' => array(
 146+ 'Which additional properties to get:',
 147+ ),
 148+ 'continue' => 'When more results are available, use this to continue',
 149+ );
 150+
 151+ $maxLen = max( array_map( 'strlen', array_keys( $wgPageProps ) ) );
 152+ $matchLen = $maxLen + 2;
 153+ if( $maxLen < 12 ) {
 154+ $matchLen = 14;
 155+ }
 156+
 157+ foreach( $wgPageProps as $propName => $desc ) {
 158+ $pretext = " $propName" . str_repeat( ' ', $matchLen - strlen( $propName ) );
 159+
 160+ $ret['prop'][] = "$pretext- $desc";
 161+ }
 162+
 163+ return $ret;
 164+ }
 165+
 166+ public function getDescription() {
 167+ return 'Get various properties about a page...';
 168+ }
 169+
 170+ public function getPossibleErrors() {
 171+ return array_merge( parent::getPossibleErrors(), array(
 172+ array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
 173+ ) );
 174+ }
 175+
 176+ protected function getExamples() {
 177+ return array(
 178+ 'api.php?action=query&prop=pageprops&titles=Category:Foo',
 179+ );
 180+ }
 181+
 182+ public function getVersion() {
 183+ return __CLASS__ . ': $Id$';
 184+ }
 185+}
Index: trunk/phase3/includes/api/ApiQuery.php
@@ -58,6 +58,7 @@
5959 'extlinks' => 'ApiQueryExternalLinks',
6060 'categoryinfo' => 'ApiQueryCategoryInfo',
6161 'duplicatefiles' => 'ApiQueryDuplicateFiles',
 62+ 'pageprops' => 'ApiQueryPageProps',
6263 );
6364
6465 private $mQueryListModules = array(
Index: trunk/phase3/includes/AutoLoader.php
@@ -306,6 +306,7 @@
307307 'ApiQueryLangLinks' => 'includes/api/ApiQueryLangLinks.php',
308308 'ApiQueryLinks' => 'includes/api/ApiQueryLinks.php',
309309 'ApiQueryLogEvents' => 'includes/api/ApiQueryLogEvents.php',
 310+ 'ApiQueryPageProps' => 'includes/api/ApiQueryPageProps.php',
310311 'ApiQueryProtectedTitles' => 'includes/api/ApiQueryProtectedTitles.php',
311312 'ApiQueryRandom' => 'includes/api/ApiQueryRandom.php',
312313 'ApiQueryRecentChanges' => 'includes/api/ApiQueryRecentChanges.php',
Index: trunk/phase3/includes/DefaultSettings.php
@@ -4432,6 +4432,15 @@
44334433 $wgExceptionHooks = array();
44344434
44354435 /**
 4436+ * List of page property names and descriptions of what they are.
 4437+ * This is used for the API prop=pageprops module to know which
 4438+ * page props to search for.
 4439+ */
 4440+$wgPageProps = array(
 4441+ 'hiddencat' => 'Whether or not the page has a category with the __HIDDENCAT__ magic word',
 4442+);
 4443+
 4444+/**
44364445 * Page property link table invalidation lists. When a page property
44374446 * changes, this may require other link tables to be updated (eg
44384447 * adding __HIDDENCAT__ means the hiddencat tracking category will
Index: trunk/phase3/RELEASE-NOTES
@@ -330,7 +330,8 @@
331331 the timestamp of the last edit
332332 * (bug 24677) axto= parameters added to allcategories, allimages, alllinks, allmessages,
333333 allpages, and allusers
334 -* (bug 24236) Add add, remove, add-self, remove-self tags to meta=siteinfo&siprop=usergroups
 334+* (bug 24236) Add add, remove, add-self, remove-self tags to meta=siteinfo&siprop=usergroups
 335+* (bug 24484) Add prop=pageprops module
335336
336337 === Languages updated in 1.17 ===
337338

Follow-up revisions

RevisionCommit summaryAuthorDate
r70639Followup to r70638: Add svn:keywordssoxred9318:55, 7 August 2010
r70641Followup r70638: Forgot to add eol-style nativesoxred9319:00, 7 August 2010
r70701Followup to r70638: Clean up code, add prop to function args, add commentsoxred9314:55, 8 August 2010
r70732Followup to r70638: Add $wgPageProps functionality to all extensions that add...soxred9301:29, 9 August 2010
r72736Followup to r70638: Add pageprop descriptions for displaytitle and defaultsortsoxred9319:12, 10 September 2010
r75282Follow-up r70638:...btongminh18:53, 23 October 2010

Comments

#Comment by Reedy (talk | contribs)   18:54, 7 August 2010

SVN props. My IRC client crashed, so no idea if that sent.

And heading out, so wasn't reloading for it.

#Comment by MZMcBride (talk | contribs)   19:08, 7 August 2010

Copyright header looks old.

#Comment by Catrope (talk | contribs)   10:55, 8 August 2010
+					if( !is_null( $prop ) && !in_array( $row->pp_propname, $prop ) ) {

$prop is unset here.

+		$maxLen = max( array_map( 'strlen', array_keys( $wgPageProps ) ) );
+		$matchLen = $maxLen + 2;
+		if( $maxLen < 12 ) {
+			$matchLen = 14;
+		}

This could use a comment to clarify what's going on.

#Comment by X! (talk | contribs)   16:29, 9 August 2010

Fixed.

#Comment by Umherirrender (talk | contribs)   18:55, 10 September 2010

Please add the page_props from r69235 also to $wgPageProps. Thanks.

#Comment by X! (talk | contribs)   19:06, 10 September 2010

Already done a while ago in r70732 (see follow-up revision section)

#Comment by X! (talk | contribs)   19:12, 10 September 2010

Strike that, misunderstood you. r72736

#Comment by Umherirrender (talk | contribs)   19:38, 10 September 2010

Thanks, but what about the double underscore magic words MagicWords::$mDoubleUnderscoreIDs?


#Comment by Bryan (talk | contribs)   19:13, 10 September 2010
  • Why are you doing one query per title, instead of fetching at once for all titles?
  • Don't use in_array; use isset
  • The function documentation for extractPageInfo is missing the $prop argument
  • Why are you iterating over missing titles as well? They have a negative page id, so they won't yield results anyway
#Comment by Umherirrender (talk | contribs)   19:41, 10 September 2010

You can use $this->getDB(); instead of wfGetDB.

Status & tagging log