Index: trunk/extensions/WikimediaIncubator/SpecialSearchWiki.php |
— | — | @@ -0,0 +1,142 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class SpecialSearchWiki extends IncludableSpecialPage { |
| 5 | + public function __construct() { |
| 6 | + parent::__construct( 'SearchWiki' ); |
| 7 | + } |
| 8 | + |
| 9 | + function getDescription() { |
| 10 | + return wfMessage( 'wminc-searchwiki' )->plain(); |
| 11 | + } |
| 12 | + |
| 13 | + /** |
| 14 | + * Show the special page |
| 15 | + * @param $subpage Mixed: parameter passed to the page or null |
| 16 | + */ |
| 17 | + public function execute( $subpage ) { |
| 18 | + global $wmincProjects, $wmincSisterProjects, $wgScript; |
| 19 | + |
| 20 | + $this->setHeaders(); |
| 21 | + |
| 22 | + # Array of projects |
| 23 | + $this->mProjects = array_merge( $wmincProjects, $wmincSisterProjects ); |
| 24 | + |
| 25 | + # Queries |
| 26 | + if( $subpage ) { |
| 27 | + $subpage = explode( '/', $subpage ); |
| 28 | + } |
| 29 | + $projectQuery = $this->getRequest()->getText( 'searchproject', isset( $subpage[0] ) ? $subpage[0] : '' ); |
| 30 | + $languageQuery = $this->getRequest()->getText( 'searchlanguage', isset( $subpage[1] ) ? $subpage[1] : '' ); |
| 31 | + |
| 32 | + # Show form |
| 33 | + $this->getOutput()->addHTML( |
| 34 | + Xml::fieldset( wfMessage( 'wminc-searchwiki' )->plain(), |
| 35 | + Html::rawElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'wminc-searchwiki-form' ), |
| 36 | + Html::hidden( 'title', SpecialPage::getTitleFor( 'SearchWiki' ) ) . |
| 37 | + '<p>' . Xml::label( wfMessage( 'wminc-searchwiki-selectproject' )->text(), 'wminc-searchproject' ) . |
| 38 | + ' ' . $this->makeProjectSelector( $projectQuery ) . '</p>' . |
| 39 | + '<p>' . Xml::inputLabel( wfMessage( 'wminc-searchwiki-inputlanguage' )->text(), 'searchlanguage', |
| 40 | + 'wminc-searchlanguage', 30, $languageQuery ) . ' ' . |
| 41 | + Xml::submitButton( wfMessage( 'wminc-searchwiki-go' )->text() ) . '</p>' |
| 42 | + ) ) |
| 43 | + ); |
| 44 | + |
| 45 | + # Search |
| 46 | + if ( $projectQuery || $languageQuery ) { |
| 47 | + $this->mNamesUserLang = Language::getTranslatedLanguageNames( $this->getLanguage()->getCode() ); |
| 48 | + $this->mEnglishNames = Language::getTranslatedLanguageNames( 'en' ); |
| 49 | + $this->mNativeNames = Language::getLanguageNames( false ); |
| 50 | + $this->doSearch( $projectQuery, $languageQuery ); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Make a <select> box with projects (Wikipedia, Wiktionary, ...) |
| 56 | + * @param $selected |
| 57 | + * @return string |
| 58 | + */ |
| 59 | + protected function makeProjectSelector( $selected ) { |
| 60 | + $select = new XmlSelect( 'searchproject', 'wminc-searchproject', $selected ); |
| 61 | + $select->addOptions( array_flip( $this->mProjects ) ); |
| 62 | + return $select->getHTML(); |
| 63 | + } |
| 64 | + |
| 65 | + protected function doSearch( $projectQuery, $languageQuery ) { |
| 66 | + ## Match project ## |
| 67 | + if( isset( $this->mProjects[$projectQuery] ) ) { |
| 68 | + $matchProject = $projectQuery; # searched with a project code (select box) |
| 69 | + } elseif( $projectCode = array_search( $projectQuery, $this->mProjects ) ) { |
| 70 | + $matchProject = $projectCode; # searched with a project name |
| 71 | + } else { |
| 72 | + return $this->getOutput()->addWikiMsg( 'wminc-searchwiki-noproject' ); |
| 73 | + } |
| 74 | + |
| 75 | + $results = array(); |
| 76 | + |
| 77 | + $lcLanguageQuery = strtolower( $languageQuery ); |
| 78 | + # The more important, the more below, because they override earlier codes |
| 79 | + $validCodes = array_keys( Language::getLanguageNames() ); |
| 80 | + if( in_array( $lcLanguageQuery, $validCodes ) ) { |
| 81 | + $builtinCode = Language::factory( $lcLanguageQuery )->getCode(); |
| 82 | + $results[$builtinCode] = 'langcode'; # Match language code |
| 83 | + } |
| 84 | + if( $codeByEnglishName = array_search( $lcLanguageQuery, array_map( 'strtolower', $this->mEnglishNames ) ) ) { |
| 85 | + $results[$codeByEnglishName] = 'englishname'; # Match name in English |
| 86 | + } |
| 87 | + if( $codeUserLang = array_search( $lcLanguageQuery, array_map( 'strtolower', $this->mNamesUserLang ) ) ) { |
| 88 | + $results[$codeUserLang] = 'userlangname'; # Match name in user language |
| 89 | + } |
| 90 | + if( $codeByNativeName = array_search( $lcLanguageQuery, array_map( 'strtolower', $this->mNativeNames ) ) ) { |
| 91 | + $results[$codeByNativeName] = 'nativename'; # Match native name |
| 92 | + } |
| 93 | + |
| 94 | + if( count( $results ) === 1 ) { |
| 95 | + self::gotoWiki( $matchProject, key( $results ) ); |
| 96 | + } elseif( count( $results ) < 1 ) { |
| 97 | + $noresult = Html::element( 'p', array( 'class' => 'error' ), wfMessage( 'wminc-searchwiki-noresults' )->text() ); |
| 98 | + return $this->getOutput()->addHTML( $noresult ); |
| 99 | + } elseif( count( $results ) > 1 ) { |
| 100 | + self::showMultipleResults( $matchProject, $languageQuery, $results ); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Go directly to the wiki if it exists, else to the Incubator test wiki |
| 106 | + * @param $project String: project code |
| 107 | + * @param $lang String: Language code |
| 108 | + */ |
| 109 | + protected function goToWiki( $project, $lang ) { |
| 110 | + $lang = self::getRootCode( $lang ); |
| 111 | + $status = IncubatorTest::getDBState( array( 'project' => $project, 'lang' => $lang, 'error' => null ) ); |
| 112 | + $url = $status == 'existing' ? IncubatorTest::getSubdomain( $lang, $project ) : |
| 113 | + Title::newFromText( 'W' . $project . '/' . $lang )->getFullURL( array( 'goto' => 'mainpage' ) ); |
| 114 | + $this->getOutput()->redirect( $url ); |
| 115 | + } |
| 116 | + |
| 117 | + protected function getRootCode( $code ) { |
| 118 | + # e.g. ks-arab -> ks |
| 119 | + $stripLangTags = array( '-arab', '-latn', '-cyrl', '-deva', '-cans', '-grek' ); |
| 120 | + return str_replace( $stripLangTags, '', $code ); |
| 121 | + } |
| 122 | + |
| 123 | + protected function showMultipleResults( $project, $languageQuery, $results ) { |
| 124 | + $this->getOutput()->addHTML( '<div id="wminc-searchwiki-results">' . |
| 125 | + Xml::element( 'p', array(), wfMessage( 'wminc-searchwiki-multiplematches' )->text() ) . '<ul>' ); |
| 126 | + foreach( $results as $resultCode => $resultType ) { |
| 127 | + $langName = $this->mNamesUserLang[$resultCode]; |
| 128 | + $infopage = Title::newFromText( IncubatorTest::displayPrefix( $project, $resultCode, true ) ); |
| 129 | + $linkInfoPage = Linker::linkKnown( $infopage, wfMessage( 'wminc-searchwiki-gotoinfopage' )->text() ); |
| 130 | + $linkMainPage = Linker::linkKnown( $infopage, wfMessage( 'wminc-infopage-title-' . $project, $langName )->text(), |
| 131 | + array(), array( 'goto' => 'mainpage' ) ); |
| 132 | + # wminc-searchwiki-match-langcode, wminc-searchwiki-match-englishname, |
| 133 | + # wminc-searchwiki-match-userlangname, wminc-searchwiki-match-nativename |
| 134 | + $this->getOutput()->addHTML( '<li>' . |
| 135 | + wfMessage( 'wminc-searchwiki-match-' . $resultType, |
| 136 | + $languageQuery, $langName, $resultCode )->escaped() . ' ' . |
| 137 | + $this->getLanguage()->getArrow() . ' ' . |
| 138 | + wfMessage( 'wminc-searchwiki-goto', $linkMainPage, $linkInfoPage )->text() . |
| 139 | + '</li>' ); |
| 140 | + } |
| 141 | + $this->getOutput()->addHTML( '</ul></div>' ); |
| 142 | + } |
| 143 | +} |
Property changes on: trunk/extensions/WikimediaIncubator/SpecialSearchWiki.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 144 | + native |
Index: trunk/extensions/WikimediaIncubator/WikimediaIncubator.php |
— | — | @@ -16,7 +16,7 @@ |
17 | 17 | 'path' => __FILE__, |
18 | 18 | 'name' => 'Wikimedia Incubator', |
19 | 19 | 'author' => 'SPQRobin', |
20 | | - 'version' => '4.8', |
| 20 | + 'version' => '5.0', |
21 | 21 | 'url' => 'https://www.mediawiki.org/wiki/Extension:WikimediaIncubator', |
22 | 22 | 'descriptionmsg' => 'wminc-desc', |
23 | 23 | ); |
— | — | @@ -155,3 +155,7 @@ |
156 | 156 | $wgHooks['SpecialSearchCreateLink'][] = 'IncubatorTest::onSpecialSearchCreateLink'; |
157 | 157 | $wgHooks['SpecialSearchPowerBox'][] = 'IncubatorTest::onSpecialSearchPowerBox'; |
158 | 158 | $wgHooks['SpecialSearchSetupEngine'][] = 'IncubatorTest::onSpecialSearchSetupEngine'; |
| 159 | + |
| 160 | +/* Search for a wiki in a language */ |
| 161 | +$wgAutoloadClasses['SpecialSearchWiki'] = $dir . 'SpecialSearchWiki.php'; |
| 162 | +$wgSpecialPages['SearchWiki'] = 'SpecialSearchWiki'; |
Index: trunk/extensions/WikimediaIncubator/WikimediaIncubator.alias.php |
— | — | @@ -13,4 +13,5 @@ |
14 | 14 | 'ViewUserLang' => array( 'ViewUserLang' ), |
15 | 15 | 'MyMainPage' => array( 'MyMainPage' ), |
16 | 16 | 'RandomByTest' => array( 'RandomByTest' ), |
| 17 | + 'SearchWiki' => array( 'SearchWiki' ), |
17 | 18 | ); |
Index: trunk/extensions/WikimediaIncubator/WikimediaIncubator.i18n.php |
— | — | @@ -62,6 +62,21 @@ |
63 | 63 | # Search |
64 | 64 | 'wminc-search-nocreate-nopref' => 'You searched for "$1". Please set your [[Special:Preferences|test wiki preference]] so we can tell you which page you can create!', |
65 | 65 | 'wminc-search-nocreate-suggest' => 'You searched for "$1". You can create a page in your wiki at <b>[[$2]]</b>!', |
| 66 | + |
| 67 | + # SearchWiki |
| 68 | + 'wminc-searchwiki' => 'Search wiki', |
| 69 | + 'wminc-searchwiki-noproject' => 'You did not select a project.', |
| 70 | + 'wminc-searchwiki-noresults' => 'No results. You can search by language codes, native names and names in your interface language or English.', |
| 71 | + 'wminc-searchwiki-selectproject' => 'Search for:', |
| 72 | + 'wminc-searchwiki-inputlanguage' => 'In language:', |
| 73 | + 'wminc-searchwiki-go' => 'Go', |
| 74 | + 'wminc-searchwiki-multiplematches' => 'Your search matches the following languages:', |
| 75 | + 'wminc-searchwiki-match-langcode' => '"$1" is an ISO 639 language code for $2.', |
| 76 | + 'wminc-searchwiki-match-userlangname' => '$1 is the name of a language.', |
| 77 | + 'wminc-searchwiki-match-englishname' => '"$1" is the name for $2 in English.', |
| 78 | + 'wminc-searchwiki-match-nativename' => '"$1" is the name for $2 in that language.', |
| 79 | + 'wminc-searchwiki-goto' => '<b>$1</b> ($2)', |
| 80 | + 'wminc-searchwiki-gotoinfopage' => 'info page', |
66 | 81 | ); |
67 | 82 | |
68 | 83 | /** Message documentation (Message documentation) |
— | — | @@ -122,6 +137,18 @@ |
123 | 138 | 'wminc-listusers-testwiki' => 'Used on [[Special:ListUsers]]. $1 is either "Incubator" or a link to pages like "Wx/xyz".', |
124 | 139 | 'wminc-search-nocreate-nopref' => 'Used on Special:Search. $1 is the search term.', |
125 | 140 | 'wminc-search-nocreate-suggest' => 'Used on Special:Search. $1 is the search term, and $2 is the prefixed form of $1.', |
| 141 | + 'wminc-searchwiki' => 'Title of Special:SearchWiki, to search for a wiki in a specific language', |
| 142 | + 'wminc-searchwiki-noproject' => 'Error message', |
| 143 | + 'wminc-searchwiki-noresults' => 'Error message', |
| 144 | + 'wminc-searchwiki-selectproject' => 'Followed by a dropdown menu with projects (Wikipedia, Wiktionary, ...)', |
| 145 | + 'wminc-searchwiki-inputlanguage' => 'Followed by an input box where you can enter a language name or code', |
| 146 | + 'wminc-searchwiki-go' => 'Submit button', |
| 147 | + 'wminc-searchwiki-multiplematches' => 'Followed by a list of matches when there are multiple matches', |
| 148 | + 'wminc-searchwiki-match-langcode' => '$1 is the input; $2 is its language name in the user interface language', |
| 149 | + 'wminc-searchwiki-match-userlangname' => '$1 is the input; $2 is its language name in the user interface language', |
| 150 | + 'wminc-searchwiki-match-englishname' => '$1 is the input; $2 is its language name in the user interface language', |
| 151 | + 'wminc-searchwiki-match-nativename' => '$1 is the input; $2 is its language name in the user interface language', |
| 152 | + 'wminc-searchwiki-gotoinfopage' => 'Link text to go to the Wx/xyz info page', |
126 | 153 | ); |
127 | 154 | |
128 | 155 | /** адыгэбзэ (адыгэбзэ) |