Merge "Add DEFAULTSORT to search index field data"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 1 Sep 2016 14:51:41 +0000 (14:51 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 1 Sep 2016 14:51:41 +0000 (14:51 +0000)
1  2 
includes/content/WikiTextStructure.php
includes/content/WikitextContentHandler.php
tests/phpunit/includes/content/WikitextStructureTest.php

@@@ -58,6 -58,50 +58,6 @@@ class WikiTextStructure 
                $this->parserOutput = $parserOutput;
        }
  
 -      /**
 -       * Get categories in the text.
 -       * @return string[]
 -       */
 -      public function categories() {
 -              $categories = [];
 -              foreach ( array_keys( $this->parserOutput->getCategories() ) as $key ) {
 -                      $categories[] = Category::newFromName( $key )->getTitle()->getText();
 -              }
 -              return $categories;
 -      }
 -
 -      /**
 -       * Get outgoing links.
 -       * @return string[]
 -       */
 -      public function outgoingLinks() {
 -              $outgoingLinks = [];
 -              foreach ( $this->parserOutput->getLinks() as $linkedNamespace => $namespaceLinks ) {
 -                      foreach ( array_keys( $namespaceLinks ) as $linkedDbKey ) {
 -                              $outgoingLinks[] =
 -                                      Title::makeTitle( $linkedNamespace, $linkedDbKey )->getPrefixedDBkey();
 -                      }
 -              }
 -              return $outgoingLinks;
 -      }
 -
 -      /**
 -       * Get templates in the text.
 -       * @return string[]
 -       */
 -      public function templates() {
 -              $templates = [];
 -              foreach ( $this->parserOutput->getTemplates() as $tNS => $templatesInNS ) {
 -                      foreach ( array_keys( $templatesInNS ) as $tDbKey ) {
 -                              $templateTitle = Title::makeTitleSafe( $tNS, $tDbKey );
 -                              if ( $templateTitle && $templateTitle->exists() ) {
 -                                      $templates[] = $templateTitle->getPrefixedText();
 -                              }
 -                      }
 -              }
 -              return $templates;
 -      }
 -
        /**
         * Get headings on the page.
         * @return string[]
                $this->extractWikitextParts();
                return $this->auxText;
        }
+       /**
+        * Get the defaultsort property
+        * @return string|null
+        */
+       public function getDefaultSort() {
+               return $this->parserOutput->getProperty( 'defaultsort' );
+       }
  }
