r113238 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113237‎ | r113238 | r113239 >
Date:15:53, 7 March 2012
Author:maxsem
Status:ok
Tags:
Comment:
Error checking and paraminfo fixes
Modified paths:
  • /trunk/extensions/MobileFrontend/api/ApiMobileView.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/api/ApiMobileView.php
@@ -22,11 +22,15 @@
2323 $sectionProp = array_flip( $params['sectionprop'] );
2424
2525 $title = Title::newFromText( $params['page'] );
26 - if ( !$title || !$title->exists() || $title->getNamespace() < 0 || !$title->isLocal() ) {
 26+ if ( !$title ) {
 27+ $this->dieUsageMsg( array( 'missingtitle', $params['page'] ) );
 28+ }
 29+ if ( !$title->exists() ) {
2730 $this->dieUsageMsg( array( 'invalidtitle', $params['page'] ) );
2831 }
2932 $data = $this->getData( $title, $params['noimages'] );
3033 $result = array();
 34+ $missingSections = array();
3135 if ( isset( $prop['sections'] ) ) {
3236 $requestedSections = array_flip( $requestedSections );
3337 for ( $i = 0; $i <= count( $data['sections'] ); $i++ ) {
@@ -37,53 +41,29 @@
3842 $section['id'] = $i;
3943 if ( isset( $requestedSections[$i] ) && isset( $data['text'][$i] ) ) {
4044 $section[$textElement] = $data['text'][$i];
 45+ unset( $requestedSections[$i] );
4146 }
4247 $result[] = $section;
4348 }
 49+ $missingSections = $requestedSections;
4450 } else {
4551 foreach ( $requestedSections as $index ) {
4652 $section = array( 'id' => $index );
4753 if ( isset( $data['text'][$index] ) ) {
4854 $section[$textElement] = $data['text'][$index];
 55+ } else {
 56+ $missingSections[] = $index;
4957 }
5058 $result[] = $section;
5159 }
5260 }
 61+ if ( count( $missingSections ) ) {
 62+ $this->setWarning( 'Section(s) ' . implode( ', ', $missingSections ) . ' not found' );
 63+ }
5364 $this->getResult()->setIndexedTagName( $result, 'section' );
5465 $this->getResult()->addValue( null, $this->getModuleName(), array( 'sections' => $result ) );
5566 }
5667
57 - public function getAllowedParams() {
58 - return array(
59 - 'page' => array(
60 - ApiBase::PARAM_REQUIRED => true,
61 - ),
62 - 'section' => null,
63 - 'prop' => array(
64 - ApiBase::PARAM_DFLT => 'text|sections',
65 - ApiBase::PARAM_ISMULTI => true,
66 - ApiBase::PARAM_TYPE => array(
67 - 'text',
68 - 'sections',
69 - )
70 - ),
71 - 'sectionprop' => array(
72 - ApiBase::PARAM_TYPE => array(
73 - 'toclevel',
74 - 'level',
75 - 'line',
76 - 'number',
77 - 'index',
78 - 'fromtitle',
79 - 'anchor',
80 - ),
81 - ApiBase::PARAM_ISMULTI => true,
82 - ApiBase::PARAM_DFLT => 'toclevel|line',
83 - ),
84 - 'noimages' => false,
85 - );
86 - }
87 -
8868 private function parseSections( $str ) {
8969 $sections = array_map( 'intval', explode( '|', $str ) );
9070 return $sections;
@@ -133,9 +113,48 @@
134114 return $data;
135115 }
136116
 117+ public function getAllowedParams() {
 118+ return array(
 119+ 'page' => array(
 120+ ApiBase::PARAM_REQUIRED => true,
 121+ ),
 122+ 'section' => null,
 123+ 'prop' => array(
 124+ ApiBase::PARAM_DFLT => 'text|sections',
 125+ ApiBase::PARAM_ISMULTI => true,
 126+ ApiBase::PARAM_TYPE => array(
 127+ 'text',
 128+ 'sections',
 129+ )
 130+ ),
 131+ 'sectionprop' => array(
 132+ ApiBase::PARAM_TYPE => array(
 133+ 'toclevel',
 134+ 'level',
 135+ 'line',
 136+ 'number',
 137+ 'index',
 138+ 'fromtitle',
 139+ 'anchor',
 140+ ),
 141+ ApiBase::PARAM_ISMULTI => true,
 142+ ApiBase::PARAM_DFLT => 'toclevel|line',
 143+ ),
 144+ 'noimages' => false,
 145+ );
 146+ }
 147+
137148 public function getParamDescription() {
138149 return array(
139 -
 150+ 'page' => 'Title of page to process',
 151+ 'section' => 'Pipe-separated list of section numbers for which to return text',
 152+ 'prop' => array(
 153+ 'Which information to get',
 154+ ' text - HTML of selected section(s)',
 155+ ' sections - information about all sections on page',
 156+ ),
 157+ 'sectionprop' => 'What information about sections to get',
 158+ 'noimages' => 'Return HTML without images',
140159 );
141160 }
142161
@@ -146,7 +165,8 @@
147166 public function getPossibleErrors() {
148167 return array_merge( parent::getPossibleErrors(),
149168 array(
150 - array( 'code' => 'invalid-section', 'info' => '' ),
 169+ array( 'missingtitle' ),
 170+ array( 'invalidtitle' ),
151171 )
152172 );
153173 }

Status & tagging log