These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace SMW; |
||
4 | |||
5 | use SMWDataItem; |
||
6 | use Title; |
||
7 | |||
8 | /** |
||
9 | * This class implements wiki page data items. |
||
10 | * |
||
11 | * @since 1.6 |
||
12 | * @ingroup SMWDataItems |
||
13 | * |
||
14 | * @author Markus Krötzsch |
||
15 | */ |
||
16 | class DIWikiPage extends SMWDataItem { |
||
17 | |||
18 | /** |
||
19 | * MediaWiki DB key string |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $m_dbkey; |
||
23 | |||
24 | /** |
||
25 | * MediaWiki namespace integer. |
||
26 | * @var integer |
||
27 | */ |
||
28 | protected $m_namespace; |
||
29 | |||
30 | /** |
||
31 | * MediaWiki interwiki prefix. |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $m_interwiki; |
||
35 | |||
36 | /** |
||
37 | * Name for subobjects of pages, or empty string if the given object is |
||
38 | * the page itself (not a subobject). |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $m_subobjectname; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $sortkey = null; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | private $contextReference = null; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | private $pageLanguage = null; |
||
57 | |||
58 | /** |
||
59 | * Contructor. We do not bother with too much detailed validation here, |
||
60 | * regarding the known namespaces, canonicity of the dbkey (namespace |
||
61 | * exrtacted?), validity of interwiki prefix (known?), and general use |
||
62 | * of allowed characters (may depend on MW configuration). All of this |
||
63 | * would be more work than it is worth, since callers will usually be |
||
64 | * careful and since errors here do not have major consequences. |
||
65 | * |
||
66 | * @param string $dbkey |
||
67 | * @param integer $namespace |
||
68 | * @param string $interwiki |
||
69 | * @param string $subobjectname |
||
70 | */ |
||
71 | 306 | public function __construct( $dbkey, $namespace, $interwiki = '', $subobjectname = '' ) { |
|
72 | // Check if the provided value holds an integer |
||
73 | // (it can be of type string or float as well, as long as the value is an int) |
||
74 | 306 | if ( !ctype_digit( ltrim( (string)$namespace, '-' ) ) ) { |
|
75 | throw new DataItemException( "Given namespace '$namespace' is not an integer." ); |
||
76 | } |
||
77 | |||
78 | // Check for a potential fragment such as Foo#Bar, Bar#_49c8ab |
||
79 | 306 | if ( strpos( $dbkey, '#' ) !== false ) { |
|
80 | 8 | list( $dbkey, $subobjectname ) = explode( '#', $dbkey ); |
|
81 | } |
||
82 | |||
83 | 306 | $this->m_dbkey = str_replace( ' ', '_', $dbkey ); |
|
84 | 306 | $this->m_namespace = (int)$namespace; // really make this an integer |
|
85 | 306 | $this->m_interwiki = $interwiki; |
|
86 | 306 | $this->m_subobjectname = $subobjectname; |
|
87 | 306 | } |
|
88 | |||
89 | 250 | public function getDIType() { |
|
90 | 250 | return SMWDataItem::TYPE_WIKIPAGE; |
|
91 | } |
||
92 | |||
93 | 271 | public function getDBkey() { |
|
94 | 271 | return $this->m_dbkey; |
|
95 | } |
||
96 | |||
97 | 273 | public function getNamespace() { |
|
98 | 273 | return $this->m_namespace; |
|
99 | } |
||
100 | |||
101 | 265 | public function getInterwiki() { |
|
102 | 265 | return $this->m_interwiki; |
|
103 | } |
||
104 | |||
105 | 281 | public function getSubobjectName() { |
|
106 | 281 | return $this->m_subobjectname; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * @since 2.1 |
||
111 | * |
||
112 | * @param string $sortkey |
||
113 | */ |
||
114 | 256 | public function setSortKey( $sortkey ) { |
|
115 | 256 | $this->sortkey = str_replace( '_', ' ', $sortkey ); |
|
116 | 256 | } |
|
117 | |||
118 | /** |
||
119 | * Get the sortkey of the wiki page data item. Note that this is not |
||
120 | * the sortkey that might have been set for the corresponding wiki |
||
121 | * page. To obtain the latter, query for the values of the property |
||
122 | * "new SMW\DIProperty( '_SKEY' )". |
||
123 | */ |
||
124 | 256 | public function getSortKey() { |
|
125 | |||
126 | 256 | if ( $this->sortkey === null || $this->sortkey === '' ) { |
|
127 | 251 | $this->sortkey = str_replace( '_', ' ', $this->m_dbkey ); |
|
128 | } |
||
129 | |||
130 | 256 | return $this->sortkey; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * @since 2.3 |
||
135 | * |
||
136 | * @param string $contextReference |
||
137 | */ |
||
138 | 197 | public function setContextReference( $contextReference ) { |
|
139 | 197 | $this->contextReference = $contextReference; |
|
140 | 197 | } |
|
141 | |||
142 | /** |
||
143 | * Returns a reference for the processing context (parser etc.). |
||
144 | * |
||
145 | * @since 2.3 |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | 190 | public function getContextReference() { |
|
150 | 190 | return $this->contextReference; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * Returns the page content language |
||
155 | * |
||
156 | * @since 2.5 |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | 13 | public function getPageLanguage() { |
|
161 | |||
162 | 13 | if ( $this->pageLanguage === null ) { |
|
163 | 13 | $this->pageLanguage = false; |
|
0 ignored issues
–
show
|
|||
164 | |||
165 | 13 | if ( ( $title = $this->getTitle() ) !== null ) { |
|
166 | 13 | $this->pageLanguage = $title->getPageLanguage()->getCode(); |
|
167 | } |
||
168 | } |
||
169 | |||
170 | 13 | return $this->pageLanguage; |
|
171 | } |
||
172 | |||
173 | /** |
||
174 | * Create a MediaWiki Title object for this DIWikiPage. The result |
||
175 | * can be null if an error occurred. |
||
176 | * |
||
177 | * @return Title|null |
||
178 | */ |
||
179 | 256 | public function getTitle() { |
|
180 | 256 | return Title::makeTitleSafe( |
|
181 | 256 | $this->m_namespace, |
|
182 | 256 | $this->m_dbkey, |
|
183 | 256 | $this->m_subobjectname, |
|
184 | 256 | $this->m_interwiki |
|
185 | ); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Returns the base part (without a fragment) of a wikipage representation. |
||
190 | * |
||
191 | * @since 2.4 |
||
192 | * |
||
193 | * @return DIWikiPage |
||
194 | */ |
||
195 | 235 | public function asBase() { |
|
196 | 235 | return new self ( |
|
197 | 235 | $this->m_dbkey, |
|
198 | 235 | $this->m_namespace, |
|
199 | 235 | $this->m_interwiki |
|
200 | ); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @since 1.6 |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | 277 | public function getSerialization() { |
|
209 | $segments = array( |
||
210 | 277 | $this->m_dbkey, |
|
211 | 277 | $this->m_namespace, |
|
212 | 277 | $this->m_interwiki |
|
213 | ); |
||
214 | |||
215 | 277 | if ( $this->m_subobjectname !== '' ) { |
|
216 | 150 | $segments[] = $this->m_subobjectname; |
|
217 | } |
||
218 | |||
219 | 277 | return implode( '#', $segments ); |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * Create a data item from the provided serialization string and type ID. |
||
224 | * |
||
225 | * @param string $serialization |
||
226 | * |
||
227 | * @return DIWikiPage |
||
228 | * @throws DataItemException |
||
229 | */ |
||
230 | 63 | public static function doUnserialize( $serialization ) { |
|
231 | 63 | $parts = explode( '#', $serialization, 4 ); |
|
232 | |||
233 | 63 | if ( count( $parts ) == 3 ) { |
|
234 | 14 | return new self( $parts[0], intval( $parts[1] ), $parts[2] ); |
|
235 | 56 | } elseif ( count( $parts ) == 4 ) { |
|
236 | 56 | return new self( $parts[0], intval( $parts[1] ), $parts[2], $parts[3] ); |
|
237 | } else { |
||
238 | throw new DataItemException( "Unserialization failed: the string \"$serialization\" was not understood." ); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Create a data item from a MediaWiki Title. |
||
244 | * |
||
245 | * @param $title Title |
||
246 | * @return DIWikiPage |
||
247 | */ |
||
248 | 273 | public static function newFromTitle( Title $title ) { |
|
249 | 273 | return new self( |
|
250 | 273 | $title->getDBkey(), |
|
251 | 273 | $title->getNamespace(), |
|
252 | 273 | $title->getInterwiki(), |
|
253 | 273 | str_replace( ' ', '_', $title->getFragment() ) |
|
254 | ); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @since 2.1 |
||
259 | * |
||
260 | * @param string $text |
||
261 | * @param integer namespace |
||
262 | * |
||
263 | * @return DIWikiPage |
||
264 | */ |
||
265 | 78 | public static function newFromText( $text, $namespace = NS_MAIN ) { |
|
266 | 78 | return new self( $text, $namespace ); |
|
267 | } |
||
268 | |||
269 | 111 | public function equals( SMWDataItem $di ) { |
|
270 | 111 | if ( $di->getDIType() !== SMWDataItem::TYPE_WIKIPAGE ) { |
|
271 | 3 | return false; |
|
272 | } |
||
273 | |||
274 | 108 | return $di->getSerialization() === $this->getSerialization(); |
|
275 | } |
||
276 | } |
||
277 | |||
278 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.