@@@ -35,7 -35,7 +35,7 @@@ class WikitextContentHandler extends Te
        }
  
        protected function getContentClass() {
 -              return 'WikitextContent';
 +              return WikitextContent::class;
        }
  
        /**
        }
  
        public function getFieldsForSearchIndex( SearchEngine $engine ) {
 -              $fields = [];
 -
 -              $fields['category'] =
 -                      $engine->makeSearchFieldMapping( 'category', SearchIndexField::INDEX_TYPE_TEXT );
 -              $fields['category']->setFlag( SearchIndexField::FLAG_CASEFOLD );
 -
 -              $fields['external_link'] =
 -                      $engine->makeSearchFieldMapping( 'external_link', SearchIndexField::INDEX_TYPE_KEYWORD );
 +              $fields = parent::getFieldsForSearchIndex( $engine );
  
                $fields['heading'] =
                        $engine->makeSearchFieldMapping( 'heading', SearchIndexField::INDEX_TYPE_TEXT );
                $fields['opening_text']->setFlag( SearchIndexField::FLAG_SCORING |
                                                  SearchIndexField::FLAG_NO_HIGHLIGHT );
  
 -              $fields['outgoing_link'] =
 -                      $engine->makeSearchFieldMapping( 'outgoing_link', SearchIndexField::INDEX_TYPE_KEYWORD );
 -
 -              $fields['template'] =
 -                      $engine->makeSearchFieldMapping( 'template', SearchIndexField::INDEX_TYPE_KEYWORD );
 -              $fields['template']->setFlag( SearchIndexField::FLAG_CASEFOLD );
 -
                // FIXME: this really belongs in separate file handler but files
                // do not have separate handler. Sadness.
                $fields['file_text'] =
                        $engine->makeSearchFieldMapping( 'file_text', SearchIndexField::INDEX_TYPE_TEXT );
  
+               $fields['defaultsort'] = $engine->makeSearchFieldMapping( 'defaultsort',
+                       SearchIndexField::INDEX_TYPE_TEXT );
+               $fields['defaultsort']->setFlag( SearchIndexField::FLAG_SOURCE_DATA );
                return $fields;
        }
  
        protected function getFileText( Title $title ) {
                $file = wfLocalFile( $title );
                if ( $file && $file->exists() ) {
 -                      return $file->getHandler()->getEntireText( $file );
 +                      $handler = $file->getHandler();
 +                      if ( !$handler ) {
 +                              return null;
 +                      }
 +                      return $handler->getEntireText( $file );
                }
  
                return null;
                $fields = parent::getDataForSearchIndex( $page, $parserOutput, $engine );
  
                $structure = new WikiTextStructure( $parserOutput );
 -              $fields['external_link'] = array_keys( $parserOutput->getExternalLinks() );
 -              $fields['category'] = $structure->categories();
                $fields['heading'] = $structure->headings();
 -              $fields['outgoing_link'] = $structure->outgoingLinks();
 -              $fields['template'] = $structure->templates();
                // text fields
                $fields['opening_text'] = $structure->getOpeningText();
                $fields['text'] = $structure->getMainText(); // overwrites one from ContentHandler
                $fields['auxiliary_text'] = $structure->getAuxiliaryText();
+               $fields['defaultsort'] = $structure->getDefaultSort();
  
                $title = $page->getTitle();
                if ( NS_FILE == $title->getNamespace() ) {
@@@ -25,6 -25,61 +25,6 @@@ class WikitextStructureTest extends Med
                return new WikiTextStructure( $this->getParserOutput( $text ) );
        }
  
 -      public function testCategories() {
 -              $text = <<<END
 -We also have a {{Template}} and an {{Another template}} in addition. 
 -This text also has [[Category:Some Category| ]] and then [[Category:Yet another category]].
 -And [[Category:Some Category| this category]] is repeated.
 -END;
 -              $struct = $this->getStructure( $text );
 -              $cats = $struct->categories();
 -              $this->assertCount( 2, $cats );
 -              $this->assertContains( "Some Category", $cats );
 -              $this->assertContains( "Yet another category", $cats );
 -      }
 -
 -      public function testOutgoingLinks() {
 -              $text = <<<END
 -Here I add link to [[Some Page]]. And [[Some Page|This same page]] gets linked twice. 
 -We also have [[File:Image.jpg|image]].
 -We also have a {{Template}} and an {{Another template}} in addition. 
 -Some templates are {{lowercase}}.
 -And [[Some_Page]] is linked again. 
 -It also has [[Category:Some Category| ]] and then [[Category:Yet another category]].
 -Also link to a [[Talk:TestTitle|talk page]] is here. 
 -END;
 -              $struct = $this->getStructure( $text );
 -              $links = $struct->outgoingLinks();
 -              $this->assertContains( "Some_Page", $links );
 -              $this->assertContains( "Template:Template", $links );
 -              $this->assertContains( "Template:Another_template", $links );
 -              $this->assertContains( "Template:Lowercase", $links );
 -              $this->assertContains( "Talk:TestTitle", $links );
 -              $this->assertCount( 5, $links );
 -      }
 -
 -      public function testTemplates() {
 -              $text = <<<END
 -We have a {{Template}} and an {{Another template}} in addition. 
 -Some templates are {{lowercase}}. And this {{Template}} is repeated. 
 -Here is {{another_template|with=argument}}.
 -This is a template that {{Xdoes not exist}}.
 -END;
 -              $this->setTemporaryHook( 'TitleExists', function ( Title $title, &$exists ) {
 -                      $txt = $title->getBaseText();
 -                      if ( $txt[0] != 'X' ) {
 -                              $exists = true;
 -                      }
 -                      return true;
 -              } );
 -              $struct = $this->getStructure( $text );
 -              $templates = $struct->templates();
 -              $this->assertCount( 3, $templates );
 -              $this->assertContains( "Template:Template", $templates );
 -              $this->assertContains( "Template:Another template", $templates );
 -              $this->assertContains( "Template:Lowercase", $templates );
 -      }
 -
        public function testHeadings() {
                $text = <<<END
  Some text here
@@@ -49,6 -104,19 +49,19 @@@ END
                $this->assertContains( "Wikitext in Heading and also html", $headings );
        }
  
+       public function testDefaultSort() {
+               $text = <<<END
+ Louise Michel
+ == Heading one ==
+ Some text
+ ==== See also ====
+ * Also things to see!
+ {{DEFAULTSORT:Michel, Louise}}
+ END;
+               $struct = $this->getStructure( $text );
+               $this->assertEquals( "Michel, Louise", $struct->getDefaultSort() );
+       }
        public function testHeadingsFirst() {
                $text = <<<END
  == Heading one ==