https://www.mediawiki.org/wiki/Special:Code/MediaWiki/113365

Revision: 113365
Author:   maxsem
Date:     2012-03-08 13:57:39 +0000 (Thu, 08 Mar 2012)
Log Message:
-----------
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: trunk/extensions/OpenSearchXml/ApiOpenSearchXml.php
===================================================================
--- trunk/extensions/OpenSearchXml/ApiOpenSearchXml.php 2012-03-08 13:28:01 UTC 
(rev 113364)
+++ trunk/extensions/OpenSearchXml/ApiOpenSearchXml.php 2012-03-08 13:57:39 UTC 
(rev 113365)
@@ -85,9 +85,10 @@
                // Open search results may be stored for a very long time
                $this->getMain()->setCacheMaxAge( 1200 );
 
-               $srchres = PrefixSearch::titleSearch( $search, $limit, 
$namespaces );
+               $data = $this->search( $search, $limit, $namespaces );
+               wfRunHooks( 'OpenSearchXml', array( &$data ) );
 
-               $items = array_filter( array_map( array( $this, 'formatItem' ), 
$srchres ) );
+               $items = array_map( array( $this, 'formatItem' ), $data );
 
                $result = $this->getResult();
                $result->addValue( null, 'version', '2.0' );
@@ -97,6 +98,28 @@
                $result->addValue( null, 'Section', $items );
        }
 
+       private function search( $search, $limit, $namespaces ) {
+               $srchres = PrefixSearch::titleSearch( $search, $limit, 
$namespaces );
+               $titles = array_filter( array_map( 'Title::newFromText', 
$srchres ) );
+               $lb = new LinkBatch( $titles );
+               $lb->setCaller( __METHOD__ );
+               $lb->execute();
+
+               $results = array();
+               foreach ( $titles as $title ) {
+                       $title = $this->_checkRedirect( $title );
+                       if( $this->_seen( $title ) ) {
+                               continue;
+                       }
+                       $results[$title->getArticleID()] = array(
+                               'title' => $title,
+                               'extract' => false,
+                               'image' => false,
+                       );
+               }
+               return $results;
+       }
+
        public function getAllowedParams() {
                $params = parent::getAllowedParams();
                $params['format'] = array(
@@ -118,45 +141,49 @@
        }
 
        /**
-        * @param $name string
+        * @param $result array
         * @return array|bool
         */
-       protected function formatItem( $name ) {
-               $title = Title::newFromText( $name );
-               if( $title ) {
-                       $title = $this->_checkRedirect( $title );
-                       if( $this->_seen( $title ) ) {
-                               return false;
-                       }
+       protected function formatItem( $result ) {
+               $title = $result['title'];
 
+               $item = array();
+               if ( $result['extract'] === false || $result['image'] === false 
) {
                        list( $extract, $badge ) = $this->getExtract( $title );
-                       $image = $this->getBadge( $title, $badge );
-
-                       $item = array();
-                       $item['Text']['*'] = $title->getPrefixedText();
-                       $item['Description']['*'] = $extract;
-                       $item['Url']['*'] = wfExpandUrl( $title->getFullUrl(), 
PROTO_CURRENT );
-                       if( $image ) {
-                               $thumb = $image->transform( array( 'width' => 
50, 'height' => 50 ), 0 );
-                               if( $thumb ) {
-                                       $item['Image'] = array(
-                                               'source' => wfExpandUrl( 
$thumb->getUrl(), PROTO_CURRENT ),
-                                               //alt
-                                               'width' => $thumb->getWidth(),
-                                               'height' => $thumb->getHeight()
-                                       );
+                       if ( $result['image'] === false ) {
+                               $image = $this->getBadge( $title, $badge );
+                               if( $image ) {
+                                       $thumb = $image->transform( array( 
'width' => 50, 'height' => 50 ), 0 );
+                                       if( $thumb ) {
+                                               $item['Image'] = array(
+                                                       'source' => 
wfExpandUrl( $thumb->getUrl(), PROTO_CURRENT ),
+                                                       //alt
+                                                       'width' => 
$thumb->getWidth(),
+                                                       'height' => 
$thumb->getHeight()
+                                               );
+                                       }
                                }
                        }
-               } else {
-                       $item = array( 'Text' => array( '*' => $name ) );
                }
+
+               if ( is_string( $result['extract'] ) ) {
+                       $extract = $result['extract'];
+               }
+               if ( is_array( $result['image'] ) ) {
+                       $item['Image'] = $result['image'];
+               }
+
+               $item['Text']['*'] = $title->getPrefixedText();
+               $item['Description']['*'] = $extract;
+               $item['Url']['*'] = wfExpandUrl( $title->getFullUrl(), 
PROTO_CURRENT );
+
                return $item;
        }
 
        /**
         * @param $title Title
         *
-        * @return
+        * @return Title
         */
        protected function _checkRedirect( $title ) {
                $art = new Article( $title );


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to