Jdlrobson has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/327129 )

Change subject: Hygiene: breakup mega-function in MobileView API
......................................................................

Hygiene: breakup mega-function in MobileView API

In order to make this block of code testable and readable we
should break it out into a separate method.

Changes:
* I've renamed parseSections to the more meaningful
getRequestedSectionIds as the name parseSections is extremely
misleading.
* I've broken out parseSectionsData and documented it as its
behaviour is not what I expected

Change-Id: I8045d5eb6cc5d1ddce7bc344d99566aa648f70fd
---
M includes/api/ApiMobileView.php
1 file changed, 52 insertions(+), 35 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/29/327129/1

diff --git a/includes/api/ApiMobileView.php b/includes/api/ApiMobileView.php
index 66af090..8ead53d 100644
--- a/includes/api/ApiMobileView.php
+++ b/includes/api/ApiMobileView.php
@@ -150,7 +150,7 @@
                if ( $this->mainPage ) {
                        if ( $onlyRequestedSections ) {
                                $requestedSections =
-                                       self::parseSections( 
$params['sections'], $data, $missingSections );
+                                       self::getRequestedSectionIds( 
$params['sections'], $data, $missingSections );
                        } else {
                                $requestedSections = [ 0 ];
                        }
@@ -158,7 +158,7 @@
                                [ 'mainpage' => true ]
                        );
                } elseif ( isset( $params['sections'] ) ) {
-                       $requestedSections = self::parseSections( 
$params['sections'], $data, $missingSections );
+                       $requestedSections = self::getRequestedSectionIds( 
$params['sections'], $data, $missingSections );
                } else {
                        $requestedSections = [];
                }
@@ -378,10 +378,10 @@
         * @param string $str String to parse
         * @param array $data Processed parser output
         * @param array $missingSections Upon return, contains the list of 
sections that were
-        * requested but are not present in parser output
+        * requested but are not present in parser output (passed by reference)
         * @return array
         */
-       public static function parseSections( $str, $data, &$missingSections ) {
+       public static function getRequestedSectionIds( $str, $data, 
&$missingSections ) {
                $str = trim( $str );
                if ( !isset( $data['sections'] ) ) {
                        return [];
@@ -468,6 +468,52 @@
        }
 
        /**
+        * Parses section data
+        * @param string $html representing the entire page
+        * @param Title $title
+        * @param ParserOutput $parserOutput
+        * @param boolean $useTidy whether the provided HTML should be tidied 
(optional)
+        * @return array structure representing the list of sections and their 
properties:
+        *  - refsections: [] under this key an associative array where all 
keys are section ids
+        *    that contain references
+        *  - sections: [] a structured array of all the sections inside the 
page
+        *  - text: [] of the text of each individual section. It should be of 
equal length to sections or of length 1
+        *      when there is a mismatch.
+        */
+       protected function parseSectionsData( $html, Title $title, ParserOutput 
$parserOutput, $useTidy = false ) {
+               $data = [];
+               $data['sections'] = $parserOutput->getSections();
+               $sectionCount = count( $data['sections'] );
+               for ( $i = 0; $i < $sectionCount; $i++ ) {
+                       $data['sections'][$i]['line'] =
+                               $title->getPageLanguage()->convert( 
$data['sections'][$i]['line'] );
+               }
+               $chunks = preg_split( '/<h(?=[1-6]\b)/i', $html );
+               if ( count( $chunks ) != count( $data['sections'] ) + 1 ) {
+                       wfDebugLog( 'mobile', __METHOD__ . "(): mismatching 
number of " .
+                               "sections from parser and split on page 
{$title->getPrefixedText()}, oldid=$latest" );
+                       // We can't be sure about anything here, return all 
page HTML as one big section
+                       $chunks = [ $html ];
+                       $data['sections'] = [];
+               }
+               $data['text'] = [];
+               $data['refsections'] = [];
+               foreach ( $chunks as $chunk ) {
+                       if ( count( $data['text'] ) ) {
+                               $chunk = "<h$chunk";
+                       }
+                       if ( $useTidy && count( $chunks ) > 1 ) {
+                               $chunk = MWTidy::tidy( $chunk );
+                       }
+                       if ( preg_match( '/<ol\b[^>]*?class="references"/', 
$chunk ) ) {
+                               $data['refsections'][count( $data['text'] )] = 
true;
+                       }
+                       $data['text'][] = $chunk;
+               }
+               return $data;
+       }
+
+       /**
         * Get data of requested article.
         * @param Title $title
         * @param boolean $noImages
@@ -477,8 +523,6 @@
         */
        private function getData( Title $title, $noImages, $oldid = null ) {
                $mfConfig = MobileContext::singleton()->getMFConfig();
-               $useTidy = $this->getConfig()->get( 'UseTidy' );
-               $mfTidyMobileViewSections = $mfConfig->get( 
'MFTidyMobileViewSections' );
                $mfMinCachedPageSize = $mfConfig->get( 'MFMinCachedPageSize' );
                $mfSpecialCaseMainPage = $mfConfig->get( 
'MFSpecialCaseMainPage' );
 
@@ -574,35 +618,8 @@
                                'refsections' => [],
                        ];
                } else {
-                       $data = [];
-                       $data['sections'] = $parserOutput->getSections();
-                       $sectionCount = count( $data['sections'] );
-                       for ( $i = 0; $i < $sectionCount; $i++ ) {
-                               $data['sections'][$i]['line'] =
-                                       $title->getPageLanguage()->convert( 
$data['sections'][$i]['line'] );
-                       }
-                       $chunks = preg_split( '/<h(?=[1-6]\b)/i', $html );
-                       if ( count( $chunks ) != count( $data['sections'] ) + 1 
) {
-                               wfDebugLog( 'mobile', __METHOD__ . "(): 
mismatching number of " .
-                                       "sections from parser and split on page 
{$title->getPrefixedText()}, oldid=$latest" );
-                               // We can't be sure about anything here, return 
all page HTML as one big section
-                               $chunks = [ $html ];
-                               $data['sections'] = [];
-                       }
-                       $data['text'] = [];
-                       $data['refsections'] = [];
-                       foreach ( $chunks as $chunk ) {
-                               if ( count( $data['text'] ) ) {
-                                       $chunk = "<h$chunk";
-                               }
-                               if ( $useTidy && $mfTidyMobileViewSections && 
count( $chunks ) > 1 ) {
-                                       $chunk = MWTidy::tidy( $chunk );
-                               }
-                               if ( preg_match( 
'/<ol\b[^>]*?class="references"/', $chunk ) ) {
-                                       $data['refsections'][count( 
$data['text'] )] = true;
-                               }
-                               $data['text'][] = $chunk;
-                       }
+                       $data = $this->parseSectionsData( $html, $title, 
$parserOutput,
+                               $mfConfig->get( 'MFTidyMobileViewSections' ) && 
$this->getConfig()->get( 'UseTidy' ) );
                        if ( $this->usePageImages ) {
                                $image = $this->getPageImage( $title );
                                if ( $image ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/327129
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8045d5eb6cc5d1ddce7bc344d99566aa648f70fd
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to