r87268 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87267‎ | r87268 | r87269 >
Date:18:21, 2 May 2011
Author:ashley
Status:deferred
Tags:
Comment:
PatchOutputMobile: stylize (+bump version number). Some of these methods and variables really should be renamed as per our coding standards...
Modified paths:
  • /trunk/extensions/PatchOutputMobile/PatchOutputMobile.php (modified) (history)

Diff [purge]

Index: trunk/extensions/PatchOutputMobile/PatchOutputMobile.php
@@ -1,247 +1,261 @@
22 <?php
33
44 # Needs to be called within MediaWiki; not standalone
5 -if ( !defined('MEDIAWIKI') ) {
6 - echo("This is an extension to the MediaWiki package and cannot be run standalone.\n" );
7 - die(-1);
 5+if ( !defined( 'MEDIAWIKI' ) ) {
 6+ echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" );
 7+ die( -1 );
88 }
99
1010 # Define the extension; allows us make sure the extension is used correctly
11 -DEFINE( 'PATCHOUTPUTMOBILE', 'PatchOutputMobile' );
 11+define( 'PATCHOUTPUTMOBILE', 'PatchOutputMobile' );
1212
 13+// Extension credits that will show up on Special:Version
1314 $wgExtensionCredits['other'][] = array(
14 - 'name' => 'PatchOutputMobile',
15 - 'version' => ExtPatchOutputMobile::VERSION,
16 - 'author' => '[http://www.mediawiki.org/wiki/User:Preilly Preilly]',
17 - 'url' => 'http://www.mediawiki.org/wiki/Extension:PatchOutputMobile',
18 - 'description' => 'patch html output for mobile'
 15+ 'name' => 'PatchOutputMobile',
 16+ 'version' => ExtPatchOutputMobile::VERSION,
 17+ 'author' => '[http://www.mediawiki.org/wiki/User:Preilly Preilly]',
 18+ 'description' => 'Patch HTML output for mobile',
 19+ 'url' => 'http://www.mediawiki.org/wiki/Extension:PatchOutputMobile',
1920 );
20 -
 21+
2122 $wgExtPatchOutputMobile = new ExtPatchOutputMobile();
22 -
23 -$wgHooks['OutputPageBeforeHTML'][] = array(&$wgExtPatchOutputMobile,
24 - 'onOutputPageBeforeHTML');
25 -
 23+
 24+$wgHooks['OutputPageBeforeHTML'][] = array( &$wgExtPatchOutputMobile,
 25+ 'onOutputPageBeforeHTML' );
 26+
2627 class ExtPatchOutputMobile {
27 - const VERSION = '0.2';
 28+ const VERSION = '0.2.1';
2829
2930 private $doc;
3031
31 - public $items_to_remove = array("#contentSub", #redirection notice
32 - "div.messagebox", #cleanup data
33 - "#siteNotice", #site notice
34 - "#siteSub", #"From Wikipedia..."
35 - "#jump-to-nav", #jump-to-nav
36 - "div.editsection", #edit blocks
37 - "div.infobox", # Infoboxes in the article
38 - "table.toc", #table of contents
39 - "#catlinks", #category links
40 - "div.stub", #stub warnings
41 - "table.metadata", #ugly metadata
42 - "form",
43 - "div.sister-project",
44 - "script",
45 - "div.magnify", #stupid magnify thing
46 - ".editsection",
47 - "span.t",
48 - 'sup[style*="help"]',
49 - ".portal",
50 - "#protected-icon",
51 - ".printfooter",
52 - ".boilerplate",
53 - "#id-articulo-destacado",
54 - "#coordinates",
55 - "#top",
56 - ".hiddenStructure",
57 - ".noprint",
58 - ".medialist",
59 - ".mw-search-createlink");
60 -
61 - public function onOutputPageBeforeHTML(&$out, &$text) {
62 - ob_start(array(&$this, 'parse'));
 32+ public $items_to_remove = array(
 33+ '#contentSub', # redirection notice
 34+ 'div.messagebox', # cleanup data
 35+ '#siteNotice', # site notice
 36+ '#siteSub', # "From Wikipedia..."
 37+ '#jump-to-nav', # jump-to-nav
 38+ 'div.editsection', # edit blocks
 39+ 'div.infobox', # Infoboxes in the article
 40+ 'table.toc', # table of contents
 41+ '#catlinks', # category links
 42+ 'div.stub', # stub warnings
 43+ 'table.metadata', # ugly metadata
 44+ 'form',
 45+ 'div.sister-project',
 46+ 'script',
 47+ 'div.magnify', # stupid magnify thing
 48+ '.editsection',
 49+ 'span.t',
 50+ 'sup[style*="help"]',
 51+ '.portal',
 52+ '#protected-icon',
 53+ '.printfooter',
 54+ '.boilerplate',
 55+ '#id-articulo-destacado',
 56+ '#coordinates',
 57+ '#top',
 58+ '.hiddenStructure',
 59+ '.noprint',
 60+ '.medialist',
 61+ '.mw-search-createlink'
 62+ );
 63+
 64+ public function onOutputPageBeforeHTML( &$out, &$text ) {
 65+ ob_start( array( &$this, 'parse' ) );
6366 return true;
6467 }
65 -
66 - private function _show_hide_callback($matches) {
 68+
 69+ private function _show_hide_callback( $matches ) {
6770 static $headings = 0;
68 - $show = "Show";
69 - $hide = "Hide";
70 - $back_to_top = "Jump Back A Section";
 71+ $show = 'Show';
 72+ $hide = 'Hide';
 73+ $back_to_top = 'Jump Back A Section';
7174 ++$headings;
7275 // Back to top link
73 - $base = "<div class='section_anchors' id='anchor_" . intval($headings - 1) . "'><a href='#section_" . intval($headings - 1) . "' class='back_to_top'>&uarr; {$back_to_top}</a></div>";
 76+ $base = "<div class='section_anchors' id='anchor_" . intval( $headings - 1 ) .
 77+ "'><a href='#section_" . intval( $headings - 1 ) .
 78+ "' class='back_to_top'>&uarr; {$back_to_top}</a></div>";
7479 // generate the HTML we are going to inject
7580 $buttons = "<button class='section_heading show' section_id='{$headings}'>{$show}</button><button class='section_heading hide' section_id='{$headings}'>{$hide}</button>";
7681 $base .= "<h2 class='section_heading' id='section_{$headings}'{$matches[1]}{$buttons} <span>{$matches[2]}</span></h2><div class='content_block' id='content_{$headings}'>";
7782
78 - if ($headings > 1) {
 83+ if ( $headings > 1 ) {
7984 // Close it up here
80 - $base = "</div>" . $base;
 85+ $base = '</div>' . $base;
8186 }
82 -
 87+
8388 $GLOBALS['headings'] = $headings;
8489
85 - return $base;
 90+ return $base;
8691 }
87 -
88 - public function javascriptize($s) {
89 - //Closures are a PHP 5.3 feature. MediaWiki currently requires PHP 5.2.3 or higher. So, using old style for now.
90 - $s = preg_replace_callback( '/<h2(.*)<span class="mw-headline" [^>]*>(.+)<\/span>\w*<\/h2>/', array(&$this, '_show_hide_callback'), $s );
91 -
 92+
 93+ public function javascriptize( $s ) {
 94+ // Closures are a PHP 5.3 feature.
 95+ // MediaWiki currently requires PHP 5.2.3 or higher.
 96+ // So, using old style for now.
 97+ $s = preg_replace_callback(
 98+ '/<h2(.*)<span class="mw-headline" [^>]*>(.+)<\/span>\w*<\/h2>/',
 99+ array( &$this, '_show_hide_callback' ),
 100+ $s
 101+ );
 102+
92103 // if we had any, make sure to close the whole thing!
93 - if (isset($GLOBALS['headings']) && $GLOBALS['headings'] > 0) {
94 - $s = str_replace('<div class="visualClear">', '</div><div class="visualClear">', $s);
 104+ if ( isset( $GLOBALS['headings'] ) && $GLOBALS['headings'] > 0 ) {
 105+ $s = str_replace(
 106+ '<div class="visualClear">',
 107+ '</div><div class="visualClear">',
 108+ $s
 109+ );
95110 }
96111
97112 return $s;
98113 }
99 -
100 - public function parse($s) {
101 - // foreach(self::$mTable as $from => $to) {
102 - // $s =& str_replace( $from, $to, $s );
 114+
 115+ public function parse( $s ) {
 116+ // foreach( self::$mTable as $from => $to ) {
 117+ // $s =& str_replace( $from, $to, $s );
103118 // }
104 -
105 - return $this->DOMParse($s);
 119+
 120+ return $this->DOMParse( $s );
106121 }
107 -
 122+
108123 private function parse_items_to_remove() {
109124 $item_to_remove_records = array();
110 -
111 - foreach ($this->items_to_remove as $item_to_remove) {
 125+
 126+ foreach ( $this->items_to_remove as $item_to_remove ) {
112127 $type = '';
113128 $raw_name = '';
114 - CSS_detection::detect_id_css_or_tag($item_to_remove, $type, $raw_name);
 129+ CSS_detection::detect_id_css_or_tag( $item_to_remove, $type, $raw_name );
115130 $item_to_remove_records[$type][] = $raw_name;
116131 }
117 -
 132+
118133 return $item_to_remove_records;
119134 }
120 -
121 - public function DOMParse($html) {
122 -
123 - libxml_use_internal_errors(true);
124 - $this->doc = DOMDocument::loadHTML($html);
125 - libxml_use_internal_errors(false);
 135+
 136+ public function DOMParse( $html ) {
 137+ libxml_use_internal_errors( true );
 138+ $this->doc = DOMDocument::loadHTML( $html );
 139+ libxml_use_internal_errors( false );
126140 $this->doc->preserveWhiteSpace = false;
127141 $this->doc->strictErrorChecking = false;
128 -
 142+
129143 $item_to_remove_records = $this->parse_items_to_remove();
130 -
131 - //Tags
132 -
133 - //You can't remove DOMNodes from a DOMNodeList as you're iterating over them
134 - // in a foreach loop. It will seemingly leave the internal iterator on the foreach out of wack
135 - // and results will be quite strange. Though, making a queue of items to remove
136 - // seems to work. For example:
137 -
138 - $title_node = $this->doc->getElementsByTagName('title');
139 -
140 - if ($title_node->length > 0) {
141 - $title = $title_node->item(0)->nodeValue;
 144+
 145+ // Tags
 146+
 147+ // You can't remove DOMNodes from a DOMNodeList as you're iterating
 148+ // over them in a foreach loop. It will seemingly leave the internal
 149+ // iterator on the foreach out of wack and results will be quite
 150+ // strange. Though, making a queue of items to remove seems to work.
 151+ // For example:
 152+ $title_node = $this->doc->getElementsByTagName( 'title' );
 153+
 154+ if ( $title_node->length > 0 ) {
 155+ $title = $title_node->item( 0 )->nodeValue;
142156 }
143 -
 157+
144158 $domElemsToRemove = array();
145 - foreach ($item_to_remove_records['TAG'] as $tag_to_remove) {
146 - $tag_to_remove_nodes = $this->doc->getElementsByTagName($tag_to_remove);
147 -
148 - foreach($tag_to_remove_nodes as $tag_to_remove_node) {
149 - if ($tag_to_remove_node) {
 159+ foreach ( $item_to_remove_records['TAG'] as $tag_to_remove ) {
 160+ $tag_to_remove_nodes = $this->doc->getElementsByTagName( $tag_to_remove );
 161+
 162+ foreach( $tag_to_remove_nodes as $tag_to_remove_node ) {
 163+ if ( $tag_to_remove_node ) {
150164 $domElemsToRemove[] = $tag_to_remove_node;
151165 }
152166 }
153167 }
154 -
155 - foreach($domElemsToRemove as $domElement){
156 - $domElement->parentNode->removeChild($domElement);
 168+
 169+ foreach( $domElemsToRemove as $domElement ) {
 170+ $domElement->parentNode->removeChild( $domElement );
157171 }
158 -
159 - //Elements with named Ids
160 - foreach ($item_to_remove_records['ID'] as $item_to_remove) {
161 - $item_to_remove_node = $this->doc->getElementById($item_to_remove);
162 - if ($item_to_remove_node) {
163 - $removed_item_to_remove = $item_to_remove_node->parentNode->removeChild($item_to_remove_node);
 172+
 173+ // Elements with named IDs
 174+ foreach ( $item_to_remove_records['ID'] as $item_to_remove ) {
 175+ $item_to_remove_node = $this->doc->getElementById( $item_to_remove );
 176+ if ( $item_to_remove_node ) {
 177+ $removed_item_to_remove = $item_to_remove_node->parentNode->removeChild( $item_to_remove_node );
164178 }
165179 }
166 -
 180+
167181 // CSS Classes
168 - $xpath = new DOMXpath($this->doc);
169 - foreach ($item_to_remove_records['CLASS'] as $class_to_remove) {
170 -
171 - $elements = $xpath->query('//*[@class="'.$class_to_remove.'"]');
172 -
173 - foreach($elements as $element) {
174 - $removed_element = $element->parentNode->removeChild($element);
 182+ $xpath = new DOMXpath( $this->doc );
 183+ foreach ( $item_to_remove_records['CLASS'] as $class_to_remove ) {
 184+ $elements = $xpath->query( '//*[@class="' . $class_to_remove . '"]' );
 185+
 186+ foreach( $elements as $element ) {
 187+ $removed_element = $element->parentNode->removeChild( $element );
175188 }
176189 }
177 -
178 - //Tags with CSS Classes
179 - foreach ($item_to_remove_records['TAG_CLASS'] as $class_to_remove) {
180 -
181 - $parts = explode(".", $class_to_remove);
182 -
183 - $elements = $xpath->query('//'.$parts[0].'[@class="'.$parts[1].'"]');
184 -
185 - foreach($elements as $element) {
186 - $removed_element = $element->parentNode->removeChild($element);
 190+
 191+ // Tags with CSS Classes
 192+ foreach ( $item_to_remove_records['TAG_CLASS'] as $class_to_remove ) {
 193+ $parts = explode( '.', $class_to_remove );
 194+
 195+ $elements = $xpath->query(
 196+ '//' . $parts[0] . '[@class="' . $parts[1] . '"]'
 197+ );
 198+
 199+ foreach( $elements as $element ) {
 200+ $removed_element = $element->parentNode->removeChild( $element );
187201 }
188202 }
189 -
190 - $content = $this->doc->getElementById('content');
191 -
192 - $content_html = $this->doc->saveXML($content, LIBXML_NOEMPTYTAG);
193 -
194 - if (empty($title)) {
 203+
 204+ $content = $this->doc->getElementById( 'content' );
 205+
 206+ $content_html = $this->doc->saveXML( $content, LIBXML_NOEMPTYTAG );
 207+
 208+ if ( empty( $title ) ) {
195209 $title = 'Wikipedia';
196210 }
197 -
198 - require('views/notices/_donate.html.php');
199 - require('views/layout/_search_webkit.html.php');
200 - require('views/layout/_footmenu_default.html.php');
201 - require('views/layout/application.html.php');
202 -
203 - return (strlen($content_html) > 4000) ? $this->javascriptize($application_html) : $application_html; //$content_html;
 211+
 212+ require( 'views/notices/_donate.html.php' );
 213+ require( 'views/layout/_search_webkit.html.php' );
 214+ require( 'views/layout/_footmenu_default.html.php' );
 215+ require( 'views/layout/application.html.php' );
 216+
 217+ return ( strlen( $content_html ) > 4000 ) ? $this->javascriptize( $application_html ) : $application_html; //$content_html;
204218 }
205219 }
206220
207221 class CSS_detection {
208222
209 - public static function detect_id_css_or_tag($snippet, &$type, &$raw_name) {
 223+ public static function detect_id_css_or_tag( $snippet, &$type, &$raw_name ) {
210224 $output = '';
211 -
212 - if (strpos($snippet, '.') === 0) {
 225+
 226+ if ( strpos( $snippet, '.' ) === 0 ) {
213227 $output = 'Class found: ';
214228 $type = 'CLASS';
215 - $raw_name = substr($snippet, 1);
 229+ $raw_name = substr( $snippet, 1 );
216230 }
217 -
218 - if (strpos($snippet, '#') === 0) {
 231+
 232+ if ( strpos( $snippet, '#' ) === 0 ) {
219233 $output = 'ID found: ';
220234 $type = 'ID';
221 - $raw_name = substr($snippet, 1);
 235+ $raw_name = substr( $snippet, 1 );
222236 }
223 -
224 - if (strpos($snippet, '.') !== 0 &&
225 - strpos($snippet, '.') !== false) {
 237+
 238+ if ( strpos( $snippet, '.' ) !== 0 &&
 239+ strpos( $snippet, '.' ) !== false ) {
226240 $output = 'Tag with Class found: ';
227241 $type = 'TAG_CLASS';
228242 $raw_name = $snippet;
229243 }
230 -
231 - if (strpos($snippet, '.') === false &&
232 - strpos($snippet, '#') === false &&
233 - strpos($snippet, '[') === false &&
234 - strpos($snippet, ']') === false) {
 244+
 245+ if ( strpos( $snippet, '.' ) === false &&
 246+ strpos( $snippet, '#' ) === false &&
 247+ strpos( $snippet, '[' ) === false &&
 248+ strpos( $snippet, ']' ) === false ) {
235249 $output = 'Tag found: ';
236250 $type = 'TAG';
237251 $raw_name = $snippet;
238252 }
239 -
240 - if (empty($output)) {
 253+
 254+ if ( empty( $output ) ) {
241255 $output = 'Unknown HTML snippet found: ';
242256 $type = 'UNKNOWN';
243257 $raw_name = $snippet;
244258 }
245 -
 259+
246260 return $output;
247261 }
248262 }
\ No newline at end of file

Status & tagging log