r113365 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113364‎ | r113365 | r113366 >
Date:13:57, 8 March 2012
Author:maxsem
Status:reverted
Tags:gerritmigration 
Comment:
Extended OpenSearchXml to use extracts and images from other extensions such as PageImages and MobileFrontend's excerpt module, both of which produce better results.
Modified paths:
  • /trunk/extensions/OpenSearchXml/ApiOpenSearchXml.php (modified) (history)

Diff [purge]

Index: trunk/extensions/OpenSearchXml/ApiOpenSearchXml.php
@@ -85,9 +85,10 @@
8686 // Open search results may be stored for a very long time
8787 $this->getMain()->setCacheMaxAge( 1200 );
8888
89 - $srchres = PrefixSearch::titleSearch( $search, $limit, $namespaces );
 89+ $data = $this->search( $search, $limit, $namespaces );
 90+ wfRunHooks( 'OpenSearchXml', array( &$data ) );
9091
91 - $items = array_filter( array_map( array( $this, 'formatItem' ), $srchres ) );
 92+ $items = array_map( array( $this, 'formatItem' ), $data );
9293
9394 $result = $this->getResult();
9495 $result->addValue( null, 'version', '2.0' );
@@ -97,6 +98,28 @@
9899 $result->addValue( null, 'Section', $items );
99100 }
100101
 102+ private function search( $search, $limit, $namespaces ) {
 103+ $srchres = PrefixSearch::titleSearch( $search, $limit, $namespaces );
 104+ $titles = array_filter( array_map( 'Title::newFromText', $srchres ) );
 105+ $lb = new LinkBatch( $titles );
 106+ $lb->setCaller( __METHOD__ );
 107+ $lb->execute();
 108+
 109+ $results = array();
 110+ foreach ( $titles as $title ) {
 111+ $title = $this->_checkRedirect( $title );
 112+ if( $this->_seen( $title ) ) {
 113+ continue;
 114+ }
 115+ $results[$title->getArticleID()] = array(
 116+ 'title' => $title,
 117+ 'extract' => false,
 118+ 'image' => false,
 119+ );
 120+ }
 121+ return $results;
 122+ }
 123+
101124 public function getAllowedParams() {
102125 $params = parent::getAllowedParams();
103126 $params['format'] = array(
@@ -118,45 +141,49 @@
119142 }
120143
121144 /**
122 - * @param $name string
 145+ * @param $result array
123146 * @return array|bool
124147 */
125 - protected function formatItem( $name ) {
126 - $title = Title::newFromText( $name );
127 - if( $title ) {
128 - $title = $this->_checkRedirect( $title );
129 - if( $this->_seen( $title ) ) {
130 - return false;
131 - }
 148+ protected function formatItem( $result ) {
 149+ $title = $result['title'];
132150
 151+ $item = array();
 152+ if ( $result['extract'] === false || $result['image'] === false ) {
133153 list( $extract, $badge ) = $this->getExtract( $title );
134 - $image = $this->getBadge( $title, $badge );
135 -
136 - $item = array();
137 - $item['Text']['*'] = $title->getPrefixedText();
138 - $item['Description']['*'] = $extract;
139 - $item['Url']['*'] = wfExpandUrl( $title->getFullUrl(), PROTO_CURRENT );
140 - if( $image ) {
141 - $thumb = $image->transform( array( 'width' => 50, 'height' => 50 ), 0 );
142 - if( $thumb ) {
143 - $item['Image'] = array(
144 - 'source' => wfExpandUrl( $thumb->getUrl(), PROTO_CURRENT ),
145 - //alt
146 - 'width' => $thumb->getWidth(),
147 - 'height' => $thumb->getHeight()
148 - );
 154+ if ( $result['image'] === false ) {
 155+ $image = $this->getBadge( $title, $badge );
 156+ if( $image ) {
 157+ $thumb = $image->transform( array( 'width' => 50, 'height' => 50 ), 0 );
 158+ if( $thumb ) {
 159+ $item['Image'] = array(
 160+ 'source' => wfExpandUrl( $thumb->getUrl(), PROTO_CURRENT ),
 161+ //alt
 162+ 'width' => $thumb->getWidth(),
 163+ 'height' => $thumb->getHeight()
 164+ );
 165+ }
149166 }
150167 }
151 - } else {
152 - $item = array( 'Text' => array( '*' => $name ) );
153168 }
 169+
 170+ if ( is_string( $result['extract'] ) ) {
 171+ $extract = $result['extract'];
 172+ }
 173+ if ( is_array( $result['image'] ) ) {
 174+ $item['Image'] = $result['image'];
 175+ }
 176+
 177+ $item['Text']['*'] = $title->getPrefixedText();
 178+ $item['Description']['*'] = $extract;
 179+ $item['Url']['*'] = wfExpandUrl( $title->getFullUrl(), PROTO_CURRENT );
 180+
154181 return $item;
155182 }
156183
157184 /**
158185 * @param $title Title
159186 *
160 - * @return
 187+ * @return Title
161188 */
162189 protected function _checkRedirect( $title ) {
163190 $art = new Article( $title );

Follow-up revisions

RevisionCommit summaryAuthorDate
r114401Revert r113365, r113372, r113395, r113474, r113475: unreviewed revisions in O...catrope20:27, 21 March 2012

Status & tagging log