Index: trunk/extensions/MobileFrontend/ApiQueryExcerpt.php |
— | — | @@ -1,6 +1,9 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | class ApiQueryExcerpt extends ApiQueryBase { |
| 5 | + /** |
| 6 | + * @var ParserOptions |
| 7 | + */ |
5 | 8 | private $parserOptions; |
6 | 9 | |
7 | 10 | public function __construct( $query, $moduleName ) { |
— | — | @@ -47,19 +50,43 @@ |
48 | 51 | private function getExcerpt( Title $title, $plainText ) { |
49 | 52 | global $wgMemc; |
50 | 53 | |
51 | | - $wp = WikiPage::factory( $title ); |
52 | | - $key = wfMemcKey( 'mf', 'excerpt', $plainText, $title->getArticleID(), $wp->getLatest() ); |
| 54 | + $page = WikiPage::factory( $title ); |
| 55 | + $key = wfMemcKey( 'mf', 'excerpt', $plainText, $title->getArticleID(), $page->getLatest() ); |
53 | 56 | $text = $wgMemc->get( $key ); |
54 | 57 | if ( $text !== false ) { |
55 | 58 | return $text; |
56 | 59 | } |
| 60 | + $text = $this->parse( $page ); |
| 61 | + $text = $this->convertText( $text, $title, $plainText ); |
| 62 | + $wgMemc->set( $key, $text ); |
| 63 | + return $text; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns HTML of page's zeroth section |
| 68 | + * @param WikiPage $page |
| 69 | + * @return string |
| 70 | + */ |
| 71 | + private function parse( WikiPage $page ) { |
57 | 72 | if ( !$this->parserOptions ) { |
58 | 73 | $this->parserOptions = new ParserOptions( new User( '127.0.0.1' ) ); |
59 | 74 | } |
60 | | - $pout = $wp->getParserOutput( $this->parserOptions ); |
61 | | - $text = $this->processText( $pout->getText(), $title, $plainText ); |
62 | | - $wgMemc->set( $key, $text ); |
63 | | - return $text; |
| 75 | + // first try finding full page in parser cache |
| 76 | + if ( $page->isParserCacheUsed( $this->parserOptions, 0 ) ) { |
| 77 | + $pout = ParserCache::singleton()->get( $page, $this->parserOptions ); |
| 78 | + if ( $pout ) { |
| 79 | + $text = $pout->getText(); |
| 80 | + return preg_replace( '/<h[1-6].*$/s', '', $text ); |
| 81 | + } |
| 82 | + } |
| 83 | + // in case of cache miss, render just the needed section |
| 84 | + $apiMain = new ApiMain( new FauxRequest( |
| 85 | + array( 'page' => $page->getTitle()->getPrefixedText(), 'section' => 0, 'prop' => 'text' ) ) |
| 86 | + ); |
| 87 | + $apiParse = new ApiParse( $apiMain, 'parse' ); |
| 88 | + $apiParse->execute(); |
| 89 | + $data = $apiParse->getResultData(); |
| 90 | + return $data['parse']['text']['*']; |
64 | 91 | } |
65 | 92 | |
66 | 93 | /** |
— | — | @@ -69,8 +96,7 @@ |
70 | 97 | * @param bool $plainText |
71 | 98 | * @return string |
72 | 99 | */ |
73 | | - private function processText( $text, Title $title, $plainText ) { |
74 | | - $text = preg_replace( '/<h[1-6].*$/s', '', $text ); |
| 100 | + private function convertText( $text, Title $title, $plainText ) { |
75 | 101 | $mf = new MobileFormatter( MobileFormatter::wrapHTML( $text, false ), $title, 'XHTML' ); |
76 | 102 | $mf->removeImages(); |
77 | 103 | $mf->remove( array( 'table', 'div', 'sup.reference', 'span.coordinates', 'span.geo-multi-punct', 'span.geo-nondefault' ) ); |