r69691 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69690‎ | r69691 | r69692 >
Date:16:57, 21 July 2010
Author:btongminh
Status:ok
Tags:
Comment:
stylize.php on SearchEngine.php
Modified paths:
  • /trunk/phase3/includes/search/SearchEngine.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/search/SearchEngine.php
@@ -101,21 +101,21 @@
102102 private static function getNearMatchInternal( $searchterm ) {
103103 global $wgContLang;
104104
105 - $allSearchTerms = array($searchterm);
 105+ $allSearchTerms = array( $searchterm );
106106
107107 if ( $wgContLang->hasVariants() ) {
108 - $allSearchTerms = array_merge($allSearchTerms,$wgContLang->convertLinkToAllVariants($searchterm));
 108+ $allSearchTerms = array_merge( $allSearchTerms, $wgContLang->convertLinkToAllVariants( $searchterm ) );
109109 }
110110
111 - if( !wfRunHooks( 'SearchGetNearMatchBefore', array( $allSearchTerms, &$titleResult ) ) ) {
 111+ if ( !wfRunHooks( 'SearchGetNearMatchBefore', array( $allSearchTerms, &$titleResult ) ) ) {
112112 return $titleResult;
113113 }
114114
115 - foreach($allSearchTerms as $term) {
 115+ foreach ( $allSearchTerms as $term ) {
116116
117117 # Exact match? No need to look further.
118118 $title = Title::newFromText( $term );
119 - if (is_null($title))
 119+ if ( is_null( $title ) )
120120 return null;
121121
122122 if ( $title->getNamespace() == NS_SPECIAL || $title->isExternal() || $title->exists() ) {
@@ -124,7 +124,7 @@
125125
126126 # See if it still otherwise has content is some sane sense
127127 $article = MediaWiki::articleFromTitle( $title );
128 - if( $article->hasViewableContent() ) {
 128+ if ( $article->hasViewableContent() ) {
129129 return $title;
130130 }
131131
@@ -150,14 +150,14 @@
151151 }
152152
153153 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
154 - $title = Title::newFromText( $wgContLang->ucwordbreaks($term) );
 154+ $title = Title::newFromText( $wgContLang->ucwordbreaks( $term ) );
155155 if ( $title && $title->exists() ) {
156156 return $title;
157157 }
158158
159159 // Give hooks a chance at better match variants
160160 $title = null;
161 - if( !wfRunHooks( 'SearchGetNearMatch', array( $term, &$title ) ) ) {
 161+ if ( !wfRunHooks( 'SearchGetNearMatch', array( $term, &$title ) ) ) {
162162 return $title;
163163 }
164164 }
@@ -165,7 +165,7 @@
166166 $title = Title::newFromText( $searchterm );
167167
168168 # Entering an IP address goes to the contributions page
169 - if ( ( $title->getNamespace() == NS_USER && User::isIP($title->getText() ) )
 169+ if ( ( $title->getNamespace() == NS_USER && User::isIP( $title->getText() ) )
170170 || User::isIP( trim( $searchterm ) ) ) {
171171 return SpecialPage::getTitleFor( 'Contributions', $title->getDBkey() );
172172 }
@@ -179,22 +179,22 @@
180180 # Go to images that exist even if there's no local page.
181181 # There may have been a funny upload, or it may be on a shared
182182 # file repository such as Wikimedia Commons.
183 - if( $title->getNamespace() == NS_FILE ) {
 183+ if ( $title->getNamespace() == NS_FILE ) {
184184 $image = wfFindFile( $title );
185 - if( $image ) {
 185+ if ( $image ) {
186186 return $title;
187187 }
188188 }
189189
190190 # MediaWiki namespace? Page may be "implied" if not customized.
191191 # Just return it, with caps forced as the message system likes it.
192 - if( $title->getNamespace() == NS_MEDIAWIKI ) {
 192+ if ( $title->getNamespace() == NS_MEDIAWIKI ) {
193193 return Title::makeTitle( NS_MEDIAWIKI, $wgContLang->ucfirst( $title->getText() ) );
194194 }
195195
196196 # Quoted term? Try without the quotes...
197197 $matches = array();
198 - if( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
 198+ if ( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
199199 return SearchEngine::getNearMatch( $matches[1] );
200200 }
201201
@@ -233,28 +233,28 @@
234234 *
235235 * @param $query String
236236 */
237 - function replacePrefixes( $query ){
 237+ function replacePrefixes( $query ) {
238238 global $wgContLang;
239239
240240 $parsed = $query;
241 - if( strpos($query,':') === false ) { // nothing to do
 241+ if ( strpos( $query, ':' ) === false ) { // nothing to do
242242 wfRunHooks( 'SearchEngineReplacePrefixesComplete', array( $this, $query, &$parsed ) );
243243 return $parsed;
244244 }
245245
246 - $allkeyword = wfMsgForContent('searchall').":";
247 - if( strncmp($query, $allkeyword, strlen($allkeyword)) == 0 ){
 246+ $allkeyword = wfMsgForContent( 'searchall' ) . ":";
 247+ if ( strncmp( $query, $allkeyword, strlen( $allkeyword ) ) == 0 ) {
248248 $this->namespaces = null;
249 - $parsed = substr($query,strlen($allkeyword));
250 - } else if( strpos($query,':') !== false ) {
251 - $prefix = substr($query,0,strpos($query,':'));
252 - $index = $wgContLang->getNsIndex($prefix);
253 - if($index !== false){
254 - $this->namespaces = array($index);
255 - $parsed = substr($query,strlen($prefix)+1);
 249+ $parsed = substr( $query, strlen( $allkeyword ) );
 250+ } else if ( strpos( $query, ':' ) !== false ) {
 251+ $prefix = substr( $query, 0, strpos( $query, ':' ) );
 252+ $index = $wgContLang->getNsIndex( $prefix );
 253+ if ( $index !== false ) {
 254+ $this->namespaces = array( $index );
 255+ $parsed = substr( $query, strlen( $prefix ) + 1 );
256256 }
257257 }
258 - if(trim($parsed) == '')
 258+ if ( trim( $parsed ) == '' )
259259 $parsed = $query; // prefix was the whole query
260260
261261 wfRunHooks( 'SearchEngineReplacePrefixesComplete', array( $this, $query, &$parsed ) );
@@ -269,8 +269,8 @@
270270 public static function searchableNamespaces() {
271271 global $wgContLang;
272272 $arr = array();
273 - foreach( $wgContLang->getNamespaces() as $ns => $name ) {
274 - if( $ns >= NS_MAIN ) {
 273+ foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
 274+ if ( $ns >= NS_MAIN ) {
275275 $arr[$ns] = $name;
276276 }
277277 }
@@ -291,18 +291,18 @@
292292
293293 // get search everything preference, that can be set to be read for logged-in users
294294 $searcheverything = false;
295 - if( ( $wgSearchEverythingOnlyLoggedIn && $user->isLoggedIn() )
 295+ if ( ( $wgSearchEverythingOnlyLoggedIn && $user->isLoggedIn() )
296296 || !$wgSearchEverythingOnlyLoggedIn )
297 - $searcheverything = $user->getOption('searcheverything');
 297+ $searcheverything = $user->getOption( 'searcheverything' );
298298
299299 // searcheverything overrides other options
300 - if( $searcheverything )
301 - return array_keys(SearchEngine::searchableNamespaces());
 300+ if ( $searcheverything )
 301+ return array_keys( SearchEngine::searchableNamespaces() );
302302
303303 $arr = Preferences::loadOldSearchNs( $user );
304304 $searchableNamespaces = SearchEngine::searchableNamespaces();
305305
306 - $arr = array_intersect( $arr, array_keys($searchableNamespaces) ); // Filter
 306+ $arr = array_intersect( $arr, array_keys( $searchableNamespaces ) ); // Filter
307307
308308 return $arr;
309309 }
@@ -313,12 +313,12 @@
314314 * @param $user User
315315 * @return Array contextlines, contextchars
316316 */
317 - public static function userHighlightPrefs( &$user ){
318 - //$contextlines = $user->getOption( 'contextlines', 5 );
319 - //$contextchars = $user->getOption( 'contextchars', 50 );
 317+ public static function userHighlightPrefs( &$user ) {
 318+ // $contextlines = $user->getOption( 'contextlines', 5 );
 319+ // $contextchars = $user->getOption( 'contextchars', 50 );
320320 $contextlines = 2; // Hardcode this. Old defaults sucked. :)
321321 $contextchars = 75; // same as above.... :P
322 - return array($contextlines, $contextchars);
 322+ return array( $contextlines, $contextchars );
323323 }
324324
325325 /**
@@ -326,10 +326,10 @@
327327 *
328328 * @return Array
329329 */
330 - public static function defaultNamespaces(){
 330+ public static function defaultNamespaces() {
331331 global $wgNamespacesToBeSearchedDefault;
332332
333 - return array_keys($wgNamespacesToBeSearchedDefault, true);
 333+ return array_keys( $wgNamespacesToBeSearchedDefault, true );
334334 }
335335
336336 /**
@@ -338,12 +338,12 @@
339339 *
340340 * @param $namespaces Array
341341 */
342 - public static function namespacesAsText( $namespaces ){
 342+ public static function namespacesAsText( $namespaces ) {
343343 global $wgContLang;
344344
345 - $formatted = array_map( array($wgContLang,'getFormattedNsText'), $namespaces );
346 - foreach( $formatted as $key => $ns ){
347 - if ( empty($ns) )
 345+ $formatted = array_map( array( $wgContLang, 'getFormattedNsText' ), $namespaces );
 346+ foreach ( $formatted as $key => $ns ) {
 347+ if ( empty( $ns ) )
348348 $formatted[$key] = wfMsg( 'blanknamespace' );
349349 }
350350 return $formatted;
@@ -379,13 +379,13 @@
380380 public static function create() {
381381 global $wgSearchType;
382382 $dbr = wfGetDB( DB_SLAVE );
383 - if( $wgSearchType ) {
 383+ if ( $wgSearchType ) {
384384 $class = $wgSearchType;
385385 } else {
386386 $class = $dbr->getSearchEngine();
387387 }
388388 $search = new $class( $dbr );
389 - $search->setLimitOffset(0,0);
 389+ $search->setLimitOffset( 0, 0 );
390390 return $search;
391391 }
392392
@@ -421,12 +421,12 @@
422422 */
423423 public static function getOpenSearchTemplate() {
424424 global $wgOpenSearchTemplate, $wgServer, $wgScriptPath;
425 - if( $wgOpenSearchTemplate ) {
 425+ if ( $wgOpenSearchTemplate ) {
426426 return $wgOpenSearchTemplate;
427427 } else {
428428 $ns = implode( '|', SearchEngine::defaultNamespaces() );
429 - if( !$ns ) $ns = "0";
430 - return $wgServer . $wgScriptPath . '/api.php?action=opensearch&search={searchTerms}&namespace='.$ns;
 429+ if ( !$ns ) $ns = "0";
 430+ return $wgServer . $wgScriptPath . '/api.php?action=opensearch&search={searchTerms}&namespace=' . $ns;
431431 }
432432 }
433433
@@ -437,7 +437,7 @@
438438 */
439439 public static function getMWSuggestTemplate() {
440440 global $wgMWSuggestTemplate, $wgServer, $wgScriptPath;
441 - if($wgMWSuggestTemplate)
 441+ if ( $wgMWSuggestTemplate )
442442 return $wgMWSuggestTemplate;
443443 else
444444 return $wgServer . $wgScriptPath . '/api.php?action=opensearch&search={searchTerms}&namespace={namespaces}&suggest';
@@ -500,14 +500,14 @@
501501 /**
502502 * @return String: suggested query, null if none
503503 */
504 - function getSuggestionQuery(){
 504+ function getSuggestionQuery() {
505505 return null;
506506 }
507507
508508 /**
509509 * @return String: HTML highlighted suggested query, '' if none
510510 */
511 - function getSuggestionSnippet(){
 511+ function getSuggestionSnippet() {
512512 return '';
513513 }
514514
@@ -571,25 +571,25 @@
572572 }
573573
574574 function numRows() {
575 - if ($this->mResultSet === false )
 575+ if ( $this->mResultSet === false )
576576 return false;
577577
578578 return $this->mResultSet->numRows();
579579 }
580580
581581 function next() {
582 - if ($this->mResultSet === false )
 582+ if ( $this->mResultSet === false )
583583 return false;
584584
585585 $row = $this->mResultSet->fetchObject();
586 - if ($row === false)
 586+ if ( $row === false )
587587 return false;
588588
589589 return SearchResult::newFromRow( $row );
590590 }
591591
592592 function free() {
593 - if ($this->mResultSet === false )
 593+ if ( $this->mResultSet === false )
594594 return false;
595595
596596 $this->mResultSet->free();
@@ -600,7 +600,7 @@
601601 * @ingroup Search
602602 */
603603 class SearchResultTooMany {
604 - ## Some search engines may bail out if too many matches are found
 604+ # # Some search engines may bail out if too many matches are found
605605 }
606606
607607
@@ -662,11 +662,11 @@
663663 */
664664 protected function initFromTitle( $title ) {
665665 $this->mTitle = $title;
666 - if( !is_null( $this->mTitle ) ){
 666+ if ( !is_null( $this->mTitle ) ) {
667667 $this->mRevision = Revision::newFromTitle( $this->mTitle );
668 - if( $this->mTitle->getNamespace() === NS_FILE )
 668+ if ( $this->mTitle->getNamespace() === NS_FILE )
669669 $this->mImage = wfFindFile( $this->mTitle );
670 - }
 670+ }
671671 }
672672
673673 /**
@@ -674,8 +674,8 @@
675675 *
676676 * @return Boolean
677677 */
678 - function isBrokenTitle(){
679 - if( is_null($this->mTitle) )
 678+ function isBrokenTitle() {
 679+ if ( is_null( $this->mTitle ) )
680680 return true;
681681 return false;
682682 }
@@ -685,7 +685,7 @@
686686 *
687687 * @return Boolean
688688 */
689 - function isMissingRevision(){
 689+ function isMissingRevision() {
690690 return !$this->mRevision && !$this->mImage;
691691 }
692692
@@ -706,9 +706,9 @@
707707 /**
708708 * Lazy initialization of article text from DB
709709 */
710 - protected function initText(){
711 - if( !isset($this->mText) ){
712 - if($this->mRevision != null)
 710+ protected function initText() {
 711+ if ( !isset( $this->mText ) ) {
 712+ if ( $this->mRevision != null )
713713 $this->mText = $this->mRevision->getText();
714714 else // TODO: can we fetch raw wikitext for commons images?
715715 $this->mText = '';
@@ -720,12 +720,12 @@
721721 * @param $terms Array: terms to highlight
722722 * @return String: highlighted text snippet, null (and not '') if not supported
723723 */
724 - function getTextSnippet($terms){
 724+ function getTextSnippet( $terms ) {
725725 global $wgUser, $wgAdvancedSearchHighlighting;
726726 $this->initText();
727 - list($contextlines,$contextchars) = SearchEngine::userHighlightPrefs($wgUser);
 727+ list( $contextlines, $contextchars ) = SearchEngine::userHighlightPrefs( $wgUser );
728728 $h = new SearchHighlighter();
729 - if( $wgAdvancedSearchHighlighting )
 729+ if ( $wgAdvancedSearchHighlighting )
730730 return $h->highlightText( $this->mText, $terms, $contextlines, $contextchars );
731731 else
732732 return $h->highlightSimple( $this->mText, $terms, $contextlines, $contextchars );
@@ -735,7 +735,7 @@
736736 * @param $terms Array: terms to highlight
737737 * @return String: highlighted title, '' if not supported
738738 */
739 - function getTitleSnippet($terms){
 739+ function getTitleSnippet( $terms ) {
740740 return '';
741741 }
742742
@@ -743,38 +743,38 @@
744744 * @param $terms Array: terms to highlight
745745 * @return String: highlighted redirect name (redirect to this page), '' if none or not supported
746746 */
747 - function getRedirectSnippet($terms){
 747+ function getRedirectSnippet( $terms ) {
748748 return '';
749749 }
750750
751751 /**
752752 * @return Title object for the redirect to this page, null if none or not supported
753753 */
754 - function getRedirectTitle(){
 754+ function getRedirectTitle() {
755755 return null;
756756 }
757757
758758 /**
759759 * @return string highlighted relevant section name, null if none or not supported
760760 */
761 - function getSectionSnippet(){
 761+ function getSectionSnippet() {
762762 return '';
763763 }
764764
765765 /**
766766 * @return Title object (pagename+fragment) for the section, null if none or not supported
767767 */
768 - function getSectionTitle(){
 768+ function getSectionTitle() {
769769 return null;
770770 }
771771
772772 /**
773773 * @return String: timestamp
774774 */
775 - function getTimestamp(){
776 - if( $this->mRevision )
 775+ function getTimestamp() {
 776+ if ( $this->mRevision )
777777 return $this->mRevision->getTimestamp();
778 - else if( $this->mImage )
 778+ else if ( $this->mImage )
779779 return $this->mImage->getTimestamp();
780780 return '';
781781 }
@@ -782,7 +782,7 @@
783783 /**
784784 * @return Integer: number of words
785785 */
786 - function getWordCount(){
 786+ function getWordCount() {
787787 $this->initText();
788788 return str_word_count( $this->mText );
789789 }
@@ -790,7 +790,7 @@
791791 /**
792792 * @return Integer: size in bytes
793793 */
794 - function getByteSize(){
 794+ function getByteSize() {
795795 $this->initText();
796796 return strlen( $this->mText );
797797 }
@@ -798,14 +798,14 @@
799799 /**
800800 * @return Boolean if hit has related articles
801801 */
802 - function hasRelated(){
 802+ function hasRelated() {
803803 return false;
804804 }
805805
806806 /**
807807 * @return String: interwiki prefix of the title (return iw even if title is broken)
808808 */
809 - function getInterwikiPrefix(){
 809+ function getInterwikiPrefix() {
810810 return '';
811811 }
812812 }
@@ -843,7 +843,7 @@
844844 class SearchHighlighter {
845845 var $mCleanWikitext = true;
846846
847 - function SearchHighlighter($cleanupWikitext = true){
 847+ function SearchHighlighter( $cleanupWikitext = true ) {
848848 $this->mCleanWikitext = $cleanupWikitext;
849849 }
850850
@@ -861,7 +861,7 @@
862862 global $wgSearchHighlightBoundaries;
863863 $fname = __METHOD__;
864864
865 - if($text == '')
 865+ if ( $text == '' )
866866 return '';
867867
868868 // spli text into text + templates/links/tables
@@ -870,10 +870,10 @@
871871 $endPatterns = array(
872872 1 => '/(\{\{)|(\}\})/', // template
873873 2 => '/(\[\[)|(\]\])/', // image
874 - 3 => "/(\n\\{\\|)|(\n\\|\\})/"); // table
 874+ 3 => "/(\n\\{\\|)|(\n\\|\\})/" ); // table
875875
876876 // FIXME: this should prolly be a hook or something
877 - if(function_exists('wfCite')){
 877+ if ( function_exists( 'wfCite' ) ) {
878878 $spat .= '|(<ref>)'; // references via cite extension
879879 $endPatterns[4] = '/(<ref>)|(<\/ref>)/';
880880 }
@@ -882,18 +882,18 @@
883883 $otherExt = array(); // other extracts
884884 wfProfileIn( "$fname-split" );
885885 $start = 0;
886 - $textLen = strlen($text);
 886+ $textLen = strlen( $text );
887887 $count = 0; // sequence number to maintain ordering
888 - while( $start < $textLen ){
 888+ while ( $start < $textLen ) {
889889 // find start of template/image/table
890 - if( preg_match( $spat, $text, $matches, PREG_OFFSET_CAPTURE, $start ) ){
 890+ if ( preg_match( $spat, $text, $matches, PREG_OFFSET_CAPTURE, $start ) ) {
891891 $epat = '';
892 - foreach($matches as $key => $val){
893 - if($key > 0 && $val[1] != -1){
894 - if($key == 2){
 892+ foreach ( $matches as $key => $val ) {
 893+ if ( $key > 0 && $val[1] != - 1 ) {
 894+ if ( $key == 2 ) {
895895 // see if this is an image link
896 - $ns = substr($val[0],2,-1);
897 - if( $wgContLang->getNsIndex($ns) != NS_FILE )
 896+ $ns = substr( $val[0], 2, - 1 );
 897+ if ( $wgContLang->getNsIndex( $ns ) != NS_FILE )
898898 break;
899899
900900 }
@@ -903,42 +903,42 @@
904904 break;
905905 }
906906 }
907 - if( $epat ){
 907+ if ( $epat ) {
908908 // find end (and detect any nested elements)
909909 $level = 0;
910910 $offset = $start + 1;
911911 $found = false;
912 - while( preg_match( $epat, $text, $endMatches, PREG_OFFSET_CAPTURE, $offset ) ){
913 - if( array_key_exists(2,$endMatches) ){
 912+ while ( preg_match( $epat, $text, $endMatches, PREG_OFFSET_CAPTURE, $offset ) ) {
 913+ if ( array_key_exists( 2, $endMatches ) ) {
914914 // found end
915 - if($level == 0){
916 - $len = strlen($endMatches[2][0]);
 915+ if ( $level == 0 ) {
 916+ $len = strlen( $endMatches[2][0] );
917917 $off = $endMatches[2][1];
918918 $this->splitAndAdd( $otherExt, $count,
919919 substr( $text, $start, $off + $len - $start ) );
920920 $start = $off + $len;
921921 $found = true;
922922 break;
923 - } else{
 923+ } else {
924924 // end of nested element
925925 $level -= 1;
926926 }
927 - } else{
 927+ } else {
928928 // nested
929929 $level += 1;
930930 }
931 - $offset = $endMatches[0][1] + strlen($endMatches[0][0]);
 931+ $offset = $endMatches[0][1] + strlen( $endMatches[0][0] );
932932 }
933 - if( ! $found ){
 933+ if ( ! $found ) {
934934 // couldn't find appropriate closing tag, skip
935 - $this->splitAndAdd( $textExt, $count, substr( $text, $start, strlen($matches[0][0]) ) );
936 - $start += strlen($matches[0][0]);
 935+ $this->splitAndAdd( $textExt, $count, substr( $text, $start, strlen( $matches[0][0] ) ) );
 936+ $start += strlen( $matches[0][0] );
937937 }
938938 continue;
939939 }
940940 }
941941 // else: add as text extract
942 - $this->splitAndAdd( $textExt, $count, substr($text,$start) );
 942+ $this->splitAndAdd( $textExt, $count, substr( $text, $start ) );
943943 break;
944944 }
945945
@@ -947,29 +947,29 @@
948948 wfProfileOut( "$fname-split" );
949949
950950 // prepare regexps
951 - foreach( $terms as $index => $term ) {
 951+ foreach ( $terms as $index => $term ) {
952952 // manually do upper/lowercase stuff for utf-8 since PHP won't do it
953 - if(preg_match('/[\x80-\xff]/', $term) ){
954 - $terms[$index] = preg_replace_callback('/./us',array($this,'caseCallback'),$terms[$index]);
 953+ if ( preg_match( '/[\x80-\xff]/', $term ) ) {
 954+ $terms[$index] = preg_replace_callback( '/./us', array( $this, 'caseCallback' ), $terms[$index] );
955955 } else {
956956 $terms[$index] = $term;
957957 }
958958 }
959959 $anyterm = implode( '|', $terms );
960 - $phrase = implode("$wgSearchHighlightBoundaries+", $terms );
 960+ $phrase = implode( "$wgSearchHighlightBoundaries+", $terms );
961961
962962 // FIXME: a hack to scale contextchars, a correct solution
963963 // would be to have contextchars actually be char and not byte
964964 // length, and do proper utf-8 substrings and lengths everywhere,
965965 // but PHP is making that very hard and unclean to implement :(
966 - $scale = strlen($anyterm) / mb_strlen($anyterm);
 966+ $scale = strlen( $anyterm ) / mb_strlen( $anyterm );
967967 $contextchars = intval( $contextchars * $scale );
968968
969969 $patPre = "(^|$wgSearchHighlightBoundaries)";
970970 $patPost = "($wgSearchHighlightBoundaries|$)";
971971
972 - $pat1 = "/(".$phrase.")/ui";
973 - $pat2 = "/$patPre(".$anyterm.")$patPost/ui";
 972+ $pat1 = "/(" . $phrase . ")/ui";
 973+ $pat2 = "/$patPre(" . $anyterm . ")$patPost/ui";
974974
975975 wfProfileIn( "$fname-extract" );
976976
@@ -981,43 +981,43 @@
982982 // show beginning only if it contains all words
983983 $first = 0;
984984 $firstText = '';
985 - foreach($textExt as $index => $line){
986 - if(strlen($line)>0 && $line[0] != ';' && $line[0] != ':'){
 985+ foreach ( $textExt as $index => $line ) {
 986+ if ( strlen( $line ) > 0 && $line[0] != ';' && $line[0] != ':' ) {
987987 $firstText = $this->extract( $line, 0, $contextchars * $contextlines );
988988 $first = $index;
989989 break;
990990 }
991991 }
992 - if( $firstText ){
 992+ if ( $firstText ) {
993993 $succ = true;
994994 // check if first text contains all terms
995 - foreach($terms as $term){
996 - if( ! preg_match("/$patPre".$term."$patPost/ui", $firstText) ){
 995+ foreach ( $terms as $term ) {
 996+ if ( ! preg_match( "/$patPre" . $term . "$patPost/ui", $firstText ) ) {
997997 $succ = false;
998998 break;
999999 }
10001000 }
1001 - if( $succ ){
 1001+ if ( $succ ) {
10021002 $snippets[$first] = $firstText;
10031003 $offsets[$first] = 0;
10041004 }
10051005 }
1006 - if( ! $snippets ) {
 1006+ if ( ! $snippets ) {
10071007 // match whole query on text
1008 - $this->process($pat1, $textExt, $left, $contextchars, $snippets, $offsets);
 1008+ $this->process( $pat1, $textExt, $left, $contextchars, $snippets, $offsets );
10091009 // match whole query on templates/tables/images
1010 - $this->process($pat1, $otherExt, $left, $contextchars, $snippets, $offsets);
 1010+ $this->process( $pat1, $otherExt, $left, $contextchars, $snippets, $offsets );
10111011 // match any words on text
1012 - $this->process($pat2, $textExt, $left, $contextchars, $snippets, $offsets);
 1012+ $this->process( $pat2, $textExt, $left, $contextchars, $snippets, $offsets );
10131013 // match any words on templates/tables/images
1014 - $this->process($pat2, $otherExt, $left, $contextchars, $snippets, $offsets);
 1014+ $this->process( $pat2, $otherExt, $left, $contextchars, $snippets, $offsets );
10151015
1016 - ksort($snippets);
 1016+ ksort( $snippets );
10171017 }
10181018
10191019 // add extra chars to each snippet to make snippets constant size
10201020 $extended = array();
1021 - if( count( $snippets ) == 0 ){
 1021+ if ( count( $snippets ) == 0 ) {
10221022 // couldn't find the target words, just show beginning of article
10231023 if ( array_key_exists( $first, $all ) ) {
10241024 $targetchars = $contextchars * $contextlines;
@@ -1026,32 +1026,32 @@
10271027 }
10281028 } else {
10291029 // if begin of the article contains the whole phrase, show only that !!
1030 - if( array_key_exists($first,$snippets) && preg_match($pat1,$snippets[$first])
1031 - && $offsets[$first] < $contextchars * 2 ){
1032 - $snippets = array ($first => $snippets[$first]);
 1030+ if ( array_key_exists( $first, $snippets ) && preg_match( $pat1, $snippets[$first] )
 1031+ && $offsets[$first] < $contextchars * 2 ) {
 1032+ $snippets = array ( $first => $snippets[$first] );
10331033 }
10341034
10351035 // calc by how much to extend existing snippets
1036 - $targetchars = intval( ($contextchars * $contextlines) / count ( $snippets ) );
 1036+ $targetchars = intval( ( $contextchars * $contextlines ) / count ( $snippets ) );
10371037 }
10381038
1039 - foreach($snippets as $index => $line){
 1039+ foreach ( $snippets as $index => $line ) {
10401040 $extended[$index] = $line;
1041 - $len = strlen($line);
1042 - if( $len < $targetchars - 20 ){
 1041+ $len = strlen( $line );
 1042+ if ( $len < $targetchars - 20 ) {
10431043 // complete this line
1044 - if($len < strlen( $all[$index] )){
1045 - $extended[$index] = $this->extract( $all[$index], $offsets[$index], $offsets[$index]+$targetchars, $offsets[$index]);
 1044+ if ( $len < strlen( $all[$index] ) ) {
 1045+ $extended[$index] = $this->extract( $all[$index], $offsets[$index], $offsets[$index] + $targetchars, $offsets[$index] );
10461046 $len = strlen( $extended[$index] );
10471047 }
10481048
10491049 // add more lines
10501050 $add = $index + 1;
1051 - while( $len < $targetchars - 20
1052 - && array_key_exists($add,$all)
1053 - && !array_key_exists($add,$snippets) ){
 1051+ while ( $len < $targetchars - 20
 1052+ && array_key_exists( $add, $all )
 1053+ && !array_key_exists( $add, $snippets ) ) {
10541054 $offsets[$add] = 0;
1055 - $tt = "\n".$this->extract( $all[$add], 0, $targetchars - $len, $offsets[$add] );
 1055+ $tt = "\n" . $this->extract( $all[$add], 0, $targetchars - $len, $offsets[$add] );
10561056 $extended[$add] = $tt;
10571057 $len += strlen( $tt );
10581058 $add++;
@@ -1059,27 +1059,27 @@
10601060 }
10611061 }
10621062
1063 - //$snippets = array_map('htmlspecialchars', $extended);
 1063+ // $snippets = array_map('htmlspecialchars', $extended);
10641064 $snippets = $extended;
1065 - $last = -1;
 1065+ $last = - 1;
10661066 $extract = '';
1067 - foreach($snippets as $index => $line){
1068 - if($last == -1)
 1067+ foreach ( $snippets as $index => $line ) {
 1068+ if ( $last == - 1 )
10691069 $extract .= $line; // first line
1070 - elseif($last+1 == $index && $offsets[$last]+strlen($snippets[$last]) >= strlen($all[$last]))
1071 - $extract .= " ".$line; // continous lines
 1070+ elseif ( $last + 1 == $index && $offsets[$last] + strlen( $snippets[$last] ) >= strlen( $all[$last] ) )
 1071+ $extract .= " " . $line; // continous lines
10721072 else
10731073 $extract .= '<b> ... </b>' . $line;
10741074
10751075 $last = $index;
10761076 }
1077 - if( $extract )
 1077+ if ( $extract )
10781078 $extract .= '<b> ... </b>';
10791079
10801080 $processed = array();
1081 - foreach($terms as $term){
1082 - if( ! isset($processed[$term]) ){
1083 - $pat3 = "/$patPre(".$term.")$patPost/ui"; // highlight word
 1081+ foreach ( $terms as $term ) {
 1082+ if ( ! isset( $processed[$term] ) ) {
 1083+ $pat3 = "/$patPre(" . $term . ")$patPost/ui"; // highlight word
10841084 $extract = preg_replace( $pat3,
10851085 "\\1<span class='searchmatch'>\\2</span>\\3", $extract );
10861086 $processed[$term] = true;
@@ -1098,11 +1098,11 @@
10991099 * @param $count Integer
11001100 * @param $text String
11011101 */
1102 - function splitAndAdd(&$extracts, &$count, $text){
1103 - $split = explode( "\n", $this->mCleanWikitext? $this->removeWiki($text) : $text );
1104 - foreach($split as $line){
1105 - $tt = trim($line);
1106 - if( $tt )
 1102+ function splitAndAdd( &$extracts, &$count, $text ) {
 1103+ $split = explode( "\n", $this->mCleanWikitext ? $this->removeWiki( $text ) : $text );
 1104+ foreach ( $split as $line ) {
 1105+ $tt = trim( $line );
 1106+ if ( $tt )
11071107 $extracts[$count++] = $tt;
11081108 }
11091109 }
@@ -1112,10 +1112,10 @@
11131113 *
11141114 * @param $matches Array
11151115 */
1116 - function caseCallback($matches){
 1116+ function caseCallback( $matches ) {
11171117 global $wgContLang;
1118 - if( strlen($matches[0]) > 1 ){
1119 - return '['.$wgContLang->lc($matches[0]).$wgContLang->uc($matches[0]).']';
 1118+ if ( strlen( $matches[0] ) > 1 ) {
 1119+ return '[' . $wgContLang->lc( $matches[0] ) . $wgContLang->uc( $matches[0] ) . ']';
11201120 } else
11211121 return $matches[0];
11221122 }
@@ -1130,23 +1130,23 @@
11311131 * @param $posEnd Integer: (out) actual end position
11321132 * @return String
11331133 */
1134 - function extract($text, $start, $end, &$posStart = null, &$posEnd = null ){
 1134+ function extract( $text, $start, $end, &$posStart = null, &$posEnd = null ) {
11351135 global $wgContLang;
11361136
1137 - if( $start != 0)
 1137+ if ( $start != 0 )
11381138 $start = $this->position( $text, $start, 1 );
1139 - if( $end >= strlen($text) )
1140 - $end = strlen($text);
 1139+ if ( $end >= strlen( $text ) )
 1140+ $end = strlen( $text );
11411141 else
11421142 $end = $this->position( $text, $end );
11431143
1144 - if(!is_null($posStart))
 1144+ if ( !is_null( $posStart ) )
11451145 $posStart = $start;
1146 - if(!is_null($posEnd))
 1146+ if ( !is_null( $posEnd ) )
11471147 $posEnd = $end;
11481148
1149 - if($end > $start)
1150 - return substr($text, $start, $end-$start);
 1149+ if ( $end > $start )
 1150+ return substr( $text, $start, $end - $start );
11511151 else
11521152 return '';
11531153 }
@@ -1159,21 +1159,21 @@
11601160 * @param $offset Integer: offset to found index
11611161 * @return Integer: nearest nonletter index, or beginning of utf8 char if none
11621162 */
1163 - function position($text, $point, $offset=0 ){
 1163+ function position( $text, $point, $offset = 0 ) {
11641164 $tolerance = 10;
11651165 $s = max( 0, $point - $tolerance );
1166 - $l = min( strlen($text), $point + $tolerance ) - $s;
 1166+ $l = min( strlen( $text ), $point + $tolerance ) - $s;
11671167 $m = array();
1168 - if( preg_match('/[ ,.!?~!@#$%^&*\(\)+=\-\\\|\[\]"\'<>]/', substr($text,$s,$l), $m, PREG_OFFSET_CAPTURE ) ){
 1168+ if ( preg_match( '/[ ,.!?~!@#$%^&*\(\)+=\-\\\|\[\]"\'<>]/', substr( $text, $s, $l ), $m, PREG_OFFSET_CAPTURE ) ) {
11691169 return $m[0][1] + $s + $offset;
1170 - } else{
 1170+ } else {
11711171 // check if point is on a valid first UTF8 char
11721172 $char = ord( $text[$point] );
1173 - while( $char >= 0x80 && $char < 0xc0 ) {
 1173+ while ( $char >= 0x80 && $char < 0xc0 ) {
11741174 // skip trailing bytes
11751175 $point++;
1176 - if($point >= strlen($text))
1177 - return strlen($text);
 1176+ if ( $point >= strlen( $text ) )
 1177+ return strlen( $text );
11781178 $char = ord( $text[$point] );
11791179 }
11801180 return $point;
@@ -1192,11 +1192,11 @@
11931193 * @param $offsets Array: map of starting points of snippets
11941194 * @protected
11951195 */
1196 - function process( $pattern, $extracts, &$linesleft, &$contextchars, &$out, &$offsets ){
1197 - if($linesleft == 0)
 1196+ function process( $pattern, $extracts, &$linesleft, &$contextchars, &$out, &$offsets ) {
 1197+ if ( $linesleft == 0 )
11981198 return; // nothing to do
1199 - foreach($extracts as $index => $line){
1200 - if( array_key_exists($index,$out) )
 1199+ foreach ( $extracts as $index => $line ) {
 1200+ if ( array_key_exists( $index, $out ) )
12011201 continue; // this line already highlighted
12021202
12031203 $m = array();
@@ -1204,22 +1204,22 @@
12051205 continue;
12061206
12071207 $offset = $m[0][1];
1208 - $len = strlen($m[0][0]);
1209 - if($offset + $len < $contextchars)
 1208+ $len = strlen( $m[0][0] );
 1209+ if ( $offset + $len < $contextchars )
12101210 $begin = 0;
1211 - elseif( $len > $contextchars)
 1211+ elseif ( $len > $contextchars )
12121212 $begin = $offset;
12131213 else
1214 - $begin = $offset + intval( ($len - $contextchars) / 2 );
 1214+ $begin = $offset + intval( ( $len - $contextchars ) / 2 );
12151215
12161216 $end = $begin + $contextchars;
12171217
12181218 $posBegin = $begin;
12191219 // basic snippet from this line
1220 - $out[$index] = $this->extract($line,$begin,$end,$posBegin);
 1220+ $out[$index] = $this->extract( $line, $begin, $end, $posBegin );
12211221 $offsets[$index] = $posBegin;
12221222 $linesleft--;
1223 - if($linesleft == 0)
 1223+ if ( $linesleft == 0 )
12241224 return;
12251225 }
12261226 }
@@ -1228,25 +1228,25 @@
12291229 * Basic wikitext removal
12301230 * @protected
12311231 */
1232 - function removeWiki($text) {
 1232+ function removeWiki( $text ) {
12331233 $fname = __METHOD__;
12341234 wfProfileIn( $fname );
12351235
1236 - //$text = preg_replace("/'{2,5}/", "", $text);
1237 - //$text = preg_replace("/\[[a-z]+:\/\/[^ ]+ ([^]]+)\]/", "\\2", $text);
1238 - //$text = preg_replace("/\[\[([^]|]+)\]\]/", "\\1", $text);
1239 - //$text = preg_replace("/\[\[([^]]+\|)?([^|]]+)\]\]/", "\\2", $text);
1240 - //$text = preg_replace("/\\{\\|(.*?)\\|\\}/", "", $text);
1241 - //$text = preg_replace("/\\[\\[[A-Za-z_-]+:([^|]+?)\\]\\]/", "", $text);
1242 - $text = preg_replace("/\\{\\{([^|]+?)\\}\\}/", "", $text);
1243 - $text = preg_replace("/\\{\\{([^|]+\\|)(.*?)\\}\\}/", "\\2", $text);
1244 - $text = preg_replace("/\\[\\[([^|]+?)\\]\\]/", "\\1", $text);
1245 - $text = preg_replace_callback("/\\[\\[([^|]+\\|)(.*?)\\]\\]/", array($this,'linkReplace'), $text);
1246 - //$text = preg_replace("/\\[\\[([^|]+\\|)(.*?)\\]\\]/", "\\2", $text);
1247 - $text = preg_replace("/<\/?[^>]+>/", "", $text);
1248 - $text = preg_replace("/'''''/", "", $text);
1249 - $text = preg_replace("/('''|<\/?[iIuUbB]>)/", "", $text);
1250 - $text = preg_replace("/''/", "", $text);
 1236+ // $text = preg_replace("/'{2,5}/", "", $text);
 1237+ // $text = preg_replace("/\[[a-z]+:\/\/[^ ]+ ([^]]+)\]/", "\\2", $text);
 1238+ // $text = preg_replace("/\[\[([^]|]+)\]\]/", "\\1", $text);
 1239+ // $text = preg_replace("/\[\[([^]]+\|)?([^|]]+)\]\]/", "\\2", $text);
 1240+ // $text = preg_replace("/\\{\\|(.*?)\\|\\}/", "", $text);
 1241+ // $text = preg_replace("/\\[\\[[A-Za-z_-]+:([^|]+?)\\]\\]/", "", $text);
 1242+ $text = preg_replace( "/\\{\\{([^|]+?)\\}\\}/", "", $text );
 1243+ $text = preg_replace( "/\\{\\{([^|]+\\|)(.*?)\\}\\}/", "\\2", $text );
 1244+ $text = preg_replace( "/\\[\\[([^|]+?)\\]\\]/", "\\1", $text );
 1245+ $text = preg_replace_callback( "/\\[\\[([^|]+\\|)(.*?)\\]\\]/", array( $this, 'linkReplace' ), $text );
 1246+ // $text = preg_replace("/\\[\\[([^|]+\\|)(.*?)\\]\\]/", "\\2", $text);
 1247+ $text = preg_replace( "/<\/?[^>]+>/", "", $text );
 1248+ $text = preg_replace( "/'''''/", "", $text );
 1249+ $text = preg_replace( "/('''|<\/?[iIuUbB]>)/", "", $text );
 1250+ $text = preg_replace( "/''/", "", $text );
12511251
12521252 wfProfileOut( $fname );
12531253 return $text;
@@ -1258,14 +1258,14 @@
12591259 *
12601260 * @param $matches Array
12611261 */
1262 - function linkReplace($matches){
 1262+ function linkReplace( $matches ) {
12631263 $colon = strpos( $matches[1], ':' );
1264 - if( $colon === false )
 1264+ if ( $colon === false )
12651265 return $matches[2]; // replace with caption
12661266 global $wgContLang;
12671267 $ns = substr( $matches[1], 0, $colon );
1268 - $index = $wgContLang->getNsIndex($ns);
1269 - if( $index !== false && ($index == NS_FILE || $index == NS_CATEGORY) )
 1268+ $index = $wgContLang->getNsIndex( $ns );
 1269+ if ( $index !== false && ( $index == NS_FILE || $index == NS_CATEGORY ) )
12701270 return $matches[0]; // return the whole thing
12711271 else
12721272 return $matches[2];
@@ -1306,7 +1306,7 @@
13071307 continue;
13081308 }
13091309 --$contextlines;
1310 - $pre = $wgContLang->truncate( $m[1], -$contextchars );
 1310+ $pre = $wgContLang->truncate( $m[1], - $contextchars );
13111311
13121312 if ( count( $m ) < 3 ) {
13131313 $post = '';

Status & tagging log