aboutsummaryrefslogtreecommitdiff
path: root/files/ja/orphaned/mdn/tools/put_api/index.html
blob: 6e05522016b1ac22a92387efdda7ca3478480939 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
---
title: PUT API
slug: MDN/Tools/PUT_API
tags:
  - Advanced
  - Automation
  - Documentation
  - Draft
  - Guide
  - MDN Meta
  - PUT API
  - Page-level
  - Tools
translation_of: MDN/Tools/PUT_API
---
<div>{{MDNSidebar}}</div>

<div> {{draft}}</div>

<p><span class="seoSummary">MDN Wiki はページの全体、または一部の更新のための<strong>実験的な</strong> <a href="/ja/docs/Web/HTTP">HTTP</a> PUT API を提供しています。</span>この機能は、次のような時に便利です:</p>

<ul>
 <li>プロジェクトのページを作成し、自動ビルド、テスト、デプロイスクリプトからある節のコンテンツを更新することができる。これはコミュニティがプロジェクトの進捗の最新についていくのに役立ちます。</li>
 <li>ソースコードと並行してドキュメントを提供する場合、HTML をプッシュして MDN のサブセクションにレンダリングできます。これでチームの作業フローにとって適切な方法でドキュメントを維持できて、MDN に貢献したりローカライズ担当がコンテンツを翻訳できるままになります。</li>
</ul>

<h2 id="Testing_your_application" name="Testing_your_application">アプリケーションをテストする</h2>

<p>MDN を実行するソフトウェアを開発する中で、次のようにいろいろなステージ上のサイトインスタンスをホストしています:</p>

