1 // ================================================================================================
\r
3 // MediaWikiのウェブサイト(システム)をあらわすモデルクラスソース</summary>
\r
5 // <copyright file="MediaWiki.cs" company="honeplusのメモ帳">
\r
6 // Copyright (C) 2011 Honeplus. All rights reserved.</copyright>
\r
9 // ================================================================================================
\r
11 namespace Honememo.Wptscs.Models
\r
14 using System.Collections.Generic;
\r
18 using System.Xml.Serialization;
\r
19 using Honememo.Utilities;
\r
20 using Honememo.Wptscs.Properties;
\r
23 /// MediaWikiのウェブサイト(システム)をあらわすモデルクラスです。
\r
25 public class MediaWiki : Website, IXmlSerializable
\r
30 /// 名前空間情報取得用にアクセスするAPI。
\r
32 private string namespacePath;
\r
35 /// 記事のXMLデータが存在するパス。
\r
37 private string exportPath;
\r
42 private string redirect;
\r
45 /// テンプレートの名前空間を示す番号。
\r
47 private int? templateNamespace;
\r
52 private int? categoryNamespace;
\r
57 private int? fileNamespace;
\r
60 /// Wikipedia書式のシステム定義変数。
\r
62 /// <remarks>初期値は http://www.mediawiki.org/wiki/Help:Magic_words を参照</remarks>
\r
63 private IList<string> magicWords;
\r
66 /// MediaWikiの名前空間の情報。
\r
68 private IDictionary<int, IList<string>> namespaces = new Dictionary<int, IList<string>>();
\r
75 /// コンストラクタ(MediaWiki全般)。
\r
77 /// <param name="language">ウェブサイトの言語。</param>
\r
78 /// <param name="location">ウェブサイトの場所。</param>
\r
79 public MediaWiki(Language language, string location)
\r
82 this.Language = language;
\r
83 this.Location = location;
\r
87 /// コンストラクタ(Wikipedia用)。
\r
89 /// <param name="language">ウェブサイトの言語。</param>
\r
90 public MediaWiki(Language language)
\r
93 // ※ オーバーロードメソッドを呼んでいないのは、languageがnullのときに先にエラーになるから
\r
94 this.Language = language;
\r
95 this.Location = String.Format(Settings.Default.WikipediaLocation, language.Code);
\r
99 /// コンストラクタ(シリアライズ or 拡張用)。
\r
101 protected MediaWiki()
\r
107 #region 設定ファイルに初期値を持つプロパティ
\r
110 /// MediaWiki名前空間情報取得用にアクセスするAPI。
\r
112 /// <remarks>値が指定されていない場合、デフォルト値を返す。</remarks>
\r
113 public string NamespacePath
\r
117 if (String.IsNullOrEmpty(this.namespacePath))
\r
119 return Settings.Default.MediaWikiNamespacePath;
\r
122 return this.namespacePath;
\r
127 this.namespacePath = value;
\r
132 /// 記事のXMLデータが存在するパス。
\r
134 /// <remarks>値が指定されていない場合、デフォルト値を返す。</remarks>
\r
135 public string ExportPath
\r
139 if (String.IsNullOrEmpty(this.exportPath))
\r
141 return Settings.Default.MediaWikiExportPath;
\r
144 return this.exportPath;
\r
149 this.exportPath = value;
\r
156 /// <remarks>値が指定されていない場合、デフォルト値を返す。</remarks>
\r
157 public string Redirect
\r
161 if (String.IsNullOrEmpty(this.redirect))
\r
163 return Settings.Default.MediaWikiRedirect;
\r
166 return this.redirect;
\r
171 this.redirect = value;
\r
176 /// テンプレートの名前空間を示す番号。
\r
178 /// <remarks>値が指定されていない場合、デフォルト値を返す。</remarks>
\r
179 public int TemplateNamespace
\r
183 return this.templateNamespace ?? Settings.Default.MediaWikiTemplateNamespace;
\r
188 this.templateNamespace = value;
\r
193 /// カテゴリの名前空間を示す番号。
\r
195 /// <remarks>値が指定されていない場合、デフォルト値を返す。</remarks>
\r
196 public int CategoryNamespace
\r
200 return this.categoryNamespace ?? Settings.Default.MediaWikiCategoryNamespace;
\r
205 this.categoryNamespace = value;
\r
212 /// <remarks>値が指定されていない場合、デフォルト値を返す。</remarks>
\r
213 public int FileNamespace
\r
217 return this.fileNamespace ?? Settings.Default.MediaWikiFileNamespace;
\r
222 this.fileNamespace = value;
\r
227 /// Wikipedia書式のシステム定義変数。
\r
229 /// <remarks>値が指定されていない場合、デフォルト値を返す。</remarks>
\r
230 public IList<string> MagicWords
\r
234 if (this.magicWords == null)
\r
236 string[] w = new string[Settings.Default.MediaWikiMagicWords.Count];
\r
237 Settings.Default.MediaWikiMagicWords.CopyTo(w, 0);
\r
241 return this.magicWords;
\r
246 this.magicWords = value;
\r
255 /// MediaWikiの名前空間の情報。
\r
257 /// <remarks>値が指定されていない場合、サーバーから情報を取得。</remarks>
\r
258 public IDictionary<int, IList<string>> Namespaces
\r
262 lock (this.namespaces)
\r
264 // 値が設定されていない場合、サーバーから取得して初期化する
\r
265 // ※ コンストラクタ等で初期化していないのは、通信の準備が整うまで行えないため
\r
266 // ※ MagicWordsがnullでこちらが空で若干条件が違うのは、あちらは設定ファイルに
\r
267 // 保存する設定だが、こちらは設定ファイルに保存しない基本的に読み込み用の設定だから。
\r
268 if (this.namespaces.Count > 0)
\r
270 return this.namespaces;
\r
273 // APIのXMLデータをMediaWikiサーバーから取得
\r
274 XmlDocument xml = new XmlDocument();
\r
275 using (Stream reader = this.GetStream(new Uri(new Uri(this.Location), this.NamespacePath)))
\r
280 // ルートエレメントまで取得し、フォーマットをチェック
\r
281 XmlElement rootElement = xml["api"];
\r
282 if (rootElement == null)
\r
284 // XMLは取得できたが空 or フォーマットが想定外
\r
285 throw new InvalidDataException("parse failed");
\r
289 XmlElement queryElement = rootElement["query"];
\r
290 if (queryElement == null)
\r
293 throw new InvalidDataException("parse failed");
\r
296 // ネームスペースブロックを取得、ネームスペースブロックまでは必須
\r
297 XmlElement namespacesElement = queryElement["namespaces"];
\r
298 if (namespacesElement == null)
\r
301 throw new InvalidDataException("parse failed");
\r
305 foreach (XmlNode node in namespacesElement.ChildNodes)
\r
307 XmlElement namespaceElement = node as XmlElement;
\r
308 if (namespaceElement != null)
\r
312 int id = Decimal.ToInt16(Decimal.Parse(namespaceElement.GetAttribute("id")));
\r
313 IList<string> values = new List<string>();
\r
314 values.Add(namespaceElement.InnerText);
\r
315 this.namespaces[id] = values;
\r
318 string canonical = namespaceElement.GetAttribute("canonical");
\r
319 if (!String.IsNullOrEmpty(canonical))
\r
321 values.Add(canonical);
\r
324 catch (Exception e)
\r
326 // キャッチしているのは、万が一想定外の書式が返された場合に、完璧に動かなくなるのを防ぐため
\r
327 System.Diagnostics.Debug.WriteLine("MediaWiki.Namespaces > 例外発生 : " + e);
\r
332 // ネームスペースエイリアスブロックを取得、無い場合も想定
\r
333 XmlElement aliasesElement = queryElement["namespacealiases"];
\r
334 if (aliasesElement != null)
\r
337 foreach (XmlNode node in aliasesElement.ChildNodes)
\r
339 XmlElement namespaceElement = node as XmlElement;
\r
340 if (namespaceElement != null)
\r
344 int id = Decimal.ToInt16(Decimal.Parse(namespaceElement.GetAttribute("id")));
\r
345 IList<string> values = new List<string>();
\r
346 if (this.namespaces.ContainsKey(id))
\r
348 values = this.namespaces[id];
\r
351 values.Add(namespaceElement.InnerText);
\r
353 catch (Exception e)
\r
355 // キャッチしているのは、万が一想定外の書式が返された場合に、完璧に動かなくなるのを防ぐため
\r
356 System.Diagnostics.Debug.WriteLine("MediaWiki.Namespaces > 例外発生 : " + e);
\r
363 return this.namespaces;
\r
368 // ※必須な情報が設定されていない場合、ArgumentNullExceptionを返す
\r
371 throw new ArgumentNullException("namespaces");
\r
374 this.namespaces = value;
\r
379 /// Template:Documentation(言語間リンク等を別ページに記述するためのテンプレート)に相当するページ名。
\r
381 /// <remarks>空の場合、その言語版にはこれに相当する機能は無いものとして扱う。</remarks>
\r
382 public string DocumentationTemplate
\r
389 /// Template:Documentationで指定が無い場合に参照するページ名。
\r
392 /// ほとんどの言語では[[/Doc]]の模様。
\r
393 /// 空の場合、明示的な指定が無い場合は参照不能として扱う。
\r
395 public string DocumentationTemplateDefaultPage
\r
408 /// <param name="title">ページタイトル。</param>
\r
409 /// <returns>取得したページ。</returns>
\r
410 /// <remarks>取得できない場合(通信エラーなど)は例外を投げる。</remarks>
\r
411 public override Page GetPage(string title)
\r
413 // & 等の特殊文字をデコード
\r
414 // ※ 本当は呼び元側ですべき処理の気がするが、現状手ごろな場所が無いので
\r
415 string decodeTitle = HttpUtility.HtmlDecode(title);
\r
417 // fileスキームの場合、記事名からファイルに使えない文字をエスケープ
\r
418 // ※ 仕組み的な処理はWebsite側に置きたいが、向こうではタイトルだけを抽出できないので
\r
419 string escapeTitle = decodeTitle;
\r
420 if (new Uri(this.Location).Scheme == "file")
\r
422 escapeTitle = FormUtils.ReplaceInvalidFileNameChars(title);
\r
425 // ページのXMLデータをMediaWikiサーバーから取得
\r
426 XmlDocument xml = new XmlDocument();
\r
427 using (Stream reader = this.GetStream(
\r
428 new Uri(new Uri(this.Location), String.Format(this.ExportPath, escapeTitle))))
\r
433 // ルートエレメントまで取得し、フォーマットをチェック
\r
434 XmlElement rootElement = xml["mediawiki"];
\r
435 if (rootElement == null)
\r
437 // XMLは取得できたが空 or フォーマットが想定外
\r
438 throw new InvalidDataException("parse failed");
\r
442 XmlElement pageElement = rootElement["page"];
\r
443 if (pageElement == null)
\r
446 throw new FileNotFoundException("page not found");
\r
449 // ページ名、ページ本文、最終更新日時
\r
450 // ※ 一応、各項目が無くても動作するようにする
\r
451 string pageTitle = XmlUtils.InnerText(pageElement["title"], title);
\r
452 string text = null;
\r
453 DateTime? time = null;
\r
454 XmlElement revisionElement = pageElement["revision"];
\r
455 if (revisionElement != null)
\r
457 text = XmlUtils.InnerText(revisionElement["text"], null);
\r
458 XmlElement timeElement = revisionElement["timestamp"];
\r
459 if (timeElement != null)
\r
461 time = new DateTime?(DateTime.Parse(timeElement.InnerText));
\r
466 return new MediaWikiPage(this, pageTitle, text, time);
\r
470 /// 指定された文字列がWikipediaのシステム変数に相当かを判定。
\r
472 /// <param name="text">チェックする文字列。</param>
\r
473 /// <returns><c>true</c> システム変数に相当。</returns>
\r
474 public bool IsMagicWord(string text)
\r
476 string s = text != null ? text : String.Empty;
\r
478 // {{CURRENTYEAR}}や{{ns:1}}みたいなパターンがある
\r
479 foreach (string variable in this.MagicWords)
\r
481 if (s == variable || s.StartsWith(variable + ":"))
\r
492 #region XMLシリアライズ用メソッド
\r
495 /// シリアライズするXMLのスキーマ定義を返す。
\r
497 /// <returns>XML表現を記述するXmlSchema。</returns>
\r
498 public System.Xml.Schema.XmlSchema GetSchema()
\r
504 /// XMLからオブジェクトをデシリアライズする。
\r
506 /// <param name="reader">デシリアライズ元のXmlReader</param>
\r
507 public void ReadXml(XmlReader reader)
\r
509 XmlDocument xml = new XmlDocument();
\r
513 // ※ 以下、基本的に無かったらNGの部分はいちいちチェックしない。例外飛ばす
\r
514 XmlElement siteElement = xml.DocumentElement;
\r
515 this.Location = siteElement.SelectSingleNode("Location").InnerText;
\r
517 using (XmlReader r = XmlReader.Create(
\r
518 new StringReader(siteElement.SelectSingleNode("Language").OuterXml), reader.Settings))
\r
520 this.Language = new XmlSerializer(typeof(Language)).Deserialize(r) as Language;
\r
523 this.NamespacePath = XmlUtils.InnerText(siteElement.SelectSingleNode("NamespacePath"));
\r
524 this.ExportPath = XmlUtils.InnerText(siteElement.SelectSingleNode("ExportPath"));
\r
525 this.Redirect = XmlUtils.InnerText(siteElement.SelectSingleNode("Redirect"));
\r
527 string text = XmlUtils.InnerText(siteElement.SelectSingleNode("TemplateNamespace"));
\r
528 if (!String.IsNullOrEmpty(text))
\r
530 this.TemplateNamespace = int.Parse(text);
\r
533 text = XmlUtils.InnerText(siteElement.SelectSingleNode("CategoryNamespace"));
\r
534 if (!String.IsNullOrEmpty(text))
\r
536 this.CategoryNamespace = int.Parse(text);
\r
539 text = XmlUtils.InnerText(siteElement.SelectSingleNode("FileNamespace"));
\r
540 if (!String.IsNullOrEmpty(text))
\r
542 this.FileNamespace = int.Parse(text);
\r
546 IList<string> variables = new List<string>();
\r
547 foreach (XmlNode variableNode in siteElement.SelectNodes("MagicWords/Variable"))
\r
549 variables.Add(variableNode.InnerText);
\r
552 if (variables.Count > 0)
\r
554 // 初期値の都合上、値がある場合のみ
\r
555 this.MagicWords = variables;
\r
558 // Template:Documentationの設定
\r
559 XmlElement docElement = siteElement.SelectSingleNode("DocumentationTemplate") as XmlElement;
\r
560 if (docElement != null)
\r
562 this.DocumentationTemplate = docElement.InnerText;
\r
563 this.DocumentationTemplateDefaultPage = docElement.GetAttribute("DefaultPage");
\r
568 /// オブジェクトをXMLにシリアライズする。
\r
570 /// <param name="writer">シリアライズ先のXmlWriter</param>
\r
571 public void WriteXml(XmlWriter writer)
\r
573 writer.WriteElementString("Location", this.Location);
\r
574 new XmlSerializer(this.Language.GetType()).Serialize(writer, this.Language);
\r
577 // ※ 設定ファイルに初期値を持つものは、プロパティではなく値から出力
\r
578 writer.WriteElementString("NamespacePath", this.namespacePath);
\r
579 writer.WriteElementString("ExportPath", this.exportPath);
\r
580 writer.WriteElementString("Redirect", this.redirect);
\r
581 writer.WriteElementString(
\r
582 "TemplateNamespace",
\r
583 this.templateNamespace.HasValue ? this.templateNamespace.ToString() : String.Empty);
\r
584 writer.WriteElementString(
\r
585 "CategoryNamespace",
\r
586 this.templateNamespace.HasValue ? this.categoryNamespace.ToString() : String.Empty);
\r
587 writer.WriteElementString(
\r
589 this.templateNamespace.HasValue ? this.fileNamespace.ToString() : String.Empty);
\r
592 writer.WriteStartElement("MagicWords");
\r
593 if (this.magicWords != null)
\r
595 foreach (string variable in this.magicWords)
\r
597 writer.WriteElementString("Variable", variable);
\r
601 writer.WriteEndElement();
\r
603 // Template:Documentationの設定は一項目で出力
\r
604 if (!String.IsNullOrEmpty(this.DocumentationTemplate))
\r
606 writer.WriteStartElement("DocumentationTemplate");
\r
607 writer.WriteAttributeString("DefaultPage", this.DocumentationTemplateDefaultPage);
\r
608 writer.WriteValue(this.DocumentationTemplate);
\r
609 writer.WriteEndElement();
\r