Extension:BypassSearch/1.0.2

From MediaWiki.org
Jump to navigation Jump to search

BypassSearch.php[edit]

<?php
/** BypassSearch - http://www.mediawiki.org/wiki/Extension:BypassSearch
        Some notes:
        -UserToggles requires 1.8.0.  If you're old school (i.e., you don't meet that version), you're gonna have to get down and dirty.
        -SpecialSearchNogomatch requires 1.6.0, and if you're running on that old of mw, you really should upgrade anyway.
        -You don't need this extension if you enable $wgGoToEdit globally.
        -Set $wgDefaultUserOptions['bypasssearch'] to 1 in LocalSettings.php to enable the toggle by default; 0 to disable by default.
 */

if (!defined('MEDIAWIKI')) {
        echo "This is an extension and has no bells and whistles you can play with by accessing it directly.\n";
        die();
}

$wgExtensionMessagesFiles['BypassSearch'] = __DIR__ . '/BypassSearch.i18n.php';

// make it...
$bypassSearch = new BypassSearch;

// ... then add it ...
$wgExtensionFunctions[] = array($bypassSearch, 'init');

// ... and finally make the preference to enable it.
#$wgHooks['UserToggles'][] = array($bypassSearch, 'togglify');
$wgHooks['GetPreferences'][] = 'BypassSearch::togglify';

$wgExtensionCredits['other'][] = array(
        'name'          => 'BypassSearch',
        'version'       => '1.0.2',
        'author'        => 'slakr, Nathan Larson',
        'url'           => 'http://www.mediawiki.org/wiki/Extension:BypassSearch',
        'description'   => 'Adds a user preference to go directly to creating a new page when the Go button is used and the target page doesn\'t exist',
);

// zee class
class BypassSearch {

        /** Say hi, offer coffee/tea.
         * @public
         */
        function init() {
                global $wgUser, $wgHooks;

                // is it enabled?  more importantly, do they have enough 's'es to sound like a snake?
                if ( ! $wgUser->getOption('bypasssearch') ) {
                        return;
                }

                // add actual handling for us.
                $wgHooks['SpecialSearchNogomatch'][] = array($this, 'run');

        }

        /** Add our toggle
         * @param $user
         * @param &$preferences
         * @public
         */
        public static function togglify( $user, &$preferences )  {
                // A checkbox
                $preferences['bypasssearch'] = array(
                        'type' => 'toggle',
                        'label-message' => 'tog-bypassearch', // a system message
                        'section' => 'searchoptions/miscellaneoussearchoptions',
                );
                return true;
        }

        /** Apparently we're enabled, so let's kick the tires and light the fires.
         * @param object &$titleObj
         * @public
         */
        function run(&$titleObj) {
                global $wgGoToEdit;

                // drumroll please ...
                $wgGoToEdit = true;

                // ... and I'm spent.  The rest is out of our hands.
                return true;
        }

} // end class BypassSearch

BypassSearch.i18n.php[edit]

<?php
$messages = array();

$messages['en'] = array(
    'tog-bypassearch' => 'Bypass automatic searches when using the "Go" button and the target page does not exist',
    'prefs-miscellaneoussearchoptions' => 'Miscellaneous options'
);