summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Utils/Page/PageEditor.php
blob: b09bf5d45c69b1a56dc1611b94021e2d2c7c7af6 (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
<?php

namespace SMW\Tests\Utils\Page;

use Revision;
use RuntimeException;
use Title;
use WikiPage;

/**
 * @group SMW
 * @group SMWExtension
 *
 * @license GNU GPL v2+
 * @since 2.1
 */
class PageEditor {

	/**
	 * @var WikiPage|null
	 */
	private $page = null;

	/**
	 * @since 2.1
	 *
	 * @return WikiPage
	 * @throws RuntimeException
	 */
	public function getPage() {

		if ( $this->page instanceof WikiPage ) {
			return $this->page;
		}

		throw new RuntimeException( 'Expected a valid WikiPage instance.' );
	}

	/**
	 * @since 2.1
	 *
	 * @param Title $title
	 *
	 * @return PageEditor
	 */
	public function editPage( Title $title ) {
		$this->page = new WikiPage( $title );
		return $this;
	}

	/**
	 * @since 2.1
	 *
	 * @param string $pageContent
	 * @param string $editMessage
	 *
	 * @return PageEditor
	 */
	public function doEdit( $pageContent = '', $editMessage = '' ) {

		if ( class_exists( 'WikitextContent' ) ) {
			$content = new \WikitextContent( $pageContent );

			$this->getPage()->doEditContent(
				$content,
				$editMessage
			);

		} else {
			$this->getPage()->doEdit( $pageContent, $editMessage );
		}

		return $this;
	}

	/**
	 * @since 2.1
	 */
	public function getEditInfo() {

		if ( class_exists( 'WikitextContent' ) ) {

			$content = $this->getPage()->getRevision()->getContent();
			$format  = $content->getContentHandler()->getDefaultFormat();

			return $this->getPage()->prepareContentForEdit(
				$content,
				null,
				null,
				$format
			);
		}

		if ( method_exists( $this->getPage()->getRevision(), 'getContent' ) ) {
			$text = $this->getPage()->getRevision()->getContent( Revision::RAW );
		} else {
			$text = $this->getPage()->getRevision()->getRawText();
		}
		return $this->getPage()->prepareTextForEdit(
			$text,
			null,
			null
		);
	}

}