Index: trunk/extensions/MobileFrontend/ApiQueryExcerpt.php |
— | — | @@ -0,0 +1,117 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class ApiQueryExcerpt extends ApiQueryBase { |
| 5 | + private $parserOptions; |
| 6 | + |
| 7 | + public function __construct( $query, $moduleName ) { |
| 8 | + parent::__construct( $query, $moduleName, 'ex' ); |
| 9 | + } |
| 10 | + |
| 11 | + public function execute() { |
| 12 | + $titles = $this->getPageSet()->getGoodTitles(); |
| 13 | + if ( count( $titles ) == 0 ) { |
| 14 | + return; |
| 15 | + } |
| 16 | + $params = $this->extractRequestParams(); |
| 17 | + foreach ( $titles as $id => $t ) { |
| 18 | + $text = $this->getExcerpt( $t ); |
| 19 | + if ( isset( $params['length'] ) ) { |
| 20 | + $text = $this->trimText( $text, $params['length'] ); |
| 21 | + } |
| 22 | + $this->addPageSubItem( $id, $text ); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + private function getExcerpt( Title $title ) { |
| 27 | + global $wgMemc; |
| 28 | + |
| 29 | + $key = wfMemcKey( 'mf', 'excerpt', $title->getPrefixedDBkey(), $title->getArticleID() ); |
| 30 | + $text = $wgMemc->get( $key ); |
| 31 | + if ( $text !== false ) { |
| 32 | + return $text; |
| 33 | + } |
| 34 | + if ( !$this->parserOptions ) { |
| 35 | + $this->parserOptions = new ParserOptions( new User( '127.0.0.1' ) ); |
| 36 | + } |
| 37 | + $wp = WikiPage::factory( $title ); |
| 38 | + $pout = $wp->getParserOutput( $this->parserOptions ); |
| 39 | + $text = $this->processText( $pout->getText(), $title ); |
| 40 | + $wgMemc->set( $key, $text ); |
| 41 | + return $text; |
| 42 | + } |
| 43 | + |
| 44 | + private function processText( $text, Title $title ) { |
| 45 | + $text = preg_replace( '/<h[1-6].*$/s', '', $text ); |
| 46 | + $mf = new MobileFormatter( $text, $title, 'XHTML' ); |
| 47 | + $mf->removeImages(); |
| 48 | + $mf->remove( array( 'table', 'div', 'sup.reference', 'span.coordinates', 'span.geo-multi-punct', 'span.geo-nondefault' ) ); |
| 49 | + $mf->flatten( array( 'span', 'a' ) ); |
| 50 | + $mf->filterContent(); |
| 51 | + $text = $mf->getText(); |
| 52 | + $text = preg_replace( '/<!--.*?-->|^.*?<body>|<\/body>.*$/s', '', $text ); |
| 53 | + return trim( $text ); |
| 54 | + } |
| 55 | + |
| 56 | + private function trimText( $text, $requestedLength ) { |
| 57 | + $length = mb_strlen( $text ); |
| 58 | + if ( $length <= $requestedLength ) { |
| 59 | + return $text; |
| 60 | + } |
| 61 | + $pattern = "#^.{{$requestedLength}}[\\w/]*>?#su"; |
| 62 | + preg_match( $pattern, $text, $m ); |
| 63 | + var_dump($pattern);var_dump($m);die; |
| 64 | + $text = $m[1]; |
| 65 | + return $text; |
| 66 | + } |
| 67 | + |
| 68 | + public function getAllowedParams() { |
| 69 | + return array( |
| 70 | + 'length' => array( |
| 71 | + ApiBase::PARAM_TYPE => 'integer', |
| 72 | + ApiBase::PARAM_MIN => 1, |
| 73 | + ), |
| 74 | + 'limit' => array( |
| 75 | + ApiBase::PARAM_DFLT => 1, |
| 76 | + ApiBase::PARAM_TYPE => 'limit', |
| 77 | + ApiBase::PARAM_MIN => 1, |
| 78 | + ApiBase::PARAM_MAX => 10, |
| 79 | + ApiBase::PARAM_MAX2 => 20, |
| 80 | + ), |
| 81 | + 'continue' => array( |
| 82 | + ApiBase::PARAM_TYPE => 'string', |
| 83 | + ), |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + public function getParamDescription() { |
| 88 | + return array( |
| 89 | + 'limit' => 'How many excerpts to return', |
| 90 | + 'continue' => 'When more results are available, use this to continue', |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + public function getDescription() { |
| 95 | + return 'Returns plain-text excerpts of the given page(s)'; |
| 96 | + } |
| 97 | + |
| 98 | + public function getPossibleErrors() { |
| 99 | + return array_merge( parent::getPossibleErrors(), array( |
| 100 | + array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ), |
| 101 | + ) ); |
| 102 | + } |
| 103 | + |
| 104 | + public function getExamples() { |
| 105 | + return false;//@todo: |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + public function getHelpUrls() { |
| 110 | + return 'https://www.mediawiki.org/wiki/Extension:MobileFrontend#New_API'; |
| 111 | + } |
| 112 | + |
| 113 | + public function getVersion() { |
| 114 | + return __CLASS__ . ': $Id: ApiQueryCoordinates.php 110649 2012-02-03 10:18:20Z maxsem $'; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | + |
Property changes on: trunk/extensions/MobileFrontend/ApiQueryExcerpt.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 119 | + native |
Index: trunk/extensions/MobileFrontend/MobileFormatter.php |
— | — | @@ -69,6 +69,7 @@ |
70 | 70 | ); |
71 | 71 | |
72 | 72 | private $itemsToRemove = array(); |
| 73 | + private $elementsToFlatten = array(); |
73 | 74 | |
74 | 75 | /** |
75 | 76 | * Constructor |
— | — | @@ -145,6 +146,14 @@ |
146 | 147 | } |
147 | 148 | |
148 | 149 | /** |
| 150 | + * Adds one or more element name to the list to flatten (remove tag, but not its content) |
| 151 | + * @param Array|string $elements: Name(s) of tag(s) to flatten |
| 152 | + */ |
| 153 | + public function flatten( $elements ) { |
| 154 | + $this->elementsToFlatten = array_merge( $this->elementsToFlatten, (array)$elements ); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
149 | 158 | * @param Array|string $ids: Id(s) of content to keep |
150 | 159 | */ |
151 | 160 | public function whitelistIds( $ids ) { |
— | — | @@ -272,14 +281,15 @@ |
273 | 282 | case 'WML': |
274 | 283 | $html = $this->headingTransform( $html ); |
275 | 284 | // Content removal for WML rendering |
276 | | - $elements = array( 'span', 'div', 'sup', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'sup', 'sub' ); |
277 | | - foreach ( $elements as $element ) { |
278 | | - $html = preg_replace( '#</?' . $element . '[^>]*>#is', '', $html ); |
279 | | - } |
| 285 | + $this->flatten( array( 'span', 'div', 'sup', 'h[1-6]', 'sup', 'sub' ) ); |
280 | 286 | // Content wrapping |
281 | 287 | $html = $this->createWMLCard( $html ); |
282 | 288 | break; |
283 | 289 | } |
| 290 | + if ( $this->elementsToFlatten ) { |
| 291 | + $elements = implode( '|', $this->elementsToFlatten ); |
| 292 | + $html = preg_replace( "#</?($elements)[^>]*>#is", '', $html ); |
| 293 | + } |
284 | 294 | |
285 | 295 | wfProfileOut( __METHOD__ ); |
286 | 296 | return $html; |
Index: trunk/extensions/MobileFrontend/MobileFrontend.php |
— | — | @@ -45,6 +45,7 @@ |
46 | 46 | 'ExtMobileFrontend' => 'MobileFrontend.body', |
47 | 47 | |
48 | 48 | 'ApiParseExtender' => 'ApiParseExtender', |
| 49 | + 'ApiQueryExcerpt' => 'ApiQueryExcerpt', |
49 | 50 | 'CssDetection' => 'CssDetection', |
50 | 51 | 'DeviceDetection' => 'DeviceDetection', |
51 | 52 | 'MobileFormatter' => 'MobileFormatter', |
— | — | @@ -88,6 +89,8 @@ |
89 | 90 | |
90 | 91 | $wgExtensionFunctions[] = 'efMobileFrontend_Setup'; |
91 | 92 | |
| 93 | +$wgAPIPropModules['excerpt'] = 'ApiQueryExcerpt'; |
| 94 | + |
92 | 95 | $wgHooks['APIGetAllowedParams'][] = 'ApiParseExtender::onAPIGetAllowedParams'; |
93 | 96 | $wgHooks['APIAfterExecute'][] = 'ApiParseExtender::onAPIAfterExecute'; |
94 | 97 | $wgHooks['APIGetParamDescription'][] = 'ApiParseExtender::onAPIGetParamDescription'; |