first brain dump for ContentHandler interface
[lhc/web/wiklou.git] / includes / ContentHandler.php
1 <?php
2
3 /**
4 * A content handler knows how do deal with a specific type of content on a wiki page.
5 * Content is stored in the database in a serialized form (using a serialization format aka mime type)
6 * and is be unserialized into it's native PHP represenation (the content model).
7 *
8 * Some content types have a flat model, that is, their native represenation is the
9 * same as their serialized form. Examples would be JavaScript and CSS code. As of now,
10 * this also applies to wikitext (mediawiki's default content type), but wikitext
11 * content may be represented by a DOM or AST structure in the future.
12 *
13 */
14 abstract class ContentHandler {
15
16 public function __construct( $modelName, $formats ) {
17 $this->mModelName = $modelName;
18 $this->mSupportedFormats = $formats;
19 }
20
21 public function getModelName() {
22 # for wikitext: wikitext; in the future: wikiast, wikidom?
23 # for wikidata: wikidata
24 return $this->mModelName;
25 }
26
27
28 public function getSupportedFormats() {
29 # for wikitext: "text/x-mediawiki-1", "text/x-mediawiki-2", etc
30 # for wikidata: "application/json", "application/x-php", etc
31 return $this->mSupportedFormats;
32 }
33
34 public function getDefaultFormat() {
35 return $this->mSupportedFormats[0];
36 }
37
38 public abstract function serialize( $obj, $format = null );
39 # for wikitext, do nothing (in the future: serialise ast/dom)
40 # for wikidata: serialize arrays to json
41
42 public abstract function unserialize( $blob, $format = null );
43 # for wikitext, do nothing (in the future: parse into ast/dom)
44 # for wikidata: serialize arrays to json
45
46
47 public function getSearchText( $obj ) {
48 # for wikitext, return wikitext
49 # for wikidata, return pseudo-wikitext composed of property values (or some such)
50 $text = $this->serialize( $obj );
51 return $text; # return the default serialization.
52 }
53
54 public function getWikitextForTransclusion( $obj ) {
55 # for wikitext, return text
56 # for wikidata, return false, or some generated wikitext
57 $text = $this->serialize( $obj );
58 return '<pre>' . $text . '</pre>'; # return a pre-formatted block containing the default serialization.
59 }
60
61 public abstract function render( $obj, Title $title, ParserOptions $options, $revid = null );
62 # returns a ParserOutput instance!
63 # are parser options, generic?!
64
65 public function doPreSaveTransform( $title, $obj );
66
67 # TODO: getPreloadText()
68 # TODO: preprocess()
69
70 /**
71 * Return an Article object suitable for viewing the given object
72 *
73 * @param type $title
74 * @param type $obj
75 * @return \Article
76 * @todo Article is being refactored into an action class, keep track of that
77 */
78 public function createArticle( $title, $obj ) {
79 $article = new Article($title);
80 return $article;
81 }
82
83 /**
84 * Return an EditPage object suitable for editing the given object
85 *
86 * @param type $title
87 * @param type $obj
88 * @param type $article
89 * @return \EditPage
90 */
91 public function createEditPage( $title, $obj, $article ) {
92 $editPage = new EditPage($article);
93 return $editPage;
94 }
95
96 /**
97 public function updatePage( $title, $obj ) {
98 }
99 **/
100
101 public function getDiffEngine( $article ) {
102 $de = new DifferenceEngine( $article->getContext() );
103 return $de;
104 }
105
106 public function getIndexUpdateJobs( $title, $parserOutput, $recursive = true ) {
107 # for wikitext, create a LinksUpdate object
108 # for wikidata: serialize arrays to json
109 $update = new LinksUpdate( $title, $parserOutput, $recursive );
110 return $update;
111 }
112
113 #XXX: is the native model for wikitext a string or the parser output? parse early or parse late?
114 }
115
116 abstract class WikitextContentHandler extends ContentHandler {
117 }
118
119 abstract class JavaScriptContentHandler extends WikitextHandler {
120 }
121
122 abstract class CssContentHandler extends WikitextHandler {
123 }