r112088 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112087‎ | r112088 | r112089 >
Date:08:16, 22 February 2012
Author:maxsem
Status:ok
Tags:
Comment:
Committing work in progress: prop=excerpt module. Ideally, it should be independent from MobileFrontend, but right now I'm just taking advantage of MobileFormatter.
Modified paths:
  • /trunk/extensions/MobileFrontend/ApiQueryExcerpt.php (added) (history)
  • /trunk/extensions/MobileFrontend/MobileFormatter.php (modified) (history)
  • /trunk/extensions/MobileFrontend/MobileFrontend.php (modified) (history)

Diff [purge]

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
1119 + native
Index: trunk/extensions/MobileFrontend/MobileFormatter.php
@@ -69,6 +69,7 @@
7070 );
7171
7272 private $itemsToRemove = array();
 73+ private $elementsToFlatten = array();
7374
7475 /**
7576 * Constructor
@@ -145,6 +146,14 @@
146147 }
147148
148149 /**
 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+ /**
149158 * @param Array|string $ids: Id(s) of content to keep
150159 */
151160 public function whitelistIds( $ids ) {
@@ -272,14 +281,15 @@
273282 case 'WML':
274283 $html = $this->headingTransform( $html );
275284 // 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' ) );
280286 // Content wrapping
281287 $html = $this->createWMLCard( $html );
282288 break;
283289 }
 290+ if ( $this->elementsToFlatten ) {
 291+ $elements = implode( '|', $this->elementsToFlatten );
 292+ $html = preg_replace( "#</?($elements)[^>]*>#is", '', $html );
 293+ }
284294
285295 wfProfileOut( __METHOD__ );
286296 return $html;
Index: trunk/extensions/MobileFrontend/MobileFrontend.php
@@ -45,6 +45,7 @@
4646 'ExtMobileFrontend' => 'MobileFrontend.body',
4747
4848 'ApiParseExtender' => 'ApiParseExtender',
 49+ 'ApiQueryExcerpt' => 'ApiQueryExcerpt',
4950 'CssDetection' => 'CssDetection',
5051 'DeviceDetection' => 'DeviceDetection',
5152 'MobileFormatter' => 'MobileFormatter',
@@ -88,6 +89,8 @@
8990
9091 $wgExtensionFunctions[] = 'efMobileFrontend_Setup';
9192
 93+$wgAPIPropModules['excerpt'] = 'ApiQueryExcerpt';
 94+
9295 $wgHooks['APIGetAllowedParams'][] = 'ApiParseExtender::onAPIGetAllowedParams';
9396 $wgHooks['APIAfterExecute'][] = 'ApiParseExtender::onAPIAfterExecute';
9497 $wgHooks['APIGetParamDescription'][] = 'ApiParseExtender::onAPIGetParamDescription';

Status & tagging log