ویکی‌پدیا:لوآ: تفاوت میان نسخه‌ها

محتوای حذف‌شده محتوای افزوده‌شده
علیرضا (بحث | مشارکت‌ها)
جز ابرابزار
بخش بزرگی از متن‌های ترجمه‌نشده را حذف کردم. خواننده اگر اهل انگلیسی خواندن باشد از میان‌ویکی انگلیسی استفاده کند راحت‌تر است
خط ۲۰:
 
لوآ یک [[پردازه‌نویسی|زبان اسکریپتی]] است که می‌تواند از طریق توابع و یا [[برنامه‌نویسی شیءگرا]] برای تحلیل داده‌ها، محاسبات، و قالب‌بندی نتایج به کار رود.
Although some Lua script can be kept simple, for easy understanding, Lua allows complex structures which would challenge a [[computer scientist]], with tables, dynamic functions, and [[associative array]]s where index [[subscript]]s can be words as well as index numbers. Lua also supports [[recursion]] of re-nested functions, so care should be taken to avoid excessive complexity where other users would not understand how to maintain a Lua module. The following is an example of Lua [[source code]] for a [[hello world]] function contained in [[Module:HelloWorld]]:
<syntaxhighlight lang="lua">
-- All Lua modules on Wikipedia must begin by defining a variable that will hold their
-- externally accessible functions. They can have any name and may also hold data.
my_object = {};
 
-- Add a function to the variable. These are callable in Wikipedia via the #invoke command.
-- "frame" will contain the data that Wikipedia sends this function when it is called.
my_object.hello = function( frame )
 
-- Declare a local variable and assign data to it.
local str = "Hello World!"
 
-- Quit this function and send the information in "str" back to Wikipedia.
-- The "print" function is not allowed, so all output is accomplished via
-- returning strings in this fashion.
return str
 
-- End the function.
end
 
-- All modules end by returning the variable containing its functions to Wikipedia.
return my_object
 
-- We can now use this module by calling {{#invoke: HelloWorld | hello }}.
-- The #invoke command begins with the module's name, in this case "HelloWorld",
-- then takes the name of one of its functions as an argument, in this case "hello".
</syntaxhighlight>
A sample of Lua is highlighted by tag "&lt;source lang="lua">... &lt;/source>" placed around the Lua source code. To view some more complex examples of Lua, see article: "[[لوآ (زبان برنامه‌نویسی)]]".
 
For instructions on how to use Lua within MediaWiki, see [[mw:Extension:Scribunto/Lua reference manual]].
 
== آزمایش واحد ==
A unit testing framework for Lua scripts on Wikipedia is available at [[Module:UnitTests]]. It allows you to execute your script on a given set of inputs and verify that the expected outputs are produced. Unit tests are especially useful for rapidly detecting regressions, where modifications to a script introduce new problems.
 
By convention, unit tests for a module like [[Module:Bananas]] are placed in [[Module:Bananas/testcases]], and are executed on [[Module talk:Bananas/testcases]] with e.g. <code><nowiki>{{#invoke: Bananas/testcases | run_tests}}</nowiki></code>. Test methods must begin with "test". A simple example from [[Module:Bananas/testcases]] is below.
<syntaxhighlight lang="lua">
-- Unit tests for [[Module:Bananas]]. Click talk page to run tests.
local p = require('Module:UnitTests')
 
function p:test_hello()
self:preprocess_equals('{{#invoke:Bananas | hello}}', 'Hello, world!')
end
 
return p
</syntaxhighlight>
 
For a list of all modules using unit tests, see [[Special:Whatlinkshere/Module:UnitTests]].
 
== قابلیت‌های مختص ویکی‌پدیا ==
Overall: Lua can only get input as text strings passed to the <code><nowiki>{{#invoke:}}</nowiki></code> and what can be fetched via frame:expandTemplate, frame:preprocess, and the like. Lua on Wikipedia can only output wikitext not including pre-save transforms or transclusions and other <code><nowiki>{{...}}</nowiki></code> constructs. Also, all Lua in the page is limited to 10 seconds CPU time (you can look in the source code of a rendered page to see how long a template or module took to parse). And relative to standard Lua, Scribunto's Lua lacks all sorts of functions (see [[mw:Extension:Scribunto/Lua reference manual#Differences from standard Lua]]).
 
=== محدودیت‌های ورودی لوآ ===
Lua code in Scribunto is only run when the page is being parsed. Therefore, the only user input that Lua can receive is by ''page editing'' - it cannot create a box that calculates the square root of a number you type in, or recalculate a piece of the Mandelbrot set depending on which part of the parent set you click on. The input Lua can receive includes any transcludeable text page on Wikipedia. This does ''not'' include graphics files (not even [[Wikipedia:SVG help|.SVG]] files, although they are actually text, unless you cut and paste it onto a Wiki text page), the list of pages listed in a [[Help:Category|category]], nor the contents of [[Help:Special|Special:]] pages.
 
=== ویکی‌متن ===
Transcluded Wikipedia headers frequently contain a hidden code such as "UNIQ5ae8f2aa414ff233-h-3--QINU" which may need to be stripped out in order for them to be parsed effectively.
 
Wikilinks of the type '''<nowiki>[[Wikipedia:Help|]]</nowiki>''' won't work if returned as output - they need to be written explicitly as '''<nowiki>[[Wikipedia:Help|Help]]</nowiki>'''. Other pre-save transforms, such as replacing <code>~~<nowiki/>~~</code> with signatures, will also fail to be processed. Template transclusions, parser function calls, and variable substitutions (i.e. anything with a <code><nowiki>{{...}}</nowiki></code>) will not be processed, nor will tags such as {{tag|ref|o}} or {{tag|nowiki|o}}.
 
== برچسب‌زدن الگوهای تبدیل‌شده ==