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