انتقل إلى المحتوى

ميدياويكي:Gadget-TemplatePreloader.js

من ويكي مصدر، المكتبة الحرة

ملاحظة: بعد النشر، أنت قد تحتاج إلى إفراغ الكاش الخاص بمتصفحك لرؤية التغييرات.

  • فايرفوكس / سافاري: أمسك Shift أثناء ضغط Reload، أو اضغط على إما Ctrl-F5 أو Ctrl-R (⌘-R على ماك)
  • جوجل كروم: اضغط Ctrl-Shift-R (⌘-Shift-R على ماك)
  • إنترنت إكسبلورر/إيدج: أمسك Ctrl أثناء ضغط Refresh، أو اضغط Ctrl-F5
  • أوبرا: اضغط Ctrl-F5.
/* هذا السكريبت يقوم بتحميل [[قالب:ترويسة]] عند بداية إنشاء صفحة. تم استيراد وتعريب هذه الصفحة من النسخة الأصلية في  ويكيمصدر الإنكليزيةWikisource 
  [[MediaWiki:Gadget-TemplatePreloader.js]]
من قبل [[مستخدم:Cipher]] في ديسمبر 2008.  
Preload Template:Header when adding a new text  */
/* For help, see '''Help:Header preloading script gadget''' */
function preloadHeaderTemplate()
{
    var wpTextbox1 = document.getElementById("wpTextbox1")
    if (wpTextbox1.value == "")
    {
        //fill in header template
        var subpageDepth = 0
        var isSubpage
        var pathToTOC = "" //typically "../"
        var subpageTitle
        var subpageTitleRegExp
        var rawBasePageRequest
        var tocPageTitle
        var beginningOfShortSectionTitle //for example, if tocPageTitle == "Democracy in America/Volume 2", then beginningOfShortSectionTitle == "Volume 2, " - this is used to determine the values for "previous" and "next"
        var rawTocPageRequest //table of contents page
        var rawTocPageWithoutHeader
        
        //title
        var wpTextbox1value = "{{ترويسة\n | عنوان      = "
        if (mw.config.get('wgTitle').indexOf("/") != -1) //a slash could indicate that it's a subpage
        {
            rawBasePageRequest = new XMLHttpRequest()
            
            //determine where the base page name ends and where the subpage name begins
            for (var slashPosition = mw.config.get('wgTitle').indexOf("/"); slashPosition != -1; slashPosition = mw.config.get('wgTitle').indexOf("/", slashPosition + 1))
            {
                rawBasePageRequest.open("GET", mw.config.get('wgServer') + mw.config.get('wgScript') + "?title=" + mw.config.get('wgTitle').substr(0, slashPosition) + "&action=raw", false)
                rawBasePageRequest.send(null)
                
                if (rawBasePageRequest.responseText != "") //base page found
                {
                    wpTextbox1value += "[["
                    
                    //output however many "../" will take us to the base page
                    for (var i = mw.config.get('wgTitle').lastIndexOf("/"); i >= slashPosition; i = mw.config.get('wgTitle').lastIndexOf("/", i - 1))
                    {
                        wpTextbox1value += "../"
                        subpageDepth += 1
                    }
                    
                    wpTextbox1value += "]]"
                    
                    isSubpage = true
                    break
                }
            }
            
            //output
            if (isSubpage == false) //despite the slash in this page's title, no base page was found, so assume that this page is the base page (for example, "9/11 Commission Report")
            {
                wpTextbox1value += mw.config.get('wgTitle')
            }
            
            //round 2: find the table of contents page, not necessarily the same as the base page
            var slashPosition = mw.config.get('wgTitle').lastIndexOf("/")
            if (subpageDepth > 1)
            {
                rawTocPageRequest = new XMLHttpRequest()
                
                for (; slashPosition != -1; slashPosition = mw.config.get('wgTitle').lastIndexOf("/", slashPosition - 1))
                {
                    pathToTOC += "../"
                    
                    tocPageTitle = mw.config.get('wgTitle').substr(0, slashPosition) //will be used later
                    rawTocPageRequest.open("GET", mw.config.get('wgServer') + mw.config.get('wgScript') + "?title=" + tocPageTitle + "&action=raw", false)
                    rawTocPageRequest.send(null)
                    
                    if (rawTocPageRequest.responseText != "") //TOC page found
                    {
                        break
                    }
                }
            }
            else //if the depth is only 1 then the base page and the TOC page are the same
            {
                //set up variables without going through the whole loop
                pathToTOC = "../"
                tocPageTitle = mw.config.get('wgTitle').substr(0, slashPosition)
                rawTocPageRequest = rawBasePageRequest
            }
            //for later
            subpageTitle = mw.config.get('wgTitle').substr(slashPosition + 1) //subpageTitle is relative to the table of contents, not the base page
            var indexOfSlashInTocPageTitle = tocPageTitle.indexOf("/")
            if (indexOfSlashInTocPageTitle != -1)
            {
                beginningOfShortSectionTitle = tocPageTitle.substring(tocPageTitle.indexOf("/") + 1).replace(/\//g, ", ") + ", " //change / to , - better than doing nothing
            }
            else
            {
                beginningOfShortSectionTitle = ""
            }
        }
        else //definitely not a subpage
        {
            wpTextbox1value += mw.config.get('wgTitle')
        }
        
        //author
        wpTextbox1value += "\n | مؤلف       = "
        if (isSubpage)
        {
            try
            {
                wpTextbox1value += /\{\{[\s]*ترويسة[2]?[\s]*[\s\S]*\|[\s]*مؤلف[\s]*\=([^\||\}\}]*)/.exec(rawTocPageRequest.responseText)[1].replace(/^\s+/,"").replace(/\s+$/,"")
            }
            catch (e)
            {
                //if there was an error, just leave the field blank
            }
        }
        
        //translator
        wpTextbox1value += "\n | مترجم      = "
        if (isSubpage)
        {
            try
            {
                wpTextbox1value += /\{\{[\s]*ترويسة[2]?[\s]*[\s\S]*\|[\s]*مترجم[\s]*\=([^\||\}\}]*)/.exec(rawTocPageRequest.responseText)[1].replace(/^\s+/,"").replace(/\s+$/,"")
            }
            catch (e)
            {
                //if there was an error, just leave the field blank
            }
        }
        
        //section
        wpTextbox1value += "\n | باب        = "
        if (isSubpage) //only subpages have section titles
        {
            try
            {
                subpageTitleRegExp = subpageTitle.replace(/[ _]/, "[ _]")  //subpageTitleRegExp will also be used later
                subpageTitleRegExp = "[" + subpageTitleRegExp[0].toUpperCase() + subpageTitleRegExp[0].toLowerCase() + "]" + subpageTitleRegExp.substr(1) //MediaWiki is not case-sensitive on the first letter
                
                wpTextbox1value += (new RegExp("\\[\\[/" + subpageTitleRegExp + "\\|(.*)\]\]")).exec(rawTocPageRequest.responseText)[1]
            }
            catch (e)
            {
                //if there was an error, fall back on just adding the section name
                wpTextbox1value += subpageTitle.replace(/\//g, ", ")
            }
        }
        
        //used for both previous page and next page determination
        var indexOfCurrentPageInTableOfContents = -1 //the -1 makes sure that if the value can't be found, "undefined" isn't used instead
        
        //previous
        wpTextbox1value += "\n | سابق       = "
        try
        {
            rawTocPageWithoutHeader = rawTocPageRequest.responseText.substring(/(\{\{ترويسة[2]?[^\}\}]*)/.exec(rawTocPageRequest.responseText)[1].length) //cut off the header before searching
            
            indexOfCurrentPageInTableOfContents = rawTocPageWithoutHeader.indexOf((new RegExp("\\[\\[/" + subpageTitleRegExp + "[\\||\\]\\]]")).exec(rawTocPageWithoutHeader)[0])
            
            var indexOfTargetLink = rawTocPageWithoutHeader.lastIndexOf("[[/", indexOfCurrentPageInTableOfContents - 3)
            var previousPageTitle = /\[\[\/([^\||\]\]]*)/.exec(rawTocPageWithoutHeader.substring(indexOfTargetLink, indexOfCurrentPageInTableOfContents))[1]
            
            wpTextbox1value += "[[" + pathToTOC + previousPageTitle + "|" + beginningOfShortSectionTitle + previousPageTitle.replace(/\//g, ", ") + "]]" //change / to , - better than doing nothing
        }
        catch (e)
        {
            //if there was an error, just leave the field blank
        }
        
        //next
        wpTextbox1value += "\n | تالي       = "
        try
        {
            if (indexOfCurrentPageInTableOfContents != -1) //if we don't know where we are in the list, do not wrap back to the beginning - strangely, an exception doesn't get thrown if I leave indexOfCurrentPageInTableOfContents alone, and the "next" algorithm jumped back to the beginning of the table of contents page
            {
                var indexOfTargetLink = rawTocPageWithoutHeader.indexOf("[[/", indexOfCurrentPageInTableOfContents + subpageTitle.length + 2)
                if (indexOfTargetLink != -1) //if we're at the end of the list, do not wrap back to the beginning
                {
                    var nextPageTitle = /\[\[\/([^\||\]\]]*)/.exec(rawTocPageWithoutHeader.substring(indexOfTargetLink))[1]
                    wpTextbox1value += "[[" + pathToTOC + nextPageTitle + "|" + beginningOfShortSectionTitle + nextPageTitle.replace(/\//g, ", ") + "]]" //change / to , - better than doing nothing
                }
            }
        }
        catch (e)
        {
            //if there was an error, just leave the field blank
        }
        
        wpTextbox1.value = wpTextbox1value + "\n | ملاحظات     = \n}}\n\n"
    }
}

/* Preload Template:Textinfo when starting a discussion page */
function preloadTextinfoTemplate()
{
    var wpTextbox1 = document.getElementById("wpTextbox1")
    if (wpTextbox1.value == "")
    {
        wpTextbox1.value = "{{معلومات نص\n | نسخة        = \n | مصدر        = \n |  مساهمون    = \n | تقدم_العمل  = \n | ملاحظات      = \n | مراجعون     = \n}}\n"
    }
}

/* Preload Template:Author when starting an author page */
function preloadAuthorTemplate()
{
    var wpTextbox1 = document.getElementById("wpTextbox1")
    if (wpTextbox1.value == "")
    {
        wpTextbox1.value ="{{مؤلف}}\n\n==أعمال==\n\n{{ضبط استنادي}}\n\n"
    }
}

if (mw.config.get('wgAction') == "edit")
{
    switch (mw.config.get('wgNamespaceNumber'))
    {
        case 0: //main
            $(document).ready(preloadHeaderTemplate)
            break
        case 1: //Talk
            $(document).ready(preloadTextinfoTemplate)
            break
        case 102: //Author
            $(document).ready(preloadAuthorTemplate)
    }
}