Index: trunk/extensions/OpenSearchXml/ApiOpenSearchXml.php |
— | — | @@ -85,9 +85,10 @@ |
86 | 86 | // Open search results may be stored for a very long time |
87 | 87 | $this->getMain()->setCacheMaxAge( 1200 ); |
88 | 88 | |
89 | | - $srchres = PrefixSearch::titleSearch( $search, $limit, $namespaces ); |
| 89 | + $data = $this->search( $search, $limit, $namespaces ); |
| 90 | + wfRunHooks( 'OpenSearchXml', array( &$data ) ); |
90 | 91 | |
91 | | - $items = array_filter( array_map( array( $this, 'formatItem' ), $srchres ) ); |
| 92 | + $items = array_map( array( $this, 'formatItem' ), $data ); |
92 | 93 | |
93 | 94 | $result = $this->getResult(); |
94 | 95 | $result->addValue( null, 'version', '2.0' ); |
— | — | @@ -97,6 +98,28 @@ |
98 | 99 | $result->addValue( null, 'Section', $items ); |
99 | 100 | } |
100 | 101 | |
| 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 | + |
101 | 124 | public function getAllowedParams() { |
102 | 125 | $params = parent::getAllowedParams(); |
103 | 126 | $params['format'] = array( |
— | — | @@ -118,45 +141,49 @@ |
119 | 142 | } |
120 | 143 | |
121 | 144 | /** |
122 | | - * @param $name string |
| 145 | + * @param $result array |
123 | 146 | * @return array|bool |
124 | 147 | */ |
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']; |
132 | 150 | |
| 151 | + $item = array(); |
| 152 | + if ( $result['extract'] === false || $result['image'] === false ) { |
133 | 153 | 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 | + } |
149 | 166 | } |
150 | 167 | } |
151 | | - } else { |
152 | | - $item = array( 'Text' => array( '*' => $name ) ); |
153 | 168 | } |
| 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 | + |
154 | 181 | return $item; |
155 | 182 | } |
156 | 183 | |
157 | 184 | /** |
158 | 185 | * @param $title Title |
159 | 186 | * |
160 | | - * @return |
| 187 | + * @return Title |
161 | 188 | */ |
162 | 189 | protected function _checkRedirect( $title ) { |
163 | 190 | $art = new Article( $title ); |