r112359 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112358‎ | r112359 | r112360 >
Date:21:56, 24 February 2012
Author:maxsem
Status:ok
Tags:
Comment:
Don't parse whole page on cache misses
Modified paths:
  • /trunk/extensions/MobileFrontend/ApiQueryExcerpt.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/ApiQueryExcerpt.php
@@ -1,6 +1,9 @@
22 <?php
33
44 class ApiQueryExcerpt extends ApiQueryBase {
 5+ /**
 6+ * @var ParserOptions
 7+ */
58 private $parserOptions;
69
710 public function __construct( $query, $moduleName ) {
@@ -47,19 +50,43 @@
4851 private function getExcerpt( Title $title, $plainText ) {
4952 global $wgMemc;
5053
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() );
5356 $text = $wgMemc->get( $key );
5457 if ( $text !== false ) {
5558 return $text;
5659 }
 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 ) {
5772 if ( !$this->parserOptions ) {
5873 $this->parserOptions = new ParserOptions( new User( '127.0.0.1' ) );
5974 }
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']['*'];
6491 }
6592
6693 /**
@@ -69,8 +96,7 @@
7097 * @param bool $plainText
7198 * @return string
7299 */
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 ) {
75101 $mf = new MobileFormatter( MobileFormatter::wrapHTML( $text, false ), $title, 'XHTML' );
76102 $mf->removeImages();
77103 $mf->remove( array( 'table', 'div', 'sup.reference', 'span.coordinates', 'span.geo-multi-punct', 'span.geo-nondefault' ) );

Status & tagging log