وحدة:Wikidata2

هذه الوحدة نسخت في البداية من sv:Modul:Wikidata2 ومن ثم تم تطويرها هنا.

الاستخدامات

أنظر قائمة قوالب تستخدم هذه الوحدة.

الوحدات الفرعية

الاستعمال

مثال

{{#invoke:Wikidata2|formatStatements|property=p17}}


انقر لإظهار الشرح >>>>

الوسائط

formatStatements

  • |property= مطلوب الخاصية المطلوبة.
  • |entityId= أو |id= معرف عنصر ويكي بيانات.
  • |page= يمكن إستخدامه بدلاً من |entityId=Q805 كالتالي: |page=اليمن .
  • |enbarten= أو |firstvalue= يعرض أول قيمة فقط في الخاصية.
    • |firstvalue=2 يعرض القيمة الثانية في الخاصية إن كانت موجودة، يمكن استخدام هذا الخيار حتى رقم 9.
  • |numval= يعرض عدد محدد من القيم في الخاصية.
    • |numval=4 يعرض القيم الأربع الأولى في الخاصية إن كانت موجودة.
  • |prefix= بادئة
  • |suffix= لاحقة
  • |formatting= يحدد تنسيق القيم يستخدم مع إحداثيات جغرافية، وعنصر، وخاصية.
  • |label= اذا كنت تريد الاختيار كيفية ظهور الوصلات. مثلا [[شعار مقاطعة بليكينج|الشعار]] يمكن الحصول عليها عن طريق اضافة |label=الشعار تؤثر هذه المعلمة على وحدات القياس.
  • |raw= يعود بالنتيجة خام على شكل جداول (يستخدم من داخل وحدات لوا أخرى) ويمكن معاينة ما سيكون الناتج عليه عبر إضافة خيار |rawtolua=.
فصل القيم
  • |separator= فاصلة بين البنود | انظر أيضا conjunction
  • |conjunction= الفاصلة الأخيرة قبل اخر بند، افتراضي ، و .
القيم الافتراضية
توصيف يظهر
{{#invoke:Wikidata2 
|formatStatements 
|entityId=Q76 
|noref=t 
|property=P106 
|rank=all 
}}
سياسي، ‏محامٍ، ‏كاتب سياسي، ‏منظم مجتمع، ‏رجل دولة، ‏محاماة، ‏مدون صوتي، ‏أكاديمي و كاتب مذكرات
|separator=br يضع القيم في أسطر. |separator=* يضع القيم في قائمة نقطية.
توصيف يظهر
{{#invoke:Wikidata2 
|formatStatements 
|separator=br 
|entityId=Q76 
|noref=t 
|property=P106 
|rank=all 
}}
سياسي
محامٍ
كاتب سياسي
منظم مجتمع
رجل دولة
محاماة
مدون صوتي
أكاديمي
كاتب مذكرات
توصيف يظهر
{{#invoke:Wikidata2 
|formatStatements 
|separator=* 
|entityId=Q76 
|noref=t 
|property=P106 
|rank=all 
}}
التحديد والإزاحة
|limit= |offset=
  • يُستخدم وسيط |limit= لتحديد عدد القيم التي يتم العمل عليها، (ملاحظة: قد تظهر عدد القيم كما حددتها أو أقل بناءًا على عوامل تصفية وتحديد أخرى)
    • مثال عند وضع |limit=4:
توصيف يظهر
{{#invoke:Wikidata2 
|formatStatements 
|limit=4 
|entityId=Q76 
|noref=t 
|property=P106 
|rank=all 
}}
محامٍ، ‏كاتب سياسي، ‏منظم مجتمع و سياسي
  • يُستخدم وسيط |offset= لتحديد عدد القيم التي يتم تخطيها بدءًا من القيمة 1.
    • مثال عند وضع |offset=3:
توصيف يظهر
{{#invoke:Wikidata2 
|formatStatements 
|offset=3 
|entityId=Q76 
|noref=t 
|property=P106 
|rank=all 
}}
منظم مجتمع، ‏رجل دولة، ‏محاماة، ‏مدون صوتي، ‏أكاديمي و كاتب مذكرات
تحديد عدد القيم
  • |numval=2 عدد القيم المراد عرضها
توصيف يظهر
{{#invoke:Wikidata2 |formatStatements |numval=2 |entityId=Q76 |noref=t |property=P106 |rank=all }}
سياسي و محامٍ
رتبة القيم / rank
  • |rank= يحدد نوع البيانات المطلوب عرضها:
    • |rank=all يظهر جميع البنود في الخاصية.
    • |rank=valid يظهر البنود المفضلة والتي بوضع "ترتيب عادي" ولكن لا يظهر المهملة.
    • |rank=best (افتراضي) يظهر القيم المفضلة.
ترتيب النتيجة / sort
  • |sortingproperty= تحديد خاصية الترتيب.
  • تستخدم وسائط الترتيب (|sortbytime=، و|sortbynumber=، و|sortingmethod=) قيمتين فقط كالتالي:
  • chronological = تصاعدي (ASC): من التاريخ الأقدم إلى التاريخ الأحدث (إذا كانت الخاصية المحددة تاريخ)
  • inverted = تنازلي (DESC): من التاريخ الأحدث إلى التاريخ الأقدم (إذا كانت الخاصية المحددة تاريخ)
حسب التصفيات
حسب الخواص

إذا لم توجد تصفيات في النتيجة فإننا بحاجة للدخول إلى العناصر وأخذ قيمة مُعينة لجعلها مفتاح للترتيب، مثل تاريخ الولادة، أو تاريخ البدء.

  • نستخدم |sortingmethod= بأحدى القيمتين (chronological أو inverted) ونستخدم الخاصية المطلوبة عبر وسيط |sortingproperty=.
الحصول على خاصية P527 وترتيب النتيجة حسب قيمة خاصية P580 للعناصر المعروضة (تنازلي)
توصيف يظهر
{{#invoke:Wikidata2 
|formatStatements 
|entityId=Q200009 
|sortingproperty=P580 
|sortingmethod=chronological 
|property=P527 
}}
فريق إنيوس 2023، ‏فريق إنيوس 2022، ‏فريق إنيوس 2021، ‏2020 إنيوس، ‏فريق إنيوس 2019، ‏فريق سكاي 2018، ‏موسم فريق سكاي 2017، ‏سكاي 2016، ‏سكاي 2015، ‏سكاي 2014، ‏سكاي 2013، ‏سكاي 2012 و سكاي 2010
ترتيب تصاعدي
توصيف يظهر
{{#invoke:Wikidata2 
|formatStatements 
|entityId=Q200009 
|sortingproperty=P580 
|sortingmethod=inverted 
|property=P527 
}}
سكاي 2010، ‏سكاي 2012، ‏سكاي 2013، ‏سكاي 2014، ‏سكاي 2015، ‏سكاي 2016، ‏موسم فريق سكاي 2017، ‏فريق سكاي 2018، ‏فريق إنيوس 2019، ‏2020 إنيوس، ‏فريق إنيوس 2021، ‏فريق إنيوس 2022 و فريق إنيوس 2023
المراجع / References / Sources
  • |noref= أدخل قيمة في هذه المعلمة إذا كنت لا تريد جلب المصادر من ويكي بيانات.
  • |justref= أدخل قيمة في هذه المعلمة إذا كنت تريد جلب المصادر فقط دون ظهور القيمة!.
  • |onlyvaluewithref= لإظهار البيانات التي لها مراجع فقط.
  • |numberofreferences= لوضع حد أقصى للمراجع لكل قيمة، مثلاً عند تحديد |numberofreferences=1 يظهر مرجع واحد فقط في كل قيمة إن كان متوفراً.
صياغة النص
  • |formatcharacters= تحديد صيغة اخراج القيمة، القيم المسموح بها هي: lc/uc/lcfirst/ucfirst/formatnum
    • |formatcharacters=lc: تحويل جميع أحرف النص الإنجليزي إلى حروف صغيرة.
    • |formatcharacters=lcfirst: تحويل الحرف الأول من النص الإنجليزي إلى حرف صغير.
    • |formatcharacters=uc : تحويل جميع أحرف النص الإنجليزي إلى حروف كبيرة.
    • |formatcharacters=ucfirst: تحويل الحرف الأول من النص الإنجليزي إلى حرف كبير.
    • |formatcharacters=formatnum: مشروحة في #كمية.
تصفيات / Qualifiers
  • |withdate= يضيف تصفية بتاريخ (P585) إذا كانت موجودة.
  • |bothdates= يضيف تصفيات تاريخ البدء (P580) وتاريخ الانتهاء (P582) إذا كانتا موجودتان كلاهما أو إحداهما.
    • |bothdates=line يضع سطر جديد قبل التاريخ، |bothdates=before يضع التاريخ قبل القيمة، |bothdates=after يضع التاريخ بعد القيمة.
  • |qual1= يضيف تصفية محددة.
فلترة النتائج
  • |avoidqualifier= يتجاهل القيم التي تحتوي تصفيات معينة، مثل: |avoidqualifier=P518 يظهر البند الذي لا يحتوي على تصفية ينطبق على جزء (P518).
    • بإستخدام |avoidqualifiervalue= مع |avoidqualifier= فإن ذلك يحدد قيمة التصفية إذا طابقت الوسيط |avoidqualifiervalue= فلن يتم إظهارها، وإذا لم تطابق فسيتم إظهارها، يدعم حالياً العناصر/items فقط.
  • |preferqualifier= تحديد خاصية تصفيات يجب أن تكون بيانات الخاصية متضمنتها.
    • |preferqualifiervalue= تحدد القيمة التي تناسب أعلاه. يدعم حالياً العناصر فقط.
  • |justthisqual= يظهر فقط التصفية، إذا لم تكن التصفية موجودة لن يظهر شيء.
وسائط حسب نوع البيانات / datatype
عنصر / item
  • |nolink= يظهر النتيجة دون وصلات.
  • |formatting=raw يعرض القيمة بشكل Q505.
  • |formatting=sitelink يعرض القيم التي تحتوي مقالة في ويكيبيديا العربية.
= فلترة النتيجة =
  • |getonly= يستخدم هذا الوسيط إذا أردنا الحصول على عناصر معينة التي تكون قيمة نموذج من (P31) فيها تساوي قيمة هذا الوسيط، مثال:
    • |getonly=Q5 للحصول على الأفراد فقط.
    • القيم المتعددة ممكنة، مثل :|getonly=Q127448,Q193556 للحصول على البلديات السويدية والمناظر الطبيعية.
  • |getonlyproperty= إذا أردنا استخدام خاصية أخرى بدلاً من نموذج من (P31) في البند السابق.
  • |avoidvalue= يستخدم هذا الوسيط إذا أردنا أن نتجاهل أو نلغي ظهور عنصر معين، مثال عند وضع |avoidvalue=Q21769847 فتظهر النتائج دون ظهور أستانا 2016 (Q21769847).
= prefervalue =
  • |prefervalue= يستخدم هذا الوسيط بشكل معاكس للوسيط |avoidvalue= حيث نقوم بتحديد القيمة التي نريد ظهورها |prefervalue=Q19481 فتظهر النتيجة للعنصر ليستر سيتي (Q19481) فقط، ولكن الفائدة من إستخدام هذا الوسيط هي التأكد من وجود العنصر في هذه الخاصية أو للحصول على التصفيات، مثال يظهر عناصر الفائز (P1346) مع تصفية الفِرَق المشاركة (P1923) بين قوسين.
توصيف يظهر
{{#invoke:Wikidata2 
|formatStatements 
|entityId=Q39052816 
|property=P1923 
|qual1=P1355 
}}
نادي آرسنال (21 )، ‏نادي بورنموث (13 )، ‏برايتون أند هوف ألبيون (9 )، ‏نادي بيرنلي (11 )، ‏كارديف سيتي (10 )، ‏تشيلسي (21 )، ‏كريستال بالاس (14 )، ‏نادي إيفرتون (15 )، ‏ليستر سيتي (15 )، ‏نادي ليفربول (30 )، ‏مانشستر سيتي (32 )، ‏مانشستر يونايتد (19 )، ‏نيوكاسل يونايتد (12 )، ‏توتنهام هوتسبير (23 )، ‏نادي واتفورد (14 )، ‏وست هام يونايتد (15 )، ‏وولفرهامبتون واندررز (16 )، ‏نادي ساوثهامبتون (9 )، ‏نادي فولهام (7 ) و هدرسفيلد تاون (3 )

إذا كنا قد حصلنا عبر خاصية الفائز (P1346) عن الفريق الفائز في الدوري الإنجليزي الممتاز 2018–19 (Q39052816). وأردنا الحصول على عدد مرات الفوز للفريق الفائز، من الجدول السابق ويتم ذلك بإستخدام الوسيط |justthisqual= ما ينتج:

توصيف يظهر
{{#invoke:Wikidata2 
|formatStatements 
|entityId=Q39052816 
|property=P1923 
|justthisqual=P1355 
}}
21 ، ‏13 ، ‏9 ، ‏11 ، ‏10 ، ‏21 ، ‏14 ، ‏15 ، ‏15 ، ‏30 ، ‏32 ، ‏19 ، ‏12 ، ‏23 ، ‏14 ، ‏15 ، ‏16 ، ‏9 ، ‏7 و 3

ولكننا نريد ظهور التصفية للفريق الفائز فقط وهو مانشستر سيتي (Q50602):

توصيف يظهر
{{#invoke:Wikidata2 |formatStatements |prefervalue=Q50602 |entityId=Q39052816 |property=P1923 |qual1=P1355 }}
مانشستر سيتي (32 )
توصيف يظهر
{{#invoke:Wikidata2 |formatStatements |prefervalue=Q50602 |justthisqual=P1355 |property=P1923 |entityId=Q39052816 }}
32
= avoidvalue =
  • |avoidvalue= يستخدم هذا الوسيط بشكل معاكس للوسيط |prefervalue= إذا أردنا أن نتجاهل أو نلغي ظهور عنصر معين، مثال عند وضع |avoidvalue=Q50602 فتظهر النتائج دون ظهور مانشستر سيتي (Q50602).
توصيف يظهر
{{#invoke:Wikidata2 
|formatStatements 
|entityId=Q39052816 
|property=P1923 
|qual1=P1355 
|avoidvalue=Q50602 
}}
نادي آرسنال (21 )، ‏نادي بورنموث (13 )، ‏برايتون أند هوف ألبيون (9 )، ‏نادي بيرنلي (11 )، ‏كارديف سيتي (10 )، ‏تشيلسي (21 )، ‏كريستال بالاس (14 )، ‏نادي إيفرتون (15 )، ‏ليستر سيتي (15 )، ‏نادي ليفربول (30 )، ‏مانشستر يونايتد (19 )، ‏نيوكاسل يونايتد (12 )، ‏توتنهام هوتسبير (23 )، ‏نادي واتفورد (14 )، ‏وست هام يونايتد (15 )، ‏وولفرهامبتون واندررز (16 )، ‏نادي ساوثهامبتون (9 )، ‏نادي فولهام (7 ) و هدرسفيلد تاون (3 )
= الحصول على خواص إضافية =
وسيط property1
  • يستخدم للحصول على قيمة خاصية من العناصر التي حصلنا عليها، بحيث تظهر قيمة |property1= بجانب القيمة الأصلية:
في هذا المثال نستخدم وسيط |property1= للحصول على خاصية صورة العلم (P41)
توصيفيظهر
{{#invoke:wikidata2|formatStatements
| property1=P41| separator=br| property=P1376| entityId=Q2471| rank=all}}
Flag of Yemen.svg اليمن[1]
Flag of North Yemen.svg الجمهورية العربية اليمنية
Flag of the Mutawakkilite Kingdom of Yemen.svg المملكة المتوكلية اليمنية
وسيط property2
  • مثل |property1= (بدون استخدام |property3=).
توصيفيظهر
{{#invoke:wikidata2|formatStatements
| entityId=Q529207| illwd2=t| limit=5| separator=br| property=P54| property2=P17| bothdates=}}
إيطاليا إيه سي ميلان
إيطاليا نادي إنتر ميلان
هولندا نادي آيندهوفن
إسبانيا ريال مدريد
إسبانيا نادي برشلونة
وسيط property3
  • يستخدم بجانب الوسيط |property2=، بحيث تظهر قيمة |property3= بدون قيمة |property2=.
في هذا المثال نستخدم هذا الوسيط للحصول على خاصية صورة العلم (P41) بدلًا من اسم البلد:
توصيفيظهر
{{#invoke:wikidata2|formatStatements
| property3=P41| illwd2=t| limit=5| separator=br| entityId=Q529207| property=P54| property2=P17| bothdates=}}
Flag of Italy.svg إيه سي ميلان
Flag of Italy.svg نادي إنتر ميلان
Flag of the Netherlands.svg نادي آيندهوفن
Flag of Spain.svg ريال مدريد
Flag of Spain.svg نادي برشلونة
مع وسيط |image=t
توصيفيظهر
{{#invoke:wikidata2|formatStatements
| entityId=Q529207| property3=P41| size=20px| illwd2=t| limit=5| separator=br| image=t| property=P54| property2=P17| bothdates=}}
Flag of Italy.svg
إيه سي ميلان
Flag of Italy.svg
نادي إنتر ميلان
Flag of the Netherlands.svg
نادي آيندهوفن
Flag of Spain.svg
ريال مدريد
Flag of Spain.svg
نادي برشلونة
ملف وسائط من كومنز / commonsMedia
  • تظهر الصور افتراضياً كالتالي:
    • {{#invoke:Wikidata2|formatStatements|entityId=Q76|property=P18|noref=true|rank=best}} ينتج: ← President Barack Obama.jpg
  • |image= عند استخدام هذا الوسيط في خواص الصور تظهر الصورة مباشرة.
|size= لتحديد حجم الصورة (60 افتراضياً)، ضع القيمة دون (بك/px إلخ). |center= لجعل الصورة في الوسط ضع قيمة هذا الخيار |center=yes.
توصيف يظهر
{{#invoke:Wikidata2 
|formatStatements 
|image=true 
|size=100 
|entityId=Q76 
|noref=true 
|property=P18 
|rank=best 
}}
President Barack Obama.jpg
توصيف يظهر
{{#invoke:Wikidata2 
|formatStatements 
|image=true 
|center=yes 
|size=100 
|qid=Q76 
|property=P18 
|noref=true 
|rank=best 
}}
President Barack Obama.jpg
إحداثيات جغرافية / coordinate
توصيف
{{#invoke:Wikidata2 
|formatStatements |entityId=Q805 |property=P625 
}}
يظهر 15°30′N 48°00′E / 15.5°N 48°E / 15.5; 48
  • |formatting=latitude يعرض خط العرض فقط.
  • |formatting=longitude يعرض خط الطول فقط.
  • |formatting=precision يعرض الدقة.
|formatting=globe يعرض عنصر الكوكب
توصيف
{{#invoke:Wikidata2 
|formatStatements |entityId=Q805 |property=P625 |formatting=globe 
}}
يظهر Q2
|formatting=longitude يعرض خط الطول
توصيف
{{#invoke:Wikidata2 
|formatStatements |entityId=Q805 |property=P625 |formatting=longitude 
}}
يظهر 48


نص أحادي اللغة / نص بلغة وحيدة / monolingualtext
  • |langpref= في الوحدة، هناك مرشح يعطي أولوية للقيم التي تحتوي على "اللغة = العربية". أضف القيمة المطلوبة لتعطيل هذا الفلتر.
    • القيم المقبولة الأخرى هي : justlang و langcode
|langpref=justlang لإظهار لغة النص فقط دون النص، مثل الإنجليزية. |langpref=langcode لإظهار رمز لغة النص فقط دون النص، مثل en.
توصيف
{{#invoke:Wikidata2 
|formatStatements |entityId=Q76 |property=P1477 |langpref=justlang 
}}
يظهر الإنجليزية
توصيف
{{#invoke:Wikidata2 
|formatStatements |entityId=Q76 |property=P1477 |langpref=langcode 
}}
يظهر en
|showlang= |textformat=text
استخدم أي قيمة لإظهار النص حسب تنسيق قالب:رمز لغة واسمها. اظهار النص فقط دون استخدام قوالب {{رمز لغة واسمها}} أو {{رمز لغة}}
توصيف يظهر
{{#invoke:Wikidata2 |formatStatements |entityId=Q76 |property=P1477 |showlang=true }}
(بالإنجليزية: Barack Hussein Obama II)‏
توصيف يظهر
{{#invoke:Wikidata2 |formatStatements |entityId=Q76 |property=P1477 |textformat=text }}
Barack Hussein Obama II
كمية / quantity
  • الافتراضي:
    • {{#invoke:Wikidata2|formatStatements|entityId=Q805|property=P2046|noref=t|firstvalue=y}} ينتج: ← 555000 كيلومتر مربع
|nounit= لا يظهر وحدة القياس. |formatcharacters=formatnum: تنسيق الأرقام بالفواصل.
توصيف
{{#invoke:Wikidata2 
|formatStatements |nounit=t |entityId=Q805 |noref=t |property=P2046 |firstvalue=y 
}}
يظهر 555000
توصيف
{{#invoke:Wikidata2 
|formatStatements |formatcharacters=formatnum |entityId=Q805 |noref=t |property=P2046 |firstvalue=y 
}}
يظهر 555,000 كيلومتر مربع
  • |unitshort= يظهر وحدات القياس مختصرة. مثلاً كم بدلاً عن كيلومتر.
مثال لاستخدام |unitshort= ومثال عند عدم استخدامه:
توصيف
{{#invoke:Wikidata2 
|formatStatements |entityId=Q219635 |property=P2295 |unitshort=t |noref=t 
}}
يظهر 5901000000 USD
توصيف
{{#invoke:Wikidata2 
|formatStatements |entityId=Q219635 |property=P2295 |noref=t 
}}
يظهر 5901000000 دولار أمريكي
نص / string
  • |stringpattern= تنسيق النص في قالب أو وصلة أو ماشابه يتم استبدال القيمة بـ$1. مثل |pattern= لكن هنا تستخدم مع نوع البيانات string.
توصيف يظهر
{{#invoke:Wikidata2 
|formatStatements 
|entityId=Q1133289 
|property=P1748 
|noref=t 
|stringpattern=[https://ncit.nci.nih.gov/ncitbrowser/ConceptReport.jsp?dictionary=NCI%20Thesaurus&code=$1 $1] 
}}
C75016 و C75016
نقطة زمنية / time
  • |modifytime= يحدد تنسيق الوقت أو التاريخ:
    • |modifytime=longdate يكتب التاريخ في شكل "31 مارس 2023"
    • |modifytime=Y يكتب التاريخ في شكل "2023" يظهر فقط السنة.
    • |modifytime= F يكتب التاريخ في شكل "مارس" يظهر فقط الشهر.
    • |modifytime= j يكتب التاريخ في شكل "31" يظهر فقط اليوم.
  • |modifyqualifiertime= تنسيق التاريخ على النحو الوارد أعلاه، ولكن يؤثر فقط على التصفيات. qualifier
معرف خارجي / external-id
افتراضي:
توصيفيظهر
{{#invoke:Wikidata2|formatStatements| entityId=Q76| property=P2002}}
BarackObama
  • |pattern= طريقة عرض الخاصية مثلاً في وصلات الضبط الاستنادي $1 مثلاً: <nowiki>https://viaf.org/viaf/$1/</nowiki> .
  • |pattern=autourl من خواص البند أعلاه، لنمط مستخدم في الخاصية المسار المنسق (P1630) (تنسيق عنوان URL) للخاصية المطلوبة.
توصيفيظهر
{{#invoke:Wikidata2|formatStatements| entityId=Q76| property=P2002| pattern=autourl}}
https://twitter.com/BarackObama


  • |pattern=autourl2 كما سبق، يقدم وصلة من نوع <nowiki>[https://viaf.org/viaf/$1/ $1] </nowiki>
توصيفيظهر
{{#invoke:Wikidata2|formatStatements| entityId=Q76| property=P2002| pattern=autourl2}}
BarackObama
المسار / url
توصيف يظهر
{{#invoke:Wikidata2 
|formatStatements 
|entityId=Q76 
|property=P856 
}}
الموقع الرسمي
توصيف يظهر
{{#invoke:Wikidata2 
|formatStatements 
|entityId=Q76 
|property=P856 
|urllabel=الموقع الرسمي 
}}
الموقع الرسمي
  • |displayformat= يقبل حالياً قيمة واحدة وهي |displayformat=weblink لاستخدام وحدة:weblink لعرض وصلة الموقع.

{{#invoke:Wikidata2|formatStatements|entityId=Q76|property=P856|displayformat=weblink}} ينتج: ← barackobama.com


خيارات إضافية
  • |NosValue= يستخدم لإظهار نتيجة ما عند عدم وجود قيمة محلية في وسيط |value= وعدم وجود قيمة في ويكي بيانات.
property-(module/function) و claim-(module/function) و value-(module/function)

يتم دعم ثلاثة أنواع من معلمات الوظائف، والتي تشير أيضًا إلى كيفية تنسيق القيم:

  • |-module= عنوان الوحدة
  • |-function= عنوان الوظيفة
property
fun( claims, options )
  • |property-module= عنوان الوحدة مثل |property-module=وحدة:Wikidata2/P54
  • |property-function= عنوان الوظيفة مثل |property-function=foot
  • تستخدم هذه الخيارات عند الحاجة لتنسيق قيم الخاصية بشكل كامل مثلاً لوضعها في جدول معين، أو فرزها بتنسيق خاص، مع إمكانية الحصول على التصفيات/qualifiers والمراجع/references وتنسيقها والتحكم بها.
  • أمثلة:
    • الوحدة وحدة:Wikidata2/P54 الخاصة بعرض فرق لاعبي كرة القدم، مثل أندية كرة القدم حيث يتم فصل المنتخبات عن الأندية.
claim
fun( statement, options )
  • |claim-module= عنوان الوحدة مثل |claim-module=وحدة:Wikidata2/P39
  • |claim-function= عنوان الوظيفة مثل |claim-function=office3
  • تستخدم هذه الخيارات عند الحاجة لتنسيق قيم الخاصية بشكل منفرد، مع إمكانية الحصول على التصفيات/qualifiers والمراجع/references وتنسيقها والتحكم بها.
  • أمثلة:
value
fun( datavalue, datatype, options )
  • |value-module= عنوان الوحدة مثل |value-module=وحدة:Wikidata2/P106
  • |value-function= عنوان الوظيفة مثل |value-function=formatEntityWithGenderClaim
  • تستخدم هذه الخيارات عند الحاجة لتنسيق قيم الخاصية بشكل منفرد لكل قيمة مثلاً لأخذ تسمية الإناث عند إستخدام خاصية المهنة (P106)، لا نستطيع هنا الحصول على التصفيات/qualifiers والمراجع/references.
بدون استخدام |value-module= باستخدام |value-module=
توصيف
{{#invoke:Wikidata2 
|formatStatements |entityId=Q76 |property=P106 |noref=t |rank=all 
}}
يظهر سياسي، ‏محامٍ، ‏كاتب سياسي، ‏منظم مجتمع، ‏رجل دولة، ‏محاماة، ‏مدون صوتي، ‏أكاديمي و كاتب مذكرات
توصيف
{{#invoke:Wikidata2 
|formatStatements |value-function=formatEntityWithGenderClaim |value-module=Wikidata2/P106 |entityId=Q6294 |noref=t |property=P106 |rank=all 
}}
يظهر سياسية، ‏محامية، ‏دبلوماسية، ‏كاتِبة، ‏كاتبة سير ذاتية، ‏أستاذة جامعية و مدونة صوتية

pageId

ينتج عنصر ويكي بيانات للصفحة المستخدم فيها الكود.

توصيف يظهر
{{#invoke:Wikidata2 
|pageId 
}}
Q24575438

labelIn

  • |1= اللغة مثل fr.
  • |2= عنصر ويكي بيانات مثل Q76.

مثال:

توصيف يظهر
{{#invoke:Wikidata2 
|labelIn 
|fa 
|Q76 
}}
باراک اوباما

descriptionIn

  • |1= اللغة مثل fr.
  • |2= عنصر ويكي بيانات مثل Q76.

مثال:

توصيف يظهر
{{#invoke:Wikidata2 
|descriptionIn 
|en 
|Q76 
}}
president of the United States from 2009 to 2017

getSiteLink / وصلات المواقع

  • |2= أو |site= الموقع مثل enwiki.
  • يتم أخذ عنصر ويكي بيانات من أحد الوسائط التالية:
    1. |1= أو |id= عنصر ويكي بيانات.
    2. |page= يمكن استخدام اسم الصفحة في هذه الويكي بدلاً من استخدام الوسيط |id=.
  • |countsitelinks= يستخدم فقط لإظهار عدد وصلات اللغات.

مثال:

توصيف يظهر
{{#invoke:Wikidata2 
|getSiteLink 
|Q76 
}}
باراك أوباما

مثال:

توصيف يظهر
{{#invoke:Wikidata2 
|getSiteLink 
|Q76 
|enwiki 
}}

أو:

توصيف يظهر
{{#invoke:Wikidata2 
|getSiteLink 
|id=Q76 
|site=enwiki 
}}
Barack Obama

أو: {{#invoke:Wikidata2|getSiteLink|page=اليمن|site=enwiki}} ينتج: ← Yemen

Dump

مثال: {{#invoke:Wikidata2{{#تبديل:{{SUBPAGENAME}}|ملعب=/ملعب}} |Dump|claims|P19}}

مثال2: {{#invoke:Wikidata2{{#تبديل:{{SUBPAGENAME}}|ملعب=/ملعب}} |Dump|id=Q76|claims|P19}}
مثال
table#1 {
    table#2 {
        ["id"] = "Q76$6c5e3740-4ca4-de6a-1557-b623372dc93a",
        ["mainsnak"] = table#3 {
            ["datatype"] = "wikibase-item",
            ["datavalue"] = table#4 {
                ["type"] = "wikibase-entityid",
                ["value"] = table#5 {
                    ["entity-type"] = "item",
                    ["id"] = "Q6366688",
                    ["numeric-id"] = 6366688,
                },
            },
            ["property"] = "P19",
            ["snaktype"] = "value",
        },
        ["qualifiers"] = table#6 {
            ["P131"] = table#7 {
                table#8 {
                    ["datatype"] = "wikibase-item",
                    ["datavalue"] = table#9 {
                        ["type"] = "wikibase-entityid",
                        ["value"] = table#10 {
                            ["entity-type"] = "item",
                            ["id"] = "Q782",
                            ["numeric-id"] = 782,
                        },
                    },
                    ["hash"] = "cb3f87742410967dc80b3283e954e888954144cf",
                    ["property"] = "P131",
                    ["snaktype"] = "value",
                },
            },
            ["P17"] = table#11 {
                table#12 {
                    ["datatype"] = "wikibase-item",
                    ["datavalue"] = table#13 {
                        ["type"] = "wikibase-entityid",
                        ["value"] = table#14 {
                            ["entity-type"] = "item",
                            ["id"] = "Q30",
                            ["numeric-id"] = 30,
                        },
                    },
                    ["hash"] = "be4c6eafa2984964f04be85667263f5642ba1a72",
                    ["property"] = "P17",
                    ["snaktype"] = "value",
                },
            },
            ["P1932"] = table#15 {
                table#16 {
                    ["datatype"] = "string",
                    ["datavalue"] = table#17 {
                        ["type"] = "string",
                        ["value"] = "Kapiolani Maternity & Gynecological Hospital",
                    },
                    ["hash"] = "6fd68e03eb688ae2bc3065f585acd78edbe8fa5b",
                    ["property"] = "P1932",
                    ["snaktype"] = "value",
                },
            },
            ["P6375"] = table#18 {
                table#19 {
                    ["datatype"] = "monolingualtext",
                    ["datavalue"] = table#20 {
                        ["type"] = "monolingualtext",
                        ["value"] = table#21 {
                            ["language"] = "en",
                            ["text"] = "6085 Kalanianaole Highway",
                        },
                    },
                    ["hash"] = "2e9be82b6a3154de13d7196988eb8b40dc8f0252",
                    ["property"] = "P6375",
                    ["snaktype"] = "value",
                },
            },
            ["P7452"] = table#22 {
                table#23 {
                    ["datatype"] = "wikibase-item",
                    ["datavalue"] = table#24 {
                        ["type"] = "wikibase-entityid",
                        ["value"] = table#25 {
                            ["entity-type"] = "item",
                            ["id"] = "Q71536040",
                            ["numeric-id"] = 71536040,
                        },
                    },
                    ["hash"] = "f9f842b41f249e5504d48025c7ad6d7b5bfc7496",
                    ["property"] = "P7452",
                    ["snaktype"] = "value",
                },
            },
        },
        ["qualifiers-order"] = table#26 {
            "P6375",
            "P1932",
            "P7452",
            "P131",
            "P17",
        },
        ["rank"] = "preferred",
        ["references"] = table#27 {
            table#28 {
                ["hash"] = "e91a853ea4e0b372b26f82a616b87eb645b0aff3",
                ["snaks"] = table#29 {
                    ["P248"] = table#30 {
                        table#31 {
                            ["datatype"] = "wikibase-item",
                            ["datavalue"] = table#32 {
                                ["type"] = "wikibase-entityid",
                                ["value"] = table#33 {
                                    ["entity-type"] = "item",
                                    ["id"] = "Q14527788",
                                    ["numeric-id"] = 14527788,
                                },
                            },
                            ["property"] = "P248",
                            ["snaktype"] = "value",
                        },
                    },
                },
                ["snaks-order"] = table#34 {
                    "P248",
                },
            },
        },
        ["type"] = "statement",
    },
    table#35 {
        ["id"] = "Q76$7ba7f268-4c19-b92f-7254-f8ed65aed53f",
        ["mainsnak"] = table#36 {
            ["datatype"] = "wikibase-item",
            ["datavalue"] = table#37 {
                ["type"] = "wikibase-entityid",
                ["value"] = table#38 {
                    ["entity-type"] = "item",
                    ["id"] = "Q18094",
                    ["numeric-id"] = 18094,
                },
            },
            ["property"] = "P19",
            ["snaktype"] = "value",
        },
        ["qualifiers"] = table#39 {
            ["P131"] = table#40 {
                table#41 {
                    ["datatype"] = "wikibase-item",
                    ["datavalue"] = table#42 {
                        ["type"] = "wikibase-entityid",
                        ["value"] = table#43 {
                            ["entity-type"] = "item",
                            ["id"] = "Q782",
                            ["numeric-id"] = 782,
                        },
                    },
                    ["hash"] = "cb3f87742410967dc80b3283e954e888954144cf",
                    ["property"] = "P131",
                    ["snaktype"] = "value",
                },
            },
            ["P17"] = table#44 {
                table#45 {
                    ["datatype"] = "wikibase-item",
                    ["datavalue"] = table#46 {
                        ["type"] = "wikibase-entityid",
                        ["value"] = table#47 {
                            ["entity-type"] = "item",
                            ["id"] = "Q30",
                            ["numeric-id"] = 30,
                        },
                    },
                    ["hash"] = "be4c6eafa2984964f04be85667263f5642ba1a72",
                    ["property"] = "P17",
                    ["snaktype"] = "value",
                },
            },
        },
        ["qualifiers-order"] = table#48 {
            "P131",
            "P17",
        },
        ["rank"] = "normal",
        ["references"] = table#49 {
            table#50 {
                ["hash"] = "207f35a0ea83b330f87438c914b0ab24558d2868",
                ["snaks"] = table#51 {
                    ["P227"] = table#52 {
                        table#53 {
                            ["datatype"] = "external-id",
                            ["datavalue"] = table#54 {
                                ["type"] = "string",
                                ["value"] = "132522136",
                            },
                            ["property"] = "P227",
                            ["snaktype"] = "value",
                        },
                    },
                    ["P248"] = table#55 {
                        table#56 {
                            ["datatype"] = "wikibase-item",
                            ["datavalue"] = table#57 {
                                ["type"] = "wikibase-entityid",
                                ["value"] = table#58 {
                                    ["entity-type"] = "item",
                                    ["id"] = "Q36578",
                                    ["numeric-id"] = 36578,
                                },
                            },
                            ["property"] = "P248",
                            ["snaktype"] = "value",
                        },
                    },
                    ["P407"] = table#59 {
                        table#60 {
                            ["datatype"] = "wikibase-item",
                            ["datavalue"] = table#61 {
                                ["type"] = "wikibase-entityid",
                                ["value"] = table#62 {
                                    ["entity-type"] = "item",
                                    ["id"] = "Q188",
                                    ["numeric-id"] = 188,
                                },
                            },
                            ["property"] = "P407",
                            ["snaktype"] = "value",
                        },
                    },
                    ["P813"] = table#63 {
                        table#64 {
                            ["datatype"] = "time",
                            ["datavalue"] = table#65 {
                                ["type"] = "time",
                                ["value"] = table#66 {
                                    ["after"] = 0,
                                    ["before"] = 0,
                                    ["calendarmodel"] = "http://www.wikidata.org/entity/Q1985727",
                                    ["precision"] = 11,
                                    ["time"] = "+2021-06-06T00:00:00Z",
                                    ["timezone"] = 0,
                                },
                            },
                            ["property"] = "P813",
                            ["snaktype"] = "value",
                        },
                    },
                },
                ["snaks-order"] = table#67 {
                    "P248",
                    "P227",
                    "P407",
                    "P813",
                },
            },
        },
        ["type"] = "statement",
    },
}
مثال موسع: {{#invoke:Wikidata2{{#تبديل:{{SUBPAGENAME}}|ملعب=/ملعب}} |Dump|claims|P1791|1|references|1|snaks|P244|1|datavalue|value}}

isSubclass / نوع فرعي من

إن كانت قيمة نموذج من (P31) أو صنف فرعي من (P279) للصفحة التي وضع فيها الكود أو للصفحة في الوسيط |id= مساوية للوسيط |parent= ينتج 'true' غير ذلك لا ينتج شيئاً. كامل الخيارات:{{#invoke:Wikidata2{{#تبديل:{{SUBPAGENAME}}|ملعب=/ملعب}} |isSubclass|parent=|maxDepth=1|property=|id=|exitItem=}}

مثال: {{#invoke:Wikidata2|isSubclass|maxDepth=1|id=Q76|parent=Q5}} ينتج: ← true

{{#invoke:Wikidata2|isSubclass|maxDepth=5|id=Q76|parent=Q45}} ينتج: ←

EntityIdForTitle

يعطي معرف ويكي بيانات من خلال عنوان مقالة ويكيبيديا العربية:

  • {{#invoke:Wikidata2|EntityIdForTitle|اليمن}} ينتج: ← Q805
  • {{#invoke:Wikidata2|EntityIdForTitle|وحدة:Wikidata2}} ينتج: ← Q24575438

Qidfortitleandwiki

  • يمكن استخدام الوظيفة للحصول على معرف ويكي بيانات لصفحات في الموسوعات الأخرى:
توصيف يظهر
{{#invoke:Wikidata2 
|Qidfortitleandwiki 
|Championnats du monde de cyclo-cross 2006 
|frwiki 
}}

آخر التحديثات

  • 4 أبريل 2019:
    • إضافة الوسيط |unitshort= لإستخدام اختصار وحدات القياس أو العملات بدلاً عن عنوان مقالتها.
  • 4 أبريل 2019:
    • إضافة الوسيط |avoidqualifiervalue= عكس الوسيط |preferqualifiervalue= .
    • إخفاء ظهور تصانيف التتبع في التصفيات عند استخدام الوسيط |raw=.
  • 2 أبريل 2019:
    • إضافة الوسيط |stringpattern= تنسيق النص.
  • 12 مارس 2019:
    • إضافة إمكانية وضع حد أقصى لعدد المراجع عبر خيار |numberofreferences=.
  • 3 مارس 2019:
    • إمكانية استخدام |numval= لعرض عدد محدد من القيم في الخاصية.
  • 11 فبراير 2019:
  • 8 فبراير 2019:
    • استخدام وحدة:weblink مع نوع بيانات url / المسار عبر وضع |displayformat=weblink.
  • 4 فبراير 2019:
    • إمكانية استخدام |firstvalue=1 لإظهار القيمة رقم 1 ويمكن الاستخدام حتى رقم 9.
  • 1 فبراير 2019:
  • 20 يناير 2019 :
  • إضافة الخيار returnnumberofvalues لمعرفة عدد قيم النتائج، (يستخدم عبر وحدات لوا)
  • 31 ديسمبر 2018:
    • إضافة الوظيفة getLabel.
  • 18 ديسمبر 2018 :
    • إمكانية حجب قيم من الظهور في خواص معينة مثل حجب العنصر نبي (Q42857) من الظهور عند استخدام خاصية المهنة (P106).
  • 2 مارس 2018 :
  • 5 فبراير 2018:
    • اضافة الخيار |formatcharacters=formatnum لإظهار تنسيق الأرقام بالفواصل مثل 10٬000٬000.
  • 25 يناير 2018:
    • اضافة الخيار |countsitelinks=t لإظهار عدد وصلات اللغات.
  • 9 ديسمبر 2017:
    • إضافة الخيار |page= يمكن أن يستخدم بديلاً عن |entityId= بوضع اسم الصفحة خاص:فرق/25985344.
  1. ^ وصلة : http://geonames.org/71137 — الرخصة: رخصة المشاع الإبداعي الملزمة بالنسب لمؤلف العمل غير القابلة للإلغاء 3.0
local p = {}
local Frame_args = {}
local Moduletext
local Modulecite
local flags
local InfoboxImage
local ModuleGlobes
local formatera
local weblink

local citetitle = "Module:Cite"
if nil ~= string.find (mw.getCurrentFrame():getTitle(), "ملعب", 1, true) then
	citetitle = "Module:Cite/ملعب"
end

local i18n = {
	["errors"] = {
		["property-param-not-provided"] = "وسيط property غير متوفر.",
		["entity-not-found"] = "الكيان غير موجود.",
		["unknown-claim-type"] = "نوع claim غير معروف.",
		["unknown-snak-type"] = "نوع snak غير معروف.",
		["unknown-datatype"] = "نوع data غير معروف.",
		["unknown-entity-type"] = "نوع entity غير معروف.",
		["unknown-value-module"] = "يجب عليك تعيين كل من  value-module و value-function.",
		["unknown-claim-module"] = "يجب عليك تعيين كل من claim-module و claim-function.",
		["unknown-property-module"] = "يجب عليك تعيين كل من property-module و property-function.",
		["property-module-not-found"] = "الوحدة المستخدمة في وسيط property-module غير موجودة.",
		["property-function-not-found"] = "الوظيفة المستخدمة في وسيط property-function غير موجودة.",
		["value-module-not-found"] = "الوحدة المستخدمة في وسيط value-module غير موجودة.",
		["value-function-not-found"] = "الوظيفة المستخدمة في وسيط value-function غير موجودة.",
		["claim-module-not-found"] = "الوحدة المستخدمة في وسيط claim-module غير موجودة.",
		["claim-function-not-found"] = "الوظيفة المستخدمة في وسيط claim-function غير موجودة."
    },
	["noarabiclabel"] = "تصنيف:صفحات_ويكي_بيانات_بحاجة_لتسمية_عربية",
	["warnDump"] = "[[Category:Called function 'Dump' from module Wikidata]]",
	["somevalue"] = "", --'"غير محدد"'
	["novalue"] ="",--قيمة مجهولة
	["cateref"] = "[[" .. "تصنيف:صفحات بها مراجع ويكي بيانات" .. "]]",
	["to translate"] = "صفحات تستعمل معطيات من ويكي بيانات بحاجة لترجمة",
	["trackingcat"] = "صفحات تستخدم خاصية $1",
	["see-wikidata-value"] = "الاطلاع ومراجعة البيانات على ويكي داتا",
	["see-wikidata"] = "راجع العنصر من ويكي بيانات المقابل",
	["see-another-project"] = "مقالة على $1",
	["see-another-language"] = "مقالة على ويكيبيديا $1",
}
local sortingproperties = {"P585","P571","P580","P569","P582","P570"}
local sorting_methods = {
	["chronological"] = "chronological",
	["تصاعدي"] = "chronological",
	["asc"] = "chronological",

	["inverted"] = "inverted",
	["تنازلي"] = "inverted",
	["desc"] = "inverted",
}

local falsetitles = {
		"قالب:قيمة ويكي بيانات",
		"وحدة:Wikidata2"
	}

local skiip_items = {
	["P106"] = {
		"Q42857", 		-- prophet
		"Q14886050", 	-- terrorist
		"Q2159907", 	-- criminal
	}
}

function isvalid(x)
    if x and x ~= "" then return x end
    return nil
end

function No_Tracking_cat(options)
	if isvalid(options.formatting) == "raw" or isvalid(options.formatting) == "sitelink" then
		return true
	end
	local notracking = isvalid(options.nocate) or isvalid(options.notracking) or isvalid(Frame_args.notracking)
	local raw = isvalid(options.raw) or isvalid(Frame_args.raw) or isvalid( options.raw2) or isvalid(Frame_args.raw2)
	local nolink = isvalid(options.nolink) or isvalid(Frame_args.nolink)
	if notracking or raw or nolink then
        return true
	end
	local pagetitle = mw.title.getCurrentTitle().text
	for _, title in pairs(falsetitles) do
		if string.find(pagetitle, title , 1, true) then
			mw.log("notracking for title with: " .. title )
			return true
		end
	end
	return false
end

function addTrackingCategory(options)
	if No_Tracking_cat(options) then return '' end
	local cat = require("Module:Wikidata/تتبع").makecategory1
	local category = cat(options)
	local nbsp = "&nbsp;"
	if isvalid(options.nbsp) then nbsp = "" end
	if isvalid(category) then
		return nbsp .. category
	end
	return ''
end

function catewikidatainfo(options)
	--[[  function to add tracking category ]]
	if No_Tracking_cat(options) then return "" end
	local cat = ""
	local prop = options.property
	cat = cat .. " [[" .. "تصنيف:صفحات بها بيانات ويكي بيانات|".. (prop or "wikidata") .. "]]"
	if (isvalid(options.nolink) == nil) then
        return cat
    else
        return ""
    end
end

function getqualifierbysortingproperty(claim, sortingproperty)
	for k, v in pairs(sortingproperty) do
		if claim.qualifiers and claim.qualifiers[v] and claim.qualifiers[v][1].snaktype == "value" then
			vali = claim.qualifiers[v][1].datavalue.value.time or claim.qualifiers[v][1].datavalue.value.amount
			if vali:sub(1,1)=="+" then vali = vali:sub(2) end
			--mw.log(vali)
			return vali
		end
	end
	return nil
end

function getDate(claim, options)
	local sortingproperty = sortingproperties
	if type(options.sortingproperty) == "table" then
		sortingproperty = options.sortingproperty
	elseif type(options.sortingproperty) == "string" and options.sortingproperty ~= "" then
		sortingproperty = {options.sortingproperty}
	end
	return getqualifierbysortingproperty(claim, sortingproperty)
end

function get_entityId(options)
	local id = options.entityId or options.entityid or options.id or options.qid
	if isvalid(id) == nil then
		if isvalid(options.page) then
			id = mw.wikibase.getEntityIdForTitle( options.page )
		end
	end
	--mw.log("id :" .. id)
	return id or ""
end

function countSiteLinks(id)
	local numb = 0
	local entity = mw.wikibase.getEntityObject(id)
	if entity and entity.sitelinks then
		for i, v in pairs(entity.sitelinks) do
			numb = numb + 1
		end
	end
	return numb
end

function make_format_num(String)
	local line = String
	line = mw.getCurrentFrame():preprocess("{{ {{{|safesubst:}}}formatnum: " .. String .. " }}")
	line = mw.ustring.gsub( line, "٫", "." )
	line = mw.ustring.gsub( line, "٬", "," )
	return line
end

function formatcharacters(label, options)
	local formatch = options.formatcharacters
	--if options.FormatfirstCharacter and options.num == 1 then
		--formatch = options.FormatfirstCharacter
	--end
	if isvalid(options.illwd2y) then
		local String2 = mw.ustring.gsub( label, "–", "-" )
		local ca = mw.ustring.match( String2, "%d%d%d%d%-%d%d%d%d", 1 ) or mw.ustring.match( String2, "%d%d%-%d%d%d%d", 1 )
		ca = ca or mw.ustring.match( String2, "%d%d%d%d", 1 ) or mw.ustring.match( String2, "%d%d%d%d%-%d%d", 1 )
		ca = ca or mw.ustring.match( String2, "%d%d%d%d", 1 ) or label
		return ca
	end
	if isvalid(formatch) == nil then
		return label
	end
    local prepr = {
        ["lcfirst"] = "{{lcfirst: " .. label .. " }}",
        ["lc"] = "{{lc: " .. label .. " }}",
        ["uc"] = "{{uc: " .. label .. " }}",
    }
	if prepr[formatch] then
		return mw.getCurrentFrame():preprocess(prepr[formatch])
	elseif formatch == "ucfirst" then
		return mw.language.getContentLanguage():ucfirst( label )
	elseif formatch == "formatnum" then
		return make_format_num( label )
	end
	return label
end

function descriptionIn(langcode, id) -- returns item description for a given language
	if isvalid(langcode) == nil then langcode = "ar" end
	langcode = mw.text.trim(langcode or "")
	id = mw.text.trim(id or "")
	if langcode == "ar" then
		local description, lange = mw.wikibase.getDescriptionWithLang( id )
		if lange == langcode then
			return description
		else
			return nil
		end
	else
		local entity = getEntityFromId(id)
		if entity and entity.descriptions then
			local description = entity.descriptions[langcode]
			if description and description.value then
				if description["language"] == langcode then
					return description.value
				end
			end
		end
	end
	return nil
end

function labelIn(langcode, id) -- returns item label for a given language
	if isvalid(langcode) == nil then langcode  = "ar" end
	if type(id) ~= "string" then id = tostring(id) end
	langcode = mw.text.trim(langcode or "")
	id = mw.text.trim(id or "")
	local label = mw.wikibase.getLabelByLang( id, langcode ) or nil
	return label
end

function get_snak_id(snak)
	if snak
	and snak.type
	and snak.type == "statement"
	and snak.mainsnak
	and snak.mainsnak.snaktype
	and snak.mainsnak.snaktype == "value"
	and snak.mainsnak.datavalue
	and snak.mainsnak.datavalue.type
	and snak.mainsnak.datavalue.type == "wikibase-entityid"
	and snak.mainsnak.datavalue.value
	and snak.mainsnak.datavalue.value.id
	then
		ID = snak.mainsnak.datavalue.value.id
		return ID
	end
end

function comparedates(a, b)  -- returns true if a is earlier than B or if a has a date but not b
	if a and b then
		return a > b
	elseif a then
		return true
	end
end

function getDateArb(claim, sorting_properties)
	local sortingproperty = sorting_properties
	if claim.mainsnak.snaktype == "value" then
		local item = claim.mainsnak.datavalue.value["numeric-id"]
		if claim.mainsnak.datavalue.value["entity-type"] == "item" then
			item = "Q" .. item
		elseif claim.mainsnak.datavalue.value["entity-type"] == "property" then
			item = "P" .. item
		end
		for k, prop in pairs(sortingproperty) do
            local date = formatStatements({property=prop, entityId=item, firstvalue="t", noref="t", modifytime="q"})
            if isvalid(date) then
                --mw.log("item:".. item .. ", prop:".. prop .. ", date:".. date)
                return date
            end
		end

	end
end

function sortbyarb(claims, sorting_properties, options)
	local sortingmethod = options.sortbyarbitrary or options.sortingmethod
	--mw.log("sortbyarb: " .. sortingmethod)

	table.sort(claims, function(a,b)
		local timeA = getDateArb(a, sorting_properties)
		local timeB = getDateArb(b, sorting_properties)
		if sortingmethod == "inverted" then
			return comparedates(timeB, timeA)
		else
			return comparedates(timeA, timeB)
		end
	end
	)
	return claims
end

function dontget(claims, options)
	-- options.dontget
	-- options.dontgetproperty
	local claims2 = {}
	for i, j in pairs(claims) do
		local id = get_snak_id(j)
		if id then
			local valid = true
			local t2 = formatStatements({property=(options.dontgetproperty or "P31"), entityId=id, noref="t", raw="t"})
			if t2 and #t2 > 0 then
				for k, state in pairs(t2) do
					for j2, falseqid in pairs(mw.text.split(options.dontget, ",")) do
						if state.item == falseqid then
							valid = false
                            break
						end
					end
				end
			end
			if valid then
				table.insert(claims2, j)
			end
		end
	end
	return claims2
end

function getonly(claims, options)
	-- options.getonly
	-- options.getonlyproperty
	local claims2 = {}
	--mw.log("wikidata2 : getonly:" .. options.getonly )
	for i, j in pairs(claims) do
		local id = get_snak_id(j)
		--if j.mainsnak and j.mainsnak.snaktype == "value" and j.mainsnak.datavalue and j.mainsnak.datavalue.value and j.mainsnak.datavalue.value.id then
			--local id = j.mainsnak.datavalue.value.id
		if id then
			local traff = false
			local t2 = formatStatements({property=(options.getonlyproperty or "P31"), entityId=id, noref="t", raw="t" })
			if t2 and #t2 > 0 then
				for k, state in pairs( t2 ) do
					for j2, only in pairs(mw.text.split(options.getonly,",")) do
						if state.item == only then
							traff = true
						end
					end
				end
			end
			if traff then
				table.insert(claims2, j)
			end
		end
	end
	return claims2
end

function claimindex(claims, options)
	local claims2 = {}
	for j, index in pairs(mw.text.split(options.claimindex,",")) do
		if tonumber(index) and #claims >= tonumber(index) then
			table.insert(claims2, claims[tonumber(index)])
		end
	end
	return claims2
end

function avoidvalue(claims, options)
	-- options.avoidvalue
	-- to avoid values
	local claims4 = {}
	local add2claims
	local avoidvalue
	if(type(options.avoidvalue) == "string") then avoidvalue = mw.text.split(options.avoidvalue,",")
		elseif(type(options.avoidvalue) == "table") then avoidvalue = options.avoidvalue
			else return claims
	end
	--mw.log("avoidvalue: " .. mw.dumpobject(options.avoidvalue))
	for i, j in pairs( claims ) do
		--mw.log("t:" .. t)
		ID = get_snak_id(j)
		add2claims = true
		if ID then
		    for k, t in pairs(avoidvalue) do
				--mw.log("ID: " .. ID)
				if ID == t then
					add2claims = false
				end
			end
			if add2claims then
				table.insert( claims4, j)
			end
		end
	end
	return claims4
end

function prefervalue(claims, options)
	local claims3 = {}
	--mw.log("prefervalue: " .. mw.dumpobject(options.prefervalue))
	local prefervalue
	if(type(options.prefervalue) == "string") then prefervalue = mw.text.split(options.prefervalue,",")
		elseif(type(options.prefervalue) == "table") then prefervalue = options.prefervalue
			else return claims
	end
	for k, t in pairs(prefervalue) do
		--mw.log("t:" .. t)
		for i, j in pairs( claims ) do
			local active = false
			ID = get_snak_id(j)
			if ID then
				--mw.log("ID: " .. ID)
				if ID == t and not active then
					table.insert( claims3, j)
					active = true
					--mw.log("ID == t" .. t)
				end
			end
		end
	end
	return claims3
end

function preferqualifier(claims, options)
	-- options.preferqualifier
	-- options.preferqualifiervalue
	mw.log("preferqualifier: " .. options.preferqualifier)
	local claims2 = {}
	for i, statement in pairs( claims ) do --
		if statement.qualifiers and statement.qualifiers[options.preferqualifier:upper()] then
			if isvalid(options.preferqualifiervalue) then
				local active = false
				for k, t in pairs(mw.text.split(options.preferqualifiervalue,",")) do
					--mw.log( "t " .. t )
					--kaso = formatStatements({property=options.preferqualifier:upper(), raw="t", formatting="raw"}, statement.qualifiers)
					----kaso = formatStatements({property=options.preferqualifier:upper(), formatting="raw"}, statement.qualifiers)
					----value = kaso
					for ii, quall in pairs( statement.qualifiers[options.preferqualifier:upper()] ) do
						--mw.log( "value " .. value )
						snaktype = quall.snaktype
						if snaktype == "value" then
							ty = quall.datavalue.value["id"]
							if ty and ty == t and not active then
								--if value == t and not active then
									table.insert( claims2, statement)
									active = true
								--end
							end
						end
					end
				end
			else
				table.insert( claims2, statement)
			end
		end
	end
	return claims2
end

function avoidqualifier(claims, options)
	-- options.avoidqualifier
	-- options.avoidqualifiervalue
	if isvalid(options.avoidqualifier) == nil then return claims end
	local av = options.avoidqualifier:upper()
	local claims2 = {}
	for i, statement in pairs( claims ) do
		if not statement.qualifiers or not statement.qualifiers[options.avoidqualifier:upper()] then
			table.insert( claims2, statement)
		elseif statement.qualifiers and statement.qualifiers[av] then
			if isvalid(options.avoidqualifiervalue) then
				list = {}
				if(type(options.avoidqualifiervalue) == "string") then
					list = mw.text.split(options.avoidqualifiervalue,",")
				elseif(type(options.avoidqualifiervalue) == "table") then
					list = options.avoidqualifiervalue
				end
				local active = true
				for k, t in pairs( list ) do
					for ii, quall in pairs( statement.qualifiers[av] ) do
						if quall.snaktype == "value" and quall.datavalue and quall.datavalue.value and quall.datavalue.value["id"] then
							if quall.datavalue.value["id"] == t then
								active = false
							end
						end
					end
				end
				if active then
					table.insert( claims2, statement)
				end
			end
		end
	end
	return claims2
end

function claims_limit(claims,limit)
	local newclaims = {}
	local ic = 0
	if #claims > limit then -- limit is not 0
	    for i = 1, #claims do
	        if i <= limit then
	            newclaims[ic] = claims[i]
	            ic = ic + 1
	        end
	    end
	end
	return newclaims
end

function claims_offset(claims,offset)
	local offsetclaims = {}
	local ic = 1
	if #claims > offset then -- offset is not 0
	    for i = 1, #claims do
	        if i > offset then
	            offsetclaims[#offsetclaims + 1] = claims[i]
	        end
	    end
	end
	return offsetclaims
end

function filter_claims( claims, options)
	local claims = claims
	
	local offset = options.offset
	if isvalid(offset) and type(offset) ~= "number" then
		offset = tonumber(offset)
		claims = claims_offset(claims,offset)
	end
	
	local limit = options.limit
	if isvalid(limit) and type(limit) ~= "number" then
		limit = tonumber(limit)
		claims = claims_limit(claims,limit)
	end
	
	if isvalid(options.avoidqualifier) then 		-- to avoid value with a given qualifier
		claims = avoidqualifier(claims, options)
	end
	
	if isvalid(options.preferqualifier) then
		claims = preferqualifier(claims, options)
	end
	
	-- options.avoidvalue
	if isvalid(options.avoidvalue) then
		claims = avoidvalue(claims, options)
	end
	
	-- options.prefervalue
	if isvalid(options.prefervalue) then
		claims = prefervalue(claims, options)
	end
	
	-- options.getonly
	if isvalid(options.getonly) then
		claims = getonly(claims, options)
	end
	
	-- options.dontget
	if isvalid(options.dontget) then
		claims = dontget(claims, options)
	end
	
	if isvalid(options.langpref) == nil then
		local claims7 = {}
		for i, statement in pairs( claims ) do
			if statement.qualifiers and statement.qualifiers.P407 then
				for k, v in pairs( statement.qualifiers.P407 ) do
					if v.snaktype == "value" and v.datavalue.value["numeric-id"] == 13955 then -- Q13955 = "العربية"
						table.insert( claims7, statement )
					end
				end
			elseif statement.qualifiers and statement.qualifiers.P282 then
				for k, v in pairs( statement.qualifiers.P282 ) do
					if v.snaktype == "value" and v.datavalue.value["numeric-id"] == 8196 then -- Q8196 = "أبجدية عربية"
						table.insert( claims7, statement )
					end
				end
			end
		end
		if #claims7 > 0 then
			claims = claims7
		end
	end
	
	local sortingmethod = options.sortbyarbitrary or options.sortingmethod
	local sorting_properties = {}

	if (type(options.sortingproperty) == "table") then
		sorting_properties = options.sortingproperty

	elseif (type(options.sortingproperty) == "string") and options.sortingproperty ~= "" then
		sorting_properties = mw.text.split(options.sortingproperty,",") --{options.sortingproperty}
	end

	if isvalid(options.sortbytime) and sorting_methods[options.sortbytime] then
		if #sorting_properties == 0 then sorting_properties = sortingproperties end
		claims = sortbyqualifier(claims, sorting_properties,options)

	elseif isvalid(options.sortbynumber) and sorting_methods[options.sortbynumber] then
		claims = sortbyqualifiernumber(claims, options, sorting_properties)

	elseif isvalid(sortingmethod) and sorting_methods[sortingmethod] then
		claims = sortbyarb(claims, sorting_properties, options)
	end
	
	local firstvalue = options.enbarten or options.firstvalue
	local first = isvalid(tonumber(firstvalue))
	if isvalid(first) and #claims > 1 then
		if first > 0 and #claims > first then
			claims = {claims[first]}
		elseif first == "2" and #claims > 1 then
			claims = {claims[2]}
		elseif first == "3" and #claims > 2 then
			claims = {claims[3]}
		elseif first == "4" and #claims > 3 then
			claims = {claims[4]}
		elseif first == "5" and #claims > 4 then
			claims = {claims[5]}
		elseif first == "6" and #claims > 5 then
			claims = {claims[6]}
		elseif first == "7" and #claims > 6 then
			claims = {claims[7]}
		elseif first == "8" and #claims > 7 then
			claims = {claims[8]}
		elseif first == "9" and #claims > 8 then
			claims = {claims[9]}
		else
			claims = {claims[1]}
		end
	elseif isvalid(firstvalue) and #claims > 0 then
		claims = {claims[1]}
	end
	
	local numval = options.numval
	if numval and type(numval) ~= "number" then  numval = tonumber(numval) end
	if numval and type(numval) == "number" and #claims > 1 and #claims > numval then
		local claimsnumval= {}
		local ic = 1
		while (numval >= ic) and (#claims >= ic) do
			table.insert( claimsnumval, claims[ic] )
			ic =ic +1
		end
		claims = claimsnumval;
	end
	
	return claims
end

function sortbyqualifier(claims, sortingproperty,options)
	table.sort(claims, function(a,b)
		local timeA = getqualifierbysortingproperty(a, sortingproperty)
		local timeB = getqualifierbysortingproperty(b, sortingproperty)
		if options.sortbytime == "inverted" then
			return comparedates(timeB, timeA)
		else
			return comparedates(timeA, timeB)
		end
	end
	)
	return claims
end

function sortbyqualifiernumber(claims, options, sorting_properties)
	if not sorting_properties then
		if (type(options.sortingproperty) == "table") then
			sorting_properties = options.sortingproperty
		elseif (type(options.sortingproperty) == "string") and options.sortingproperty ~= "" then
			sorting_properties = mw.text.split(options.sortingproperty,",")
		else
			sorting_properties = sortingproperties
		end
	end
	table.sort(claims, function(a,b)
		local timeA = getqualifierbysortingproperty(a, sorting_properties)
		local timeB = getqualifierbysortingproperty(b, sorting_properties)
		if options.sortbynumber == "inverted" then
			return comparedates(timeB, timeA)
		else
			return comparedates(timeA, timeB)
		end
	end
	)
	return claims
end

function getEntityFromId( id )
	if isvalid(id) then
	--	if not(mw.wikibase.isValidEntityId(id)) or not(mw.wikibase.entityExists(id)) then
		--	return false
		--end
		return mw.wikibase.getEntityObject( id )
	else
		return mw.wikibase.getEntityObject()
	end
end

function formatError( key )
    return i18n.errors[key]
end

function formatOneStatement( statement, ref, options)
	local value = nil
	local stat = formatStatement( statement, options )
	if not stat then
		return { v = value, raw=stat}
	end
	
	local s = stat.value
	if isvalid(s) == nil then
		return { v = value, raw=stat}
	end

	if not stat.qualifiers then
		stat.qualifiers = {}
	end

	local P585 = stat.qualifiers.P585 or ""
	local tf = stat.tifr
	local awardqual = stat.foto
	local pr = stat.pr
	local ro = stat.ro
	local qp1a = stat.qp1a
	local onlyqualifier = stat.onlyqualifier
	local qp1 = stat.qp1
	local qp2 = stat.qp2
	local qp3 = stat.qp3
	local qp4 = stat.qp4
	local qp5 = stat.qp5
	local reff	=stat.reff
	local QPrefix = isvalid(options.qualifierprefix)
	local QSuffix = isvalid(options.qualifiersuffix)

	if reff and isvalid(options.reff)	 then
		s = s .. reff
	end
	if isvalid(options.template) then
		s = mw.getCurrentFrame():expandTemplate{ title = options.template,
		args = {
			stat.QQ1, s, stat.QQ2, stat.QQ3, stat.QQ4, stat.QQ5, stat.QQ6, stat.QQ7, stat.QQ8, stat.QQ9, stat.QQ10, entityId=options.entityId, id=stat.ID
			}
		}
	end
	if isvalid(options.football) then
		s = mw.getCurrentFrame():expandTemplate{ title = "صندوق معلومات سيرة كرة قدم/سطر فريق",
		args = {
			stat.qualifiers.P580 or "", stat.qualifiers.P582 or "", s, stat.amatch, stat.goal }
		}
	end

	if isvalid(options.office) then
		s = mw.getCurrentFrame():expandTemplate{ title = "معلومات صاحب منصب/منصب ويكي بيانات/نواة", args =
		{office=s,
		termstart = stat.qualifiers.P580 or "",
		termend = stat.qualifiers.P582 or "",
		constituency = stat.qualifiers.P768 or "",
		predecessor = stat.qualifiers.P1365 or "",
		successor = stat.qualifiers.P1366 or "",
		series= stat.qualifiers.P1545 or "",
		of=stat.qualifiers.P642 or "",
		electedin=stat.qualifiers.P2715 or "",
		jurisdiction=stat.qualifiers.P1001 or "",
		employer=stat.qualifiers.P108 or "", entityId=options.entityId
		} }
	end

	function qoo(Prefix,qualpref,p,Suffix)
		if isvalid(p) then
			stri = (Prefix or " (").. (qualpref or "") .. p .. (Suffix or ")")
			if isvalid(options.nosmall) then
				return stri
			else
				return mw.text.tag("small", {}, stri )
			end
		end
	end

	if isvalid(qp1) and options.qual1 and options.qual1 and isvalid(qp1a) and isvalid(options.qual1a)
	then
		s = s .. qoo(QPrefix, options.qual1pref, qp1, QSuffix) .. qoo(QPrefix, options.qp1apref, qp1a, QSuffix)
	elseif isvalid(qp1) and isvalid(options.qual1) then
		s = s .. qoo(QPrefix, options.qual1pref, qp1, QSuffix)
	elseif isvalid(qp1a) and isvalid(options.qual1a) then
		s = s .. qoo(QPrefix, options.qp1apref, qp1a, QSuffix)
	end

	if isvalid(qp2) and isvalid(options.qual2) then
		s = s .. qoo(QPrefix, options.qual2pref, qp2, QSuffix)
	end
	if isvalid(qp3) and isvalid(options.qual3) then
		s = s .. qoo(QPrefix, options.qual3pref, qp3, QSuffix)
	end
	if isvalid(qp4) and isvalid(options.qual4) then
		s = s .. qoo(QPrefix, options.qual4pref, qp4, QSuffix)
	end
	if isvalid(qp5) and isvalid(options.qual5) then
		s = s .. qoo(QPrefix, options.qual5pref, qp5, QSuffix)
	end

	if isvalid(options.justthisqual) then
		if onlyqualifier then
			s = onlyqualifier
		else
			s = nil	  -- We need only the qualifier
		end
	end
	if isvalid(ro) and isvalid(options.withro) then s = s .. qoo(QPrefix, "", ro, QSuffix) end

	if isvalid(options.withdate) then
		if isvalid(P585) then
			if options.withdate == "y" then
				s = s .. qoo(QPrefix, "سنة ", P585, QSuffix)
			elseif options.withdate == "before" then
				s = "*" .. P585 ..":" .. s.."\n"
			else
				s = s .. qoo(QPrefix, "", P585, QSuffix)
			end
		end
	end

	if awardqual and isvalid(options.awardqua) then
		s = s .. qoo(QPrefix, "", awardqual, QSuffix)
	end

	local bothdates = options.withintervall or options.bothdates
	if tf and isvalid(bothdates) then
		if bothdates == "line" then
			s = s .. mw.text.tag("br") .. qoo(QPrefix, "", tf, QSuffix)
		elseif bothdates == "before" then
			s = qoo(QPrefix, "", tf, QSuffix) .. s
		else
			s = s .. qoo(QPrefix, "", tf, QSuffix)
		end
	end

	if type(ref) == "table" or (isvalid(options.noref)) or (isvalid(options.justthisqual)) then
		value =	 s
	else
		local t = formatReferences( statement, options )
		stat.ref = t
		if isvalid(options.justref) then
			value =	 t
		elseif isvalid(options.onlyvaluewithref) then
			if isvalid(t) then
				value =	 s .. t
			end
		else
			value =	 s .. t
		end
	end
	return { v = value, raw=stat}
end

function formatStatements( options, LuaClaims )
   	local valuetable = {} -- formattedStatements
   	local claims = {}
   	if not options.property then return formatError( "property-param-not-provided" ) end
   	local option1 = options.option1
   	if option1 and options.option1value then
   		options[ option1 ] = options.option1value
   		options['"' .. option1 .. '"'] = options.option1value
   		--mw.log( "option1: " .. option1 .. "value: " .. options.option1value  )
   		end
    if type(LuaClaims) == "table" then -- f?r de fall d?r funktionen anropas och alla claims redan finns i en tabell
		claims = LuaClaims[options.property] or {}
	else
    	--Get entity
    	local entity = nil
    	if options.entity and type( options.entity ) == "table" then
        	entity = options.entity
    	else
        	id = get_entityId( options )
            if isvalid(id) then
				local check = mw.ustring.match( id, "Q%d+" ) or mw.ustring.match( id, "P%d+" )
				if check == nil then
					mw.addWarning(id .. " لا يمثل معرف ويكي بيانات صحيح")
					return ""
				else
					options.entityId = id
					options.qid = id
				end
            end
        	entity = getEntityFromId( id )
    	end
    	--local property = mw.wikibase.resolvePropertyId( options.property:upper() )
    	local property = options.property:upper()
    	--property = options.property:upper()

    	if not entity then return "" end --TODO error?
    	if not entity.claims or not entity.claims[property] then
    		if isvalid(options.otherproperty) then
	    		options.property = options.otherproperty
	    		property = options.otherproperty
	    	end
    	end
    	if not entity.claims or not entity.claims[property] then
        	return "" --TODO error?
    	end

    	--Format statement and concat them cleanly
		if options.rank == "best" or not options.rank then
			--claims = entity:getAllStatements( property )
			claims = entity:getBestStatements( property )
			elseif options.rank == "valid" then
				for i, statement in pairs( entity.claims[ property ] ) do
		    		if statement.rank == "preferred" or statement.rank == "normal" then
	    				table.insert( claims, statement )
	    			end
				end
			elseif options.rank == "all" then
				for i, statement in pairs( entity.claims[property] ) do
					table.insert( claims, statement )
				end
		else
			for i, statement in pairs( entity.claims[property] ) do
				if statement.rank == options.rank then
					table.insert( claims, statement )
				end
			end
		end
	end
	claims = filter_claims( claims, options)
	local statementsraw = {}
	local All_claims = claims
	if claims then

		if options["property-module"] or options["property-function"] then
			if not options["property-module"] or not options["property-function"] then
				return formatError( "unknown-property-module" )
			end
			local formatter = require ("Module:" .. options["property-module"])
			if not formatter then
				return formatError( "property-module-not-found" )
			end
			local fun = formatter[options["property-function"]]
			if not fun then
				return formatError( "property-function-not-found" )
			end

			mw.log("work with property-module: " .. options["property-module"] .. "|" .. options["property-function"])
			return fun( claims, options )
		else

			for i, statement in pairs( claims ) do
				options.num = i
				local va = formatOneStatement( statement, LuaClaims, options )
				if va.v then
					table.insert( valuetable, va.v )
				end
				table.insert( statementsraw, va.raw )
			end
		end
	end

	if isvalid(options.raw) then
		if isvalid(options.rawtolua) then
			return mw.getCurrentFrame():extensionTag("source", mw.dumpObject(statementsraw), {lang = "lua"})
		end
		return statementsraw
	end

	local priff = ""
	local Separator = options.separator-- or ""
	local Conjunction = options.conjunction-- or ""


	if Separator == "br" or Conjunction == "br" then
		Separator = mw.text.tag("br")
		Conjunction = mw.text.tag("br")
	end
	if Separator == "*" then
		priff = "\n*"
		Separator = "\n*"
		Conjunction = "\n*"
	end
	if Separator == "#" then
		priff = "\n#"
		Separator = "\n#"
		Conjunction = "\n#"
	end
	if isvalid(options.justref) then 
		priff = ""
		Separator = ""
		Conjunction = ""
	end
	local tot = mw.text.listToText( valuetable, Separator, Conjunction )
	if #valuetable > 1 then
		tot = priff .. tot
	end

	if tot == "" then tot = nil end
	if isvalid(options.returnnumberofvalues) then
		return tot, #valuetable
	end
	if isvalid(options.numberofclaims) then
		return #All_claims
	end
	return tot
end

function p.formatAndCat(args)
	if args then Frame_args = args end
	if not args then return nil end
	args.linkback = args.linkback or true
	args.addcat = true
	if isvalid(args.value) then
		if args.value == "-" then return nil end
		local val = args.value .. addTrackingCategory(args) --catewikidatainfo({property=args.property})
		val = p.addLinkBack(val, args.entity, args.property)
		return val
	end
	return p.formatStatementsFromLua( args )
end

function formatReferences( statement, options )
	local ic = 1
	local s
	local statementreferences = statement.references
	local reference = {}
	local numberofref = tonumber(options.numberofreferences) or 7
	if statementreferences then
		if Modulecite == nil then
			Modulecite = require(citetitle)
		end
		for i, ref in pairs( statementreferences ) do
			if ref.snaks then
				if numberofref >= ic then
					s = Modulecite.citeitem2(ref, options )
					if isvalid(s) then
						ic = ic + 1
						table.insert(reference, s)
					end
				end
			end
		end
	end
	local final = table.concat(reference)
	if isvalid(final) then
		final = final .. i18n.cateref
	end
	return final or ""
end

function formatqualifiers(statement, s, options)
	s.qualifiers = {}
	function qua(p, firstvalue, modifytime)
		if isvalid(p) then
			vvv = formatStatements({property=p, enlabelcate="t", firstvalue=(firstvalue or ""), modifytime=(modifytime or "longdate"), noref="t"}, statement.qualifiers) or ""
			s.qualifiers[p] = vvv
			return vvv
		end
	end

	if isvalid(options.template) then
		s.ID = statement.mainsnak.datavalue.value.id
		s.QQ1 = qua(options.Q1)
		s.QQ2 = qua(options.Q2)
		s.QQ3 = qua(options.Q3)
		s.QQ4 = qua(options.Q4)
		s.QQ5 = qua(options.Q5)
		s.QQ6 = qua(options.Q6)
		s.QQ7 = qua(options.Q7)
		s.QQ8 = qua(options.Q8)
		s.QQ9 = qua(options.Q10)
		s.QQ10 = qua(options.Q10)
	end
	if isvalid(options.football) then
		if statement.qualifiers.P1350 or statement.qualifiers.P1351 then
			s.amatch = qua("P1350","true")
			s.goal = qua("P1351","true")
		end
	end
	if (isvalid(options.football) ) or (isvalid(options.office)) then
		s.start1 = qua("P580","true")

		s.finish1 = qua("P582","true")
	end
	if isvalid(options.office) then
		if statement.qualifiers.P580 or statement.qualifiers.P582 or statement.qualifiers.P1365 or statement.qualifiers.P1366 then
			s.before1 = qua("P1365","true")
			s.after1 = qua("P1366","true")
			s.constituency1 = qua("P768")
			s.series1 = qua("P1545")
			s.electedin1 = qua("P2715","")
			s.pp1001 = qua("P1001")
			s.pp108 = qua("P108")
			s.pp642 = qua("P642")
		end
	end

	if isvalid(options.withdate) then
	--if statement.qualifiers.P585 then
		s.P585 = qua("P585","true",options.modifyqualifiertime)
	end
    local qwe = options.qwer
	if statement.qualifiers.qwe then
		s.ro = qua(qwe,"true")
	end
	local bothdates_option = options.withintervall or options.bothdates
	if isvalid(bothdates_option) then
		if statement.qualifiers.P580 or statement.qualifiers.P582 then
			local f = qua("P580","true",options.modifyqualifiertime)
			local t = qua("P582","true",options.modifyqualifiertime)
				s.tifr = f .. "–" .. t

		end
	end
	if isvalid(options.awardqua) then
		if statement.qualifiers.P585 or statement.qualifiers.P1346 then
			local fo = qua("P585","true",options.modifyqualifiertime)
			local to = qua("P1346","true")
			s.foto = fo .. " " .. mw.text.tag("span", {}, " " .. to  .. "")
		end
	end
	function quaaal(opti,options)
		if isvalid(opti) and statement.qualifiers[opti] then
			local kkk = formatStatements({property=opti, noref="t", separator=options.qualifierseparator, conjunction=options.qualifierconjunction, size=options.size, image=options.image, modifytime=options.modifyqualifiertime, enlabelcate="t", langpref=options.langpref, showlang=options.showlang}, statement.qualifiers) or ""

			s.qualifiers[ opti ] = kkk
			return kkk
		end
	end
	if isvalid(options.justthisqual) and statement.qualifiers[options.justthisqual] then
		s.onlyqualifier = quaaal(options.justthisqual,options)
	end
	if isvalid(options.qual1) and statement.qualifiers[options.qual1] then
		s.qp1 = quaaal(options.qual1,options)
	end
	if isvalid(options.qual1a) and statement.qualifiers[options.qual1a] then
		s.qp1a = quaaal(options.qual1a,options)
	end
	if isvalid(options.qual2) and statement.qualifiers[options.qual2] then
		s.qp2 = quaaal(options.qual2,options)
	end
	if isvalid(options.qual3) and statement.qualifiers[options.qual3] then
		s.qp3 = quaaal(options.qual3,options)
	end
	if isvalid(options.qual4) and statement.qualifiers[options.qual4] then
		s.qp4 = quaaal(options.qual4,options)
	end
	if isvalid(options.qual5) and statement.qualifiers[options.qual5] then
		s.qp5 = quaaal(options.qual5,options)
	end
end

function formatSnak( snak, options )
    if snak.snaktype == "somevalue" then
    	if isvalid(options.somevalue) then
			return {value=options.somevalue}
		end
		return {value=i18n["somevalue"]}

	elseif snak.snaktype == "novalue" then
    	if isvalid(options.novalue) then
			return {value=options.novalue}
    	end
        return {value=i18n["novalue"]}

    elseif snak.snaktype == "value" then
		local s = formatDatavalue( snak.datavalue, snak.datatype, options )
    	if s and s.value and isvalid(s.value) and isvalid(options.prefix) then
    		s.value = options.prefix .. s.value
    	end
    	if s and s.value and isvalid(s.value) and isvalid(options.suffix) then
    		s.value = s.value .. options.suffix
    	end
		return s
	else
		return {value=formatError( "unknown-snak-type" )}
	end
end

function formatStatement( statement, options )
    if options["claim-module"] or options["claim-function"] then
		if not options["claim-module"] or not options["claim-function"] then
            return {value=formatError( "unknown-claim-module" )}
        end
        local formatter = require ("Module:" .. options["claim-module"])
        if not formatter then
            return {value=formatError( "claim-module-not-found" )}
        end
        local fun = formatter[options["claim-function"]]
        if not fun then
            return {value=formatError( "claim-function-not-found" )}
        end
        return {value=fun( statement, options )}

    elseif statement.type == "statement" then
    	local s = formatSnak( statement.mainsnak, options )
		if isvalid(s) then
			s.qualifiers = {}
			if statement.qualifiers then
				qualu = formatqualifiers( statement, s, options )
				--if isvalid(qualu) then table.insert(qualu) end
			end
			if statement.references then
				if isvalid(options.reff) then
					s.reff = formatReferences( statement, options )
				end
			end
		end
		return s
	elseif not statement.type then
		return formatSnak( statement, options )
    end
	return {value=formatError( "unknown-claim-type" )}
end

function get_property1(options, item)
    --[[ function to get countries flags without reload large countries items ]]
	local flagprop = { "p27","p1532","p17","p495","p1376" }
	local work_flag = false
	if string.lower(options.property1) == "p41" then
		for k, l in pairs(flagprop) do
			if string.lower(options.property) == l then
				work_flag = true
			end
		end
	end
	caca = ""
	local size = options.size or ""
	if size == "" then size = "20" end
	if work_flag then
		if flags == nil then
			flags = require("Module:Wikidata2/Flags")
		end
		local flag = flags[item]
		if isvalid(flag) == nil then
			flag = formatStatements({property=options.property1, otherproperty=options.otherproperty1, entityId=item, rank=options.property1rank, pattern=options.property1pattern, formatting=options.property1formatting, noref="t", firstvalue="t"})
			--mw.log("get flag2 :" .. flag .. ", for item ".. item )
		end
		if isvalid(flag) then -- return real image
			if isvalid(options.image) then -- return real image
				caca = "[[file:" .. flag  .. "|".. size .."px|".."border".."]]"
				--caca = Infobox_Image( {image=flag, size=size, maxsize = "280x330px", center=options.center} )
				mw.log("get flag :" .. flag .. ", for item ".. item )
			end
		end
	end
	if caca == "" then
		caca = formatStatements({property=options.property1, otherproperty=options.otherproperty1, entityId=item, rank=options.property1rank, pattern=options.property1pattern, formatting=options.property1formatting, size=options.size, image=options.image, noref="t", firstvalue="t"
		})
	end
	return caca
end

function formatwikibaseitem( datavalue, datatype, options )
    --[[  datatype  wikibase-item   ]]
    local value
    local itemqid = datavalue.value.id
    local Skipped = skiip_items[options.property] or {}
    for k, v in pairs(Skipped) do
        if datavalue.value.id == v then
            return {value="", item=""}
        end
    end
    if isvalid(options.formatting) then
        if options.formatting == "raw" then
            return {value=itemqid, item=itemqid}

        elseif options.formatting == "rawtotemplate" and isvalid(options.rawtotemplate) then
            local args = {q = itemqid, no1=options.no1 or "", no2=options.no2 or ""}
            value = mw.getCurrentFrame():expandTemplate{ title = options.rawtotemplate, args = args } .. "\n"
			return {value=value, item=itemqid}

        elseif options.formatting == "fu" then
            value = mw.getCurrentFrame():expandTemplate{ title="Cycling race/stageclassification2", args={itemqid, type="1"}}
			return {value=value, item=itemqid}

        elseif options.formatting == "sitelink" then
            value = formatsitelink(datavalue.value.id, options)
			return {value=value, item=itemqid}
        else
            value = formatcharacters(datavalue.value, options)
            if isvalid(options.pattern) then
                value = formatFromPattern(value, options)
            end
			return {value=value, item=itemqid}
        end
	end

    local itemValue = formatEntityId( itemqid, options ).value
	if isvalid(itemValue) then
		if isvalid(options.property1) and options.property1:upper():sub(1,1) == "P" then
			local prop1value = get_property1(options, itemqid)
			if isvalid(prop1value) then
				prop1value = (options.property1pref or "") .."".. prop1value .."".. (options.property1suff or "")
				value = prop1value .. " ".. itemValue
				if isvalid(options.property1after) then
					value = itemValue .. prop1value
				end
			else
				value = itemValue
			end
			return {value=value, item=itemqid}

		elseif isvalid(options.propertyimage) then
			local p_f = options.propertyimageformatting or options.formattingpropertyimage
			local vas = formatStatements({
				property=options.propertyimage, formatting=p_f, entityId=itemqid, rank=options.rank, 
				pattern=options.pattern, size=options.size, image=options.image, noref="t", 
				avoidvalue=options.avoidvaluepropertyimage,  firstvalue="t", nolink=options.nolink
			})
			if isvalid(vas) then
				return {value=vas }
			end
		elseif isvalid(options.property2) then
			local caca = formatStatements({property=options.property2, entityId=itemqid, noref=options.noref, rank=options.rank, pattern=options.property2pattern, size=options.size, image=options.image, propertyimage=options.property3, firstvalue="t"
				})
			if isvalid(caca) then
				return {value=caca .. " ".. itemValue  }
			end
		end
	end
    return {value=itemValue, item = itemqid }
end

function formatwikibaseproperty( datavalue, datatype, options )
 --[[  datatype  wikibase-property   ]]
	local tid = ""
	if isvalid(options.formatting) then
		if options.formatting == "raw" then
			tid = datavalue.value.id
		end
	else
		tid = formatEntityId( datavalue.value.id, options ).value
	end
	return {value=tid}
end

function formattabulardata( datavalue, datatype, options )
 --[[  tabular-data ]]
	data= "[[commons:" .. datavalue.value .. "|" .. datavalue.value .. "]]"
	return {value=data}
end

function formatgeoshape( datavalue, datatype, options )
 --[[  geo-shape  ]]
	shape = "[[commons:" .. datavalue.value .. "|" .. datavalue.value .. "]]"
	return {value=shape}
end
function formatcommonsMedia( datavalue, datatype, options )

 --[[ commonsMedia ]]
	local size = options.size or ""
	if isvalid(options.image) then -- return real image
		--tid = "[[file:" .. datavalue.value  .. "|".. (options.size or "60").."px|".."border".."]]"
		params = {image=datavalue.value, maxsize = "280x330px", center=(options.center or "")}
		if isvalid(options.size) then params.size = size end
        if InfoboxImage == nil then
            InfoboxImage = require("Module:InfoboxImage")
        end
		tid = InfoboxImage.Infobox_Image( params )
		--tid = "[[file:" .. datavalue.value  .. "|".. (options.size or "20").."px|border]]"
		--if isvalid(options.center) then
			--tid = "[[file:" .. datavalue.value  .. "|".. (options.size or "20").."px|border|center]]"
		--end
	else
		tid = formatcharacters(datavalue.value, options)
	end
	return {value=tid}
end

function formatmath( datavalue, datatype, options )
 --[[datatype math ]]
		 --return	{value=mw.text.tag("math", {}, "".. datavalue.value.."") } -- that doesn't work well
	local result = mw.getCurrentFrame():callParserFunction( "#tag:math", datavalue.value )
	return	{value=result}
end

function formatstring( datavalue, datatype, options )
	--[[  datatype string ]]
	local par = options.pattern
	local result = formatcharacters(datavalue.value, options)
	local tid = result

	if isvalid(options.stringpattern) then
		tid = mw.ustring.gsub( options.stringpattern, "$1", datavalue.value )
	elseif isvalid(par) then
		if par ~= "autourl" and par ~= "autourl2" and par ~= "autourl3" and par ~= "autourl4" then
			tid = formatFromPattern( result, options )
		end
	end
	return {value=tid}
end

function formatexternalid( datavalue, datatype, options )
	local result = formatcharacters(datavalue.value, options)
	if isvalid(options.pattern) == nil then
		return {value=result }--just return value
	end
	local patter = formatStatements({property="P1630", entityId=options.property, firstvalue="t", noref="t", rank="all"}) -- get formatter URL

	local par = options.pattern
	local tid = result

	if isvalid(patter) then   -- if P1630 are there
		local pp = formatFromPattern( datavalue.value, {pattern=patter} )
		local plabel = mw.wikibase.label( options.property ) or pp
		local ppp = mw.ustring.gsub( pp, " ", "_" )

		local results = {
			["autourl"]  = ppp,                                             		-- like http://example.com/$1.html
			["autourl2"] = "[" .. ppp .. " " .. datavalue.value .. "]",     		-- like [http://example.com/$1.html $1]
			["autourl3"] = "[" .. ppp .. " " .. ppp .. "]",                 		-- like [http://example.com/$1.html http://example.com/$1.html]
			["autourl4"] = "[" .. ppp .. " " .. plabel .. "]",
		}
		if results[par] then
            tid = results[par]
		else
			tid = formatFromPattern( result, options )
		end
	elseif isvalid(par) then
		if par ~= "autourl" and par ~= "autourl2" and par ~= "autourl3" and par ~= "autourl4" then
			tid = formatFromPattern( result, options )
		end
	end
	return {value=tid}
end

function formattime( datavalue, datatype, options )
 --[[  datatype  time  ]]
        local ModuleTime = require "Module:wikidata2/time"
        local timen = datavalue.value
        local tid = ModuleTime.getdate( timen, options)
       -- local tid = mw.getCurrentFrame():preprocess(mall)
        if isvalid(options.modifytime) then
			if options.modifytime == "q" then
				local mall = datavalue.value.time
				tid = mw.getCurrentFrame():preprocess(mall)
			elseif options.modifytime == "precision" then
				local mall = datavalue.value.precision
				tid = mw.getCurrentFrame():preprocess(mall)
			end
		end
	return {value=tid}
end

function formatcoordinate( datavalue, datatype, options )
	--[[  datatype  globe-coordinate  ]]
	--local GlobeCoordinate = require "Module:GlobeCoordinate"
	--return {value=GlobeCoordinate.newFromWikidataValue( datavalue.value ):toHtml()}
    if ModuleGlobes == nil then
        ModuleGlobes = require("Module:Wikidata2/Globes")
    end
	local coord = datavalue.value
	local globe = datavalue.value.globe
	--local globe = globe:match("Q%d+")
	local globe2 = ModuleGlobes[globe] or ""
	local results = {
		["latitude"]  = coord.latitude,
		["longitude"] = coord.longitude,
		["dimension"] = coord.dimension,
		["precision"] = coord.precision,
		["globe"]	  = globe:match("Q%d+"),
		["globe2"] 	  = globe2,
	}
	local pro = ""
	if isvalid(options.formatting) then
		pro = results[options.formatting]
	else
		pro = mw.getCurrentFrame():preprocess("{{ {{{|safesubst:}}}#invoke:Coordinates|coord"
			.. "|".. coord.latitude
			.. "|".. coord.longitude
			.. "|display=inline"
			.. "|globe:".. globe2 .."_type:landmark"
			.. "|format="..(options.formatcoord or "").."}}"
			)..catewikidatainfo(options)
	end
	return {value=pro }
end

function formatquantity( datavalue, datatype, options )
--[[  datatype quantity  ]]
    local amount, unit = datavalue.value.amount, datavalue.value.unit
    amount = mw.ustring.gsub( amount, "+", "" )
    if unit then
        unit = unit:match("Q%d+")
    end
	if formatera == nil then
		formatera = require("Module:Wikidata2/Math")
	end
    local number = formatera.newFromWikidataValue(datavalue.value)
    local unitraw = unit
    if unit then
        -- يتحقق اذا كان هناك اي اختصار لوحدة القياس
        --if lab and ( isvalid(options.nounitshort) == nil ) then
        if isvalid(options.unitshort) then
            local lab = options.label or formatStatements({property="P498", entityId=unit, firstvalue="t", noref="t"}) or formatStatements({property="P5061", entityId=unit, firstvalue="t", langpref=options.langpref, noref="t"}) or ""
            local s = formatEntityId( unit, {label=lab , enlabelcate="t", nolink=(options.nounitlink or options.nolink) })
            unit = s.value
        else
            local s = formatEntityId( unit, {nolink=options.nounitlink, enlabelcate="t"})
            unit = s.value
        end

    end
	if options.formatcharacters and options.formatcharacters =="formatnum" then
		amount = make_format_num( amount )
	end
		local Value = amount .. " " .. (unit or "")
		if isvalid(options.nounit) then
			Value = amount
		end
		return {value=Value, amount = amount, unit = unit, unitraw = unitraw}
end

function formaturl( datavalue, datatype, options )
--[[  datatype  url  ]]
	local label = options.label
	if isvalid(options.urllabel) then
		label = options.urllabel
	end
	va = mw.ustring.gsub( datavalue.value, " ", "_" )
	if not label and options.property =="P856" then label = "الموقع الرسمي" end
	if isvalid(options.displayformat) == "weblink" then
        if weblink == nil then
            weblink = require("Module:Weblink")
        end
		return {value=weblink.makelink(va ) }
	end
	if isvalid(options.formatting) == "raw" then
		return {value=va}
	end
    local pro = va
    if isvalid(label) then
        pro = "[" .. va .. " " .. label .. "]"
    end
	return {value=pro}
end

function formatmonolingualtext( datavalue, datatype, options ) -- showlang
	local text = datavalue.value.text
	if isvalid(options.textformat) == "text" then
		return {value=text}
	end
    if Moduletext == nil then
        Moduletext = require "Module:wikidata2/monolingualtext"
    end
	local tid = Moduletext._main( datavalue, datatype, options )
	return {value=tid}
end

function formatDatavalue( datavalue, datatype, options )
   	--[[functions = {
   		["P172"] = {["value-module"] ="Wikidata2/P172" ,["value-function"] ="get_P172_lab"} ,
   		["P106"] = {["value-module"] ="Wikidata2/P106" ,["value-function"] ="formatEntityWithGenderClaim"} ,
   	}
   	functions["P413"] = functions["P106"]
   	if ( isvalid(options["value-module"]) == nil )
   			or ( isvalid(options["value-function"]) == nil )
   		or  ( isvalid(options["property-module"]) == nil )
   			or ( isvalid(options["property-function"]) == nil )
   		or  ( isvalid(options["claim-module"]) == nil )
   			or ( isvalid(options["claim-function"]) == nil )
   	then
	   	if functions[options.property:upper()] then
	   		options["value-module"] = functions[options.property:upper()]["value-module"]
	   		options["value-function"] = functions[options.property:upper()]["value-function"]
	   	end
   	end]]
    --Use the customize handler if provided
    if options["value-module"] or options["value-function"] then
        if not options["value-module"] or not options["value-function"] then
            return {value=formatError( "unknown-value-module" )}
        end
        local formatter = require ("Module:" .. options["value-module"])
        if not formatter then
            return {value=formatError( "value-module-not-found" )}
        end
        local fun = formatter[options["value-function"]]
        if not fun then
            return {value=formatError( "value-function-not-found" )}
        end
        return {value=fun( datavalue, datatype, options )}
    end

    --Default dataformatters
    local dataformatters = {
        ["wikibase-item"] 		= formatwikibaseitem,
        ["wikibase-property"]	= formatwikibaseproperty,
        ["commonsMedia"]		= formatcommonsMedia,
        ["math"]				= formatmath,
        ["time"] 				= formattime,
        ["external-id"] 		= formatexternalid,
        ["string"] 				= formatstring,
        ["globe-coordinate"] 	= formatcoordinate,
        ["quantity"] 			= formatquantity,
        ["url"] 				= formaturl,
        ["monolingualtext"] 	= formatmonolingualtext,
        ["geo-shape"] 			= formatgeoshape,
        ["tabular-data"] 		= formattabulardata,
    }

    local dataformatter = dataformatters[datatype]
    if not dataformatter then
        return {value=formatError( "unknown-data-type" )}
    end
    return dataformatter( datavalue, datatype, options )

end

function Labelfunction( qid, arlabel, options) -- label with no arwiki sitelink
	local value
	local cat = ""
	local use_en_label = isvalid(options.enlabelcate) or isvalid(options.use_en_labels)
	local Args = { fromlua="t", ["المعرف"] = qid }
	if isvalid(options.illwd2noy) then Args.noy="t" end
	if isvalid(options.illwd2y) then Args.y="t" end

	if isvalid(arlabel) then
		value = arlabel
		if isvalid(options.illwd2) then
			Args.label = arlabel
			value = mw.getCurrentFrame():expandTemplate{ title = "Ill-WD2", args = Args}
		end
	elseif isvalid(options.justarabic) == nil then
	-- else
		--cat = " [[".. i18n.noarabiclabel .."|".. qid .."]]"
		en_label = mw.wikibase.label( qid ) or ""
        if isvalid(options.illwd2noarlabel) then
            if isvalid(en_label) then
                Args.enlabel = en_label
            end
            value = mw.getCurrentFrame():expandTemplate{ title = "Ill-WD2", args = Args}
        elseif use_en_label and isvalid(en_label) then
            value = en_label
		end
	end
	return {value=value or "", cat = cat }
end

function formatEntityId( qid, options )
	local labeloption = options.label
	local label = ""
	local value
	local arlabel = labelIn("ar", qid ) or "" -- The arabic label
    local link = mw.wikibase.sitelink( qid )

	if isvalid(labeloption) then
		label = labeloption
	elseif isvalid(arlabel) then
		--mw.log("arlabel" .. arlabel)
		label = arlabel

	elseif isvalid(link) then
		label = link
		arlabel = link
	end

    if isvalid(link) then
		local linklabel = isvalid(label) or link
		if (isvalid(options.nolink) == nil) then
			value = "[[:" .. link .. "|" .. formatcharacters(linklabel, options) .. "]]"
			label = linklabel
		else
			value = formatcharacters(linklabel, options)
			label = linklabel
		end
    -- elseif isvalid(arlabel) then
	else
		local va = Labelfunction( qid, arlabel, options)
		label = va.value
		value = va.value-- .. va.cat
    end
	return {value=value or "", label=label or "" }
end

function sitelink_g( id, wikisite )
	--[[ function to get any link from any sister project ]]
	local site = wikisite or "arwiki"
	local link = mw.wikibase.sitelink( id, site ) or ""
	--mw.log("mw.wikibase.sitelink,site: " .. site.. ",link:" .. link )
	return link
end

function sitelink( id, wikisite )
	local site = wikisite or "arwiki"
	local link = ""
	--local link = mw.wikibase.getSitelink( id, site ) or ""

	local entity = mw.wikibase.getEntityObject(id)
	if entity and entity.sitelinks
		and entity.sitelinks[""..site..""]
		and entity.sitelinks[""..site..""].site
		and entity.sitelinks[""..site..""].title then
			if entity.sitelinks[""..site..""].site == site then
				link = entity.sitelinks[""..site..""].title
			end
	end
	return link
end

function formatsitelink( entityId, options )
 --[[ function to get only the value with link ]]
    local link = sitelink( entityId )
    if isvalid(link) and isvalid(options.nolink) == nil then
		return "[[" .. link .. "]]".. catewikidatainfo(options)
	end
	return link
end

function formatFromPattern( str, options )
 -- [[  function to replace $1 with string  ]]
	if isvalid(options.pattern) then
		str = string.gsub( str, "%%", "%%%%" )
		str = mw.ustring.gsub( options.pattern, "$1", str )
	end
	return str
end

function p.getEntity( id )
	if type(id) == "table" then return id end
	return getEntityFromId(id)
end

function p.translate(str, rep1, rep2)
	str = i18n[str] or str
	if rep1 and (type (rep1) == "string") then
		str = str:gsub("$1", rep1)
	end
	if rep2 and (type (rep2) == "string")then
		str = str:gsub("$2", rep2)
	end
	return str
end

function p.getId(snak)
	if (snak.snaktype == "value") then
		if snak.datavalue.type == "wikibase-entityid" then
			return "Q" .. snak.datavalue.value["numeric-id"]
		end
	end
end

function p.addLinkBack(str, id, property)
	if not id then id = p.getEntity() end
	if not id then return str end
	if type(property) == "table" then property = property[1] end
	if type(id) == "table" then id = id.id end
	local class = ""
	if property then
		class = "wd_" .. string.lower(property)
	end
	local icon = "[[File:Blue pencil.svg|%s|10px|baseline|class=noviewer|link=%s]]"
	local title = i18n["see-wikidata-value"]
	local url = mw.uri.fullUrl("d:" .. id, "uselang=ar")
	url.fragment = property
	url = tostring(url)
	local v = mw.html.create("span")
		:addClass(class)
		:wikitext(str)
		:tag("span")
			:addClass("noprint wikidata-linkback")
			:css("padding-left", "0.5em")
			:wikitext(icon:format(title, url))
		:allDone()
	return tostring(v)
end

-- Function to check whether a certain item is a parent of a given item.
-- If pExitItem is reached without finding the searched parent item, the search stops.
-- A parent is connected via P31 or P279.
-- Attention: very intensive function, use carefully!
function isParent(property, pItem, pParent, pExitItem, pMaxDepth, pDepth)
	if not pDepth then pDepth = 0 end

	local entity = mw.wikibase.getEntity(pItem)
	if not entity then return false end
	local claims31
	local claims279
	if entity.claims then
		if isvalid(property) then
			claims31 = entity.claims[property]
		else
			claims31 = entity.claims["P31"]
			claims279 = entity.claims["P279"]
		end
	else
		return false
	end
	if not claims31 and not claims279 then return false end
	local parentIds = {}
		if claims31 and #claims31 > 0 then
			for i, v in ipairs(claims31) do parentIds[#parentIds+1] = p.getId(v.mainsnak) end
		end
		if claims279 and #claims279 > 0 then
			for i, v in ipairs(claims279) do parentIds[#parentIds+1] = p.getId(v.mainsnak) end
		end
	-- check if searched parent or exit item is reached or do recursive call
	if not parentIds[1] or #parentIds == 0 then return false end
	local itemString = ""
	local result = nil
	for i, qid in ipairs(parentIds) do
		if not qid then return false end
		--itemString = "Q" .. v
		itemString = qid
		if itemString == pParent then
			-- successful!
			return true
		elseif itemString == pExitItem then --or itemString == "Q35120"
			-- exit if either "exit item" or node item (Q35120) is reached
			return false
		else
			if pDepth+1 < pMaxDepth then
				result = isParent(property, itemString, pParent, pExitItem, pMaxDepth, pDepth+1)
			else return false end

			if result == true then return result end
		end
	end
	do return false end
end

function p.Subclass(options)
	if options then Frame_args = options end
	local parent = options.parent or ""
	local id = options.id or ""
	local Entity = getEntityFromId(id)
	if Entity then id = Entity.id end
	local property = options.property or "P31"
	if parent == "" or id == "" or property == "" then return false end
	local tab = mw.text.split(options.parent,",")
	local result = mw.wikibase.getReferencedEntityId( id, property, tab  ) -- { "Q5", "Q2095" } )
	if not result and property == "P31" then
		result = mw.wikibase.getReferencedEntityId( id, "P279", tab  )
	end
	if result then return true end
end

function old_isSubclass(options)
	if not options.parent then return false end
	local maxDepth
	maxDepth = tonumber(options.maxDepth) or 10
	if not type(maxDepth) == "number" then maxDepth = 5 end
	property = options.property
	local result
	result = isParent(property, options.id, options.parent, options.exitItem, maxDepth)
	if options.returnInt then
		if result == true then return 1 else return nil end
	else
		return result
	end
end

function p.formatSnak( snak, options )
    return formatSnak( snak, options )
end

function p.formatEntityId( entityId, options )
	return formatEntityId( entityId, (options or {}) )
end

function p.formatStatements( frame, key )
	if frame.args then
		if type(key)  == "table" and key ~= {} then else Frame_args = frame.args end
	end
 	--[[ The main function ]]
    local args = frame.args
    --If a value if already set, use it
    if isvalid(args.value) then
        return args.value
    end
    local wd_arg = frame:getParent().args["ويكي بيانات"] or frame.args["ويكي بيانات"]  -- arg used to ban wikidata value
    if isvalid(wd_arg) == "لا" then
        return ""
    end

    local valuesnumb = 0
    local prop = formatStatements( args, key )
    if isvalid(args.returnnumberofvalues) then
        s, valuesnumb = formatStatements(  frame.args, key )
    end
	if isvalid(prop) then
    	if isvalid(args.mainprefix) then -- mainprefix
    		prop = args.mainprefix .. prop
    	end
    	if isvalid(args.mainsuffix) then  -- mainsuffix
    		prop = prop .. args.mainsuffix
    	end
		if isvalid(args.addTrackingCat) then   -- add tracking cat
		prop = prop .. addTrackingCategory(frame.args)
		end
		if isvalid(args.mainsuffixAfterIcon) then   -- another suffix but after wikidata icon
			prop = prop .. args.mainsuffixAfterIcon
		end
	else
    	if isvalid(args.NoPropValue) then -- value if no local value and no wikidata value
    		prop = args.NoPropValue
    	end
	end
    if isvalid(args.returnnumberofvalues) then
		mw.log( "valuesnumb: " .. valuesnumb )
        return s, valuesnumb
    end
    return prop
end

function p.formatStatementsFromLua( options, key ) --  main function but to use from lua module
	if options then if type(key) == "table" and key ~= {} then else Frame_args = options end end

    --If a value if already set, use it
    if isvalid(options.value) then
        return options.value
    end
    local valuesnumb = 0
    local s = formatStatements( options, key )
    if isvalid(options.returnnumberofvalues) then
        s, valuesnumb = formatStatements( options, key )
    end
    if s == "" then
    	s = nil
    end
	if isvalid(s) then
    	if isvalid(options.mainprefix) then -- mainprefix
    		s = options.mainprefix .. s
    	end
    	if isvalid(options.mainsuffix) then  -- mainsuffix
    		s = s .. options.mainsuffix
    	end
		if isvalid(options.addTrackingCat) then   -- add tracking cat
			s = s .. addTrackingCategory(options)
    	end
		if isvalid(options.mainsuffixAfterIcon) then   -- another suffix but after wikidata icon
		s = s .. options.mainsuffixAfterIcon
		end
	else
    	if isvalid(options.NosValue) then -- value if no local value and no wikidata value
    		s = options.NosValue
    	end
	end
    if isvalid(options.returnnumberofvalues) then
	--	mw.log( "valuesnumb: " .. valuesnumb )
        return s, valuesnumb
    end
    return s
end

function p.isSubclass(frame)
	return p.Subclass(frame.args)
end

-- Return the site link for a given data item and a given site (the current site by default)

function p.getSiteLink( frame )
	local site = frame.args[2] or frame.args.site
	local id = frame.args[1] or frame.args.id
	local count = frame.args.countsitelinks
	if isvalid(id) == nil then
		if isvalid(frame.args.page) then
			id = mw.wikibase.getEntityIdForTitle( frame.args.page )
		end
	end
	if isvalid(count) then
		return countSiteLinks(id)
	end
    local link = sitelink( id, site )
    if isvalid(link) then
        return link
	end
end

function p.getSiteLink1(frame)
	local project = frame.args[1]
	local id = frame.args[2]
	local link = sitelink( id, project )
	var = link
	return var
end

-- returns the page id (Q...) of the current page or nothing of the page is not connected to Wikidata
function p.pageId(frame)
	return  mw.wikibase.getEntityIdForCurrentPage()
end

function p.descriptionIn(frame)
	local langcode = frame.args[1] or frame.args["lang"]
	local id = frame.args[2] or frame.args["id"]
	return descriptionIn( langcode ,id  )
end

function p.labelIn(frame)
	local langcode = frame.args[1] or frame.args["lang"]
	local id = frame.args[2] or frame.args["id"]
	return labelIn( langcode, id )
end

function p.getLabel( entity, lang )
     return labelIn( lang, entity )
end

function p.ViewSomething(frame)   -- from en:Module:Wikidata

	local f = (frame.args[1] or frame.args.id) and frame or frame:getParent()
          if isvalid(f.args.id) then aa = f.args.id end
	local data = mw.wikibase.getEntityObject(aa)
	if not data then
		return nil
	end
	local i = 1
	while true do
		local index = f.args[i]
		if not index then
			if type(data) == "table" then
				return mw.text.jsonEncode(data, mw.text.JSON_PRESERVE_KEYS + mw.text.JSON_PRETTY)
			else
				return tostring(data)
			end
		end
		data = data[index] or data[tonumber(index)]
		if not data then
			return
		end
		i = i + 1
	end
end

function p.Dump(frame)
	local f = (frame.args[1] or frame.args.id) and frame or frame:getParent()
	if isvalid(f.args.id) then aa = f.args.id end
	local data = mw.wikibase.getEntityObject(aa)
	if not data then
		return i18n.warnDump
	end
	local i = 1
	while true do
		local index = f.args[i]
		if not index then
			return frame:extensionTag("source", mw.dumpObject(data),{ lang= "lua"}).. i18n.warnDump
		end
		data = data[index] or data[tonumber(index)]
		if not data then
			return i18n.warnDump
		end
		i = i + 1
	end
end

function p.countSiteLinks(id)
	return countSiteLinks(id)
end

function p.EntityIdForTitle(frame)
	local title = frame.args[1]
	local str = mw.wikibase.getEntityIdForTitle( title )
	--mw.log(str)
	return str
end

function p.Qidfortitleandwiki(frame)
	local title = frame.args[1]
	local wiki = frame.args[2]
	local str = mw.wikibase.getEntityIdForTitle( title, wiki )
	return str
end

return p