واجهة برمجة التطبيقات:عرض معلومات الصفحة في نتائج البحث

From mediawiki.org
This page is a translated version of the page API:Page info in search results and the translation is 51% complete.

مقدمة

يعرض هذا نتائج واجهة برمجة التطبيقات التي تستخدمها نتائج البحث في عرض معلومات إضافية عن المقالات، من بينها صورة رئيسية بيان بموضوع المقالة مأخوذ من ويكي بيانات.

عرض معلومات مفيدة عن الصفحة

نتائج البحث في تطبيق ويكيبيديا لأجهزة أندرويد

حينما تبحث في تطبيقات ويكيبيديا للأجهزة المحمولة، تعرض هذه بينما تكتب ما تبحث عنه قائمة منسدلة بالصفحات التي تطابق ما تكتبه. كما أنها تعرض صورة رئيسية للمقالة وبيان بها.

تجيء الصورة الرئيسية من Extension:PageImages الذي يضيف خاصية page_image إلى صفحات والذي يخمّن ما هي الصورة الملائمة للصفحة. أما البيان فهو مأخوذ من ويكي بيانات، التي تصون بيان باللغة الهدف لموضوع كل صفحة ويكي.

أحد السبل البطيئة لتحقيق هذا هو الاستفسار عن الصفحات التي تطابق ما يكتبه المستخدم، ومن ثم إرسال طلب واجهة برمجة تطبيقات من نوع action=query لأغراض خاصية pageimages لمجموعة من العناوين ومن ثم إرسال استفسار واجهة برمجة تطبيقات آخر على wikidata.org يطلب بيانها من ويكي بيانات. هذا الأمر ممكن إلا أنه يتطلب إجراء عدة استفسارات عبر واجهة برمجة التطبيقات.

كيف يعمل الأمر على مواقع الويكي التابعة لويكيميديا

بدلًا من ذلك، غيرت مؤسسة ويكيميديا أغلب مواقع الويكي (إلا أنه كما هو في مايو 2015 لا ينطبق على موقع www.mediawiki.org) كي تحمّل امتداد عميل ويكي قاعدة البيانات للوصول إلى ويكي بيانات. هذا الأمر يسمح لك بالاستعلام عن ‎prop=pageterms مع ‎prop=pageimages على موقع الويكي المصدر، عوضًا عن إرسال طلب استعلام ثاني إلى ‎www.wikidata.org.

مثال: استعلام بسيط عن خصائص pageimages و pageterms لصفحة Albert Einstein:

النتيجة
{
    "query": {
        "pages": [
            {
                "pageid": 736,
                "ns": 0,
                "title": "Albert Einstein",
                "thumbnail": {
                    "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Einstein_1921_by_F_Schmutzer_-_restoration.jpg/38px-Einstein_1921_by_F_Schmutzer_-_restoration.jpg",
                    "width": 38,
                    "height": 50
                },
                "pageimage": "Einstein_1921_by_F_Schmutzer_-_restoration.jpg",
                "terms": {
                    "alias": [
                        "Einstein"
                    ],
                    "description": [
                        "German-American physicist and founder of the theory of relativity"
                    ],
                    "label": [
                        "Albert Einstein"
                    ]
                }
            }
        ]
    }
}
يجب عليك دائمًا استخدام ‎formatversion=2 في طلبات استعلام واجهة برمجة التطبيقات لو كنت تطلب نتائج بصيغة جيسون. سوف تجلب هذه الصيغة النتائج في تنظيم يسهل معالجته ويستخدم ترميز UTF8 بصفة افتراضية.

لو كانت لديك مجموعة من عناوين الصفحات، يمكنك أن تطلب معلومات هذه الصفحات مرة واحدة. اضبط pilimit لتصبح عدد العناوين التي تستعلم عنها وإلا سوف يجلب لك هذا الاستعلام صورة مصغرة واحدة فقط، من أول مقالة مذكورة في المجموعة التي توجد فيها صورة ممكنة. يجب عليك أيضًا أن تقلل حجم رد واجهة برمجة التطبيقات عن طريق تحديد الخصائص التي تريد لوحدات واجهة برمجة التطبيقات البرمجية أن تقدمها لك لا غير، في هذه الحالة هي الصور المصغرة وبيان ويكي بيانات. ربما ترغب في النهاية من الاستعلام التعامل مع الصفحات التي هي صفحات إعادة توجيه.

مثال: استعلام عن المصغرات في pageterms و pageimage في عدة صفحات

