r94418 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94417‎ | r94418 | r94419 >
Date:15:44, 13 August 2011
Author:reedy
Status:resolved (Comments)
Tags:
Comment:
* (bug 28893) Proofread Page extension needs an API module to retrieve page status

Patch by John Du Hart
Modified paths:
  • /trunk/extensions/ProofreadPage/ApiQueryProofread.php (added) (history)
  • /trunk/extensions/ProofreadPage/ProofreadPage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ProofreadPage/ApiQueryProofread.php
@@ -0,0 +1,104 @@
 2+<?php
 3+/*
 4+ * Created on August 8, 2011
 5+ *
 6+ * API module for MediaWiki's Proofread extension
 7+ *
 8+ * This program is free software; you can redistribute it and/or modify
 9+ * it under the terms of the GNU General Public License as published by
 10+ * the Free Software Foundation; either version 2 of the License, or
 11+ * (at your option) any later version.
 12+ *
 13+ * This program is distributed in the hope that it will be useful,
 14+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 16+ * GNU General Public License for more details.
 17+ *
 18+ * You should have received a copy of the GNU General Public License along
 19+ * with this program; if not, write to the Free Software Foundation, Inc.,
 20+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 21+ * http://www.gnu.org/copyleft/gpl.html
 22+ */
 23+
 24+class ApiQueryProofread extends ApiQueryBase {
 25+
 26+ public function execute() {
 27+ $pageSet = $this->getPageSet();
 28+ $pages = $pageSet->getGoodTitles();
 29+ if ( !count( $pages ) ) {
 30+ return true;
 31+ }
 32+
 33+ // Only include pages that can use the proofread tag
 34+ // Why doesn't it strtolower in getCanonicalIndex?
 35+ $pageNamespaceText = strtolower( wfMsgForContent( 'proofreadpage_namespace' ) );
 36+ $pageNamespaceId = MWNamespace::getCanonicalIndex( $pageNamespaceText );
 37+ $pageIds = array();
 38+ foreach ( $pages AS $pageId => $title ) {
 39+ if ( $title->getNamespace() == $pageNamespaceId ) {
 40+ $pageIds[] = $pageId;
 41+ }
 42+ }
 43+
 44+ if ( !count( $pageIds ) ) {
 45+ return true;
 46+ }
 47+
 48+ // Determine the categories defined in MediaWiki: pages
 49+ $qualityCategories = $qualityText = array();
 50+ for ( $i = 0; $i < 5; $i++ ) {
 51+ $cat = Title::makeTitleSafe( NS_CATEGORY, wfMsgForContent( "proofreadpage_quality{$i}_category" ) );
 52+ if ( $cat ) {
 53+ $qualityCategories[$i] = $cat->getPrefixedText();
 54+ $qualityText[$i] = $cat->getText();
 55+ }
 56+ }
 57+ $qualityLevels = array_flip( $qualityCategories );
 58+
 59+ // <Reedy> johnduhart, it'd seem sane rather than duplicating the functionality
 60+ $params = new FauxRequest(array(
 61+ 'action' => 'query',
 62+ 'prop' => 'categories',
 63+ 'pageids' => implode( '|', $pageIds ),
 64+ 'clcategories' => implode( '|', $qualityCategories ),
 65+ 'cllimit' => 'max'
 66+ ));
 67+
 68+ $api = new ApiMain($params);
 69+ $api->execute();
 70+ $data = $api->getResultData();
 71+ unset( $api );
 72+
 73+ $result = $this->getResult();
 74+ foreach ( $data['query']['pages'] as $pageid => $data) {
 75+ $pageQuality = $qualityLevels[ $data['categories'][0]['title'] ];
 76+ if ( $pageQuality === null ) {
 77+ continue;
 78+ }
 79+ $val = array( 'quality' => $pageQuality, 'quality_text' => $qualityText[ $pageQuality ] );
 80+ $result->addValue( array( 'query', 'pages', $pageid ), 'proofread', $val );
 81+ }
 82+ }
 83+
 84+ public function getCacheMode( $params ) {
 85+ return 'public';
 86+ }
 87+
 88+ public function getAllowedParams() {
 89+ return array();
 90+ }
 91+
 92+ public function getDescription() {
 93+ return 'Returns information about the current proofread status of the given pages.';
 94+ }
 95+
 96+ protected function getExamples() {
 97+ return array(
 98+ 'api.php?action=query&generator=allpages&gapnamespace=104&prop=proofread'
 99+ );
 100+ }
 101+
 102+ public function getVersion() {
 103+ return __CLASS__ . ': $Id$';
 104+ }
 105+}
Property changes on: trunk/extensions/ProofreadPage/ApiQueryProofread.php
___________________________________________________________________
Added: svn:eol-style
1106 + native
Added: svn:keywords
2107 + Id
Index: trunk/extensions/ProofreadPage/ProofreadPage.php
@@ -44,6 +44,10 @@
4545 $wgSpecialPages['PagesWithoutScans'] = 'PagesWithoutScans';
4646 $wgSpecialPageGroups['PagesWithoutScans'] = 'maintenance';
4747
 48+# api prop
 49+$wgAutoloadClasses['ApiQueryProofread'] = $dir . 'ApiQueryProofread.php';
 50+$wgAPIPropModules['proofread'] = 'ApiQueryProofread';
 51+
4852 # Group allowed to modify pagequality
4953 $wgGroupPermissions['user']['pagequality'] = true;
5054

Follow-up revisions

RevisionCommit summaryAuthorDate
r95292Followup r94418, use isset() to check if a value is in the arrayjohnduhart00:51, 23 August 2011

Comments

#Comment by Catrope (talk | contribs)   09:57, 14 August 2011
+			$pageQuality = $qualityLevels[ $data['categories'][0]['title'] ];
+			if ( $pageQuality === null ) {

This is not the right way to check for nonexistent keys, it'll throw notices. Use isset( $qualityLevels[ ... ] ) instead.

+			'api.php?action=query&generator=allpages&gapnamespace=104&prop=proofread'

Copypaste leftover?

#Comment by Johnduhart (talk | contribs)   17:52, 14 August 2011

No the example should work on Wikisource wikis.

#Comment by Catrope (talk | contribs)   18:03, 14 August 2011

Oh, whoops. I read the first 30 bytes or so, saw 'allpages' and dismissed it. So yeah, this is a valid example, sorry about that.

Status & tagging log