<ul>
 <li>Production (<a href="http://developer.mozilla.org/" title="http://developer.mozilla.org/">http://developer.mozilla.org/</a>), the real site with stable code and where changes to content matter.</li>
 <li>Staging (<a href="http://developer.allizom.org/" title="http://developer.allizom.org/">http://developer.allizom.org/</a>), a version of the site where changes are throwaway and upcoming features are tested.</li>
 <li>Development (<a href="http://developer-dev.allizom.org/" title="http://developer-dev.allizom.org/">http://developer-dev.allizom.org/</a>), a version of the site running the absolute latest and untested code.</li>
</ul>

<p>プロダクションサイトを無駄にしないようにするには、まずステージングに対してアプリケーションを開発する必要があります。それから、あなたが望むことを合理的に実行することができたら、それをプロダクションに反映するように再構成します。また、開発に取り組むこともできますが、問題が発生する可能性があります。</p>

<h2 id="Creating_an_API_key" name="Creating_an_API_key">API キーを作成する</h2>

<p>APIキーを使うと、毎回 Persona ログインするような介入を要求せずに、アプリケーションを代理人として動作させることができます。SSL 上の HTTP ベーシック認証を使ってユーザー名とパスワードを提供します。基本的な使用のトラッキングを集めて、どのように使われているかがわかるようにします。そして、たまたま持つべきでない人々に渡った場合は、アクセスを無効にするよう API キーを削除できます。</p>

<p>If you have the correct privileges to do so, to create an API key, sign into MDN and <a href="https://developer.mozilla.org/ja/users/account/keys" title="https://developer.mozilla.org/ja/users/account/keys">visit the API keys management page</a>. This page lets you create and delete API keys, as well as inspect recent usage history.  Only Mozillians in good standing can currently get API keys, since they grant abilities to automate changes to content rapidly, so unprivileged users must request the ability by filing a bug.</p>

<p>{{NoteStart}}The above link goes to the Production site, and the same keys do not work between Production and Staging. You can also get to this page by visiting your profile on the respective site: Click on your username in the upper right of the site. On your profile page, you should see a "Manage API Keys" button.{{NoteEnd}}</p>

<p>From there, clicking on the "<a href="https://developer.mozilla.org/ja/users/account/keys/new" title="https://developer.mozilla.org/ja/users/account/keys/new">Create a new API key</a>" button should take you to an entry form so you can submit a request for an API key.</p>

<p>After filling out and submitting the form, you will receive a key ID and secret. These are your username and password, respectively. Copy these down somewhere safe (eg. to your application's configuration settings); the site will never display them again, and there is no recovery method. If you lose them, simply delete the API key and create another.</p>

<h2 id="Making_a_PUT_request" name="Making_a_PUT_request">PUT リクエストを作成する</h2>

<p>Since the PUT API works by way of HTTP, it should be compatible with the application environment and libraries of your choice. This first example uses <a href="http://curl.haxx.se/" title="http://curl.haxx.se/">the command-line tool cURL</a> and a UNIX shell to demonstrate how to issue a simple PUT request to MDN.</p>

<h3 id="Request" name="Request">リクエスト</h3>

<pre class="brush: bash notranslate"># Base URL and API key from staging (example only; substitute your own)
MDN_BASE_URL="https://developer.allizom.org"
MDN_KEY_ID="frsNFFR3w0yEALRE9IA9oN1KwoDno8vVGrzsBNvCofI"
MDN_SECRET="423PdCvnvraH0FkCDTKnizTmKGNkEdgQTi6RlEFTiWs"

# Document-specific details
DOC_USERNAME="lmorchard"  # Change this to your name
DOC_PATH=/ja//docs/User:$DOC_USERNAME/PutExample"
DOC_TYPE="text/html"
DOC_DATA="&lt;b&gt;HELLO WORLD&lt;/b&gt;"

# Putting it all together...
curl -si -X PUT -H"Content-Type: $DOC_TYPE" -d"$DOC_DATA" -u"$MDN_KEY_ID:$MDN_SECRET" "$MDN_BASE_URL$DOC_PATH"</pre>

<p>Since there's a lot going on in this cURL invocation, the example is broken into variables:</p>

<ul>
 <li><code>MDN_BASE_URL</code> - as mentioned before, you should plan to switch your application between staging and production servers on MDN. This variable allows for that.</li>
 <li><code>MDN_KEY_ID</code> - the key ID from the API key you created. Note that these are server-specific - the same keys do not work between staging and production.</li>
 <li><code>MDN_SECRET</code> - the secret from the API key that corresponds with the key ID.</li>
 <li><code>DOC_USERNAME</code> - change this to your MDN username.</li>
 <li><code>DOC_PATH</code> - the URL path to the document with content you want to manipulate.</li>
 <li><code>DOC_TYPE</code> - the content in the request will be <code>text/html</code></li>
 <li><code>DOC_DATA</code> - the content sent in the PUT request body; this is the content that will be used in a new revision to the document</li>
</ul>

<p>So, along with the variables, here are some general notes on the example and its use of the PUT API:</p>

<ul>
 <li>The key ID and secret are supplied as username and password, respectively, in HTTP Basic authentication over SSL.</li>
 <li>The <code>DOC_PATH</code> for this example includes a username - presumably yours - but that's just for the sake of example and ensuring you have your own sample document to play with. You can use any URL path to any document on the wiki.</li>
 <li>A <code>Content-Type</code> header is required, and lets MDN know how to process the content sent in the PUT request. Several content types are supported, and this feature will be described in greater detail shortly.</li>
 <li>Content intended for the document is sent in the request body, using the representation promised in the <code>Content-Type</code> header</li>
</ul>

<h3 id="Response" name="Response">レスポンス</h3>

<p>There are several responses you may see if you try this example: <code>403</code>, <code>404</code>, <code>201</code>, or <code>205</code>. (You may see others, but those suggest something has gone wrong with the site. That will, hopefully, be rare.)</p>

<h4 id="403_Forbidden" name="403_Forbidden">403 Forbidden</h4>

<p>If either the key ID or secret are incorrect, you'll see a <code>403 Forbidden</code> response. Double check your key details and that you're using the right pair for the right server. Create a new API key, if necessary.</p>

<h4 id="404_Not_Found" name="404_Not_Found">404 Not Found</h4>

<p>If you've never created a document at the URL path <code>/en-US/docs/User:$MDN_USERNAME</code>, you'll see a <code>404 Not Found</code> response.</p>

<p>{{NoteStart}}The PUT API will not automatically create parent documents. If you're creating a number of documents intended to comprise a subsection of MDN, make sure to create parent documents first from the top down in the hierarchy.{{NoteEnd}}</p>

<h4 id="201_Created" name="201_Created">201 Created</h4>

<p>If the parent document exists, but the path itself doesn't, you should see a <code>201 Created</code> response. This signifies that a new document was created, as opposed to an existing one having been updated.</p>

<h4 id="205_Reset_Content" name="205_Reset_Content">205 Reset Content</h4>

<p>In the case of an updated document, you'll see a <code>205 Reset Content</code> response. This means that the document content has been updated, and that you should reload the document if you happen to need to see the results.</p>

<p>{{NoteStart}}MDN performs certain filtering and processing steps on content, so what you put in may not be exactly what gets served back.{{NoteEnd}}</p>

<h2 id="Supported_Content_Types" name="Supported_Content_Types">サポートされるコンテンツのタイプ</h2>

<p>The PUT API accepts one of several content types in the request body.</p>

<h3 id="texthtml" name="texthtml">text/html</h3>

<p>There are actually two forms of <code>text/html</code> accepted: fragment and document.</p>

<h4 id="Fragment" name="Fragment">Fragment</h4>

<p>An HTML fragment is just an arbitrary chunk of markup, and is used as-is to revise document content. This is the simplest way to update documents.</p>

<h4 id="Document" name="Document">Document</h4>

<p>However, if the request body consists of an <code>&lt;html&gt;</code> element containing <code>&lt;head&gt;</code> and <code>&lt;body&gt;</code> elements, it's treated as a full HTML document. In this case, the following processing happens:</p>

<ul>
 <li>From the <code>&lt;head&gt;</code> element, the contents of <code>&lt;title&gt;</code> is extracted and used as the title for the document on MDN.</li>
 <li>The contents of <code>&lt;body&gt;</code> is extracted as the content for a new revision. </li>
</ul>

<p>This is a more complex way to update documents, but is intended as a convenience to accomodate submission of existing HTML pages.</p>

<h3 id="applicationjson" name="applicationjson">application/json</h3>

<p>Although the <code>text/html</code> content type is handy, there are more fields belonging to documents that are useful to manage. These include the following:</p>

<ul>
 <li><code>title</code> - the document title</li>
 <li><code>content</code> - the content intended for the new revision</li>
 <li><code>tags</code> - tags used to organize documents: this is given as a single string, with tags separated by commas</li>
 <li><code>review_tags</code> - tags used to request content reviews: this is given as a single string, with tags separated by commas</li>
 <li><code>summary</code> - a comment describing the revision to be made</li>
 <li><code>show_toc</code> - a flag (0/1) indicating whether the table of contents should be shown for this document</li>
</ul>

<p>These fields can be supplied as string values in a JSON-encoded object with the <code>application/json</code> content-type in a PUT request.</p>

<pre class="brush: bash notranslate"># Auth Stuff
DOC_USERNAME="lmorchard"  # Change this to your name
MDN_KEY_ID="frsNFFR3w0yEALRE9IA9oN1KwoDno8vVGrzsBNvCofI"
MDN_SECRET="423PdCvnvraH0FkCDTKnizTmKGNkEdgQTi6RlEFTiWs"

# Base Settings (for Staging Env)
MDN_BASE_URL="https://developer.allizom.org"
DOC_PATH=/ja//docs/User:$DOC_USERNAME/PutExample"
DOC_TYPE="application/json"

<code class="language-bash"># Doc Content
echo '{"content": "&lt;b&gt;Hello World&lt;/b&gt;", "title": "New Sample Title", "show_toc": 1, "tags": "Beginner, Tutorial", "review_tags": "editorial, technical", "summary": "Sample JSON update from the API"}' &gt; /tmp/mdn.json

# Submitting Content</code>
curl -X PUT -H "Content-Type: $DOC_TYPE" -d @/tmp/mdn.json -u"$MDN_KEY_ID:$MDN_SECRET" "$MDN_BASE_URL$DOC_PATH"
</pre>

<h3 id="multipartform-data" name="multipartform-data">multipart/form-data</h3>

<p>This content type is handled basically like <code>application/json</code> - the same fields are accepted. But, it might be less useful than JSON and is supported mainly for testing purposes.</p>

<h2 id="Updating_a_single_section" name="Updating_a_single_section">1 つのセクションを更新する</h2>

<p>Normally, an HTTP PUT request replaces the entirety of a document with the submitted content in a new revision. However, you can use the query parameter <code>?section</code> to constrain revision to a single section of the document and leave the rest of the content as-is. This is handy for automating changes to one part of a document that is otherwise managed by hand, or even for aggregating changes from many sources or scripts into one document.</p>

<h3 id="Creating_document_sections" name="Creating_document_sections">文書のセクションを作成する</h3>

<p>Documents on MDN can be broken up into sections. These sections are useful for building a table of contents, linking to specific parts, and editing subsets of document content.</p>

<h4 id="Using_headers" name="Using_headers">Using headers</h4>

<p>Headers (ie. <code>&lt;h2&gt;</code> .. <code>&lt;h6&gt;</code>) make sections in MDN documents. The text of each header is transformed automatically into an ID, and that's used for anchor links in the table of contents sidebar on most documents. Those auto-generated IDs can be overriden with the <code>name</code> attribute on headers. Either way, looking at the table of contents is the easiest way to see how a document is broken up into sections, and to discover the IDs for those sections.</p>

<p>The contents of a section include its header and everything following the header up to (but not including) another header of the same or higher level. So, a section that starts with an <code>&lt;h2&gt;</code> continues until the next <code>&lt;h2&gt;</code>, including any subsections started by <code>&lt;h3&gt;</code> .. <code>&lt;h6&gt;</code>. That also means sections can be nested: An <code>&lt;h3&gt;</code> appearing after an <code>&lt;h2&gt;</code> creates a subsection, including any further nested subsections started by <code>&lt;h4&gt;</code> .. <code>&lt;h6&gt;</code>, up to the next <code>&lt;h3&gt;</code> or <code>&lt;h2&gt;</code>.</p>

<p>@@TODO: Show an HTML example with headers, here. This is a bit confusing.</p>

<h4 id="Using_container_elements" name="Using_container_elements">Using container elements</h4>

<p>Setting an <code>id</code> attribute on a container element (eg. a <code>&lt;div&gt;</code> or <code>&lt;span&gt;</code> or <code>&lt;section&gt;</code>) in the source editor also creates a section, at least with respect to the PUT API. This is a bit more advanced and requires manual changes to raw HTML, rather than using the WYSIWYG editor. But, if you want to update a chunk of the page without the need for headers, this is how to do it.</p>

<h3 id="Specifying_a_section" name="Specifying_a_section">セクションを指定する</h3>

<ul>
 <li>Look at the table of contents, note the anchor ID for the link (ie. the <code>#hash</code> part of the URL).<br>
  Example: <code>https://developer.mozilla.org/ja/docs/User:lmorchard/PUT-API#Specifying_a_section</code></li>
 <li>Take everything after the "#" character, and you have the section ID.<br>
  Example: <code>Specifying_a_section</code></li>
 <li>Add <code>?section={ID}</code> to the URL for the document, substituting the section ID for <code>{ID}</code>.<br>
  Example: <code>https://developer.mozilla.org/ja/docs/User:lmorchard/PUT-API?section=Specifying_a_section</code></li>
 <li>If you view <a href="https://developer.mozilla.org/ja/docs/User:lmorchard/PUT-API?section=Specifying_a_section" title="https://developer.mozilla.org/ja/docs/User:lmorchard/PUT-API?section=Specifying_a_section">that URL</a> in a browser (ie. HTTP GET), you'll see just that section of the document.<br>
  (For more details on what you can do with HTTP GET, see also: <a href="/ja/docs/Project:The_Kuma_API" title='/ja/docs/Project:The_Kuma_API""'>Project:The_Kuma_API</a>)</li>
 <li>If you issue a PUT request to that URL, you'll modify just that section of the document.<br>
  (But, don't do that to the example URL, or you'll clobber the page you're reading right now!)</li>
</ul>