النتيجة
{
    "query": {
        "pages": [
            {
                "pageid": 736,
                "ns": 0,
                "title": "Albert Einstein",
                "thumbnail": {
                    "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Einstein_1921_by_F_Schmutzer_-_restoration.jpg/38px-Einstein_1921_by_F_Schmutzer_-_restoration.jpg",
                    "width": 38,
                    "height": 50
                },
                "terms": {
                    "description": [
                        "German-American physicist and founder of the theory of relativity"
                    ]
                }
            },
            {
                "pageid": 243597,
                "ns": 0,
                "title": "Albert Ellis",
                "thumbnail": {
                    "source": "https://upload.wikimedia.org/wikipedia/en/thumb/3/3e/Albert_Ellis.jpg/50px-Albert_Ellis.jpg",
                    "width": 50,
                    "height": 33
                },
                "terms": {
                    "description": [
                        "American psychologist"
                    ]
                }
            },
            {
                "pageid": 4463732,
                "ns": 0,
                "title": "Albert Estopinal",
                "thumbnail": {
                    "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/EstopinalOfLouisiana.jpg/35px-EstopinalOfLouisiana.jpg",
                    "width": 35,
                    "height": 50
                },
                "terms": {
                    "description": [
                        "American politician"
                    ]
                }
            }
        ]
    }
}

Querying query results in one request

The above example is incomplete, since the set of page titles whose properties we are querying – Albert Einstein|Albert Ellis|Albert Estopinal – must have come from another query.

In many situations you can combine getting the page properties you want with the initial query for a set of pages, using the MediaWiki API's generator feature. The list of pages from the generator become the set of pages for the other part of the query, all in a single API request. The MediaWiki API's query module has a prefixsearch submodule that queries for a list of pages starting with the prefix you specify ("Albert Ei"), and list queries can act as a generator. The MobileFrontend extension and mobile apps do this, If you look at MobileFrontend's API query in SearchApi.js, you can see it combines the generator=prefixsearch with a query for the pageimages property. We can do the same, asking for the Wikidata description as well with prop=pageimages|pageterms.

Example: A query feeding a prefixsearch generator into useful property queries.


The prefixsearch generator provides an index for each page in the pages array in the result; you can use this to sort the page titles, each with its thumbnail image and description, in the correct order.

You used to have to append a separate list=prefixsearch query to get the titles in the correct order (phab:T98125).

Further niceties

If the set of articles that start with what the user types does not fill the search results list, the Wikimedia mobile apps go on to search for in-page matches that you would get from Special:Search. The Wikipedia Android and iOS mobile apps combines generator=prefixsearch with querying for the pageterms and pageimages properties and getting a list of search terms. From its implementation file:

	 @"action": @"query",
	 @"generator": @"prefixsearch",
	 @"gpssearch": self.searchTerm,
	 @"gpsnamespace": @0,
	 @"gpslimit": @(SEARCH_MAX_RESULTS),
	 @"prop": @"pageterms|pageimages",
	 @"piprop": @"thumbnail",
	 @"wbptterms": @"description",
	 @"pithumbsize" : @(SEARCH_THUMBNAIL_WIDTH),
	 @"pilimit": @(SEARCH_MAX_RESULTS),
	 // -- Parameters causing prefix search to efficiently return suggestion.
	 @"list": @"search",
	 @"srsearch": self.searchTerm,
	 @"srnamespace": @0,
	 @"srwhat": @"text",
	 @"srinfo": @"suggestion",
	 @"srprop": @"",
	 @"sroffset": @0,
	 @"srlimit": @1,


Going beyond

The Wikipedia iOS mobile app uses face detection to detect the focal region of the image!

Alternatives

As an alternative, the Popups extension behind the Hovercards beta feature uses the extracts query submodule of the TextExtracts extension to show two sentences from the lead text of an article from the local wiki (together with its image, "last edited", etc.) when you hover over a link. This text from the local wiki is usually longer and less definitive than the Wikidata description. Its API request is in resources/ext.popups.renderer.article.js

Example: Sample Hovercards API request including textextracts properties of Albert Einstein:

النتيجة
{
    "query": {
        "pages": [
            {
                "pageid": 736,
                "ns": 0,
                "title": "Albert Einstein",
                "extract": "Albert Einstein (/ˈælbərt ˈaɪnʃtaɪn/; German: [ˈalbɐrt ˈaɪnʃtaɪn]; 14 March 1879 – 18 April 1955) was a German-born theoretical physicist. He developed the general theory of relativity, one of the two pillars of modern physics (alongside quantum mechanics).",
                "thumbnail": {
                    "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Einstein_1921_by_F_Schmutzer_-_restoration.jpg/228px-Einstein_1921_by_F_Schmutzer_-_restoration.jpg",
                    "width": 228,
                    "height": 300
                },
                "revisions": [
                    {
                        "timestamp": "2015-06-24T12:17:17Z"
                    }
                ]
            }
        ]
    }
}

The query returns an array of pages, if successful this will have one element, the single matching page. This also requests the last-changed timestamp (`prop=revisions&rvprop=timestamp`) to display "Edited N days/hours ago."

Next steps

Try these API requests in the Special:ApiSandbox page, then make the same API requests from your own applications.


انظر أيضا