wikimedia/mediawiki-core

View on GitHub
includes/import/WikiImporter.php

Summary

Maintainability
F
1 wk
Test Coverage

File WikiImporter.php has 855 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
/**
 * MediaWiki page data importer.
 *
 * Copyright © 2003,2005 Brooke Vibber <bvibber@wikimedia.org>
Severity: Major
Found in includes/import/WikiImporter.php - About 2 days to fix

    WikiImporter has 51 functions (exceeds 20 allowed). Consider refactoring.
    Open

    class WikiImporter {
        /** @var XMLReader|null */
        private $reader;
    
        /** @var string */
    Severity: Major
    Found in includes/import/WikiImporter.php - About 7 hrs to fix

      Function handlePage has a Cognitive Complexity of 33 (exceeds 5 allowed). Consider refactoring.
      Open

          private function handlePage() {
              // Handle page data.
              $this->debug( "Enter page handler." );
              $pageInfo = [ 'revisionCount' => 0, 'successfulRevisionCount' => 0 ];
      
      
      Severity: Minor
      Found in includes/import/WikiImporter.php - About 4 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function doImport has a Cognitive Complexity of 27 (exceeds 5 allowed). Consider refactoring.
      Open

          public function doImport() {
              $this->syntaxCheckXML();
      
              // Calls to reader->read need to be wrapped in calls to
              // libxml_disable_entity_loader() to avoid local file
      Severity: Minor
      Found in includes/import/WikiImporter.php - About 3 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Method doImport has 61 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          public function doImport() {
              $this->syntaxCheckXML();
      
              // Calls to reader->read need to be wrapped in calls to
              // libxml_disable_entity_loader() to avoid local file
      Severity: Major
      Found in includes/import/WikiImporter.php - About 2 hrs to fix

        Function handleUpload has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
        Open

            private function handleUpload( &$pageInfo ) {
                $this->debug( "Enter upload handler" );
                $uploadInfo = [];
        
                $normalFields = [ 'timestamp', 'comment', 'filename', 'text',
        Severity: Minor
        Found in includes/import/WikiImporter.php - About 2 hrs to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Method handlePage has 55 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            private function handlePage() {
                // Handle page data.
                $this->debug( "Enter page handler." );
                $pageInfo = [ 'revisionCount' => 0, 'successfulRevisionCount' => 0 ];
        
        
        Severity: Major
        Found in includes/import/WikiImporter.php - About 2 hrs to fix

          Method handleUpload has 38 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              private function handleUpload( &$pageInfo ) {
                  $this->debug( "Enter upload handler" );
                  $uploadInfo = [];
          
                  $normalFields = [ 'timestamp', 'comment', 'filename', 'text',
          Severity: Minor
          Found in includes/import/WikiImporter.php - About 1 hr to fix

            Method processRevision has 37 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                private function processRevision( $pageInfo, $revisionInfo ) {
                    $revision = new WikiRevision();
            
                    $revId = $revisionInfo['id'] ?? 0;
                    if ( $revId ) {
            Severity: Minor
            Found in includes/import/WikiImporter.php - About 1 hr to fix

              Function handleRevision has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
              Open

                  private function handleRevision( &$pageInfo ) {
                      $this->debug( "Enter revision handler" );
                      $revisionInfo = [];
              
                      $normalFields = [ 'id', 'parentid', 'timestamp', 'comment', 'minor', 'origin',
              Severity: Minor
              Found in includes/import/WikiImporter.php - About 1 hr to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              Method __construct has 11 arguments (exceeds 4 allowed). Consider refactoring.
              Open

                      ImportSource $source,
                      Authority $performer,
                      Config $config,
                      HookContainer $hookContainer,
                      Language $contentLanguage,
              Severity: Major
              Found in includes/import/WikiImporter.php - About 1 hr to fix

                Method processUpload has 33 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                    private function processUpload( $pageInfo, $uploadInfo ) {
                        $revision = new WikiRevision();
                        $revId = $pageInfo['id'];
                        $title = $pageInfo['_title'];
                        // T292348: text key may be absent, force addition if null
                Severity: Minor
                Found in includes/import/WikiImporter.php - About 1 hr to fix

                  Method setTargetRootPage has 32 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                      public function setTargetRootPage( $rootpage ) {
                          $status = Status::newGood();
                          $nsInfo = $this->namespaceInfo;
                          if ( $rootpage === null ) {
                              // No rootpage
                  Severity: Minor
                  Found in includes/import/WikiImporter.php - About 1 hr to fix

                    Method processLogItem has 29 lines of code (exceeds 25 allowed). Consider refactoring.
                    Open

                        private function processLogItem( $logInfo ) {
                            $revision = new WikiRevision();
                    
                            if ( isset( $logInfo['id'] ) ) {
                                $revision->setID( $logInfo['id'] );
                    Severity: Minor
                    Found in includes/import/WikiImporter.php - About 1 hr to fix

                      Method handleRevision has 29 lines of code (exceeds 25 allowed). Consider refactoring.
                      Open

                          private function handleRevision( &$pageInfo ) {
                              $this->debug( "Enter revision handler" );
                              $revisionInfo = [];
                      
                              $normalFields = [ 'id', 'parentid', 'timestamp', 'comment', 'minor', 'origin',
                      Severity: Minor
                      Found in includes/import/WikiImporter.php - About 1 hr to fix

                        Method processTitle has 28 lines of code (exceeds 25 allowed). Consider refactoring.
                        Open

                            private function processTitle( $text, $ns = null ) {
                                if ( $this->foreignNamespaces === null ) {
                                    $foreignTitleFactory = new NaiveForeignTitleFactory(
                                        $this->contentLanguage
                                    );
                        Severity: Minor
                        Found in includes/import/WikiImporter.php - About 1 hr to fix

                          Method makeContent has 28 lines of code (exceeds 25 allowed). Consider refactoring.
                          Open

                              private function makeContent( Title $title, $revisionId, $contentInfo ) {
                                  $maxArticleSize = $this->config->get( MainConfigNames::MaxArticleSize );
                          
                                  if ( !isset( $contentInfo['text'] ) ) {
                                      throw new InvalidArgumentException( 'Missing text field in import.' );
                          Severity: Minor
                          Found in includes/import/WikiImporter.php - About 1 hr to fix

                            Function processRevision has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
                            Open

                                private function processRevision( $pageInfo, $revisionInfo ) {
                                    $revision = new WikiRevision();
                            
                                    $revId = $revisionInfo['id'] ?? 0;
                                    if ( $revId ) {
                            Severity: Minor
                            Found in includes/import/WikiImporter.php - About 1 hr to fix

                            Cognitive Complexity

                            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                            A method's cognitive complexity is based on a few simple rules:

                            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                            • Code is considered more complex for each "break in the linear flow of the code"
                            • Code is considered more complex when "flow breaking structures are nested"

                            Further reading

                            Function handleLogItem has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
                            Open

                                private function handleLogItem() {
                                    $this->debug( "Enter log item handler." );
                                    $logInfo = [];
                            
                                    // Fields that can just be stuffed in the pageInfo object
                            Severity: Minor
                            Found in includes/import/WikiImporter.php - About 1 hr to fix

                            Cognitive Complexity

                            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                            A method's cognitive complexity is based on a few simple rules:

                            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                            • Code is considered more complex for each "break in the linear flow of the code"
                            • Code is considered more complex when "flow breaking structures are nested"

                            Further reading

                            Method __construct has 26 lines of code (exceeds 25 allowed). Consider refactoring.
                            Open

                                public function __construct(
                                    ImportSource $source,
                                    Authority $performer,
                                    Config $config,
                                    HookContainer $hookContainer,
                            Severity: Minor
                            Found in includes/import/WikiImporter.php - About 1 hr to fix

                              Function handleContent has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                              Open

                                  private function handleContent() {
                                      $this->debug( "Enter content handler" );
                                      $contentInfo = [];
                              
                                      $normalFields = [ 'role', 'origin', 'model', 'format', 'text' ];
                              Severity: Minor
                              Found in includes/import/WikiImporter.php - About 55 mins to fix

                              Cognitive Complexity

                              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                              A method's cognitive complexity is based on a few simple rules:

                              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                              • Code is considered more complex for each "break in the linear flow of the code"
                              • Code is considered more complex when "flow breaking structures are nested"

                              Further reading

                              Function handleSiteInfo has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                              Open

                                  private function handleSiteInfo() {
                                      $this->debug( "Enter site info handler." );
                                      $siteInfo = [];
                              
                                      // Fields that can just be stuffed in the siteInfo object
                              Severity: Minor
                              Found in includes/import/WikiImporter.php - About 45 mins to fix

                              Cognitive Complexity

                              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                              A method's cognitive complexity is based on a few simple rules:

                              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                              • Code is considered more complex for each "break in the linear flow of the code"
                              • Code is considered more complex when "flow breaking structures are nested"

                              Further reading

                              Function handleContributor has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                              Open

                                  private function handleContributor() {
                                      $this->debug( "Enter contributor handler." );
                              
                                      if ( $this->reader->isEmptyElement ) {
                                          return [];
                              Severity: Minor
                              Found in includes/import/WikiImporter.php - About 45 mins to fix

                              Cognitive Complexity

                              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                              A method's cognitive complexity is based on a few simple rules:

                              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                              • Code is considered more complex for each "break in the linear flow of the code"
                              • Code is considered more complex when "flow breaking structures are nested"

                              Further reading

                              Function finishImportPage has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                              Open

                                  public function finishImportPage( PageIdentity $pageIdentity, $foreignTitle, $revCount,
                                      $sRevCount, $pageInfo
                                  ) {
                                      // Update article count statistics (T42009)
                                      // The normal counting logic in WikiPage->doEditUpdates() is designed for
                              Severity: Minor
                              Found in includes/import/WikiImporter.php - About 45 mins to fix

                              Cognitive Complexity

                              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                              A method's cognitive complexity is based on a few simple rules:

                              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                              • Code is considered more complex for each "break in the linear flow of the code"
                              • Code is considered more complex when "flow breaking structures are nested"

                              Further reading

                              Function processLogItem has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                              Open

                                  private function processLogItem( $logInfo ) {
                                      $revision = new WikiRevision();
                              
                                      if ( isset( $logInfo['id'] ) ) {
                                          $revision->setID( $logInfo['id'] );
                              Severity: Minor
                              Found in includes/import/WikiImporter.php - About 45 mins to fix

                              Cognitive Complexity

                              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                              A method's cognitive complexity is based on a few simple rules:

                              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                              • Code is considered more complex for each "break in the linear flow of the code"
                              • Code is considered more complex when "flow breaking structures are nested"

                              Further reading

                              Method pageOutCallback has 5 arguments (exceeds 4 allowed). Consider refactoring.
                              Open

                                  private function pageOutCallback( PageIdentity $pageIdentity, $foreignTitle, $revCount,
                                          $sucCount, $pageInfo ) {
                              Severity: Minor
                              Found in includes/import/WikiImporter.php - About 35 mins to fix

                                Method finishImportPage has 5 arguments (exceeds 4 allowed). Consider refactoring.
                                Open

                                    public function finishImportPage( PageIdentity $pageIdentity, $foreignTitle, $revCount,
                                        $sRevCount, $pageInfo
                                Severity: Minor
                                Found in includes/import/WikiImporter.php - About 35 mins to fix

                                  Function setTargetRootPage has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                                  Open

                                      public function setTargetRootPage( $rootpage ) {
                                          $status = Status::newGood();
                                          $nsInfo = $this->namespaceInfo;
                                          if ( $rootpage === null ) {
                                              // No rootpage
                                  Severity: Minor
                                  Found in includes/import/WikiImporter.php - About 35 mins to fix

                                  Cognitive Complexity

                                  Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                  A method's cognitive complexity is based on a few simple rules:

                                  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                  • Code is considered more complex for each "break in the linear flow of the code"
                                  • Code is considered more complex when "flow breaking structures are nested"

                                  Further reading

                                  Avoid too many return statements within this method.
                                  Open

                                          return [ $title, $foreignTitle ];
                                  Severity: Major
                                  Found in includes/import/WikiImporter.php - About 30 mins to fix

                                    Function openReader has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                                    Open

                                        private function openReader() {
                                            // Enable the entity loader, as it is needed for loading external URLs via
                                            // XMLReader::open (T86036)
                                            // phpcs:ignore Generic.PHP.NoSilencedErrors -- suppress deprecation per T268847
                                            $oldDisable = @libxml_disable_entity_loader( false );
                                    Severity: Minor
                                    Found in includes/import/WikiImporter.php - About 25 mins to fix

                                    Cognitive Complexity

                                    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                    A method's cognitive complexity is based on a few simple rules:

                                    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                    • Code is considered more complex for each "break in the linear flow of the code"
                                    • Code is considered more complex when "flow breaking structures are nested"

                                    Further reading

                                    Function processTitle has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                                    Open

                                        private function processTitle( $text, $ns = null ) {
                                            if ( $this->foreignNamespaces === null ) {
                                                $foreignTitleFactory = new NaiveForeignTitleFactory(
                                                    $this->contentLanguage
                                                );
                                    Severity: Minor
                                    Found in includes/import/WikiImporter.php - About 25 mins to fix

                                    Cognitive Complexity

                                    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                    A method's cognitive complexity is based on a few simple rules:

                                    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                    • Code is considered more complex for each "break in the linear flow of the code"
                                    • Code is considered more complex when "flow breaking structures are nested"

                                    Further reading

                                    Similar blocks of code found in 2 locations. Consider refactoring.
                                    Open

                                            if ( isset( $revisionInfo['contributor']['username'] ) ) {
                                                $revision->setUsername(
                                                    $this->externalUserNames->applyPrefix( $revisionInfo['contributor']['username'] )
                                                );
                                            } elseif ( isset( $revisionInfo['contributor']['ip'] ) ) {
                                    Severity: Minor
                                    Found in includes/import/WikiImporter.php and 1 other location - About 30 mins to fix
                                    includes/import/WikiImporter.php on lines 841..849

                                    Duplicated Code

                                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                    Tuning

                                    This issue has a mass of 91.

                                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                    Refactorings

                                    Further Reading

                                    Similar blocks of code found in 2 locations. Consider refactoring.
                                    Open

                                            if ( isset( $logInfo['contributor']['username'] ) ) {
                                                $revision->setUsername(
                                                    $this->externalUserNames->applyPrefix( $logInfo['contributor']['username'] )
                                                );
                                            } elseif ( isset( $logInfo['contributor']['ip'] ) ) {
                                    Severity: Minor
                                    Found in includes/import/WikiImporter.php and 1 other location - About 30 mins to fix
                                    includes/import/WikiImporter.php on lines 1097..1105

                                    Duplicated Code

                                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                    Tuning

                                    This issue has a mass of 91.

                                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                    Refactorings

                                    Further Reading

                                    There are no issues that match your filters.

                                    Category
                                    Status