Index: trunk/extensions/MobileFrontend/ApiQueryExcerpts.php |
— | — | @@ -1,214 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -class ApiQueryExcerpts extends ApiQueryBase { |
5 | | - /** |
6 | | - * @var ParserOptions |
7 | | - */ |
8 | | - private $parserOptions; |
9 | | - |
10 | | - public function __construct( $query, $moduleName ) { |
11 | | - parent::__construct( $query, $moduleName, 'ex' ); |
12 | | - } |
13 | | - |
14 | | - public function execute() { |
15 | | - wfProfileIn( __METHOD__ ); |
16 | | - $titles = $this->getPageSet()->getGoodTitles(); |
17 | | - if ( count( $titles ) == 0 ) { |
18 | | - wfProfileOut( __METHOD__ ); |
19 | | - return; |
20 | | - } |
21 | | - $params = $this->extractRequestParams(); |
22 | | - $continue = 0; |
23 | | - if ( isset( $params['continue'] ) ) { |
24 | | - $continue = intval( $params['continue'] ); |
25 | | - if ( $continue < 0 || $continue > count( $titles ) ) { |
26 | | - $this->dieUsageMsg( '_badcontinue' ); |
27 | | - } |
28 | | - $titles = array_slice( $titles, $continue, null, true ); |
29 | | - } |
30 | | - $count = 0; |
31 | | - foreach ( $titles as $id => $t ) { |
32 | | - if ( ++$count > $params['limit'] ) { |
33 | | - $this->setContinueEnumParameter( 'continue', $continue + $count - 1 ); |
34 | | - break; |
35 | | - } |
36 | | - $text = $this->getExcerpt( $t, $params['plaintext'] ); |
37 | | - if ( isset( $params['length'] ) ) { |
38 | | - $text = $this->trimText( $text, $params['length'], $params['plaintext'] ); |
39 | | - } |
40 | | - $fit = $this->addPageSubItem( $id, $text ); |
41 | | - if ( !$fit ) { |
42 | | - $this->setContinueEnumParameter( 'continue', $continue + $count - 1 ); |
43 | | - break; |
44 | | - } |
45 | | - } |
46 | | - wfProfileOut( __METHOD__ ); |
47 | | - } |
48 | | - |
49 | | - /** |
50 | | - * Returns a processed, but not trimmed excerpt |
51 | | - * @param Title $title |
52 | | - * @return string |
53 | | - */ |
54 | | - private function getExcerpt( Title $title, $plainText ) { |
55 | | - global $wgMemc; |
56 | | - |
57 | | - wfProfileIn( __METHOD__ ); |
58 | | - $page = WikiPage::factory( $title ); |
59 | | - $key = wfMemcKey( 'mf', 'excerpt', $plainText, $title->getArticleID(), $page->getLatest() ); |
60 | | - $text = $wgMemc->get( $key ); |
61 | | - if ( $text !== false ) { |
62 | | - wfProfileOut( __METHOD__ ); |
63 | | - return $text; |
64 | | - } |
65 | | - $text = $this->parse( $page ); |
66 | | - $text = $this->convertText( $text, $title, $plainText ); |
67 | | - $wgMemc->set( $key, $text ); |
68 | | - wfProfileOut( __METHOD__ ); |
69 | | - return $text; |
70 | | - } |
71 | | - |
72 | | - /** |
73 | | - * Returns HTML of page's zeroth section |
74 | | - * @param WikiPage $page |
75 | | - * @return string |
76 | | - */ |
77 | | - private function parse( WikiPage $page ) { |
78 | | - wfProfileIn( __METHOD__ ); |
79 | | - if ( !$this->parserOptions ) { |
80 | | - $this->parserOptions = new ParserOptions( new User( '127.0.0.1' ) ); |
81 | | - } |
82 | | - // first try finding full page in parser cache |
83 | | - if ( $page->isParserCacheUsed( $this->parserOptions, 0 ) ) { |
84 | | - $pout = ParserCache::singleton()->get( $page, $this->parserOptions ); |
85 | | - if ( $pout ) { |
86 | | - $text = $pout->getText(); |
87 | | - $s = preg_replace( '/<h[1-6].*$/s', '', $text ); |
88 | | - wfProfileOut( __METHOD__ ); |
89 | | - return $s; |
90 | | - } |
91 | | - } |
92 | | - // in case of cache miss, render just the needed section |
93 | | - $apiMain = new ApiMain( new FauxRequest( |
94 | | - array( 'page' => $page->getTitle()->getPrefixedText(), 'section' => 0, 'prop' => 'text' ) ) |
95 | | - ); |
96 | | - $apiParse = new ApiParse( $apiMain, 'parse' ); |
97 | | - $apiParse->execute(); |
98 | | - $data = $apiParse->getResultData(); |
99 | | - wfProfileOut( __METHOD__ ); |
100 | | - return $data['parse']['text']['*']; |
101 | | - } |
102 | | - |
103 | | - /** |
104 | | - * Converts page HTML into an excerpt |
105 | | - * @param string $text |
106 | | - * @param Title $title |
107 | | - * @param bool $plainText |
108 | | - * @return string |
109 | | - */ |
110 | | - private function convertText( $text, Title $title, $plainText ) { |
111 | | - wfProfileIn( __METHOD__ ); |
112 | | - $mf = new MobileFormatter( MobileFormatter::wrapHTML( $text, false ), $title, 'XHTML' ); |
113 | | - $mf->removeImages(); |
114 | | - $mf->remove( array( 'table', 'div', 'sup.reference', 'span.coordinates', |
115 | | - 'span.geo-multi-punct', 'span.geo-nondefault', '.noexcerpt', '.error' ) |
116 | | - ); |
117 | | - if ( $plainText ) { |
118 | | - $mf->flatten( '[?!]?[a-z0-9]+' ); |
119 | | - } else { |
120 | | - $mf->flatten( array( 'span', 'a' ) ); |
121 | | - } |
122 | | - $mf->filterContent(); |
123 | | - $text = $mf->getText(); |
124 | | - $text = preg_replace( '/<!--.*?-->|^.*?<body>|<\/body>.*$/s', '', $text ); |
125 | | - if ( $plainText ) { |
126 | | - $text = html_entity_decode( $text ); |
127 | | - } |
128 | | - wfProfileOut( __METHOD__ ); |
129 | | - return trim( $text ); |
130 | | - } |
131 | | - |
132 | | - /** |
133 | | - * |
134 | | - * @param string $text |
135 | | - * @param int $requestedLength |
136 | | - * @param bool $plainText |
137 | | - * @return string |
138 | | - */ |
139 | | - private function trimText( $text, $requestedLength, $plainText ) { |
140 | | - global $wgUseTidy; |
141 | | - |
142 | | - wfProfileIn( __METHOD__ ); |
143 | | - $length = mb_strlen( $text ); |
144 | | - if ( $length <= $requestedLength ) { |
145 | | - wfProfileOut( __METHOD__ ); |
146 | | - return $text; |
147 | | - } |
148 | | - $pattern = "#^.{{$requestedLength}}[\\w/]*>?#su"; |
149 | | - preg_match( $pattern, $text, $m ); |
150 | | - $text = $m[0]; |
151 | | - // Fix possibly unclosed tags |
152 | | - if ( $wgUseTidy && !$plainText ) { |
153 | | - $text = trim ( MWTidy::tidy( $text ) ); |
154 | | - } |
155 | | - $text .= wfMessage( 'ellipsis' )->inContentLanguage()->text(); |
156 | | - wfProfileOut( __METHOD__ ); |
157 | | - return $text; |
158 | | - } |
159 | | - |
160 | | - public function getAllowedParams() { |
161 | | - return array( |
162 | | - 'length' => array( |
163 | | - ApiBase::PARAM_TYPE => 'integer', |
164 | | - ApiBase::PARAM_MIN => 1, |
165 | | - ), |
166 | | - 'limit' => array( |
167 | | - ApiBase::PARAM_DFLT => 1, |
168 | | - ApiBase::PARAM_TYPE => 'limit', |
169 | | - ApiBase::PARAM_MIN => 1, |
170 | | - ApiBase::PARAM_MAX => 20, |
171 | | - ApiBase::PARAM_MAX2 => 20, |
172 | | - ), |
173 | | - 'plaintext' => false, |
174 | | - 'continue' => array( |
175 | | - ApiBase::PARAM_TYPE => 'integer', |
176 | | - ), |
177 | | - ); |
178 | | - } |
179 | | - |
180 | | - public function getParamDescription() { |
181 | | - return array( |
182 | | - 'length' => 'How many characters to return, actual text returned might be slightly longer.', |
183 | | - 'limit' => 'How many excerpts to return', |
184 | | - 'plaintext' => 'Return excerpts as plaintext instead of limited HTML', |
185 | | - 'continue' => 'When more results are available, use this to continue', |
186 | | - ); |
187 | | - } |
188 | | - |
189 | | - public function getDescription() { |
190 | | - return 'Returns excerpts of the given page(s)'; |
191 | | - } |
192 | | - |
193 | | - public function getPossibleErrors() { |
194 | | - return array_merge( parent::getPossibleErrors(), array( |
195 | | - array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ), |
196 | | - ) ); |
197 | | - } |
198 | | - |
199 | | - public function getExamples() { |
200 | | - return array( |
201 | | - 'api.php?action=query&prop=excerpt&length=175&titles=Therion' => 'Get a 175-character excerpt', |
202 | | - ); |
203 | | - } |
204 | | - |
205 | | - |
206 | | - public function getHelpUrls() { |
207 | | - return 'https://www.mediawiki.org/wiki/Extension:MobileFrontend#New_API'; |
208 | | - } |
209 | | - |
210 | | - public function getVersion() { |
211 | | - return __CLASS__ . ': $Id$'; |
212 | | - } |
213 | | -} |
214 | | - |
215 | | - |
Index: trunk/extensions/MobileFrontend/ApiParseExtender.php |
— | — | @@ -1,92 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -/** |
5 | | - * Extends API action=parse with mobile goodies |
6 | | - */ |
7 | | -class ApiParseExtender { |
8 | | - /** |
9 | | - * APIGetAllowedParams hook handler |
10 | | - * @see https://www.mediawiki.org/wiki/Manual:Hooks/APIGetAllowedParams |
11 | | - * @param ApiBase $module |
12 | | - * @param array|bool $params |
13 | | - */ |
14 | | - public static function onAPIGetAllowedParams( ApiBase &$module, &$params ) { |
15 | | - if ( $module->getModuleName() == 'parse' ) { |
16 | | - $params['mobileformat'] = array( |
17 | | - ApiBase::PARAM_TYPE => array( 'wml', 'html' ), |
18 | | - ); |
19 | | - $params['noimages'] = false; |
20 | | - $params['mainpage'] = false; |
21 | | - } |
22 | | - return true; |
23 | | - } |
24 | | - |
25 | | - /** |
26 | | - * APIGetParamDescription hook handler |
27 | | - * @see: https://www.mediawiki.org/wiki/Manual:Hooks/APIGetParamDescription |
28 | | - * @param ApiBase $module |
29 | | - * @param Array|bool $params |
30 | | - */ |
31 | | - public static function onAPIGetParamDescription( ApiBase &$module, &$params ) { |
32 | | - if ( $module->getModuleName() == 'parse' ) { |
33 | | - $params['mobileformat'] = 'Return parse output in a format suitable for mobile devices'; |
34 | | - $params['noimages'] = 'Disable images in mobile output'; |
35 | | - $params['mainpage'] = 'Apply mobile main page transformations'; |
36 | | - } |
37 | | - return true; |
38 | | - } |
39 | | - |
40 | | - /** |
41 | | - * APIGetDescription hook handler |
42 | | - * @see: https://www.mediawiki.org/wiki/Manual:Hooks/APIGetDescription |
43 | | - * @param ApiBase $module |
44 | | - * @param Array|string $desc |
45 | | - */ |
46 | | - public static function onAPIGetDescription( ApiBase &$module, &$desc ) { |
47 | | - if ( $module->getModuleName() == 'parse' ) { |
48 | | - $desc = (array)$desc; |
49 | | - $desc[] = 'Extended by MobileFrontend'; |
50 | | - } |
51 | | - return true; |
52 | | - } |
53 | | - |
54 | | - /** |
55 | | - * APIAfterExecute hook handler |
56 | | - * @see: https://www.mediawiki.org/wiki/Manual:Hooks/ |
57 | | - * @param ApiBase $module |
58 | | - */ |
59 | | - public static function onAPIAfterExecute( ApiBase &$module ) { |
60 | | - if ( $module->getModuleName() == 'parse' ) { |
61 | | - wfProfileIn( __METHOD__ ); |
62 | | - $data = $module->getResultData(); |
63 | | - $params = $module->extractRequestParams(); |
64 | | - if ( isset( $data['parse']['text'] ) && isset( $params['mobileformat'] ) ) { |
65 | | - $result = $module->getResult(); |
66 | | - $result->reset(); |
67 | | - |
68 | | - $title = Title::newFromText( $data['parse']['title'] ); |
69 | | - $context = new WmlContext(); |
70 | | - $context->setCurrentUrl( $title->getCanonicalURL() ); |
71 | | - $context->setRequestedSegment( isset( $params['section'] ) |
72 | | - ? $params['section'] + 1 // Segment numbers start from 1 |
73 | | - : 0 |
74 | | - ); |
75 | | - $context->setUseFormat( 'wml' ); // Force WML links just in case |
76 | | - $context->setOnlyThisSegment( isset( $params['section'] ) ); |
77 | | - $mf = new MobileFormatter( MobileFormatter::wrapHTML( $data['parse']['text']['*'] ), |
78 | | - $title, |
79 | | - ExtMobileFrontend::parseContentFormat( $params['mobileformat'] ), |
80 | | - $context |
81 | | - ); |
82 | | - $mf->removeImages( $params['noimages'] ); |
83 | | - $mf->setIsMainPage( $params['mainpage'] ); |
84 | | - $mf->filterContent(); |
85 | | - $data['parse']['text'] = $mf->getText( 'content' ); |
86 | | - |
87 | | - $result->addValue( null, $module->getModuleName(), $data['parse'] ); |
88 | | - } |
89 | | - wfProfileOut( __METHOD__ ); |
90 | | - } |
91 | | - return true; |
92 | | - } |
93 | | -} |
Index: trunk/extensions/MobileFrontend/api/ApiQueryExcerpts.php |
— | — | @@ -0,0 +1,214 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class ApiQueryExcerpts extends ApiQueryBase { |
| 5 | + /** |
| 6 | + * @var ParserOptions |
| 7 | + */ |
| 8 | + private $parserOptions; |
| 9 | + |
| 10 | + public function __construct( $query, $moduleName ) { |
| 11 | + parent::__construct( $query, $moduleName, 'ex' ); |
| 12 | + } |
| 13 | + |
| 14 | + public function execute() { |
| 15 | + wfProfileIn( __METHOD__ ); |
| 16 | + $titles = $this->getPageSet()->getGoodTitles(); |
| 17 | + if ( count( $titles ) == 0 ) { |
| 18 | + wfProfileOut( __METHOD__ ); |
| 19 | + return; |
| 20 | + } |
| 21 | + $params = $this->extractRequestParams(); |
| 22 | + $continue = 0; |
| 23 | + if ( isset( $params['continue'] ) ) { |
| 24 | + $continue = intval( $params['continue'] ); |
| 25 | + if ( $continue < 0 || $continue > count( $titles ) ) { |
| 26 | + $this->dieUsageMsg( '_badcontinue' ); |
| 27 | + } |
| 28 | + $titles = array_slice( $titles, $continue, null, true ); |
| 29 | + } |
| 30 | + $count = 0; |
| 31 | + foreach ( $titles as $id => $t ) { |
| 32 | + if ( ++$count > $params['limit'] ) { |
| 33 | + $this->setContinueEnumParameter( 'continue', $continue + $count - 1 ); |
| 34 | + break; |
| 35 | + } |
| 36 | + $text = $this->getExcerpt( $t, $params['plaintext'] ); |
| 37 | + if ( isset( $params['length'] ) ) { |
| 38 | + $text = $this->trimText( $text, $params['length'], $params['plaintext'] ); |
| 39 | + } |
| 40 | + $fit = $this->addPageSubItem( $id, $text ); |
| 41 | + if ( !$fit ) { |
| 42 | + $this->setContinueEnumParameter( 'continue', $continue + $count - 1 ); |
| 43 | + break; |
| 44 | + } |
| 45 | + } |
| 46 | + wfProfileOut( __METHOD__ ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns a processed, but not trimmed excerpt |
| 51 | + * @param Title $title |
| 52 | + * @return string |
| 53 | + */ |
| 54 | + private function getExcerpt( Title $title, $plainText ) { |
| 55 | + global $wgMemc; |
| 56 | + |
| 57 | + wfProfileIn( __METHOD__ ); |
| 58 | + $page = WikiPage::factory( $title ); |
| 59 | + $key = wfMemcKey( 'mf', 'excerpt', $plainText, $title->getArticleID(), $page->getLatest() ); |
| 60 | + $text = $wgMemc->get( $key ); |
| 61 | + if ( $text !== false ) { |
| 62 | + wfProfileOut( __METHOD__ ); |
| 63 | + return $text; |
| 64 | + } |
| 65 | + $text = $this->parse( $page ); |
| 66 | + $text = $this->convertText( $text, $title, $plainText ); |
| 67 | + $wgMemc->set( $key, $text ); |
| 68 | + wfProfileOut( __METHOD__ ); |
| 69 | + return $text; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns HTML of page's zeroth section |
| 74 | + * @param WikiPage $page |
| 75 | + * @return string |
| 76 | + */ |
| 77 | + private function parse( WikiPage $page ) { |
| 78 | + wfProfileIn( __METHOD__ ); |
| 79 | + if ( !$this->parserOptions ) { |
| 80 | + $this->parserOptions = new ParserOptions( new User( '127.0.0.1' ) ); |
| 81 | + } |
| 82 | + // first try finding full page in parser cache |
| 83 | + if ( $page->isParserCacheUsed( $this->parserOptions, 0 ) ) { |
| 84 | + $pout = ParserCache::singleton()->get( $page, $this->parserOptions ); |
| 85 | + if ( $pout ) { |
| 86 | + $text = $pout->getText(); |
| 87 | + $s = preg_replace( '/<h[1-6].*$/s', '', $text ); |
| 88 | + wfProfileOut( __METHOD__ ); |
| 89 | + return $s; |
| 90 | + } |
| 91 | + } |
| 92 | + // in case of cache miss, render just the needed section |
| 93 | + $apiMain = new ApiMain( new FauxRequest( |
| 94 | + array( 'page' => $page->getTitle()->getPrefixedText(), 'section' => 0, 'prop' => 'text' ) ) |
| 95 | + ); |
| 96 | + $apiParse = new ApiParse( $apiMain, 'parse' ); |
| 97 | + $apiParse->execute(); |
| 98 | + $data = $apiParse->getResultData(); |
| 99 | + wfProfileOut( __METHOD__ ); |
| 100 | + return $data['parse']['text']['*']; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Converts page HTML into an excerpt |
| 105 | + * @param string $text |
| 106 | + * @param Title $title |
| 107 | + * @param bool $plainText |
| 108 | + * @return string |
| 109 | + */ |
| 110 | + private function convertText( $text, Title $title, $plainText ) { |
| 111 | + wfProfileIn( __METHOD__ ); |
| 112 | + $mf = new MobileFormatter( MobileFormatter::wrapHTML( $text, false ), $title, 'XHTML' ); |
| 113 | + $mf->removeImages(); |
| 114 | + $mf->remove( array( 'table', 'div', 'sup.reference', 'span.coordinates', |
| 115 | + 'span.geo-multi-punct', 'span.geo-nondefault', '.noexcerpt', '.error' ) |
| 116 | + ); |
| 117 | + if ( $plainText ) { |
| 118 | + $mf->flatten( '[?!]?[a-z0-9]+' ); |
| 119 | + } else { |
| 120 | + $mf->flatten( array( 'span', 'a' ) ); |
| 121 | + } |
| 122 | + $mf->filterContent(); |
| 123 | + $text = $mf->getText(); |
| 124 | + $text = preg_replace( '/<!--.*?-->|^.*?<body>|<\/body>.*$/s', '', $text ); |
| 125 | + if ( $plainText ) { |
| 126 | + $text = html_entity_decode( $text ); |
| 127 | + } |
| 128 | + wfProfileOut( __METHOD__ ); |
| 129 | + return trim( $text ); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * |
| 134 | + * @param string $text |
| 135 | + * @param int $requestedLength |
| 136 | + * @param bool $plainText |
| 137 | + * @return string |
| 138 | + */ |
| 139 | + private function trimText( $text, $requestedLength, $plainText ) { |
| 140 | + global $wgUseTidy; |
| 141 | + |
| 142 | + wfProfileIn( __METHOD__ ); |
| 143 | + $length = mb_strlen( $text ); |
| 144 | + if ( $length <= $requestedLength ) { |
| 145 | + wfProfileOut( __METHOD__ ); |
| 146 | + return $text; |
| 147 | + } |
| 148 | + $pattern = "#^.{{$requestedLength}}[\\w/]*>?#su"; |
| 149 | + preg_match( $pattern, $text, $m ); |
| 150 | + $text = $m[0]; |
| 151 | + // Fix possibly unclosed tags |
| 152 | + if ( $wgUseTidy && !$plainText ) { |
| 153 | + $text = trim ( MWTidy::tidy( $text ) ); |
| 154 | + } |
| 155 | + $text .= wfMessage( 'ellipsis' )->inContentLanguage()->text(); |
| 156 | + wfProfileOut( __METHOD__ ); |
| 157 | + return $text; |
| 158 | + } |
| 159 | + |
| 160 | + public function getAllowedParams() { |
| 161 | + return array( |
| 162 | + 'length' => array( |
| 163 | + ApiBase::PARAM_TYPE => 'integer', |
| 164 | + ApiBase::PARAM_MIN => 1, |
| 165 | + ), |
| 166 | + 'limit' => array( |
| 167 | + ApiBase::PARAM_DFLT => 1, |
| 168 | + ApiBase::PARAM_TYPE => 'limit', |
| 169 | + ApiBase::PARAM_MIN => 1, |
| 170 | + ApiBase::PARAM_MAX => 20, |
| 171 | + ApiBase::PARAM_MAX2 => 20, |
| 172 | + ), |
| 173 | + 'plaintext' => false, |
| 174 | + 'continue' => array( |
| 175 | + ApiBase::PARAM_TYPE => 'integer', |
| 176 | + ), |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + public function getParamDescription() { |
| 181 | + return array( |
| 182 | + 'length' => 'How many characters to return, actual text returned might be slightly longer.', |
| 183 | + 'limit' => 'How many excerpts to return', |
| 184 | + 'plaintext' => 'Return excerpts as plaintext instead of limited HTML', |
| 185 | + 'continue' => 'When more results are available, use this to continue', |
| 186 | + ); |
| 187 | + } |
| 188 | + |
| 189 | + public function getDescription() { |
| 190 | + return 'Returns excerpts of the given page(s)'; |
| 191 | + } |
| 192 | + |
| 193 | + public function getPossibleErrors() { |
| 194 | + return array_merge( parent::getPossibleErrors(), array( |
| 195 | + array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ), |
| 196 | + ) ); |
| 197 | + } |
| 198 | + |
| 199 | + public function getExamples() { |
| 200 | + return array( |
| 201 | + 'api.php?action=query&prop=excerpt&length=175&titles=Therion' => 'Get a 175-character excerpt', |
| 202 | + ); |
| 203 | + } |
| 204 | + |
| 205 | + |
| 206 | + public function getHelpUrls() { |
| 207 | + return 'https://www.mediawiki.org/wiki/Extension:MobileFrontend#New_API'; |
| 208 | + } |
| 209 | + |
| 210 | + public function getVersion() { |
| 211 | + return __CLASS__ . ': $Id$'; |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | + |
Property changes on: trunk/extensions/MobileFrontend/api/ApiQueryExcerpts.php |
___________________________________________________________________ |
Added: svn:keywords |
1 | 216 | + Id |
Added: svn:eol-style |
2 | 217 | + native |
Index: trunk/extensions/MobileFrontend/api/ApiParseExtender.php |
— | — | @@ -0,0 +1,92 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Extends API action=parse with mobile goodies |
| 6 | + */ |
| 7 | +class ApiParseExtender { |
| 8 | + /** |
| 9 | + * APIGetAllowedParams hook handler |
| 10 | + * @see https://www.mediawiki.org/wiki/Manual:Hooks/APIGetAllowedParams |
| 11 | + * @param ApiBase $module |
| 12 | + * @param array|bool $params |
| 13 | + */ |
| 14 | + public static function onAPIGetAllowedParams( ApiBase &$module, &$params ) { |
| 15 | + if ( $module->getModuleName() == 'parse' ) { |
| 16 | + $params['mobileformat'] = array( |
| 17 | + ApiBase::PARAM_TYPE => array( 'wml', 'html' ), |
| 18 | + ); |
| 19 | + $params['noimages'] = false; |
| 20 | + $params['mainpage'] = false; |
| 21 | + } |
| 22 | + return true; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * APIGetParamDescription hook handler |
| 27 | + * @see: https://www.mediawiki.org/wiki/Manual:Hooks/APIGetParamDescription |
| 28 | + * @param ApiBase $module |
| 29 | + * @param Array|bool $params |
| 30 | + */ |
| 31 | + public static function onAPIGetParamDescription( ApiBase &$module, &$params ) { |
| 32 | + if ( $module->getModuleName() == 'parse' ) { |
| 33 | + $params['mobileformat'] = 'Return parse output in a format suitable for mobile devices'; |
| 34 | + $params['noimages'] = 'Disable images in mobile output'; |
| 35 | + $params['mainpage'] = 'Apply mobile main page transformations'; |
| 36 | + } |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * APIGetDescription hook handler |
| 42 | + * @see: https://www.mediawiki.org/wiki/Manual:Hooks/APIGetDescription |
| 43 | + * @param ApiBase $module |
| 44 | + * @param Array|string $desc |
| 45 | + */ |
| 46 | + public static function onAPIGetDescription( ApiBase &$module, &$desc ) { |
| 47 | + if ( $module->getModuleName() == 'parse' ) { |
| 48 | + $desc = (array)$desc; |
| 49 | + $desc[] = 'Extended by MobileFrontend'; |
| 50 | + } |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * APIAfterExecute hook handler |
| 56 | + * @see: https://www.mediawiki.org/wiki/Manual:Hooks/ |
| 57 | + * @param ApiBase $module |
| 58 | + */ |
| 59 | + public static function onAPIAfterExecute( ApiBase &$module ) { |
| 60 | + if ( $module->getModuleName() == 'parse' ) { |
| 61 | + wfProfileIn( __METHOD__ ); |
| 62 | + $data = $module->getResultData(); |
| 63 | + $params = $module->extractRequestParams(); |
| 64 | + if ( isset( $data['parse']['text'] ) && isset( $params['mobileformat'] ) ) { |
| 65 | + $result = $module->getResult(); |
| 66 | + $result->reset(); |
| 67 | + |
| 68 | + $title = Title::newFromText( $data['parse']['title'] ); |
| 69 | + $context = new WmlContext(); |
| 70 | + $context->setCurrentUrl( $title->getCanonicalURL() ); |
| 71 | + $context->setRequestedSegment( isset( $params['section'] ) |
| 72 | + ? $params['section'] + 1 // Segment numbers start from 1 |
| 73 | + : 0 |
| 74 | + ); |
| 75 | + $context->setUseFormat( 'wml' ); // Force WML links just in case |
| 76 | + $context->setOnlyThisSegment( isset( $params['section'] ) ); |
| 77 | + $mf = new MobileFormatter( MobileFormatter::wrapHTML( $data['parse']['text']['*'] ), |
| 78 | + $title, |
| 79 | + ExtMobileFrontend::parseContentFormat( $params['mobileformat'] ), |
| 80 | + $context |
| 81 | + ); |
| 82 | + $mf->removeImages( $params['noimages'] ); |
| 83 | + $mf->setIsMainPage( $params['mainpage'] ); |
| 84 | + $mf->filterContent(); |
| 85 | + $data['parse']['text'] = $mf->getText( 'content' ); |
| 86 | + |
| 87 | + $result->addValue( null, $module->getModuleName(), $data['parse'] ); |
| 88 | + } |
| 89 | + wfProfileOut( __METHOD__ ); |
| 90 | + } |
| 91 | + return true; |
| 92 | + } |
| 93 | +} |
Property changes on: trunk/extensions/MobileFrontend/api/ApiParseExtender.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 94 | + native |
Index: trunk/extensions/MobileFrontend/MobileFrontend.php |
— | — | @@ -44,13 +44,14 @@ |
45 | 45 | $autoloadClasses = array ( |
46 | 46 | 'ExtMobileFrontend' => 'MobileFrontend.body', |
47 | 47 | |
48 | | - 'ApiParseExtender' => 'ApiParseExtender', |
49 | | - 'ApiQueryExcerpts' => 'ApiQueryExcerpts', |
50 | 48 | 'CssDetection' => 'CssDetection', |
51 | 49 | 'DeviceDetection' => 'DeviceDetection', |
52 | 50 | 'MobileFormatter' => 'MobileFormatter', |
53 | 51 | 'WmlContext' => 'WmlContext', |
54 | 52 | |
| 53 | + 'ApiParseExtender' => 'api/ApiParseExtender', |
| 54 | + 'ApiQueryExcerpts' => 'api/ApiQueryExcerpts', |
| 55 | + |
55 | 56 | 'MobileFrontendTemplate' => 'templates/MobileFrontendTemplate', |
56 | 57 | 'ApplicationTemplate' => 'templates/ApplicationTemplate', |
57 | 58 | 'SearchTemplate' => 'templates/SearchTemplate', |